From 1b6845e8ab62ee6fea8b63e4eba04d3f904568ed Mon Sep 17 00:00:00 2001 From: Jack Waller Date: Thu, 27 Nov 2025 15:11:36 +1100 Subject: [PATCH 1/3] chore: test publish at least uncrossed 20 levels --- src/utils/utils.ts | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index fdea26d..b35c559 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -310,26 +310,39 @@ export function publishGroupings( asks: fullAggregatedAsks, }); - const aggregatedBids = fullAggregatedBids.slice(0, 20); - const aggregatedAsks = fullAggregatedAsks.slice(0, 20); + // Count crossed levels at the beginning + let crossedBids = 0; + const bestAsk = fullAggregatedAsks[0]?.[0]; + if (bestAsk !== undefined) { + for (const bid of fullAggregatedBids) { + if (bid[0] >= bestAsk) { + crossedBids++; + } else { + break; // Stop at first uncrossed level + } + } + } + + let crossedAsks = 0; + const bestBid = fullAggregatedBids[0]?.[0]; + if (bestBid !== undefined) { + for (const ask of fullAggregatedAsks) { + if (ask[0] <= bestBid) { + crossedAsks++; + } else { + break; // Stop at first uncrossed level + } + } + } + + const aggregatedBids = fullAggregatedBids.slice(0, 20 + crossedBids); + const aggregatedAsks = fullAggregatedAsks.slice(0, 20 + crossedAsks); + const l2Formatted_grouped20 = Object.assign({}, l2Formatted, { bids: aggregatedBids, asks: aggregatedAsks, }); - if ( - (['SOL-PERP', 'BTC-PERP', 'ETH-PERP'].includes( - l2Formatted_grouped20.marketName - ) && - aggregatedBids.length !== 20) || - aggregatedAsks.length !== 20 - ) { - logger.error( - `Error aggregating dlob levels: group=${group}, bids=${fullAggregatedBids.length}, asks=${fullAggregatedAsks.length}` - ); - logger.error(`Response: ${JSON.stringify(l2Formatted_grouped20)}`); - } - redisClient.publish( `${clientPrefix}orderbook_${marketType}_${ marketArgs.marketIndex From 792463c8f84a967bfc2d48fbd2f130c8509e8ccf Mon Sep 17 00:00:00 2001 From: Jack Waller Date: Thu, 27 Nov 2025 16:09:40 +1100 Subject: [PATCH 2/3] chore: test different logic --- src/utils/utils.ts | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index b35c559..47fbf08 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -312,32 +312,39 @@ export function publishGroupings( // Count crossed levels at the beginning let crossedBids = 0; - const bestAsk = fullAggregatedAsks[0]?.[0]; + const bestAsk = fullAggregatedAsks[0]?.price; if (bestAsk !== undefined) { for (const bid of fullAggregatedBids) { - if (bid[0] >= bestAsk) { + if (bid.price >= bestAsk) { crossedBids++; } else { - break; // Stop at first uncrossed level + break; } } } - + let crossedAsks = 0; - const bestBid = fullAggregatedBids[0]?.[0]; + const bestBid = fullAggregatedBids[0]?.price; if (bestBid !== undefined) { for (const ask of fullAggregatedAsks) { - if (ask[0] <= bestBid) { + if (ask.price <= bestBid) { crossedAsks++; } else { - break; // Stop at first uncrossed level + break; } } } - - const aggregatedBids = fullAggregatedBids.slice(0, 20 + crossedBids); - const aggregatedAsks = fullAggregatedAsks.slice(0, 20 + crossedAsks); - + + const maxCrossed = Math.max(crossedBids, crossedAsks); + const levelsToTake = 20 + maxCrossed; + + if (group === 10) { + console.log(bestAsk, bestBid, crossedAsks, crossedBids, levelsToTake); + } + + const aggregatedBids = fullAggregatedBids.slice(0, levelsToTake); + const aggregatedAsks = fullAggregatedAsks.slice(0, levelsToTake); + const l2Formatted_grouped20 = Object.assign({}, l2Formatted, { bids: aggregatedBids, asks: aggregatedAsks, @@ -719,7 +726,8 @@ export function createMarketBasedAuctionParams( // Set market-specific defaults (only used if values are undefined) const marketSpecificDefaults: Partial = { ...DEFAULT_AUCTION_PARAMS, - auctionStartPriceOffsetFrom: isMajorMarket && version === 1 ? 'mark' : 'bestOffer', + auctionStartPriceOffsetFrom: + isMajorMarket && version === 1 ? 'mark' : 'bestOffer', auctionStartPriceOffset: isMajorMarket && version === 1 ? 0 : -0.1, }; @@ -1490,8 +1498,11 @@ export const calculateDynamicSlippage = ( const minSlippage = parseFloat(process.env.DYNAMIC_SLIPPAGE_MIN || '0.035'); // 0.035% minimum const maxSlippage = parseFloat(process.env.DYNAMIC_SLIPPAGE_MAX || '5'); // 5% maximum - let finalSlippage = Math.min(Math.max(dynamicSlippage, minSlippage), maxSlippage); - + let finalSlippage = Math.min( + Math.max(dynamicSlippage, minSlippage), + maxSlippage + ); + // Apply 10% boost for API v2 if (apiVersion === 2) { finalSlippage = finalSlippage * 1.1; From 232dd28b98737db9b1c5eb597389448e5fd7f037 Mon Sep 17 00:00:00 2001 From: Jack Waller Date: Thu, 27 Nov 2025 16:28:26 +1100 Subject: [PATCH 3/3] chore: remove logging --- src/utils/utils.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 47fbf08..57a4ead 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -338,10 +338,6 @@ export function publishGroupings( const maxCrossed = Math.max(crossedBids, crossedAsks); const levelsToTake = 20 + maxCrossed; - if (group === 10) { - console.log(bestAsk, bestBid, crossedAsks, crossedBids, levelsToTake); - } - const aggregatedBids = fullAggregatedBids.slice(0, levelsToTake); const aggregatedAsks = fullAggregatedAsks.slice(0, levelsToTake);