Skip to content

Commit

Permalink
sdk: add dlob filter function to getRestingLimitBids/Asks (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney authored Oct 18, 2023
1 parent d17af92 commit 71ef972
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions sdk/src/dlob/DLOB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -1065,7 +1073,8 @@ export class DLOB {
currentDLOBNode: DLOBNode,
slot: number,
oraclePriceData: OraclePriceData
) => boolean
) => boolean,
filterFcn?: DLOBFilterFcn
): Generator<DLOBNode> {
const generators = generatorList.map((generator) => {
return {
Expand Down Expand Up @@ -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 {
Expand All @@ -1114,7 +1128,8 @@ export class DLOB {
marketIndex: number,
slot: number,
marketType: MarketType,
oraclePriceData: OraclePriceData
oraclePriceData: OraclePriceData,
filterFcn?: DLOBFilterFcn
): Generator<DLOBNode> {
if (isVariant(marketType, 'spot') && !oraclePriceData) {
throw new Error('Must provide OraclePriceData to get spot asks');
Expand Down Expand Up @@ -1142,15 +1157,17 @@ export class DLOB {
return bestNode
.getPrice(oraclePriceData, slot)
.lt(currentNode.getPrice(oraclePriceData, slot));
}
},
filterFcn
);
}

*getRestingLimitBids(
marketIndex: number,
slot: number,
marketType: MarketType,
oraclePriceData: OraclePriceData
oraclePriceData: OraclePriceData,
filterFcn?: DLOBFilterFcn
): Generator<DLOBNode> {
if (isVariant(marketType, 'spot') && !oraclePriceData) {
throw new Error('Must provide OraclePriceData to get spot bids');
Expand Down Expand Up @@ -1178,7 +1195,8 @@ export class DLOB {
return bestNode
.getPrice(oraclePriceData, slot)
.gt(currentNode.getPrice(oraclePriceData, slot));
}
},
filterFcn
);
}

Expand Down

0 comments on commit 71ef972

Please sign in to comment.