@drift-labs/sdk 2.18.0-beta.1 → 2.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/dlob/DLOB.d.ts +1 -1
- package/lib/dlob/DLOB.js +2 -18
- package/lib/driftClient.js +2 -2
- package/lib/idl/drift.json +6 -1
- package/lib/math/spotBalance.js +9 -4
- package/lib/math/utils.d.ts +1 -0
- package/lib/math/utils.js +12 -1
- package/package.json +1 -1
- package/src/dlob/DLOB.ts +2 -39
- package/src/driftClient.ts +2 -2
- package/src/idl/drift.json +6 -1
- package/src/math/spotBalance.ts +11 -5
- package/src/math/utils.ts +13 -1
package/lib/dlob/DLOB.d.ts
CHANGED
|
@@ -88,7 +88,7 @@ export declare class DLOB {
|
|
|
88
88
|
getMakerLimitBids(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData, fallbackAsk?: BN): Generator<DLOBNode>;
|
|
89
89
|
getAsks(marketIndex: number, fallbackAsk: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
90
90
|
getBids(marketIndex: number, fallbackBid: BN | undefined, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): Generator<DLOBNode>;
|
|
91
|
-
findCrossingRestingLimitOrders(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData
|
|
91
|
+
findCrossingRestingLimitOrders(marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: OraclePriceData): NodeToFill[];
|
|
92
92
|
determineMakerAndTaker(askNode: DLOBNode, bidNode: DLOBNode): {
|
|
93
93
|
takerNode: DLOBNode;
|
|
94
94
|
makerNode: DLOBNode;
|
package/lib/dlob/DLOB.js
CHANGED
|
@@ -310,7 +310,7 @@ class DLOB {
|
|
|
310
310
|
}
|
|
311
311
|
findRestingLimitOrderNodesToFill(marketIndex, slot, marketType, oraclePriceData, isAmmPaused, minAuctionDuration, fallbackAsk, fallbackBid) {
|
|
312
312
|
const nodesToFill = new Array();
|
|
313
|
-
const crossingNodes = this.findCrossingRestingLimitOrders(marketIndex, slot, marketType, oraclePriceData
|
|
313
|
+
const crossingNodes = this.findCrossingRestingLimitOrders(marketIndex, slot, marketType, oraclePriceData);
|
|
314
314
|
for (const crossingNode of crossingNodes) {
|
|
315
315
|
nodesToFill.push(crossingNode);
|
|
316
316
|
}
|
|
@@ -711,7 +711,7 @@ class DLOB {
|
|
|
711
711
|
.gt(currentNode.getPrice(oraclePriceData, slot));
|
|
712
712
|
});
|
|
713
713
|
}
|
|
714
|
-
findCrossingRestingLimitOrders(marketIndex, slot, marketType, oraclePriceData
|
|
714
|
+
findCrossingRestingLimitOrders(marketIndex, slot, marketType, oraclePriceData) {
|
|
715
715
|
const nodesToFill = new Array();
|
|
716
716
|
for (const askNode of this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData)) {
|
|
717
717
|
const bidGenerator = this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData);
|
|
@@ -735,22 +735,6 @@ class DLOB {
|
|
|
735
735
|
continue;
|
|
736
736
|
}
|
|
737
737
|
const { takerNode, makerNode } = makerAndTaker;
|
|
738
|
-
// extra guard against bad fills for limit orders where auction is incomplete
|
|
739
|
-
if (!__1.isFallbackAvailableLiquiditySource(takerNode.order, minAuctionDuration, slot)) {
|
|
740
|
-
let bidPrice;
|
|
741
|
-
let askPrice;
|
|
742
|
-
if (__1.isVariant(takerNode.order.direction, 'long')) {
|
|
743
|
-
bidPrice = __1.BN.min(takerNode.getPrice(oraclePriceData, slot), fallbackAsk || __1.BN_MAX);
|
|
744
|
-
askPrice = makerNode.getPrice(oraclePriceData, slot);
|
|
745
|
-
}
|
|
746
|
-
else {
|
|
747
|
-
bidPrice = makerNode.getPrice(oraclePriceData, slot);
|
|
748
|
-
askPrice = __1.BN.max(takerNode.getPrice(oraclePriceData, slot), fallbackBid || __1.ZERO);
|
|
749
|
-
}
|
|
750
|
-
if (bidPrice.lt(askPrice)) {
|
|
751
|
-
continue;
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
738
|
const bidBaseRemaining = bidOrder.baseAssetAmount.sub(bidOrder.baseAssetAmountFilled);
|
|
755
739
|
const askBaseRemaining = askOrder.baseAssetAmount.sub(askOrder.baseAssetAmountFilled);
|
|
756
740
|
const baseFilled = __1.BN.min(bidBaseRemaining, askBaseRemaining);
|
package/lib/driftClient.js
CHANGED
|
@@ -1841,7 +1841,7 @@ class DriftClient {
|
|
|
1841
1841
|
? newTriggerCondition || openOrder.triggerCondition
|
|
1842
1842
|
: undefined,
|
|
1843
1843
|
oraclePriceOffset: newOraclePriceOffset || openOrder.oraclePriceOffset,
|
|
1844
|
-
auctionDuration: auctionDuration
|
|
1844
|
+
auctionDuration: auctionDuration !== null && auctionDuration !== void 0 ? auctionDuration : openOrder.auctionDuration,
|
|
1845
1845
|
maxTs: openOrder.maxTs,
|
|
1846
1846
|
auctionStartPrice: auctionStartPrice || openOrder.auctionStartPrice,
|
|
1847
1847
|
auctionEndPrice: auctionEndPrice || openOrder.auctionEndPrice,
|
|
@@ -2094,7 +2094,7 @@ class DriftClient {
|
|
|
2094
2094
|
writablePerpMarketIndexes: [marketIndex],
|
|
2095
2095
|
writableSpotMarketIndexes: [numericConstants_1.QUOTE_SPOT_MARKET_INDEX],
|
|
2096
2096
|
});
|
|
2097
|
-
const spotMarket = this.
|
|
2097
|
+
const spotMarket = this.getQuoteSpotMarketAccount();
|
|
2098
2098
|
return await this.program.instruction.resolvePerpBankruptcy(numericConstants_1.QUOTE_SPOT_MARKET_INDEX, marketIndex, {
|
|
2099
2099
|
accounts: {
|
|
2100
2100
|
state: await this.getStatePublicKey(),
|
package/lib/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.18.0
|
|
2
|
+
"version": "2.18.0",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -8596,6 +8596,11 @@
|
|
|
8596
8596
|
"code": 6226,
|
|
8597
8597
|
"name": "MarginOrdersOpen",
|
|
8598
8598
|
"msg": "MarginOrdersOpen"
|
|
8599
|
+
},
|
|
8600
|
+
{
|
|
8601
|
+
"code": 6227,
|
|
8602
|
+
"name": "TierViolationLiquidatingPerpPnl",
|
|
8603
|
+
"msg": "TierViolationLiquidatingPerpPnl"
|
|
8599
8604
|
}
|
|
8600
8605
|
]
|
|
8601
8606
|
}
|
package/lib/math/spotBalance.js
CHANGED
|
@@ -6,6 +6,7 @@ const anchor_1 = require("@project-serum/anchor");
|
|
|
6
6
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
7
|
const margin_1 = require("./margin");
|
|
8
8
|
const numericConstants_2 = require("../constants/numericConstants");
|
|
9
|
+
const utils_1 = require("./utils");
|
|
9
10
|
function getBalance(tokenAmount, spotMarket, balanceType) {
|
|
10
11
|
const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
|
|
11
12
|
const cumulativeInterest = types_1.isVariant(balanceType, 'deposit')
|
|
@@ -20,10 +21,14 @@ function getBalance(tokenAmount, spotMarket, balanceType) {
|
|
|
20
21
|
exports.getBalance = getBalance;
|
|
21
22
|
function getTokenAmount(balanceAmount, spotMarket, balanceType) {
|
|
22
23
|
const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
if (types_1.isVariant(balanceType, 'deposit')) {
|
|
25
|
+
return balanceAmount
|
|
26
|
+
.mul(spotMarket.cumulativeDepositInterest)
|
|
27
|
+
.div(precisionDecrease);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
return utils_1.divCeil(balanceAmount.mul(spotMarket.cumulativeBorrowInterest), precisionDecrease);
|
|
31
|
+
}
|
|
27
32
|
}
|
|
28
33
|
exports.getTokenAmount = getTokenAmount;
|
|
29
34
|
function getSignedTokenAmount(tokenAmount, balanceType) {
|
package/lib/math/utils.d.ts
CHANGED
package/lib/math/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.squareRootBN = exports.clampBN = void 0;
|
|
3
|
+
exports.divCeil = exports.squareRootBN = exports.clampBN = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
function clampBN(x, min, max) {
|
|
6
6
|
return __1.BN.max(min, __1.BN.min(x, max));
|
|
@@ -23,3 +23,14 @@ const squareRootBN = (n) => {
|
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
exports.squareRootBN = squareRootBN;
|
|
26
|
+
const divCeil = (a, b) => {
|
|
27
|
+
const quotient = a.div(b);
|
|
28
|
+
const remainder = a.mod(b);
|
|
29
|
+
if (remainder.gt(__1.ZERO)) {
|
|
30
|
+
return quotient.add(__1.ONE);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return quotient;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
exports.divCeil = divCeil;
|
package/package.json
CHANGED
package/src/dlob/DLOB.ts
CHANGED
|
@@ -24,8 +24,6 @@ import {
|
|
|
24
24
|
UserMap,
|
|
25
25
|
OrderRecord,
|
|
26
26
|
OrderActionRecord,
|
|
27
|
-
ZERO,
|
|
28
|
-
BN_MAX,
|
|
29
27
|
isRestingLimitOrder,
|
|
30
28
|
isTakingOrder,
|
|
31
29
|
isFallbackAvailableLiquiditySource,
|
|
@@ -535,10 +533,7 @@ export class DLOB {
|
|
|
535
533
|
marketIndex,
|
|
536
534
|
slot,
|
|
537
535
|
marketType,
|
|
538
|
-
oraclePriceData
|
|
539
|
-
minAuctionDuration,
|
|
540
|
-
fallbackAsk,
|
|
541
|
-
fallbackBid
|
|
536
|
+
oraclePriceData
|
|
542
537
|
);
|
|
543
538
|
|
|
544
539
|
for (const crossingNode of crossingNodes) {
|
|
@@ -1294,10 +1289,7 @@ export class DLOB {
|
|
|
1294
1289
|
marketIndex: number,
|
|
1295
1290
|
slot: number,
|
|
1296
1291
|
marketType: MarketType,
|
|
1297
|
-
oraclePriceData: OraclePriceData
|
|
1298
|
-
minAuctionDuration: number,
|
|
1299
|
-
fallbackAsk: BN | undefined,
|
|
1300
|
-
fallbackBid: BN | undefined
|
|
1292
|
+
oraclePriceData: OraclePriceData
|
|
1301
1293
|
): NodeToFill[] {
|
|
1302
1294
|
const nodesToFill = new Array<NodeToFill>();
|
|
1303
1295
|
|
|
@@ -1341,35 +1333,6 @@ export class DLOB {
|
|
|
1341
1333
|
|
|
1342
1334
|
const { takerNode, makerNode } = makerAndTaker;
|
|
1343
1335
|
|
|
1344
|
-
// extra guard against bad fills for limit orders where auction is incomplete
|
|
1345
|
-
if (
|
|
1346
|
-
!isFallbackAvailableLiquiditySource(
|
|
1347
|
-
takerNode.order,
|
|
1348
|
-
minAuctionDuration,
|
|
1349
|
-
slot
|
|
1350
|
-
)
|
|
1351
|
-
) {
|
|
1352
|
-
let bidPrice: BN;
|
|
1353
|
-
let askPrice: BN;
|
|
1354
|
-
if (isVariant(takerNode.order.direction, 'long')) {
|
|
1355
|
-
bidPrice = BN.min(
|
|
1356
|
-
takerNode.getPrice(oraclePriceData, slot),
|
|
1357
|
-
fallbackAsk || BN_MAX
|
|
1358
|
-
);
|
|
1359
|
-
askPrice = makerNode.getPrice(oraclePriceData, slot);
|
|
1360
|
-
} else {
|
|
1361
|
-
bidPrice = makerNode.getPrice(oraclePriceData, slot);
|
|
1362
|
-
askPrice = BN.max(
|
|
1363
|
-
takerNode.getPrice(oraclePriceData, slot),
|
|
1364
|
-
fallbackBid || ZERO
|
|
1365
|
-
);
|
|
1366
|
-
}
|
|
1367
|
-
|
|
1368
|
-
if (bidPrice.lt(askPrice)) {
|
|
1369
|
-
continue;
|
|
1370
|
-
}
|
|
1371
|
-
}
|
|
1372
|
-
|
|
1373
1336
|
const bidBaseRemaining = bidOrder.baseAssetAmount.sub(
|
|
1374
1337
|
bidOrder.baseAssetAmountFilled
|
|
1375
1338
|
);
|
package/src/driftClient.ts
CHANGED
|
@@ -3160,7 +3160,7 @@ export class DriftClient {
|
|
|
3160
3160
|
? newTriggerCondition || openOrder.triggerCondition
|
|
3161
3161
|
: undefined,
|
|
3162
3162
|
oraclePriceOffset: newOraclePriceOffset || openOrder.oraclePriceOffset,
|
|
3163
|
-
auctionDuration: auctionDuration
|
|
3163
|
+
auctionDuration: auctionDuration ?? openOrder.auctionDuration,
|
|
3164
3164
|
maxTs: openOrder.maxTs,
|
|
3165
3165
|
auctionStartPrice: auctionStartPrice || openOrder.auctionStartPrice,
|
|
3166
3166
|
auctionEndPrice: auctionEndPrice || openOrder.auctionEndPrice,
|
|
@@ -3695,7 +3695,7 @@ export class DriftClient {
|
|
|
3695
3695
|
writableSpotMarketIndexes: [QUOTE_SPOT_MARKET_INDEX],
|
|
3696
3696
|
});
|
|
3697
3697
|
|
|
3698
|
-
const spotMarket = this.
|
|
3698
|
+
const spotMarket = this.getQuoteSpotMarketAccount();
|
|
3699
3699
|
|
|
3700
3700
|
return await this.program.instruction.resolvePerpBankruptcy(
|
|
3701
3701
|
QUOTE_SPOT_MARKET_INDEX,
|
package/src/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.18.0
|
|
2
|
+
"version": "2.18.0",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -8596,6 +8596,11 @@
|
|
|
8596
8596
|
"code": 6226,
|
|
8597
8597
|
"name": "MarginOrdersOpen",
|
|
8598
8598
|
"msg": "MarginOrdersOpen"
|
|
8599
|
+
},
|
|
8600
|
+
{
|
|
8601
|
+
"code": 6227,
|
|
8602
|
+
"name": "TierViolationLiquidatingPerpPnl",
|
|
8603
|
+
"msg": "TierViolationLiquidatingPerpPnl"
|
|
8599
8604
|
}
|
|
8600
8605
|
]
|
|
8601
8606
|
}
|
package/src/math/spotBalance.ts
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
} from './margin';
|
|
22
22
|
import { OraclePriceData } from '../oracles/types';
|
|
23
23
|
import { PERCENTAGE_PRECISION } from '../constants/numericConstants';
|
|
24
|
+
import { divCeil } from './utils';
|
|
24
25
|
|
|
25
26
|
export function getBalance(
|
|
26
27
|
tokenAmount: BN,
|
|
@@ -49,11 +50,16 @@ export function getTokenAmount(
|
|
|
49
50
|
): BN {
|
|
50
51
|
const precisionDecrease = TEN.pow(new BN(19 - spotMarket.decimals));
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
if (isVariant(balanceType, 'deposit')) {
|
|
54
|
+
return balanceAmount
|
|
55
|
+
.mul(spotMarket.cumulativeDepositInterest)
|
|
56
|
+
.div(precisionDecrease);
|
|
57
|
+
} else {
|
|
58
|
+
return divCeil(
|
|
59
|
+
balanceAmount.mul(spotMarket.cumulativeBorrowInterest),
|
|
60
|
+
precisionDecrease
|
|
61
|
+
);
|
|
62
|
+
}
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
export function getSignedTokenAmount(
|
package/src/math/utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BN } from '../';
|
|
1
|
+
import { BN, ONE, ZERO } from '../';
|
|
2
2
|
|
|
3
3
|
export function clampBN(x: BN, min: BN, max: BN): BN {
|
|
4
4
|
return BN.max(min, BN.min(x, max));
|
|
@@ -21,3 +21,15 @@ export const squareRootBN = (n: BN): BN => {
|
|
|
21
21
|
return largeCand;
|
|
22
22
|
}
|
|
23
23
|
};
|
|
24
|
+
|
|
25
|
+
export const divCeil = (a: BN, b: BN): BN => {
|
|
26
|
+
const quotient = a.div(b);
|
|
27
|
+
|
|
28
|
+
const remainder = a.mod(b);
|
|
29
|
+
|
|
30
|
+
if (remainder.gt(ZERO)) {
|
|
31
|
+
return quotient.add(ONE);
|
|
32
|
+
} else {
|
|
33
|
+
return quotient;
|
|
34
|
+
}
|
|
35
|
+
};
|