@drift-labs/sdk 0.1.21-master.0 → 0.1.21-master.3
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/accounts/bulkAccountLoader.d.ts +0 -1
- package/lib/accounts/bulkAccountLoader.js +4 -2
- package/lib/accounts/bulkUserSubscription.d.ts +0 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +5 -2
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +23 -1
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +0 -1
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +3 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +13 -0
- package/lib/accounts/types.d.ts +8 -3
- package/lib/accounts/utils.d.ts +0 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +0 -1
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +5 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +25 -0
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +3 -2
- package/lib/accounts/webSocketUserAccountSubscriber.js +11 -0
- package/lib/addresses.d.ts +4 -1
- package/lib/addresses.js +28 -1
- package/lib/admin.d.ts +10 -4
- package/lib/admin.js +48 -3
- package/lib/assert/assert.d.ts +0 -1
- package/lib/clearingHouse.d.ts +27 -2
- package/lib/clearingHouse.js +307 -7
- package/lib/clearingHouseUser.d.ts +15 -17
- package/lib/clearingHouseUser.js +105 -84
- package/lib/config.d.ts +0 -1
- package/lib/constants/markets.d.ts +0 -1
- package/lib/constants/markets.js +8 -0
- package/lib/constants/numericConstants.d.ts +1 -1
- package/lib/constants/numericConstants.js +2 -1
- package/lib/examples/makeTradeExample.d.ts +0 -1
- package/lib/factory/clearingHouse.d.ts +0 -1
- package/lib/factory/clearingHouseUser.d.ts +0 -1
- package/lib/idl/clearing_house.json +1066 -39
- package/lib/index.d.ts +3 -1
- package/lib/index.js +3 -0
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +31 -8
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/funding.d.ts +0 -1
- package/lib/math/insuranceFund.d.ts +0 -1
- package/lib/math/market.d.ts +2 -2
- package/lib/math/market.js +11 -1
- package/lib/math/orders.d.ts +3 -0
- package/lib/math/orders.js +32 -0
- package/lib/math/position.d.ts +4 -2
- package/lib/math/position.js +18 -4
- package/lib/math/trade.d.ts +0 -1
- package/lib/math/utils.d.ts +0 -1
- package/lib/mockUSDCFaucet.d.ts +0 -1
- package/lib/orderParams.d.ts +7 -0
- package/lib/orderParams.js +108 -0
- package/lib/orders.d.ts +6 -0
- package/lib/orders.js +136 -0
- package/lib/pythClient.d.ts +0 -1
- package/lib/token/index.d.ts +0 -1
- package/lib/tx/defaultTxSender.d.ts +0 -1
- package/lib/tx/types.d.ts +0 -1
- package/lib/tx/utils.d.ts +0 -1
- package/lib/types.d.ts +145 -2
- package/lib/types.js +36 -1
- package/lib/util/computeUnits.d.ts +0 -1
- package/lib/util/tps.d.ts +0 -1
- package/lib/wallet.d.ts +0 -1
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +3 -3
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +33 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +30 -2
- package/src/accounts/types.ts +11 -2
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +47 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +29 -2
- package/src/addresses.ts +37 -0
- package/src/admin.ts +84 -6
- package/src/clearingHouse.ts +430 -5
- package/src/clearingHouseUser.ts +164 -102
- package/src/constants/markets.ts +8 -0
- package/src/constants/numericConstants.ts +1 -0
- package/src/idl/clearing_house.json +1066 -39
- package/src/index.ts +3 -0
- package/src/math/amm.ts +47 -14
- package/src/math/market.ts +28 -2
- package/src/math/orders.ts +44 -0
- package/src/math/position.ts +23 -3
- package/src/orderParams.ts +151 -0
- package/src/orders.ts +236 -0
- package/src/types.ts +129 -1
- package/tsconfig.json +0 -1
- package/lib/accounts/bulkAccountLoader.d.ts.map +0 -1
- package/lib/accounts/bulkUserSubscription.d.ts.map +0 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/pollingUserAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/types.d.ts.map +0 -1
- package/lib/accounts/utils.d.ts.map +0 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts.map +0 -1
- package/lib/addresses.d.ts.map +0 -1
- package/lib/admin.d.ts.map +0 -1
- package/lib/assert/assert.d.ts.map +0 -1
- package/lib/clearingHouse.d.ts.map +0 -1
- package/lib/clearingHouseUser.d.ts.map +0 -1
- package/lib/config.d.ts.map +0 -1
- package/lib/constants/markets.d.ts.map +0 -1
- package/lib/constants/numericConstants.d.ts.map +0 -1
- package/lib/examples/makeTradeExample.d.ts.map +0 -1
- package/lib/factory/clearingHouse.d.ts.map +0 -1
- package/lib/factory/clearingHouseUser.d.ts.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/math/amm.d.ts.map +0 -1
- package/lib/math/conversion.d.ts.map +0 -1
- package/lib/math/funding.d.ts.map +0 -1
- package/lib/math/insuranceFund.d.ts.map +0 -1
- package/lib/math/market.d.ts.map +0 -1
- package/lib/math/position.d.ts.map +0 -1
- package/lib/math/trade.d.ts.map +0 -1
- package/lib/math/utils.d.ts.map +0 -1
- package/lib/mockUSDCFaucet.d.ts.map +0 -1
- package/lib/pythClient.d.ts.map +0 -1
- package/lib/token/index.d.ts.map +0 -1
- package/lib/tx/defaultTxSender.d.ts.map +0 -1
- package/lib/tx/types.d.ts.map +0 -1
- package/lib/tx/utils.d.ts.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/util/computeUnits.d.ts.map +0 -1
- package/lib/util/tps.d.ts.map +0 -1
- package/lib/wallet.d.ts.map +0 -1
package/lib/clearingHouseUser.js
CHANGED
|
@@ -73,6 +73,9 @@ class ClearingHouseUser {
|
|
|
73
73
|
getUserPositionsAccount() {
|
|
74
74
|
return this.accountSubscriber.getUserPositionsAccount();
|
|
75
75
|
}
|
|
76
|
+
getUserOrdersAccount() {
|
|
77
|
+
return this.accountSubscriber.getUserOrdersAccount();
|
|
78
|
+
}
|
|
76
79
|
/**
|
|
77
80
|
* Gets the user's current position for a given market. If the user has no position returns undefined
|
|
78
81
|
* @param marketIndex
|
|
@@ -87,8 +90,23 @@ class ClearingHouseUser {
|
|
|
87
90
|
lastCumulativeFundingRate: numericConstants_1.ZERO,
|
|
88
91
|
marketIndex,
|
|
89
92
|
quoteAssetAmount: numericConstants_1.ZERO,
|
|
93
|
+
openOrders: numericConstants_1.ZERO,
|
|
90
94
|
};
|
|
91
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* @param orderId
|
|
98
|
+
* @returns Order
|
|
99
|
+
*/
|
|
100
|
+
getOrder(orderId) {
|
|
101
|
+
return this.getUserOrdersAccount().orders.find((order) => order.orderId.eq(orderId));
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @param userOrderId
|
|
105
|
+
* @returns Order
|
|
106
|
+
*/
|
|
107
|
+
getOrderByUserOrderId(userOrderId) {
|
|
108
|
+
return this.getUserOrdersAccount().orders.find((order) => order.userOrderId === userOrderId);
|
|
109
|
+
}
|
|
92
110
|
getUserAccountPublicKey() {
|
|
93
111
|
return __awaiter(this, void 0, void 0, function* () {
|
|
94
112
|
if (this.userAccountPublicKey) {
|
|
@@ -98,6 +116,15 @@ class ClearingHouseUser {
|
|
|
98
116
|
return this.userAccountPublicKey;
|
|
99
117
|
});
|
|
100
118
|
}
|
|
119
|
+
getUserOrdersAccountPublicKey() {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
if (this.userOrdersAccountPublicKey) {
|
|
122
|
+
return this.userOrdersAccountPublicKey;
|
|
123
|
+
}
|
|
124
|
+
this.userOrdersAccountPublicKey = yield _1.getUserOrdersAccountPublicKey(this.clearingHouse.program.programId, yield this.getUserAccountPublicKey());
|
|
125
|
+
return this.userOrdersAccountPublicKey;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
101
128
|
exists() {
|
|
102
129
|
return __awaiter(this, void 0, void 0, function* () {
|
|
103
130
|
const userAccountPublicKey = yield this.getUserAccountPublicKey();
|
|
@@ -323,6 +350,7 @@ class ClearingHouseUser {
|
|
|
323
350
|
baseAssetAmount: currentMarketPositionBaseSize.add(positionBaseSizeChange),
|
|
324
351
|
lastCumulativeFundingRate: new _1.BN(0),
|
|
325
352
|
quoteAssetAmount: new _1.BN(0),
|
|
353
|
+
openOrders: new _1.BN(0),
|
|
326
354
|
};
|
|
327
355
|
const market = this.clearingHouse.getMarket(proposedMarketPosition.marketIndex);
|
|
328
356
|
const proposedMarketPositionValueUSDC = _1.calculateBaseAssetValue(market, proposedMarketPosition);
|
|
@@ -383,14 +411,14 @@ class ClearingHouseUser {
|
|
|
383
411
|
liquidationPrice(targetMarket, positionBaseSizeChange = numericConstants_1.ZERO, partial = false) {
|
|
384
412
|
// solves formula for example calc below
|
|
385
413
|
/* example: assume BTC price is $40k (examine 10% up/down)
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
414
|
+
|
|
415
|
+
if 10k deposit and levered 10x short BTC => BTC up $400 means:
|
|
416
|
+
1. higher base_asset_value (+$4k)
|
|
417
|
+
2. lower collateral (-$4k)
|
|
418
|
+
3. (10k - 4k)/(100k + 4k) => 6k/104k => .0576
|
|
419
|
+
|
|
420
|
+
for 10x long, BTC down $400:
|
|
421
|
+
3. (10k - 4k) / (100k - 4k) = 6k/96k => .0625 */
|
|
394
422
|
const tc = this.getTotalCollateral();
|
|
395
423
|
const tpv = this.getTotalPositionValue();
|
|
396
424
|
const partialLev = 16;
|
|
@@ -408,6 +436,7 @@ class ClearingHouseUser {
|
|
|
408
436
|
baseAssetAmount: proposedBaseAssetAmount,
|
|
409
437
|
lastCumulativeFundingRate: currentMarketPosition.lastCumulativeFundingRate,
|
|
410
438
|
quoteAssetAmount: new _1.BN(0),
|
|
439
|
+
openOrders: new _1.BN(0),
|
|
411
440
|
};
|
|
412
441
|
const market = this.clearingHouse.getMarket(proposedMarketPosition.marketIndex);
|
|
413
442
|
const proposedMarketPositionValueUSDC = _1.calculateBaseAssetValue(market, proposedMarketPosition);
|
|
@@ -484,42 +513,32 @@ class ClearingHouseUser {
|
|
|
484
513
|
}
|
|
485
514
|
/**
|
|
486
515
|
* Get the maximum trade size for a given market, taking into account the user's current leverage, positions, collateral, etc.
|
|
487
|
-
*
|
|
488
|
-
* To Calculate Max Quote Available:
|
|
489
|
-
*
|
|
490
|
-
* Case 1: SameSide
|
|
491
|
-
* => Remaining quote to get to maxLeverage
|
|
492
|
-
*
|
|
493
|
-
* Case 2: NOT SameSide && currentLeverage <= maxLeverage
|
|
494
|
-
* => Current opposite position x2 + remaining to get to maxLeverage
|
|
495
|
-
*
|
|
496
|
-
* Case 3: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition > maxLeverage
|
|
497
|
-
* => strictly reduce current position size
|
|
498
|
-
*
|
|
499
|
-
* Case 4: NOT SameSide && currentLeverage > maxLeverage && otherPositions - currentPosition < maxLeverage
|
|
500
|
-
* => current position + remaining to get to maxLeverage
|
|
501
|
-
*
|
|
502
516
|
* @param marketIndex
|
|
503
517
|
* @param tradeSide
|
|
504
518
|
* @param userMaxLeverageSetting - leverage : Precision TEN_THOUSAND
|
|
505
519
|
* @returns tradeSizeAllowed : Precision QUOTE_PRECISION
|
|
506
520
|
*/
|
|
507
521
|
getMaxTradeSizeUSDC(targetMarketIndex, tradeSide, userMaxLeverageSetting) {
|
|
522
|
+
// inline function which get's the current position size on the opposite side of the target trade
|
|
523
|
+
const getOppositePositionValueUSDC = () => {
|
|
524
|
+
if (!currentPosition)
|
|
525
|
+
return numericConstants_1.ZERO;
|
|
526
|
+
const side = tradeSide === _1.PositionDirection.SHORT ? 'short' : 'long';
|
|
527
|
+
if (side === 'long' && (currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())) {
|
|
528
|
+
return this.getPositionValue(targetMarketIndex);
|
|
529
|
+
}
|
|
530
|
+
else if (side === 'short' &&
|
|
531
|
+
!(currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())) {
|
|
532
|
+
return this.getPositionValue(targetMarketIndex);
|
|
533
|
+
}
|
|
534
|
+
return numericConstants_1.ZERO;
|
|
535
|
+
};
|
|
508
536
|
const currentPosition = this.getUserPosition(targetMarketIndex) ||
|
|
509
537
|
this.getEmptyPosition(targetMarketIndex);
|
|
510
|
-
const targetSide = tradeSide === _1.PositionDirection.SHORT ? 'short' : 'long';
|
|
511
|
-
const currentPositionSide = (currentPosition === null || currentPosition === void 0 ? void 0 : currentPosition.baseAssetAmount.isNeg())
|
|
512
|
-
? 'short'
|
|
513
|
-
: 'long';
|
|
514
|
-
const targettingSameSide = !currentPosition
|
|
515
|
-
? true
|
|
516
|
-
: targetSide === currentPositionSide;
|
|
517
|
-
// add any position we have on the opposite side of the current trade, because we can "flip" the size of this position without taking any extra leverage.
|
|
518
|
-
const oppositeSizeValueUSDC = targettingSameSide
|
|
519
|
-
? numericConstants_1.ZERO
|
|
520
|
-
: this.getPositionValue(targetMarketIndex);
|
|
521
538
|
// get current leverage
|
|
522
539
|
const currentLeverage = this.getLeverage();
|
|
540
|
+
// remaining leverage
|
|
541
|
+
// let remainingLeverage = userMaxLeverageSetting;
|
|
523
542
|
const remainingLeverage = _1.BN.max(userMaxLeverageSetting.sub(currentLeverage), numericConstants_1.ZERO);
|
|
524
543
|
// get total collateral
|
|
525
544
|
const totalCollateral = this.getTotalCollateral();
|
|
@@ -527,43 +546,9 @@ class ClearingHouseUser {
|
|
|
527
546
|
let maxPositionSize = remainingLeverage
|
|
528
547
|
.mul(totalCollateral)
|
|
529
548
|
.div(numericConstants_1.TEN_THOUSAND);
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
// do nothing
|
|
534
|
-
}
|
|
535
|
-
else {
|
|
536
|
-
// case 2 : trade where current total position less than max, but need to account for flipping the current position over to the other side
|
|
537
|
-
maxPositionSize = maxPositionSize.add(oppositeSizeValueUSDC.mul(new _1.BN(2)));
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
else {
|
|
541
|
-
// current leverage is greater than max leverage - can only reduce position size
|
|
542
|
-
if (!targettingSameSide) {
|
|
543
|
-
const currentPositionQuoteSize = this.getPositionValue(targetMarketIndex);
|
|
544
|
-
const currentTotalQuoteSize = currentLeverage
|
|
545
|
-
.mul(totalCollateral)
|
|
546
|
-
.div(numericConstants_1.TEN_THOUSAND);
|
|
547
|
-
const otherPositionsTotalQuoteSize = currentTotalQuoteSize.sub(currentPositionQuoteSize);
|
|
548
|
-
const quoteValueOfMaxLeverage = userMaxLeverageSetting
|
|
549
|
-
.mul(totalCollateral)
|
|
550
|
-
.div(numericConstants_1.TEN_THOUSAND);
|
|
551
|
-
if (otherPositionsTotalQuoteSize
|
|
552
|
-
.sub(currentPositionQuoteSize)
|
|
553
|
-
.gte(quoteValueOfMaxLeverage)) {
|
|
554
|
-
// case 3: Can only reduce the current position because it will still be greater than max leverage
|
|
555
|
-
maxPositionSize = currentPositionQuoteSize;
|
|
556
|
-
}
|
|
557
|
-
else {
|
|
558
|
-
// case 4: Can reduce the position, and then take extra remaining quote to get to max leverage
|
|
559
|
-
const allowedQuoteSizeAfterClosingCurrentPosition = quoteValueOfMaxLeverage.sub(otherPositionsTotalQuoteSize);
|
|
560
|
-
maxPositionSize = currentPositionQuoteSize.add(allowedQuoteSizeAfterClosingCurrentPosition);
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
else {
|
|
564
|
-
// do nothing if targetting same side
|
|
565
|
-
}
|
|
566
|
-
}
|
|
549
|
+
// add any position we have on the opposite side of the current trade, because we can "flip" the size of this position without taking any extra leverage.
|
|
550
|
+
const oppositeSizeValueUSDC = getOppositePositionValueUSDC();
|
|
551
|
+
maxPositionSize = maxPositionSize.add(oppositeSizeValueUSDC.mul(new _1.BN(2)));
|
|
567
552
|
// subtract oneMillionth of maxPositionSize
|
|
568
553
|
// => to avoid rounding errors when taking max leverage
|
|
569
554
|
const oneMilli = maxPositionSize.div(numericConstants_1.QUOTE_PRECISION);
|
|
@@ -592,18 +577,12 @@ class ClearingHouseUser {
|
|
|
592
577
|
.add(tradeQuoteAmount)
|
|
593
578
|
.abs();
|
|
594
579
|
const totalPositionAfterTradeExcludingTargetMarket = this.getTotalPositionValueExcludingMarket(targetMarketIndex);
|
|
595
|
-
const
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
.div(totalCollateral);
|
|
602
|
-
return newLeverage;
|
|
603
|
-
}
|
|
604
|
-
else {
|
|
605
|
-
return new _1.BN(0);
|
|
606
|
-
}
|
|
580
|
+
const newLeverage = currentMarketPositionAfterTrade
|
|
581
|
+
.add(totalPositionAfterTradeExcludingTargetMarket)
|
|
582
|
+
.abs()
|
|
583
|
+
.mul(numericConstants_1.TEN_THOUSAND)
|
|
584
|
+
.div(this.getTotalCollateral());
|
|
585
|
+
return newLeverage;
|
|
607
586
|
}
|
|
608
587
|
/**
|
|
609
588
|
* Calculates how much fee will be taken for a given sized trade
|
|
@@ -626,9 +605,51 @@ class ClearingHouseUser {
|
|
|
626
605
|
this.getEmptyPosition(marketToIgnore);
|
|
627
606
|
let currentMarketPositionValueUSDC = numericConstants_1.ZERO;
|
|
628
607
|
if (currentMarketPosition) {
|
|
629
|
-
|
|
608
|
+
const market = this.clearingHouse.getMarket(currentMarketPosition.marketIndex);
|
|
609
|
+
currentMarketPositionValueUSDC = _1.calculateBaseAssetValue(market, currentMarketPosition);
|
|
630
610
|
}
|
|
631
611
|
return this.getTotalPositionValue().sub(currentMarketPositionValueUSDC);
|
|
632
612
|
}
|
|
613
|
+
canFillOrder(order) {
|
|
614
|
+
const userAccount = this.getUserAccount();
|
|
615
|
+
const userPositionsAccount = this.getUserPositionsAccount();
|
|
616
|
+
const userPosition = this.getUserPosition(order.marketIndex);
|
|
617
|
+
const market = this.clearingHouse.getMarket(order.marketIndex);
|
|
618
|
+
if (position_1.isEmptyPosition(userPosition)) {
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
const newState = _1.calculateNewStateAfterOrder(userAccount, userPosition, market, order);
|
|
622
|
+
if (newState === null) {
|
|
623
|
+
return false;
|
|
624
|
+
}
|
|
625
|
+
const [userAccountAfter, userPositionAfter, marketAfter] = newState;
|
|
626
|
+
const totalPositionValue = userPositionsAccount.positions.reduce((positionValue, marketPosition) => {
|
|
627
|
+
let market = this.clearingHouse.getMarket(marketPosition.marketIndex);
|
|
628
|
+
if (marketPosition.marketIndex.eq(order.marketIndex)) {
|
|
629
|
+
market = marketAfter;
|
|
630
|
+
marketPosition = userPositionAfter;
|
|
631
|
+
}
|
|
632
|
+
return positionValue.add(_1.calculateBaseAssetValue(market, marketPosition));
|
|
633
|
+
}, numericConstants_1.ZERO);
|
|
634
|
+
if (totalPositionValue.eq(numericConstants_1.ZERO)) {
|
|
635
|
+
return true;
|
|
636
|
+
}
|
|
637
|
+
const unrealizedPnL = userPositionsAccount.positions.reduce((pnl, marketPosition) => {
|
|
638
|
+
let market = this.clearingHouse.getMarket(marketPosition.marketIndex);
|
|
639
|
+
pnl = pnl.add(_1.calculatePositionFundingPNL(market, marketPosition).div(numericConstants_1.PRICE_TO_QUOTE_PRECISION));
|
|
640
|
+
if (marketPosition.marketIndex.eq(order.marketIndex)) {
|
|
641
|
+
market = marketAfter;
|
|
642
|
+
marketPosition = userPositionAfter;
|
|
643
|
+
}
|
|
644
|
+
// update
|
|
645
|
+
return pnl.add(_1.calculatePositionPNL(market, marketPosition, false));
|
|
646
|
+
}, numericConstants_1.ZERO);
|
|
647
|
+
const totalCollateral = userAccountAfter.collateral.add(unrealizedPnL);
|
|
648
|
+
const marginRatioAfter = totalCollateral
|
|
649
|
+
.mul(numericConstants_1.TEN_THOUSAND)
|
|
650
|
+
.div(totalPositionValue);
|
|
651
|
+
const marginRatioInitial = this.clearingHouse.getStateAccount().marginRatioInitial;
|
|
652
|
+
return marginRatioAfter.gte(marginRatioInitial);
|
|
653
|
+
}
|
|
633
654
|
}
|
|
634
655
|
exports.ClearingHouseUser = ClearingHouseUser;
|
package/lib/config.d.ts
CHANGED
package/lib/constants/markets.js
CHANGED
|
@@ -99,6 +99,14 @@ exports.Markets = [
|
|
|
99
99
|
mainnetPythOracle: '8JPJJkmDScpcNmBRKGZuPuG2GYAveQgP3t5gFuMymwvF',
|
|
100
100
|
launchTs: 1644382122000,
|
|
101
101
|
},
|
|
102
|
+
{
|
|
103
|
+
symbol: 'LTC-PERP',
|
|
104
|
+
baseAssetSymbol: 'LTC',
|
|
105
|
+
marketIndex: new __1.BN(12),
|
|
106
|
+
devnetPythOracle: 'BLArYBCUYhdWiY8PCUTpvFE21iaJq85dvxLk9bYMobcU',
|
|
107
|
+
mainnetPythOracle: '8RMnV1eD55iqUFJLMguPkYBkq8DCtx81XcmAja93LvRR',
|
|
108
|
+
launchTs: 1645027429000,
|
|
109
|
+
},
|
|
102
110
|
// {
|
|
103
111
|
// symbol: 'mSOL-PERP',
|
|
104
112
|
// baseAssetSymbol: 'mSOL',
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { BN } from '../';
|
|
3
3
|
export declare const ZERO: BN;
|
|
4
4
|
export declare const ONE: BN;
|
|
5
|
+
export declare const TWO: BN;
|
|
5
6
|
export declare const TEN_THOUSAND: BN;
|
|
6
7
|
export declare const BN_MAX: BN;
|
|
7
8
|
export declare const MAX_LEVERAGE: BN;
|
|
@@ -16,4 +17,3 @@ export declare const BASE_PRECISION: BN;
|
|
|
16
17
|
export declare const AMM_TO_QUOTE_PRECISION_RATIO: BN;
|
|
17
18
|
export declare const PRICE_TO_QUOTE_PRECISION: BN;
|
|
18
19
|
export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: BN;
|
|
19
|
-
//# sourceMappingURL=numericConstants.d.ts.map
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.PARTIAL_LIQUIDATION_RATIO = exports.FULL_LIQUIDATION_RATIO = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.ONE = exports.ZERO = void 0;
|
|
3
|
+
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.PARTIAL_LIQUIDATION_RATIO = exports.FULL_LIQUIDATION_RATIO = exports.MAX_LEVERAGE = exports.BN_MAX = exports.TEN_THOUSAND = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
exports.ZERO = new __1.BN(0);
|
|
6
6
|
exports.ONE = new __1.BN(1);
|
|
7
|
+
exports.TWO = new __1.BN(2);
|
|
7
8
|
exports.TEN_THOUSAND = new __1.BN(10000);
|
|
8
9
|
exports.BN_MAX = new __1.BN(Number.MAX_SAFE_INTEGER);
|
|
9
10
|
exports.MAX_LEVERAGE = new __1.BN(5);
|
|
@@ -23,4 +23,3 @@ export declare function getPollingClearingHouseConfig(connection: Connection, wa
|
|
|
23
23
|
export declare function getClearingHouse(config: ClearingHouseConfig): ClearingHouse;
|
|
24
24
|
export declare function getAdmin(config: ClearingHouseConfig): Admin;
|
|
25
25
|
export {};
|
|
26
|
-
//# sourceMappingURL=clearingHouse.d.ts.map
|
|
@@ -17,4 +17,3 @@ export declare function getWebSocketClearingHouseUserConfig(clearingHouse: Clear
|
|
|
17
17
|
export declare function getPollingClearingHouseUserConfig(clearingHouse: ClearingHouse, authority: PublicKey, accountLoader: BulkAccountLoader): PollingClearingHouseUserConfig;
|
|
18
18
|
export declare function getClearingHouseUser(config: ClearingHouseUserConfig): ClearingHouseUser;
|
|
19
19
|
export {};
|
|
20
|
-
//# sourceMappingURL=clearingHouseUser.d.ts.map
|