@drift-labs/sdk 0.2.0-master.36 → 0.2.0-master.38
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/addresses/pda.js +2 -2
- package/lib/admin.d.ts +6 -3
- package/lib/admin.js +30 -3
- package/lib/clearingHouse.d.ts +17 -6
- package/lib/clearingHouse.js +162 -45
- package/lib/clearingHouseUser.js +8 -27
- package/lib/config.js +1 -1
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +26 -4
- package/lib/idl/clearing_house.json +425 -285
- package/lib/math/funding.js +3 -2
- package/lib/math/market.js +4 -6
- package/lib/math/position.d.ts +7 -1
- package/lib/math/position.js +18 -2
- package/lib/math/spotBalance.js +5 -5
- package/lib/types.d.ts +18 -16
- package/package.json +1 -1
- package/src/addresses/pda.ts +2 -2
- package/src/admin.ts +73 -13
- package/src/clearingHouse.ts +214 -54
- package/src/clearingHouseUser.ts +10 -26
- package/src/config.ts +1 -1
- package/src/factory/bigNum.ts +27 -4
- package/src/idl/clearing_house.json +425 -285
- package/src/math/funding.ts +3 -5
- package/src/math/market.ts +4 -6
- package/src/math/position.ts +18 -1
- package/src/math/spotBalance.ts +9 -9
- package/src/types.ts +18 -16
- package/tests/dlob/helpers.ts +47 -43
package/lib/clearingHouseUser.js
CHANGED
|
@@ -71,6 +71,7 @@ class ClearingHouseUser {
|
|
|
71
71
|
marketIndex,
|
|
72
72
|
quoteAssetAmount: numericConstants_1.ZERO,
|
|
73
73
|
quoteEntryAmount: numericConstants_1.ZERO,
|
|
74
|
+
quoteBreakEvenAmount: numericConstants_1.ZERO,
|
|
74
75
|
openOrders: 0,
|
|
75
76
|
openBids: numericConstants_1.ZERO,
|
|
76
77
|
openAsks: numericConstants_1.ZERO,
|
|
@@ -124,7 +125,7 @@ class ClearingHouseUser {
|
|
|
124
125
|
*/
|
|
125
126
|
getLPBidAsks(marketIndex) {
|
|
126
127
|
const position = this.getUserPosition(marketIndex);
|
|
127
|
-
if (position.lpShares.eq(numericConstants_1.ZERO)) {
|
|
128
|
+
if (position === undefined || position.lpShares.eq(numericConstants_1.ZERO)) {
|
|
128
129
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
129
130
|
}
|
|
130
131
|
const market = this.clearingHouse.getPerpMarketAccount(marketIndex);
|
|
@@ -443,30 +444,9 @@ class ClearingHouseUser {
|
|
|
443
444
|
perpPosition.baseAssetAmount =
|
|
444
445
|
settledPosition.baseAssetAmount.add(dustBaa);
|
|
445
446
|
perpPosition.quoteAssetAmount = settledPosition.quoteAssetAmount;
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
openAsks = market.amm.maxBaseAssetReserve
|
|
450
|
-
.sub(market.amm.baseAssetReserve)
|
|
451
|
-
.mul(perpPosition.lpShares)
|
|
452
|
-
.div(market.amm.sqrtK)
|
|
453
|
-
.mul(new _1.BN(-1));
|
|
454
|
-
}
|
|
455
|
-
else {
|
|
456
|
-
openAsks = numericConstants_1.ZERO;
|
|
457
|
-
}
|
|
458
|
-
let openBids;
|
|
459
|
-
if (market.amm.minBaseAssetReserve < market.amm.baseAssetReserve) {
|
|
460
|
-
openBids = market.amm.baseAssetReserve
|
|
461
|
-
.sub(market.amm.minBaseAssetReserve)
|
|
462
|
-
.mul(perpPosition.lpShares)
|
|
463
|
-
.div(market.amm.sqrtK);
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
openBids = numericConstants_1.ZERO;
|
|
467
|
-
}
|
|
468
|
-
perpPosition.openAsks = perpPosition.openAsks.add(openAsks);
|
|
469
|
-
perpPosition.openBids = perpPosition.openBids.add(openBids);
|
|
447
|
+
const [totalOpenBids, totalOpenAsks] = this.getPerpBidAsks(market.marketIndex);
|
|
448
|
+
perpPosition.openAsks = totalOpenAsks;
|
|
449
|
+
perpPosition.openBids = totalOpenBids;
|
|
470
450
|
}
|
|
471
451
|
let valuationPrice = this.getOracleDataForPerpMarket(market.marketIndex).price;
|
|
472
452
|
if ((0, types_1.isVariant)(market.status, 'settlement')) {
|
|
@@ -673,6 +653,7 @@ class ClearingHouseUser {
|
|
|
673
653
|
remainderBaseAssetAmount: 0,
|
|
674
654
|
quoteAssetAmount: new _1.BN(0),
|
|
675
655
|
lastCumulativeFundingRate: numericConstants_1.ZERO,
|
|
656
|
+
quoteBreakEvenAmount: new _1.BN(0),
|
|
676
657
|
quoteEntryAmount: new _1.BN(0),
|
|
677
658
|
openOrders: 0,
|
|
678
659
|
openBids: new _1.BN(0),
|
|
@@ -903,7 +884,7 @@ class ClearingHouseUser {
|
|
|
903
884
|
const precisionIncrease = numericConstants_1.TEN.pow(new _1.BN(spotMarket.decimals - 6));
|
|
904
885
|
const amountWithdrawable = freeCollateral
|
|
905
886
|
.mul(numericConstants_1.MARGIN_PRECISION)
|
|
906
|
-
.div(spotMarket.initialAssetWeight)
|
|
887
|
+
.div(new _1.BN(spotMarket.initialAssetWeight))
|
|
907
888
|
.mul(numericConstants_1.PRICE_PRECISION)
|
|
908
889
|
.div(oracleData.price)
|
|
909
890
|
.mul(precisionIncrease);
|
|
@@ -923,7 +904,7 @@ class ClearingHouseUser {
|
|
|
923
904
|
: freeCollateral;
|
|
924
905
|
const maxLiabilityAllowed = freeCollatAfterWithdraw
|
|
925
906
|
.mul(numericConstants_1.MARGIN_PRECISION)
|
|
926
|
-
.div(spotMarket.initialLiabilityWeight)
|
|
907
|
+
.div(new _1.BN(spotMarket.initialLiabilityWeight))
|
|
927
908
|
.mul(numericConstants_1.PRICE_PRECISION)
|
|
928
909
|
.div(oracleData.price)
|
|
929
910
|
.mul(precisionIncrease);
|
package/lib/config.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.configs = {
|
|
|
7
7
|
devnet: {
|
|
8
8
|
ENV: 'devnet',
|
|
9
9
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
10
|
-
CLEARING_HOUSE_PROGRAM_ID: '
|
|
10
|
+
CLEARING_HOUSE_PROGRAM_ID: 'jAEeKs9twxAJmXZHqS2p459xW7FMDjoyvuqthRo9qGS',
|
|
11
11
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
12
12
|
SERUM_V3: 'DESVgJVGajEgKGXhb6XmqDHGz3VjdgP7rEVESBgxmroY',
|
|
13
13
|
V2_ALPHA_TICKET_MINT_ADDRESS: 'DeEiGWfCMP9psnLGkxGrBBMEAW5Jv8bBGMN8DCtFRCyB',
|
package/lib/factory/bigNum.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export declare class BigNum {
|
|
|
5
5
|
precision: BN;
|
|
6
6
|
static delim: string;
|
|
7
7
|
static spacer: string;
|
|
8
|
+
static setLocale(locale: string): void;
|
|
8
9
|
constructor(val: BN | number | string, precisionVal?: BN | number | string);
|
|
9
10
|
private bigNumFromParam;
|
|
10
11
|
add(bn: BigNum): BigNum;
|
package/lib/factory/bigNum.js
CHANGED
|
@@ -10,6 +10,10 @@ class BigNum {
|
|
|
10
10
|
this.val = new anchor_1.BN(val);
|
|
11
11
|
this.precision = new anchor_1.BN(precisionVal);
|
|
12
12
|
}
|
|
13
|
+
static setLocale(locale) {
|
|
14
|
+
BigNum.delim = (1.1).toLocaleString(locale).slice(1, 2) || '.';
|
|
15
|
+
BigNum.spacer = (1000).toLocaleString(locale).slice(1, 2) || ',';
|
|
16
|
+
}
|
|
13
17
|
bigNumFromParam(bn) {
|
|
14
18
|
return anchor_1.BN.isBN(bn) ? BigNum.from(bn) : bn;
|
|
15
19
|
}
|
|
@@ -381,7 +385,21 @@ class BigNum {
|
|
|
381
385
|
* @returns
|
|
382
386
|
*/
|
|
383
387
|
toNum() {
|
|
384
|
-
|
|
388
|
+
let printedValue = this.print();
|
|
389
|
+
// Must convert any non-US delimiters and spacers to US format before using parseFloat
|
|
390
|
+
if (BigNum.delim !== '.' || BigNum.spacer !== ',') {
|
|
391
|
+
printedValue = printedValue
|
|
392
|
+
.split('')
|
|
393
|
+
.map((char) => {
|
|
394
|
+
if (char === BigNum.delim)
|
|
395
|
+
return '.';
|
|
396
|
+
if (char === BigNum.spacer)
|
|
397
|
+
return ',';
|
|
398
|
+
return char;
|
|
399
|
+
})
|
|
400
|
+
.join('');
|
|
401
|
+
}
|
|
402
|
+
return parseFloat(printedValue);
|
|
385
403
|
}
|
|
386
404
|
static fromJSON(json) {
|
|
387
405
|
return BigNum.from(new anchor_1.BN(json.val), new anchor_1.BN(json.precision));
|
|
@@ -407,10 +425,14 @@ class BigNum {
|
|
|
407
425
|
// Handle empty number edge cases
|
|
408
426
|
if (!val)
|
|
409
427
|
return BigNum.from(numericConstants_1.ZERO, precisionShift);
|
|
410
|
-
if (!val.replace(BigNum.delim, ''))
|
|
428
|
+
if (!val.replace(BigNum.delim, '')) {
|
|
411
429
|
return BigNum.from(numericConstants_1.ZERO, precisionShift);
|
|
412
|
-
|
|
413
|
-
const
|
|
430
|
+
}
|
|
431
|
+
const sides = val.split(BigNum.delim);
|
|
432
|
+
const rightSide = sides[1];
|
|
433
|
+
const leftSide = sides[0].replace(/\D/g, '');
|
|
434
|
+
const bnInput = `${leftSide !== null && leftSide !== void 0 ? leftSide : ''}${rightSide !== null && rightSide !== void 0 ? rightSide : ''}`;
|
|
435
|
+
const rawBn = new anchor_1.BN(bnInput);
|
|
414
436
|
const rightSideLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
|
|
415
437
|
const totalShift = precisionShift
|
|
416
438
|
? precisionShift.sub(new anchor_1.BN(rightSideLength))
|