Skip to content

Commit 375f386

Browse files
authored
sdk: handle unfillable reduce only orders (#1790)
* sdk: handle unfillable reduce only orders * fix dlob tests build errors * fix some test build errors
1 parent 59c678f commit 375f386

File tree

6 files changed

+160
-55
lines changed

6 files changed

+160
-55
lines changed

sdk/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
"build": "yarn clean && tsc -p tsconfig.json && tsc -p tsconfig.browser.json && node scripts/postbuild.js",
1616
"build:browser": "yarn clean && tsc -p tsconfig.json && tsc -p tsconfig.browser.json && node scripts/postbuild.js --force-env browser",
1717
"clean": "rm -rf lib",
18-
"test": "mocha -r ts-node/register tests/**/*.ts",
18+
"test": "mocha -r ts-node/register tests/**/*.ts --ignore 'tests/dlob/**/*.ts'",
1919
"test:inspect": "mocha --inspect-brk -r ts-node/register tests/**/*.ts",
2020
"test:bignum": "mocha -r ts-node/register tests/bn/**/*.ts",
2121
"test:ci": "mocha -r ts-node/register tests/ci/**/*.ts",
22+
"test:dlob": "mocha -r ts-node/register tests/dlob/**/*.ts",
2223
"patch-and-pub": "npm version patch --force && npm publish",
2324
"prettify": "prettier --check './src/***/*.ts'",
2425
"prettify:fix": "prettier --write './{src,tests}/***/*.ts'",

sdk/src/dlob/DLOB.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,23 @@ export class DLOB {
508508
new BN(slot)
509509
);
510510

511+
const stepSize = isVariant(marketType, 'perp')
512+
? (marketAccount as PerpMarketAccount).amm.orderStepSize
513+
: (marketAccount as SpotMarketAccount).orderStepSize;
514+
515+
const cancelReduceOnlyNodesToFill =
516+
this.findUnfillableReduceOnlyOrdersToCancel(
517+
marketIndex,
518+
marketType,
519+
stepSize
520+
);
521+
511522
return this.mergeNodesToFill(
512523
restingLimitOrderNodesToFill,
513524
takingOrderNodesToFill
514-
).concat(expiredNodesToFill);
525+
)
526+
.concat(expiredNodesToFill)
527+
.concat(cancelReduceOnlyNodesToFill);
515528
}
516529

517530
getMakerRebate(
@@ -1011,6 +1024,52 @@ export class DLOB {
10111024
return nodesToFill;
10121025
}
10131026

1027+
public findUnfillableReduceOnlyOrdersToCancel(
1028+
marketIndex: number,
1029+
marketType: MarketType,
1030+
stepSize: BN
1031+
): NodeToFill[] {
1032+
const nodesToFill = new Array<NodeToFill>();
1033+
1034+
const marketTypeStr = getVariant(marketType) as MarketTypeStr;
1035+
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
1036+
1037+
if (!nodeLists) {
1038+
return nodesToFill;
1039+
}
1040+
1041+
const generators = [
1042+
nodeLists.takingLimit.bid.getGenerator(),
1043+
nodeLists.restingLimit.bid.getGenerator(),
1044+
nodeLists.floatingLimit.bid.getGenerator(),
1045+
nodeLists.market.bid.getGenerator(),
1046+
nodeLists.signedMsg.bid.getGenerator(),
1047+
nodeLists.takingLimit.ask.getGenerator(),
1048+
nodeLists.restingLimit.ask.getGenerator(),
1049+
nodeLists.floatingLimit.ask.getGenerator(),
1050+
nodeLists.market.ask.getGenerator(),
1051+
nodeLists.signedMsg.ask.getGenerator(),
1052+
nodeLists.trigger.above.getGenerator(),
1053+
nodeLists.trigger.below.getGenerator(),
1054+
];
1055+
1056+
for (const generator of generators) {
1057+
for (const node of generator) {
1058+
if (!node.order.reduceOnly) {
1059+
continue;
1060+
}
1061+
1062+
if (node.baseAssetAmount.lt(stepSize)) {
1063+
nodesToFill.push({
1064+
node,
1065+
makerNodes: [],
1066+
});
1067+
}
1068+
}
1069+
}
1070+
1071+
return nodesToFill;
1072+
}
10141073
*getTakingBids<T extends MarketType>(
10151074
marketIndex: number,
10161075
marketType: T,

sdk/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export class OrderBitFlag {
196196
static readonly SignedMessage = 1;
197197
static readonly OracleTriggerMarket = 2;
198198
static readonly SafeTriggerOrder = 4;
199+
static readonly NewTriggerReduceOnly = 8;
199200
}
200201

201202
export class OrderAction {

0 commit comments

Comments
 (0)