From b4ed7cc31c70a1699e76fdc4a861b0305b78565c Mon Sep 17 00:00:00 2001 From: moosecat Date: Mon, 16 Jun 2025 09:38:31 -0700 Subject: [PATCH] indicative quotes v2 (#420) * indicative quotes v2 * bump common --- src/dlob-subscriber/DLOBSubscriberIO.ts | 165 ++++++++++++------------ src/index.ts | 65 ++++++---- 2 files changed, 121 insertions(+), 109 deletions(-) diff --git a/src/dlob-subscriber/DLOBSubscriberIO.ts b/src/dlob-subscriber/DLOBSubscriberIO.ts index b023028..0970ffa 100644 --- a/src/dlob-subscriber/DLOBSubscriberIO.ts +++ b/src/dlob-subscriber/DLOBSubscriberIO.ts @@ -145,98 +145,101 @@ export class DLOBSubscriberIO extends DLOBSubscriber { const mms = await this.indicativeQuotesRedisClient.smembers( `market_mms_${marketType}_${marketArgs.marketIndex}` ); - const mmQuotes = await Promise.all( + let mmQuotes: any = await Promise.all( mms.map((mm) => { return this.indicativeQuotesRedisClient.get( - `mm_quotes_${marketType}_${marketArgs.marketIndex}_${mm}` + `mm_quotes_v2_${marketType}_${marketArgs.marketIndex}_${mm}` ); }) ); + mmQuotes = mmQuotes.filter((x) => !!x); const nowMinus1000Ms = Date.now() - 1000; - for (const quote of mmQuotes) { + for (const quotes of mmQuotes) { try { - if (Number(quote['ts']) > nowMinus1000Ms) { - const indicativeBaseOrder: Order = { - status: OrderStatus.OPEN, - orderType: OrderType.LIMIT, - orderId: 0, - slot: new BN(this.slotSource.getSlot()), - marketIndex: marketArgs.marketIndex, - marketType: marketArgs.marketType, - baseAssetAmount: ZERO, - immediateOrCancel: false, - direction: PositionDirection.LONG, - oraclePriceOffset: 0, - maxTs: new BN(quote['ts'] + 1000), - reduceOnly: false, - triggerCondition: OrderTriggerCondition.ABOVE, - price: ZERO, - userOrderId: 0, - postOnly: true, - auctionDuration: 0, - auctionStartPrice: ZERO, - auctionEndPrice: ZERO, - // Rest are not necessary and set for type conforming - existingPositionDirection: PositionDirection.LONG, - triggerPrice: ZERO, - baseAssetAmountFilled: ZERO, - quoteAssetAmountFilled: ZERO, - quoteAssetAmount: ZERO, - bitFlags: 0, - postedSlotTail: 0, - }; + if (Number(quotes['ts']) > nowMinus1000Ms) { + for (const quote of quotes['quotes']) { + const indicativeBaseOrder: Order = { + status: OrderStatus.OPEN, + orderType: OrderType.LIMIT, + orderId: 0, + slot: new BN(this.slotSource.getSlot()), + marketIndex: marketArgs.marketIndex, + marketType: marketArgs.marketType, + baseAssetAmount: ZERO, + immediateOrCancel: false, + direction: PositionDirection.LONG, + oraclePriceOffset: 0, + maxTs: new BN(quote['ts'] + 1000), + reduceOnly: false, + triggerCondition: OrderTriggerCondition.ABOVE, + price: ZERO, + userOrderId: 0, + postOnly: true, + auctionDuration: 0, + auctionStartPrice: ZERO, + auctionEndPrice: ZERO, + // Rest are not necessary and set for type conforming + existingPositionDirection: PositionDirection.LONG, + triggerPrice: ZERO, + baseAssetAmountFilled: ZERO, + quoteAssetAmountFilled: ZERO, + quoteAssetAmount: ZERO, + bitFlags: 0, + postedSlotTail: 0, + }; - if (quote['bid_size'] && quote['bid_price'] != null) { - // Sanity check bid price and size + if (quote['bid_size'] && quote['bid_price'] != null) { + // Sanity check bid price and size - const indicativeBid: Order = Object.assign( - {}, - indicativeBaseOrder, - { - orderId: indicativeOrderId, - oraclePriceOffset: quote['is_oracle_offset'] - ? quote['bid_price'] - : 0, - price: quote['is_oracle_offset'] - ? 0 - : new BN(quote['bid_price']), - baseAssetAmount: new BN(quote['bid_size']), - direction: PositionDirection.LONG, - } - ); - this.dlob.insertOrder( - indicativeBid, - INDICATIVE_QUOTES_PUBKEY, - this.slotSource.getSlot(), - false - ); - indicativeOrderId += 1; - } + const indicativeBid: Order = Object.assign( + {}, + indicativeBaseOrder, + { + orderId: indicativeOrderId, + oraclePriceOffset: quote['is_oracle_offset'] + ? quote['bid_price'] + : 0, + price: quote['is_oracle_offset'] + ? 0 + : new BN(quote['bid_price']), + baseAssetAmount: new BN(quote['bid_size']), + direction: PositionDirection.LONG, + } + ); + this.dlob.insertOrder( + indicativeBid, + INDICATIVE_QUOTES_PUBKEY, + this.slotSource.getSlot(), + false + ); + indicativeOrderId += 1; + } - if (quote['ask_size'] && quote['ask_price'] != null) { - const indicativeAsk: Order = Object.assign( - {}, - indicativeBaseOrder, - { - orderId: indicativeOrderId, - oraclePriceOffset: quote['is_oracle_offset'] - ? quote['ask_price'] - : 0, - price: quote['is_oracle_offset'] - ? 0 - : new BN(quote['ask_price']), - baseAssetAmount: new BN(quote['ask_size']), - direction: PositionDirection.SHORT, - } - ); - this.dlob.insertOrder( - indicativeAsk, - INDICATIVE_QUOTES_PUBKEY, - this.slotSource.getSlot(), - false - ); - indicativeOrderId += 1; + if (quote['ask_size'] && quote['ask_price'] != null) { + const indicativeAsk: Order = Object.assign( + {}, + indicativeBaseOrder, + { + orderId: indicativeOrderId, + oraclePriceOffset: quote['is_oracle_offset'] + ? quote['ask_price'] + : 0, + price: quote['is_oracle_offset'] + ? 0 + : new BN(quote['ask_price']), + baseAssetAmount: new BN(quote['ask_size']), + direction: PositionDirection.SHORT, + } + ); + this.dlob.insertOrder( + indicativeAsk, + INDICATIVE_QUOTES_PUBKEY, + this.slotSource.getSlot(), + false + ); + indicativeOrderId += 1; + } } } } catch (error) { diff --git a/src/index.ts b/src/index.ts index 352e8b4..4cb4dd3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -786,14 +786,20 @@ const main = async (): Promise => { try { // Check origin validation const origin = req.get('Origin') || req.get('Referer'); - const allowedOrigins = ['https://app.drift.trade', 'https://beta.drift.trade']; - - const hasAuth = ( - (req.headers.Authorization || req.headers.authorization) === - process.env.INTERNAL_SECRET - ) + const allowedOrigins = [ + 'https://app.drift.trade', + 'https://beta.drift.trade', + ]; - if (!hasAuth && (!origin || !allowedOrigins.some(allowed => origin.startsWith(allowed)))) { + const hasAuth = + (req.headers.Authorization || req.headers.authorization) === + process.env.INTERNAL_SECRET; + + if ( + !hasAuth && + (!origin || + !allowedOrigins.some((allowed) => origin.startsWith(allowed))) + ) { res.status(403).json({ error: 'Forbidden: Invalid origin' }); return; } @@ -815,23 +821,20 @@ const main = async (): Promise => { channel: 'real_time', jsonBinaryEncoding: 'hex', }; - - const latestPriceRes = await fetch( - pythLazerEndpoint, - { - method: 'POST', - headers: { - Authorization: `Bearer ${pythLazerDriftToken}`, - }, - body: JSON.stringify(latestLazerPricePayload), - } - ).then((res) => res.json()); - + + const latestPriceRes = await fetch(pythLazerEndpoint, { + method: 'POST', + headers: { + Authorization: `Bearer ${pythLazerDriftToken}`, + }, + body: JSON.stringify(latestLazerPricePayload), + }).then((res) => res.json()); + const data = latestPriceRes.solana.data; if (data) { res.status(200).json({ - data + data, }); return; @@ -848,14 +851,20 @@ const main = async (): Promise => { try { // Check origin validation const origin = req.get('Origin') || req.get('Referer'); - const allowedOrigins = ['https://app.drift.trade', 'https://beta.drift.trade']; - - const hasAuth = ( + const allowedOrigins = [ + 'https://app.drift.trade', + 'https://beta.drift.trade', + ]; + + const hasAuth = (req.headers.Authorization || req.headers.authorization) === - process.env.INTERNAL_SECRET - ) - - if (!hasAuth && (!origin || !allowedOrigins.some(allowed => origin.startsWith(allowed)))) { + process.env.INTERNAL_SECRET; + + if ( + !hasAuth && + (!origin || + !allowedOrigins.some((allowed) => origin.startsWith(allowed))) + ) { res.status(403).json({ error: 'Forbidden: Invalid origin' }); return; } @@ -881,7 +890,7 @@ const main = async (): Promise => { if (data) { res.status(200).json({ - data + data, }); return;