From 71ef9726b1b5f8213a10967a39765a4258bd58ff Mon Sep 17 00:00:00 2001 From: lil perp Date: Wed, 18 Oct 2023 16:04:06 -0400 Subject: [PATCH] sdk: add dlob filter function to getRestingLimitBids/Asks (#644) --- sdk/src/dlob/DLOB.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/sdk/src/dlob/DLOB.ts b/sdk/src/dlob/DLOB.ts index 267458bd3..f953973ea 100644 --- a/sdk/src/dlob/DLOB.ts +++ b/sdk/src/dlob/DLOB.ts @@ -73,6 +73,14 @@ export type MarketNodeLists = { type OrderBookCallback = () => void; +/** + * Receives a DLOBNode and is expected to return true if the node should + * be taken into account when generating, or false otherwise. + * + * Currently used in getRestingLimitBids and getRestingLimitAsks. + */ +export type DLOBFilterFcn = (node: DLOBNode) => boolean; + export type NodeToFill = { node: DLOBNode; makerNodes: DLOBNode[]; @@ -1065,7 +1073,8 @@ export class DLOB { currentDLOBNode: DLOBNode, slot: number, oraclePriceData: OraclePriceData - ) => boolean + ) => boolean, + filterFcn?: DLOBFilterFcn ): Generator { const generators = generatorList.map((generator) => { return { @@ -1102,6 +1111,11 @@ export class DLOB { continue; } + if (filterFcn && filterFcn(bestGenerator.next.value)) { + bestGenerator.next = bestGenerator.generator.next(); + continue; + } + yield bestGenerator.next.value; bestGenerator.next = bestGenerator.generator.next(); } else { @@ -1114,7 +1128,8 @@ export class DLOB { marketIndex: number, slot: number, marketType: MarketType, - oraclePriceData: OraclePriceData + oraclePriceData: OraclePriceData, + filterFcn?: DLOBFilterFcn ): Generator { if (isVariant(marketType, 'spot') && !oraclePriceData) { throw new Error('Must provide OraclePriceData to get spot asks'); @@ -1142,7 +1157,8 @@ export class DLOB { return bestNode .getPrice(oraclePriceData, slot) .lt(currentNode.getPrice(oraclePriceData, slot)); - } + }, + filterFcn ); } @@ -1150,7 +1166,8 @@ export class DLOB { marketIndex: number, slot: number, marketType: MarketType, - oraclePriceData: OraclePriceData + oraclePriceData: OraclePriceData, + filterFcn?: DLOBFilterFcn ): Generator { if (isVariant(marketType, 'spot') && !oraclePriceData) { throw new Error('Must provide OraclePriceData to get spot bids'); @@ -1178,7 +1195,8 @@ export class DLOB { return bestNode .getPrice(oraclePriceData, slot) .gt(currentNode.getPrice(oraclePriceData, slot)); - } + }, + filterFcn ); }