@drift-labs/sdk 2.149.1 → 2.150.0-alpha.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/.env +4 -0
- package/VERSION +1 -1
- package/lib/browser/constants/perpMarkets.js +11 -0
- package/lib/browser/constants/spotMarkets.js +13 -0
- package/lib/browser/decode/user.js +2 -2
- package/lib/browser/driftClient.d.ts +20 -8
- package/lib/browser/driftClient.js +216 -17
- package/lib/browser/idl/drift.json +225 -21
- package/lib/browser/math/margin.js +2 -1
- package/lib/browser/math/position.d.ts +1 -0
- package/lib/browser/math/position.js +10 -2
- package/lib/browser/math/superStake.d.ts +3 -2
- package/lib/browser/types.d.ts +12 -6
- package/lib/browser/types.js +11 -6
- package/lib/browser/user.d.ts +3 -2
- package/lib/browser/user.js +24 -8
- package/lib/node/constants/perpMarkets.d.ts.map +1 -1
- package/lib/node/constants/perpMarkets.js +11 -0
- package/lib/node/constants/spotMarkets.d.ts.map +1 -1
- package/lib/node/constants/spotMarkets.js +13 -0
- package/lib/node/decode/user.d.ts.map +1 -1
- package/lib/node/decode/user.js +2 -2
- package/lib/node/driftClient.d.ts +20 -8
- package/lib/node/driftClient.d.ts.map +1 -1
- package/lib/node/driftClient.js +216 -17
- package/lib/node/idl/drift.json +225 -21
- package/lib/node/math/margin.d.ts.map +1 -1
- package/lib/node/math/margin.js +2 -1
- package/lib/node/math/position.d.ts +1 -0
- package/lib/node/math/position.d.ts.map +1 -1
- package/lib/node/math/position.js +10 -2
- package/lib/node/math/spotBalance.d.ts.map +1 -1
- package/lib/node/math/superStake.d.ts +3 -2
- package/lib/node/math/superStake.d.ts.map +1 -1
- package/lib/node/types.d.ts +12 -6
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/types.js +11 -6
- package/lib/node/user.d.ts +3 -2
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +24 -8
- package/package.json +1 -1
- package/scripts/deposit-isolated-positions.ts +110 -0
- package/scripts/single-grpc-client-test.ts +71 -21
- package/scripts/withdraw-isolated-positions.ts +174 -0
- package/src/constants/perpMarkets.ts +11 -0
- package/src/constants/spotMarkets.ts +14 -0
- package/src/decode/user.ts +2 -3
- package/src/driftClient.ts +464 -41
- package/src/idl/drift.json +226 -22
- package/src/margin/README.md +143 -0
- package/src/math/margin.ts +3 -4
- package/src/math/position.ts +12 -2
- package/src/math/spotBalance.ts +0 -1
- package/src/types.ts +15 -7
- package/src/user.ts +49 -15
- package/tests/amm/test.ts +1 -1
- package/tests/dlob/helpers.ts +1 -1
- package/tests/user/test.ts +0 -7
package/src/user.ts
CHANGED
|
@@ -204,6 +204,7 @@ export class User {
|
|
|
204
204
|
}
|
|
205
205
|
|
|
206
206
|
public async unsubscribe(): Promise<void> {
|
|
207
|
+
this.eventEmitter.removeAllListeners();
|
|
207
208
|
await this.accountSubscriber.unsubscribe();
|
|
208
209
|
this.isSubscribed = false;
|
|
209
210
|
}
|
|
@@ -352,6 +353,10 @@ export class User {
|
|
|
352
353
|
};
|
|
353
354
|
}
|
|
354
355
|
|
|
356
|
+
public isPositionEmpty(position: PerpPosition): boolean {
|
|
357
|
+
return position.baseAssetAmount.eq(ZERO) && position.openOrders === 0;
|
|
358
|
+
}
|
|
359
|
+
|
|
355
360
|
public getIsolatePerpPositionTokenAmount(perpMarketIndex: number): BN {
|
|
356
361
|
const perpPosition = this.getPerpPosition(perpMarketIndex);
|
|
357
362
|
if (!perpPosition) return ZERO;
|
|
@@ -485,7 +490,8 @@ export class User {
|
|
|
485
490
|
marketIndex: number,
|
|
486
491
|
collateralBuffer = ZERO,
|
|
487
492
|
enterHighLeverageMode = undefined,
|
|
488
|
-
maxMarginRatio = undefined
|
|
493
|
+
maxMarginRatio = undefined,
|
|
494
|
+
positionType: 'isolated' | 'cross' = 'cross'
|
|
489
495
|
): BN {
|
|
490
496
|
const perpPosition = this.getPerpPositionOrEmpty(marketIndex);
|
|
491
497
|
|
|
@@ -499,10 +505,33 @@ export class User {
|
|
|
499
505
|
)
|
|
500
506
|
: ZERO;
|
|
501
507
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
508
|
+
let freeCollateral: BN = ZERO;
|
|
509
|
+
// if position is isolated, we always add on available quote from the cross account
|
|
510
|
+
if (positionType === 'isolated') {
|
|
511
|
+
const {
|
|
512
|
+
totalAssetValue: quoteSpotMarketAssetValue,
|
|
513
|
+
totalLiabilityValue: quoteSpotMarketLiabilityValue,
|
|
514
|
+
} = this.getSpotMarketAssetAndLiabilityValue(
|
|
515
|
+
perpMarket.quoteSpotMarketIndex,
|
|
516
|
+
'Initial',
|
|
517
|
+
undefined,
|
|
518
|
+
undefined,
|
|
519
|
+
true
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
freeCollateral = quoteSpotMarketAssetValue.sub(
|
|
523
|
+
quoteSpotMarketLiabilityValue
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// adding free collateral from the cross account or from within isolated margin calc for this marketIndex
|
|
528
|
+
freeCollateral = freeCollateral.add(
|
|
529
|
+
this.getFreeCollateral(
|
|
530
|
+
'Initial',
|
|
531
|
+
enterHighLeverageMode,
|
|
532
|
+
positionType === 'isolated' ? marketIndex : undefined
|
|
533
|
+
).sub(collateralBuffer)
|
|
534
|
+
);
|
|
506
535
|
|
|
507
536
|
return this.getPerpBuyingPowerFromFreeCollateralAndBaseAssetAmount(
|
|
508
537
|
marketIndex,
|
|
@@ -544,17 +573,20 @@ export class User {
|
|
|
544
573
|
enterHighLeverageMode = false,
|
|
545
574
|
perpMarketIndex?: number
|
|
546
575
|
): BN {
|
|
547
|
-
const
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
});
|
|
576
|
+
const calc = this.getMarginCalculation(marginCategory, {
|
|
577
|
+
enteringHighLeverage: enterHighLeverageMode,
|
|
578
|
+
strict: marginCategory === 'Initial',
|
|
579
|
+
});
|
|
552
580
|
|
|
553
581
|
if (perpMarketIndex !== undefined) {
|
|
554
|
-
|
|
582
|
+
// getIsolatedFreeCollateral will throw if no existing isolated position but we are fetching for potential new position, so we wrap in a try/catch
|
|
583
|
+
try {
|
|
584
|
+
return calc.getIsolatedFreeCollateral(perpMarketIndex);
|
|
585
|
+
} catch (error) {
|
|
586
|
+
return ZERO;
|
|
587
|
+
}
|
|
555
588
|
} else {
|
|
556
|
-
|
|
557
|
-
return freeCollateral.gte(ZERO) ? freeCollateral : ZERO;
|
|
589
|
+
return calc.getCrossFreeCollateral();
|
|
558
590
|
}
|
|
559
591
|
}
|
|
560
592
|
|
|
@@ -2752,7 +2784,8 @@ export class User {
|
|
|
2752
2784
|
tradeSide: PositionDirection,
|
|
2753
2785
|
isLp = false,
|
|
2754
2786
|
enterHighLeverageMode = undefined,
|
|
2755
|
-
maxMarginRatio = undefined
|
|
2787
|
+
maxMarginRatio = undefined,
|
|
2788
|
+
positionType: 'isolated' | 'cross' = 'cross'
|
|
2756
2789
|
): { tradeSize: BN; oppositeSideTradeSize: BN } {
|
|
2757
2790
|
let tradeSize = ZERO;
|
|
2758
2791
|
let oppositeSideTradeSize = ZERO;
|
|
@@ -2792,7 +2825,8 @@ export class User {
|
|
|
2792
2825
|
targetMarketIndex,
|
|
2793
2826
|
lpBuffer,
|
|
2794
2827
|
enterHighLeverageMode,
|
|
2795
|
-
maxMarginRatio
|
|
2828
|
+
maxMarginRatio,
|
|
2829
|
+
positionType
|
|
2796
2830
|
);
|
|
2797
2831
|
|
|
2798
2832
|
if (maxPositionSize.gte(ZERO)) {
|
package/tests/amm/test.ts
CHANGED
package/tests/dlob/helpers.ts
CHANGED
package/tests/user/test.ts
CHANGED
|
@@ -49,7 +49,6 @@ async function makeMockUser(
|
|
|
49
49
|
oraclePriceMap[myMockSpotMarkets[i].oracle.toString()] =
|
|
50
50
|
spotOraclePriceList[i];
|
|
51
51
|
}
|
|
52
|
-
// console.log(oraclePriceMap);
|
|
53
52
|
|
|
54
53
|
function getMockUserAccount(): UserAccount {
|
|
55
54
|
return myMockUserAccount;
|
|
@@ -61,12 +60,6 @@ async function makeMockUser(
|
|
|
61
60
|
return myMockSpotMarkets[marketIndex];
|
|
62
61
|
}
|
|
63
62
|
function getMockOracle(oracleKey: PublicKey) {
|
|
64
|
-
// console.log('oracleKey.toString():', oracleKey.toString());
|
|
65
|
-
// console.log(
|
|
66
|
-
// 'oraclePriceMap[oracleKey.toString()]:',
|
|
67
|
-
// oraclePriceMap[oracleKey.toString()]
|
|
68
|
-
// );
|
|
69
|
-
|
|
70
63
|
const QUOTE_ORACLE_PRICE_DATA: OraclePriceData = {
|
|
71
64
|
price: new BN(
|
|
72
65
|
oraclePriceMap[oracleKey.toString()] * PRICE_PRECISION.toNumber()
|