use mm oracle

This commit is contained in:
Nour Alharithi
2025-07-29 11:30:11 -07:00
parent ff4a79c5b4
commit bb476e97f7
3 changed files with 26 additions and 20 deletions

View File

@@ -1,6 +1,10 @@
git submodule init git submodule init
git submodule update --recursive git submodule update --recursive
rm -rf node_modules
rm -rf drift-common/protocol/sdk/node_modules
rm -rf drift-common/common-ts/node_modules
echo "building sdk..." echo "building sdk..."
cd drift-common/protocol/sdk cd drift-common/protocol/sdk
yarn clean && yarn && yarn build && yarn link yarn clean && yarn && yarn build && yarn link

View File

@@ -13,7 +13,7 @@ import {
PerpOperation, PerpOperation,
PositionDirection, PositionDirection,
ZERO, ZERO,
calculateBidAskPrice, calculateAMMBidAskPrice,
getLimitPrice, getLimitPrice,
isOperationPaused, isOperationPaused,
isVariant, isVariant,
@@ -177,9 +177,11 @@ export class DLOBSubscriberIO extends DLOBSubscriber {
let bestBid = bestDlobBid; let bestBid = bestDlobBid;
let bestAsk = bestDlobAsk; let bestAsk = bestDlobAsk;
if (marketType === 'perp') { if (marketType === 'perp') {
const [bestVammBid, bestVammAsk] = calculateBidAskPrice( const [bestVammBid, bestVammAsk] = calculateAMMBidAskPrice(
this.driftClient.getPerpMarketAccount(marketArgs.marketIndex).amm, this.driftClient.getPerpMarketAccount(marketArgs.marketIndex).amm,
oraclePriceData, this.driftClient.getMMOracleDataForPerpMarket(
marketArgs.marketIndex
),
true true
); );

View File

@@ -1,7 +1,7 @@
import { import {
BN, BN,
BigNum, BigNum,
DriftClient, IDriftClient,
DriftEnv, DriftEnv,
L2OrderBook, L2OrderBook,
L3OrderBook, L3OrderBook,
@@ -92,7 +92,7 @@ export function parsePositiveIntArray(
} }
export const getOracleForMarket = ( export const getOracleForMarket = (
driftClient: DriftClient, driftClient: IDriftClient,
marketType: MarketType, marketType: MarketType,
marketIndex: number marketIndex: number
): number => { ): number => {
@@ -129,7 +129,7 @@ const getSerializableOraclePriceData = (
}; };
export const getOracleDataForMarket = ( export const getOracleDataForMarket = (
driftClient: DriftClient, driftClient: IDriftClient,
marketType: MarketType, marketType: MarketType,
marketIndex: number marketIndex: number
): SerializableOraclePriceData => { ): SerializableOraclePriceData => {
@@ -146,7 +146,7 @@ export const getOracleDataForMarket = (
export const addOracletoResponse = ( export const addOracletoResponse = (
response: L2OrderBook | L3OrderBook, response: L2OrderBook | L3OrderBook,
driftClient: DriftClient, driftClient: IDriftClient,
marketType: MarketType, marketType: MarketType,
marketIndex: number marketIndex: number
): void => { ): void => {
@@ -177,7 +177,7 @@ export const addOracletoResponse = (
export const addMarketSlotToResponse = ( export const addMarketSlotToResponse = (
response: L2OrderBook | L3OrderBook, response: L2OrderBook | L3OrderBook,
driftClient: DriftClient, driftClient: IDriftClient,
marketType: MarketType, marketType: MarketType,
marketIndex: number marketIndex: number
): void => { ): void => {
@@ -418,7 +418,7 @@ export const validateWsSubscribeMsg = (
}; };
export const validateDlobQuery = ( export const validateDlobQuery = (
driftClient: DriftClient, driftClient: IDriftClient,
driftEnv: DriftEnv, driftEnv: DriftEnv,
marketType?: string, marketType?: string,
marketIndex?: string, marketIndex?: string,
@@ -571,7 +571,7 @@ export function errorHandler(
*/ */
export const getPhoenixSubscriber = ( export const getPhoenixSubscriber = (
driftClient: DriftClient, driftClient: IDriftClient,
marketConfig: SpotMarketConfig, marketConfig: SpotMarketConfig,
sdkConfig sdkConfig
): PhoenixSubscriber => { ): PhoenixSubscriber => {
@@ -586,7 +586,7 @@ export const getPhoenixSubscriber = (
}; };
export const getSerumSubscriber = ( export const getSerumSubscriber = (
driftClient: DriftClient, driftClient: IDriftClient,
marketConfig: SpotMarketConfig, marketConfig: SpotMarketConfig,
sdkConfig sdkConfig
): SerumSubscriber => { ): SerumSubscriber => {
@@ -601,7 +601,7 @@ export const getSerumSubscriber = (
}; };
export const getOpenbookSubscriber = ( export const getOpenbookSubscriber = (
driftClient: DriftClient, driftClient: IDriftClient,
marketConfig: SpotMarketConfig, marketConfig: SpotMarketConfig,
sdkConfig sdkConfig
): OpenbookV2Subscriber => { ): OpenbookV2Subscriber => {
@@ -748,7 +748,7 @@ export const convertRawL2ToBN = (rawL2: any): L2OrderBook => {
/** /**
* Get L2 orderbook data and calculate estimated prices * Get L2 orderbook data and calculate estimated prices
* @param driftClient - DriftClient instance * @param driftClient - IDriftClient instance
* @param marketType - MarketType enum * @param marketType - MarketType enum
* @param marketIndex - Market index number * @param marketIndex - Market index number
* @param direction - Position direction * @param direction - Position direction
@@ -759,7 +759,7 @@ export const convertRawL2ToBN = (rawL2: any): L2OrderBook => {
* @returns Price data object with oracle, best, entry, worst, and mark prices * @returns Price data object with oracle, best, entry, worst, and mark prices
*/ */
export const getEstimatedPrices = async ( export const getEstimatedPrices = async (
driftClient: DriftClient, driftClient: IDriftClient,
marketType: MarketType, marketType: MarketType,
marketIndex: number, marketIndex: number,
direction: PositionDirection, direction: PositionDirection,
@@ -857,14 +857,14 @@ export const getEstimatedPrices = async (
/** /**
* Maps AuctionParamArgs to the format expected by deriveMarketOrderParams * Maps AuctionParamArgs to the format expected by deriveMarketOrderParams
* @param params - AuctionParamArgs from the API request * @param params - AuctionParamArgs from the API request
* @param driftClient - DriftClient instance (optional, for price calculation) * @param driftClient - IDriftClient instance (optional, for price calculation)
* @param fetchFromRedis - Redis fetch function (optional, for price calculation) * @param fetchFromRedis - Redis fetch function (optional, for price calculation)
* @param selectMostRecentBySlot - Slot selection function (optional, for price calculation) * @param selectMostRecentBySlot - Slot selection function (optional, for price calculation)
* @returns Object formatted for deriveMarketOrderParams function or error response * @returns Object formatted for deriveMarketOrderParams function or error response
*/ */
export const mapToMarketOrderParams = async ( export const mapToMarketOrderParams = async (
params: AuctionParamArgs, params: AuctionParamArgs,
driftClient?: DriftClient, driftClient?: IDriftClient,
fetchFromRedis?: ( fetchFromRedis?: (
key: string, key: string,
selectionCriteria: (responses: any) => any selectionCriteria: (responses: any) => any
@@ -1064,14 +1064,14 @@ export const fetchL2FromRedis = async (
* @param direction - Position direction ('long' or 'short') * @param direction - Position direction ('long' or 'short')
* @param marketIndex - Market index number * @param marketIndex - Market index number
* @param marketType - Market type ('spot' or 'perp') * @param marketType - Market type ('spot' or 'perp')
* @param driftClient - DriftClient instance for oracle data * @param driftClient - IDriftClient instance for oracle data
* @param l2Formatted - Already formatted L2OrderBook data * @param l2Formatted - Already formatted L2OrderBook data
* @returns Dynamic slippage tolerance as a number * @returns Dynamic slippage tolerance as a number
*/ */
export const calculateDynamicSlippage = ( export const calculateDynamicSlippage = (
marketIndex: number, marketIndex: number,
marketType: string, marketType: string,
driftClient: DriftClient, driftClient: IDriftClient,
l2Formatted: L2OrderBook l2Formatted: L2OrderBook
): number => { ): number => {
// Determine if this is a major market (PERP with marketIndex 0, 1, or 2) // Determine if this is a major market (PERP with marketIndex 0, 1, or 2)
@@ -1128,7 +1128,7 @@ export const calculateDynamicSlippage = (
/** /**
* Get L2 orderbook data and calculate estimated prices using pre-fetched L2 data * Get L2 orderbook data and calculate estimated prices using pre-fetched L2 data
* @param driftClient - DriftClient instance * @param driftClient - IDriftClient instance
* @param marketType - MarketType enum * @param marketType - MarketType enum
* @param marketIndex - Market index number * @param marketIndex - Market index number
* @param direction - Position direction * @param direction - Position direction
@@ -1138,7 +1138,7 @@ export const calculateDynamicSlippage = (
* @returns Price data object with oracle, best, entry, worst, and mark prices * @returns Price data object with oracle, best, entry, worst, and mark prices
*/ */
export const getEstimatedPricesWithL2 = async ( export const getEstimatedPricesWithL2 = async (
driftClient: DriftClient, driftClient: IDriftClient,
marketType: MarketType, marketType: MarketType,
marketIndex: number, marketIndex: number,
direction: PositionDirection, direction: PositionDirection,