flash-sdk 2.3.16 → 2.3.17
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/OraclePrice.d.ts +1 -1
- package/dist/OraclePrice.js +17 -8
- package/dist/PerpetualsClient.d.ts +5 -6
- package/dist/PerpetualsClient.js +231 -193
- package/dist/PoolConfig.json +229 -15
- package/dist/PoolDataClient.js +3 -3
- package/dist/constants/index.d.ts +1 -0
- package/dist/constants/index.js +2 -1
- package/dist/idl/perpetuals.d.ts +420 -14
- package/dist/idl/perpetuals.js +420 -14
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/index.d.ts +14 -5
- package/package.json +1 -1
package/dist/PerpetualsClient.js
CHANGED
@@ -341,7 +341,7 @@ var PerpetualsClient = (function () {
|
|
341
341
|
}
|
342
342
|
});
|
343
343
|
}); };
|
344
|
-
this.addPool = function (name, maxAumUsd, permissions, metadataSymbol, metadataTitle, metadataUri) { return __awaiter(_this, void 0, void 0, function () {
|
344
|
+
this.addPool = function (name, maxAumUsd, permissions, metadataSymbol, metadataTitle, metadataUri, stakingFeeShareBps, vpVolumeFactor) { return __awaiter(_this, void 0, void 0, function () {
|
345
345
|
return __generator(this, function (_a) {
|
346
346
|
switch (_a.label) {
|
347
347
|
case 0: return [4, this.program.methods
|
@@ -351,7 +351,9 @@ var PerpetualsClient = (function () {
|
|
351
351
|
permissions: permissions,
|
352
352
|
metadataSymbol: metadataSymbol,
|
353
353
|
metadataTitle: metadataTitle,
|
354
|
-
metadataUri: metadataUri
|
354
|
+
metadataUri: metadataUri,
|
355
|
+
stakingFeeShareBps: stakingFeeShareBps,
|
356
|
+
vpVolumeFactor: vpVolumeFactor
|
355
357
|
})
|
356
358
|
.accounts({
|
357
359
|
admin: this.provider.wallet.publicKey,
|
@@ -584,7 +586,7 @@ var PerpetualsClient = (function () {
|
|
584
586
|
throw new Error("exponent mistach");
|
585
587
|
}
|
586
588
|
var minMaxPrice = _this.getMinAndMaxOraclePriceSync(inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount);
|
587
|
-
var fee = _this.getFeeHelper(types_1.FeesAction.AddLiquidity, amountIn, constants_1.BN_ZERO, inputTokenCustodyAccount, minMaxPrice.max
|
589
|
+
var fee = _this.getFeeHelper(types_1.FeesAction.AddLiquidity, amountIn, constants_1.BN_ZERO, inputTokenCustodyAccount, minMaxPrice.max, poolAumUsdMax, poolAccount, poolConfig).feeAmount;
|
588
590
|
var tokenAmountUsd = minMaxPrice.min.getAssetAmountUsd((amountIn.sub(fee)), inputTokenCustodyAccount.decimals);
|
589
591
|
var lpTokenOut;
|
590
592
|
if (poolAumUsdMax.isZero()) {
|
@@ -612,13 +614,13 @@ var PerpetualsClient = (function () {
|
|
612
614
|
else {
|
613
615
|
removeAmount = minMaxPrice.max.getTokenAmount(removeAmountUsd, outputTokenCustodyAccount.decimals);
|
614
616
|
}
|
615
|
-
var fee = _this.getFeeHelper(types_1.FeesAction.RemoveLiquidity, constants_1.BN_ZERO, removeAmount, outputTokenCustodyAccount, minMaxPrice.max
|
617
|
+
var fee = _this.getFeeHelper(types_1.FeesAction.RemoveLiquidity, constants_1.BN_ZERO, removeAmount, outputTokenCustodyAccount, minMaxPrice.max, poolAumUsdMax, poolAccount, poolConfig).feeAmount;
|
616
618
|
return {
|
617
619
|
tokenAmountOut: removeAmount.sub(fee),
|
618
620
|
fee: fee,
|
619
621
|
};
|
620
622
|
};
|
621
|
-
this.getFeeHelper = function (action, amountAdd, amountRemove, inputTokenCustodyAccount,
|
623
|
+
this.getFeeHelper = function (action, amountAdd, amountRemove, inputTokenCustodyAccount, maxOraclePrice, poolAumUsdMax, poolAccount, poolConfig) {
|
622
624
|
var fees;
|
623
625
|
switch (action) {
|
624
626
|
case types_1.FeesAction.AddLiquidity:
|
@@ -637,7 +639,7 @@ var PerpetualsClient = (function () {
|
|
637
639
|
if (inputTokenCustodyAccount.fees.mode == types_1.FeesMode.Fixed) {
|
638
640
|
return { feeBps: fees.minFee, feeAmount: anchor_1.BN.max(amountAdd, amountRemove).mul(fees.minFee).div(new anchor_1.BN(constants_1.RATE_POWER)) };
|
639
641
|
}
|
640
|
-
var newRatio = _this.getNewRatioHelper(amountAdd, amountRemove, inputTokenCustodyAccount,
|
642
|
+
var newRatio = _this.getNewRatioHelper(amountAdd, amountRemove, inputTokenCustodyAccount, maxOraclePrice, poolAumUsdMax);
|
641
643
|
var inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.custodyAccount.equals(inputTokenCustodyAccount.publicKey); });
|
642
644
|
var index = inputCustodyConfig.custodyId;
|
643
645
|
var ratios = poolAccount.ratios[index];
|
@@ -674,45 +676,43 @@ var PerpetualsClient = (function () {
|
|
674
676
|
return { feeBps: fee, feeAmount: feeAmount };
|
675
677
|
};
|
676
678
|
this.getMinAndMaxOraclePriceSync = function (price, emaPrice, custodyAccount) {
|
677
|
-
var
|
678
|
-
var
|
679
|
+
var maxPrice = price;
|
680
|
+
var minPrice = price;
|
679
681
|
var divergenceBps;
|
680
682
|
if (custodyAccount.isStable) {
|
681
|
-
|
682
|
-
price: new anchor_1.BN(10).pow(
|
683
|
-
exponent:
|
684
|
-
confidence:
|
685
|
-
timestamp:
|
686
|
-
})
|
683
|
+
var oneUsdPrice = OraclePrice_1.OraclePrice.from({
|
684
|
+
price: new anchor_1.BN(10).pow(maxPrice.exponent.abs()),
|
685
|
+
exponent: maxPrice.exponent,
|
686
|
+
confidence: maxPrice.confidence,
|
687
|
+
timestamp: maxPrice.timestamp
|
688
|
+
});
|
689
|
+
divergenceBps = maxPrice.getDivergenceFactor(oneUsdPrice);
|
687
690
|
}
|
688
691
|
else {
|
689
|
-
|
690
|
-
divergenceBps = maxPriceUsd.getDeviationFactor(emaPriceInPriceDecimals);
|
692
|
+
divergenceBps = maxPrice.getDivergenceFactor(emaPrice);
|
691
693
|
}
|
692
694
|
if (divergenceBps.gte(custodyAccount.oracle.maxDivergenceBps)) {
|
693
|
-
var
|
694
|
-
var confBps = (maxPriceUsd.confidence.muln(constants_1.BPS_POWER)).div(maxPriceUsd.price);
|
695
|
+
var confBps = (maxPrice.confidence.muln(constants_1.BPS_POWER)).div(maxPrice.price);
|
695
696
|
if (confBps.lt(custodyAccount.oracle.maxConfBps)) {
|
696
|
-
|
697
|
-
|
698
|
-
minPriceUsd.price = maxPriceUsd.price.sub(confScale);
|
697
|
+
minPrice.price = maxPrice.price.sub(maxPrice.confidence);
|
698
|
+
maxPrice.price = maxPrice.price.add(maxPrice.confidence);
|
699
699
|
return {
|
700
|
-
min:
|
701
|
-
max:
|
700
|
+
min: minPrice,
|
701
|
+
max: maxPrice
|
702
702
|
};
|
703
703
|
}
|
704
704
|
else {
|
705
|
-
|
705
|
+
minPrice.price = maxPrice.price.sub(maxPrice.confidence);
|
706
706
|
return {
|
707
|
-
min:
|
708
|
-
max:
|
707
|
+
min: minPrice,
|
708
|
+
max: maxPrice
|
709
709
|
};
|
710
710
|
}
|
711
711
|
}
|
712
712
|
else {
|
713
713
|
return {
|
714
|
-
min:
|
715
|
-
max:
|
714
|
+
min: maxPrice,
|
715
|
+
max: maxPrice
|
716
716
|
};
|
717
717
|
}
|
718
718
|
};
|
@@ -720,10 +720,10 @@ var PerpetualsClient = (function () {
|
|
720
720
|
var minPrice = price;
|
721
721
|
var divergenceBps;
|
722
722
|
if (custodyAccount.isStable) {
|
723
|
-
divergenceBps = price.
|
723
|
+
divergenceBps = price.getDivergenceFactor(OraclePrice_1.OraclePrice.from({ price: new anchor_1.BN(10).pow(price.exponent.abs()), exponent: price.exponent, confidence: price.confidence, timestamp: price.timestamp }));
|
724
724
|
}
|
725
725
|
else {
|
726
|
-
divergenceBps = price.
|
726
|
+
divergenceBps = price.getDivergenceFactor(emaPrice);
|
727
727
|
}
|
728
728
|
if (divergenceBps.gte(custodyAccount.oracle.maxDivergenceBps)) {
|
729
729
|
var factorBps = (divergenceBps.mul(new anchor_1.BN(constants_1.BPS_POWER))).div(custodyAccount.oracle.maxDivergenceBps);
|
@@ -757,10 +757,10 @@ var PerpetualsClient = (function () {
|
|
757
757
|
}
|
758
758
|
var deviation;
|
759
759
|
if (custodyAccount.isStable) {
|
760
|
-
deviation = price.
|
760
|
+
deviation = price.getDivergenceFactor(OraclePrice_1.OraclePrice.from({ price: new anchor_1.BN(10).pow(price.exponent.abs()), exponent: price.exponent, confidence: price.confidence, timestamp: price.timestamp }));
|
761
761
|
}
|
762
762
|
else {
|
763
|
-
deviation = price.
|
763
|
+
deviation = price.getDivergenceFactor(emaPrice);
|
764
764
|
}
|
765
765
|
if (deviation.gte(custodyAccount.oracle.maxDivergenceBps)) {
|
766
766
|
var confFactor = (price.confidence.muln(constants_1.BPS_POWER)).div(price.price);
|
@@ -802,11 +802,10 @@ var PerpetualsClient = (function () {
|
|
802
802
|
finalCollateralAmount = swapAmountOut;
|
803
803
|
}
|
804
804
|
}
|
805
|
-
var
|
805
|
+
var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount);
|
806
806
|
var openFeeUsd = constants_1.BN_ZERO;
|
807
807
|
if (sizeDeltaAmount != constants_1.BN_ZERO) {
|
808
|
-
var
|
809
|
-
var sizeDeltaUsd = newEntryPriceOracle.getAssetAmountUsd(sizeDeltaAmount, targetTokenCustodyAccount.decimals);
|
808
|
+
var sizeDeltaUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetTokenCustodyAccount.decimals);
|
810
809
|
openFeeUsd = sizeDeltaUsd.mul(targetTokenCustodyAccount.fees.openPosition).div(new anchor_1.BN(constants_1.RATE_POWER));
|
811
810
|
}
|
812
811
|
if (positionAccount === null) {
|
@@ -820,16 +819,14 @@ var PerpetualsClient = (function () {
|
|
820
819
|
exponent: new anchor_1.BN(positionAccount.entryPrice.exponent),
|
821
820
|
confidence: constants_1.BN_ZERO,
|
822
821
|
timestamp: constants_1.BN_ZERO
|
823
|
-
})
|
824
|
-
|
825
|
-
entryPriceUsdBN = _this.getAveragePriceSync(positionEntryPrice.price, positionAccount.sizeAmount, entryPriceUsdBN, sizeDeltaAmount);
|
822
|
+
});
|
823
|
+
entryOraclePrice.price = _this.getAveragePriceSync(positionEntryPrice.price, positionAccount.sizeAmount, entryOraclePrice.price, sizeDeltaAmount);
|
826
824
|
}
|
827
825
|
var collateralMinOraclePrice = _this.getMinAndMaxOraclePriceSync(collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount).min;
|
828
826
|
var collateralAmount = positionAccount.collateralAmount.add(finalCollateralAmount);
|
829
827
|
var currentCollateralUsd = collateralMinOraclePrice.getAssetAmountUsd(collateralAmount, collateralTokenCustodyAccount.decimals);
|
830
828
|
var currentCollateralUsdIncludingPnl = currentCollateralUsd.add(pnlUsd).sub(openFeeUsd);
|
831
829
|
var sizeAmount = positionAccount.sizeAmount.add(sizeDeltaAmount);
|
832
|
-
var entryOraclePrice = new OraclePrice_1.OraclePrice({ price: entryPriceUsdBN, exponent: new anchor_1.BN(constants_1.USD_DECIMALS).neg(), confidence: targetTokenPrice.confidence, timestamp: targetTokenPrice.timestamp });
|
833
830
|
var sizeAmountUsd = entryOraclePrice.getAssetAmountUsd(sizeAmount, targetTokenCustodyAccount.decimals);
|
834
831
|
if (currentCollateralUsdIncludingPnl.gt(constants_1.BN_ZERO)) {
|
835
832
|
return sizeAmountUsd.mul(new anchor_1.BN(constants_1.BPS_POWER)).div(currentCollateralUsdIncludingPnl);
|
@@ -843,12 +840,11 @@ var PerpetualsClient = (function () {
|
|
843
840
|
if (collateralDeltaAmount.isNeg() || sizeDeltaAmount.isNeg()) {
|
844
841
|
throw new Error("Delta Amounts cannot be negative.");
|
845
842
|
}
|
846
|
-
var
|
847
|
-
var oracleEntryPrice = OraclePrice_1.OraclePrice.from({ price: entryPriceUsdBN, exponent: new anchor_1.BN(-1 * constants_1.USD_DECIMALS), confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
843
|
+
var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetPrice, targetEmaPrice, targetCustodyAccount);
|
848
844
|
if (positionAccount === null) {
|
849
845
|
var data = __assign({}, types_1.DEFAULT_POSITION);
|
850
846
|
positionAccount = PositionAccount_1.PositionAccount.from(web3_js_1.PublicKey.default, data);
|
851
|
-
var sizeUsd =
|
847
|
+
var sizeUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
|
852
848
|
positionAccount.sizeUsd = sizeUsd;
|
853
849
|
positionAccount.sizeDecimals = targetCustodyAccount.decimals;
|
854
850
|
positionAccount.collateralDecimals = collateralCustodyAccount.decimals;
|
@@ -861,17 +857,15 @@ var PerpetualsClient = (function () {
|
|
861
857
|
exponent: new anchor_1.BN(positionAccount.entryPrice.exponent),
|
862
858
|
confidence: constants_1.BN_ZERO,
|
863
859
|
timestamp: constants_1.BN_ZERO
|
864
|
-
})
|
865
|
-
|
866
|
-
|
867
|
-
var sizeDeltaUsd = oracleEntryPrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
|
860
|
+
});
|
861
|
+
entryOraclePrice.price = _this.getAveragePriceSync(positionEntryPrice.price, positionAccount.sizeAmount, entryOraclePrice.price, sizeDeltaAmount);
|
862
|
+
var sizeDeltaUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
|
868
863
|
positionAccount.sizeUsd = positionAccount.sizeUsd.add(sizeDeltaUsd);
|
869
864
|
}
|
870
865
|
positionAccount.collateralAmount = positionAccount.collateralAmount.add(collateralDeltaAmount);
|
871
866
|
positionAccount.sizeAmount = positionAccount.sizeAmount.add(sizeDeltaAmount);
|
872
867
|
var lockFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionAccount, collateralCustodyAccount, currentTimestamp);
|
873
|
-
var liquidationPrice = _this.getLiquidationPriceSync(positionAccount.collateralAmount, positionAccount.sizeAmount,
|
874
|
-
var entryOraclePrice = new OraclePrice_1.OraclePrice({ price: entryPriceUsdBN, exponent: new anchor_1.BN(constants_1.USD_DECIMALS).neg(), confidence: targetPrice.confidence, timestamp: targetPrice.timestamp });
|
868
|
+
var liquidationPrice = _this.getLiquidationPriceSync(positionAccount.collateralAmount, positionAccount.sizeAmount, entryOraclePrice, lockFeeUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, positionAccount);
|
875
869
|
var sizeAmountUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
|
876
870
|
var collateralTokenMinOraclePrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
|
877
871
|
var feeUsd = constants_1.BN_ZERO;
|
@@ -894,7 +888,7 @@ var PerpetualsClient = (function () {
|
|
894
888
|
}
|
895
889
|
}
|
896
890
|
return {
|
897
|
-
|
891
|
+
entryOraclePrice: entryOraclePrice,
|
898
892
|
feeUsd: feeUsd,
|
899
893
|
feeAmount: feeAmount,
|
900
894
|
feeUsdAfterDiscount: feeUsdAfterDiscount,
|
@@ -903,11 +897,12 @@ var PerpetualsClient = (function () {
|
|
903
897
|
};
|
904
898
|
};
|
905
899
|
this.getEntryPriceUsdSync = function (side, targetPrice, targetEmaPrice, targetCustodyAccount) {
|
906
|
-
var _a = _this.
|
900
|
+
var _a = _this.getMinAndMaxOraclePriceSync(targetPrice, targetEmaPrice, targetCustodyAccount), minPrice = _a.min, maxPrice = _a.max;
|
907
901
|
var entryPriceBN = (0, types_1.isVariant)(side, 'long') === types_1.Side.Long ?
|
908
|
-
maxPrice.add(maxPrice.mul(targetCustodyAccount.pricing.tradeSpreadLong).div(new anchor_1.BN(constants_1.BPS_POWER))) :
|
909
|
-
minPrice.sub(minPrice.mul(targetCustodyAccount.pricing.tradeSpreadShort).div(new anchor_1.BN(constants_1.BPS_POWER)));
|
910
|
-
|
902
|
+
maxPrice.price.add(maxPrice.price.mul(targetCustodyAccount.pricing.tradeSpreadLong).div(new anchor_1.BN(constants_1.BPS_POWER))) :
|
903
|
+
minPrice.price.sub(minPrice.price.mul(targetCustodyAccount.pricing.tradeSpreadShort).div(new anchor_1.BN(constants_1.BPS_POWER)));
|
904
|
+
var entryOraclePrice = OraclePrice_1.OraclePrice.from({ price: entryPriceBN, exponent: maxPrice.exponent, confidence: maxPrice.confidence, timestamp: maxPrice.timestamp });
|
905
|
+
return entryOraclePrice;
|
911
906
|
};
|
912
907
|
this.getExitFeeSync = function (positionAccount, targetCustody, collateralCustodyAccount, collateralPrice, collateralEmaPrice, discountBps) {
|
913
908
|
if (discountBps === void 0) { discountBps = constants_1.BN_ZERO; }
|
@@ -954,12 +949,11 @@ var PerpetualsClient = (function () {
|
|
954
949
|
}
|
955
950
|
var positionEntryOraclePrice = new OraclePrice_1.OraclePrice({
|
956
951
|
price: positionAccount.entryPrice.price, exponent: new anchor_1.BN(positionAccount.entryPrice.exponent), confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO
|
957
|
-
})
|
958
|
-
var positionEntryPriceUsd = positionEntryOraclePrice.price;
|
952
|
+
});
|
959
953
|
resultingPositionAccount.sizeUsd = positionEntryOraclePrice.getAssetAmountUsd(resultingPositionAccount.sizeAmount, targetCustodyAccount.decimals);
|
960
|
-
var liquidationPrice = _this.getLiquidationPriceSync(resultingPositionAccount.collateralAmount, resultingPositionAccount.sizeAmount,
|
954
|
+
var liquidationPrice = _this.getLiquidationPriceSync(resultingPositionAccount.collateralAmount, resultingPositionAccount.sizeAmount, positionEntryOraclePrice, lockAndUnsettledFeeUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, positionAccount);
|
961
955
|
return {
|
962
|
-
|
956
|
+
exitOraclePrice: exitOraclePrice,
|
963
957
|
borrowFeeUsd: lockAndUnsettledFeeUsd,
|
964
958
|
borrowFeeAmount: lockAndUnsettledFee,
|
965
959
|
exitFeeUsd: exitFeeUsd,
|
@@ -989,8 +983,8 @@ var PerpetualsClient = (function () {
|
|
989
983
|
if (discountBps === void 0) { discountBps = constants_1.BN_ZERO; }
|
990
984
|
var collateralTokenMinPrice = _this.getMinAndMaxPriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
|
991
985
|
var collateralTokenMinPriceUi = new bignumber_js_1.default(collateralTokenMinPrice.toString()).dividedBy(Math.pow(10, constants_1.USD_DECIMALS));
|
992
|
-
var
|
993
|
-
var entryPriceUsdUi = new bignumber_js_1.default(
|
986
|
+
var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetPrice, targetEmaPrice, targetCustodyAccount);
|
987
|
+
var entryPriceUsdUi = new bignumber_js_1.default(entryOraclePrice.toUiPrice(constants_1.ORACLE_EXPONENT));
|
994
988
|
var collateralAmtMinUsdUi = new bignumber_js_1.default(collateralAmtWithFee.toString()).dividedBy(Math.pow(10, collateralToken.decimals))
|
995
989
|
.multipliedBy(collateralTokenMinPriceUi);
|
996
990
|
var openPosFeeRateUi = new bignumber_js_1.default(targetCustodyAccount.fees.openPosition.toString()).dividedBy(Math.pow(10, constants_1.RATE_DECIMALS));
|
@@ -1016,8 +1010,8 @@ var PerpetualsClient = (function () {
|
|
1016
1010
|
}
|
1017
1011
|
var collateralTokenMinPrice = _this.getMinAndMaxPriceSync(collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount).min;
|
1018
1012
|
var collateralTokenMinPriceUi = new bignumber_js_1.default(collateralTokenMinPrice.toString()).dividedBy(Math.pow(10, constants_1.USD_DECIMALS));
|
1019
|
-
var
|
1020
|
-
var entryPriceUsdUi = new bignumber_js_1.default(
|
1013
|
+
var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount);
|
1014
|
+
var entryPriceUsdUi = new bignumber_js_1.default(entryOraclePrice.toUiPrice(constants_1.ORACLE_EXPONENT));
|
1021
1015
|
var collateralAmtMinUsdUi = new bignumber_js_1.default(finalCollateralAmount.toString()).dividedBy(Math.pow(10, collateralTokenCustodyAccount.decimals))
|
1022
1016
|
.multipliedBy(collateralTokenMinPriceUi);
|
1023
1017
|
var openPosFeeRateUi = new bignumber_js_1.default(targetTokenCustodyAccount.fees.openPosition.toString()).dividedBy(Math.pow(10, constants_1.RATE_DECIMALS));
|
@@ -1034,8 +1028,8 @@ var PerpetualsClient = (function () {
|
|
1034
1028
|
if (discountBps === void 0) { discountBps = constants_1.BN_ZERO; }
|
1035
1029
|
var collateralTokenMinPrice = _this.getMinAndMaxPriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
|
1036
1030
|
var collateralTokenMinPriceUi = new bignumber_js_1.default(collateralTokenMinPrice.toString()).dividedBy(Math.pow(10, constants_1.USD_DECIMALS));
|
1037
|
-
var
|
1038
|
-
var entryPriceUsdUi = new bignumber_js_1.default(
|
1031
|
+
var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetPrice, targetEmaPrice, targetCustodyAccount);
|
1032
|
+
var entryPriceUsdUi = new bignumber_js_1.default(entryOraclePrice.toUiPrice(constants_1.ORACLE_EXPONENT));
|
1039
1033
|
var openPosFeeRateUi = new bignumber_js_1.default(targetCustodyAccount.fees.openPosition.toString()).dividedBy(Math.pow(10, constants_1.RATE_DECIMALS));
|
1040
1034
|
if (!discountBps.isZero()) {
|
1041
1035
|
var discountBpsUi = new bignumber_js_1.default(discountBps.toString()).dividedBy(Math.pow(10, constants_1.BPS_DECIMALS));
|
@@ -1050,8 +1044,8 @@ var PerpetualsClient = (function () {
|
|
1050
1044
|
this.getCollateralAmountWithSwapSync = function (sizeAmount, leverage, side, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, swapPoolAumUsdMax, poolConfigPosition, poolConfigSwap) {
|
1051
1045
|
var collateralTokenMinPrice = _this.getMinAndMaxPriceSync(collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount).min;
|
1052
1046
|
var collateralTokenMinPriceUi = new bignumber_js_1.default(collateralTokenMinPrice.toString()).dividedBy(Math.pow(10, constants_1.USD_DECIMALS));
|
1053
|
-
var
|
1054
|
-
var entryPriceUsdUi = new bignumber_js_1.default(
|
1047
|
+
var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount);
|
1048
|
+
var entryPriceUsdUi = new bignumber_js_1.default(entryOraclePrice.toUiPrice(constants_1.ORACLE_EXPONENT));
|
1055
1049
|
var openPosFeeRateUi = new bignumber_js_1.default(targetTokenCustodyAccount.fees.openPosition.toString()).dividedBy(Math.pow(10, constants_1.RATE_DECIMALS));
|
1056
1050
|
var sizeAmountUi = new bignumber_js_1.default(sizeAmount.toString()).dividedBy(Math.pow(10, targetTokenCustodyAccount.decimals));
|
1057
1051
|
var sizeUsdUi = entryPriceUsdUi.multipliedBy(sizeAmountUi);
|
@@ -1158,7 +1152,7 @@ var PerpetualsClient = (function () {
|
|
1158
1152
|
newPosition.collateralUsd = collateralMinMaxPrice.min.getAssetAmountUsd(newPosition.collateralAmount, collateralCustodyAccount.decimals);
|
1159
1153
|
var entryPrice = OraclePrice_1.OraclePrice.from({ price: newPosition.entryPrice.price, exponent: new anchor_1.BN(newPosition.entryPrice.exponent), confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1160
1154
|
var finalInterestUsd = _this.getLockFeeAndUnsettledUsdForPosition(newPosition, collateralCustodyAccount, currentTimestamp);
|
1161
|
-
var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, entryPrice
|
1155
|
+
var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, entryPrice, finalInterestUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, newPosition);
|
1162
1156
|
var finalPnl = _this.getPnlSync(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
|
1163
1157
|
var finalPnlUsd = finalPnl.profitUsd.sub(finalPnl.lossUsd);
|
1164
1158
|
var newExitFee = _this.getExitFeeSync(newPosition, targetCustodyAccount, collateralCustodyAccount, collateralPrice, collateralEmaPrice);
|
@@ -1178,7 +1172,7 @@ var PerpetualsClient = (function () {
|
|
1178
1172
|
else {
|
1179
1173
|
var entryPrice = OraclePrice_1.OraclePrice.from({ price: newPosition.entryPrice.price, exponent: new anchor_1.BN(newPosition.entryPrice.exponent), confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1180
1174
|
var finalInterestUsd = _this.getLockFeeAndUnsettledUsdForPosition(newPosition, collateralCustodyAccount, currentTimestamp);
|
1181
|
-
var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, entryPrice
|
1175
|
+
var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, entryPrice, finalInterestUsd, marketCorrelation, side, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, newPosition);
|
1182
1176
|
var finalPnl = _this.getPnlSync(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
|
1183
1177
|
var finalPnlUsd = finalPnl.profitUsd.sub(finalPnl.lossUsd);
|
1184
1178
|
var exitFee = _this.getExitFeeSync(newPosition, targetCustodyAccount, collateralCustodyAccount, collateralPrice, collateralEmaPrice);
|
@@ -1279,73 +1273,133 @@ var PerpetualsClient = (function () {
|
|
1279
1273
|
lockFeeUsd = lockFeeUsd.add(position.unsettledFeesUsd);
|
1280
1274
|
return lockFeeUsd;
|
1281
1275
|
};
|
1282
|
-
this.getLiquidationPriceSync = function (collateralAmount, sizeAmount,
|
1276
|
+
this.getLiquidationPriceSync = function (collateralAmount, sizeAmount, entryOraclePrice, lockAndUnsettledFeeUsd, marketCorrelation, side, custodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, positionAccount) {
|
1277
|
+
var zeroOraclePrice = OraclePrice_1.OraclePrice.from({
|
1278
|
+
price: constants_1.BN_ZERO,
|
1279
|
+
exponent: constants_1.BN_ZERO,
|
1280
|
+
confidence: constants_1.BN_ZERO,
|
1281
|
+
timestamp: constants_1.BN_ZERO
|
1282
|
+
});
|
1283
1283
|
if (collateralAmount.isZero() || sizeAmount.isZero()) {
|
1284
|
-
return
|
1284
|
+
return zeroOraclePrice;
|
1285
|
+
}
|
1286
|
+
if (positionAccount.entryPrice.exponent && !entryOraclePrice.exponent.eq(new anchor_1.BN(positionAccount.entryPrice.exponent))) {
|
1287
|
+
throw new Error("Exponent mismatch : ".concat(positionAccount.entryPrice.exponent, " & ").concat(entryOraclePrice.exponent.toString(), " ").concat(entryOraclePrice === null || entryOraclePrice === void 0 ? void 0 : entryOraclePrice.toUiPrice(8)));
|
1285
1288
|
}
|
1286
1289
|
var exitFeeUsd = positionAccount.sizeUsd.mul(custodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
|
1287
1290
|
var unsettledLossUsd = exitFeeUsd.add(lockAndUnsettledFeeUsd);
|
1288
|
-
var
|
1289
|
-
var
|
1290
|
-
|
1291
|
-
|
1292
|
-
if (currentCollateralUsd.gt(liablityThresholdUsd)) {
|
1293
|
-
nominalSolvency = true;
|
1294
|
-
}
|
1295
|
-
else {
|
1296
|
-
nominalSolvency = false;
|
1297
|
-
}
|
1298
|
-
var remainingMarginUsd, requiredMarginUsd;
|
1299
|
-
if (nominalSolvency) {
|
1300
|
-
remainingMarginUsd = currentCollateralUsd.sub(liablityThresholdUsd);
|
1301
|
-
requiredMarginUsd = constants_1.BN_ZERO;
|
1291
|
+
var liablitiesUsd = positionAccount.sizeUsd.mul(new anchor_1.BN(constants_1.BPS_POWER)).div(custodyAccount.pricing.maxLeverage).add(unsettledLossUsd);
|
1292
|
+
var assetsUsd = constants_1.BN_ZERO;
|
1293
|
+
if (marketCorrelation) {
|
1294
|
+
assetsUsd = positionAccount.collateralUsd;
|
1302
1295
|
}
|
1303
1296
|
else {
|
1304
|
-
|
1305
|
-
|
1297
|
+
var collateralMinPriceOracle = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
|
1298
|
+
assetsUsd = collateralMinPriceOracle.getAssetAmountUsd(collateralAmount, collateralCustodyAccount.decimals);
|
1306
1299
|
}
|
1307
|
-
var liquidationPrice
|
1308
|
-
if ((0, types_1.isVariant)(side, 'long')) {
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
}
|
1316
|
-
|
1317
|
-
var pricePremium = (requiredMarginUsd.mul(new anchor_1.BN(10).pow(new anchor_1.BN(positionAccount.sizeDecimals)))).div(sizeAmount);
|
1318
|
-
liquidationPrice = entryPriceUsd.add(pricePremium);
|
1319
|
-
}
|
1300
|
+
var liquidationPrice;
|
1301
|
+
if (marketCorrelation && (0, types_1.isVariant)(side, 'long')) {
|
1302
|
+
var lp = OraclePrice_1.OraclePrice.from({
|
1303
|
+
price: (positionAccount.sizeUsd.add(liablitiesUsd)).mul(new anchor_1.BN(Math.pow(10, (positionAccount.sizeDecimals + 3))))
|
1304
|
+
.div(sizeAmount.add(collateralAmount)),
|
1305
|
+
exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
|
1306
|
+
confidence: constants_1.BN_ZERO,
|
1307
|
+
timestamp: constants_1.BN_ZERO
|
1308
|
+
});
|
1309
|
+
liquidationPrice = lp.scale_to_exponent(new anchor_1.BN(entryOraclePrice.exponent));
|
1320
1310
|
}
|
1321
1311
|
else {
|
1322
|
-
if (
|
1323
|
-
var
|
1324
|
-
|
1312
|
+
if (assetsUsd.gte(liablitiesUsd)) {
|
1313
|
+
var priceDiffLossOracle = OraclePrice_1.OraclePrice.from({
|
1314
|
+
price: (assetsUsd.sub(liablitiesUsd)).mul(new anchor_1.BN(Math.pow(10, (positionAccount.sizeDecimals + 3))))
|
1315
|
+
.div(positionAccount.sizeAmount),
|
1316
|
+
exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
|
1317
|
+
confidence: constants_1.BN_ZERO,
|
1318
|
+
timestamp: constants_1.BN_ZERO
|
1319
|
+
}).scale_to_exponent(new anchor_1.BN(entryOraclePrice.exponent));
|
1320
|
+
if ((0, types_1.isVariant)(side, 'long')) {
|
1321
|
+
liquidationPrice = OraclePrice_1.OraclePrice.from({
|
1322
|
+
price: entryOraclePrice.price.sub(priceDiffLossOracle.price),
|
1323
|
+
exponent: new anchor_1.BN(entryOraclePrice.exponent),
|
1324
|
+
confidence: constants_1.BN_ZERO,
|
1325
|
+
timestamp: constants_1.BN_ZERO
|
1326
|
+
});
|
1327
|
+
}
|
1328
|
+
else {
|
1329
|
+
liquidationPrice = OraclePrice_1.OraclePrice.from({
|
1330
|
+
price: entryOraclePrice.price.add(priceDiffLossOracle.price),
|
1331
|
+
exponent: new anchor_1.BN(entryOraclePrice.exponent),
|
1332
|
+
confidence: constants_1.BN_ZERO,
|
1333
|
+
timestamp: constants_1.BN_ZERO
|
1334
|
+
});
|
1335
|
+
}
|
1325
1336
|
}
|
1326
1337
|
else {
|
1327
|
-
var
|
1328
|
-
|
1338
|
+
var priceDiffProfitOracle = OraclePrice_1.OraclePrice.from({
|
1339
|
+
price: (liablitiesUsd.sub(assetsUsd)).mul(new anchor_1.BN(Math.pow(10, (positionAccount.sizeDecimals + 3))))
|
1340
|
+
.div(positionAccount.sizeAmount),
|
1341
|
+
exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
|
1342
|
+
confidence: constants_1.BN_ZERO,
|
1343
|
+
timestamp: constants_1.BN_ZERO
|
1344
|
+
}).scale_to_exponent(new anchor_1.BN(entryOraclePrice.exponent));
|
1345
|
+
if ((0, types_1.isVariant)(side, 'long')) {
|
1346
|
+
liquidationPrice = OraclePrice_1.OraclePrice.from({
|
1347
|
+
price: entryOraclePrice.price.add(priceDiffProfitOracle.price),
|
1348
|
+
exponent: new anchor_1.BN(entryOraclePrice.exponent),
|
1349
|
+
confidence: constants_1.BN_ZERO,
|
1350
|
+
timestamp: constants_1.BN_ZERO
|
1351
|
+
});
|
1352
|
+
}
|
1353
|
+
else {
|
1354
|
+
liquidationPrice = OraclePrice_1.OraclePrice.from({
|
1355
|
+
price: entryOraclePrice.price.sub(priceDiffProfitOracle.price),
|
1356
|
+
exponent: new anchor_1.BN(entryOraclePrice.exponent),
|
1357
|
+
confidence: constants_1.BN_ZERO,
|
1358
|
+
timestamp: constants_1.BN_ZERO
|
1359
|
+
});
|
1360
|
+
}
|
1329
1361
|
}
|
1330
1362
|
}
|
1331
|
-
return liquidationPrice.isNeg() ?
|
1363
|
+
return liquidationPrice.price.isNeg() ? zeroOraclePrice : liquidationPrice;
|
1332
1364
|
};
|
1333
|
-
this.getMaxProfitPriceSync = function (
|
1365
|
+
this.getMaxProfitPriceSync = function (entryPrice, marketCorrelation, side, positionAccount) {
|
1366
|
+
var zeroOraclePrice = OraclePrice_1.OraclePrice.from({
|
1367
|
+
price: constants_1.BN_ZERO,
|
1368
|
+
exponent: constants_1.BN_ZERO,
|
1369
|
+
confidence: constants_1.BN_ZERO,
|
1370
|
+
timestamp: constants_1.BN_ZERO
|
1371
|
+
});
|
1334
1372
|
if (positionAccount.sizeAmount.isZero()) {
|
1335
|
-
return
|
1373
|
+
return zeroOraclePrice;
|
1336
1374
|
}
|
1337
|
-
var priceDiffProfit =
|
1338
|
-
|
1375
|
+
var priceDiffProfit = OraclePrice_1.OraclePrice.from({
|
1376
|
+
price: positionAccount.lockedUsd.mul(new anchor_1.BN(10).pow(new anchor_1.BN(positionAccount.sizeDecimals + 3)))
|
1377
|
+
.div(positionAccount.sizeAmount),
|
1378
|
+
exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
|
1379
|
+
confidence: constants_1.BN_ZERO,
|
1380
|
+
timestamp: constants_1.BN_ZERO
|
1381
|
+
}).scale_to_exponent(new anchor_1.BN(positionAccount.entryPrice.exponent));
|
1382
|
+
var maxProfitPrice;
|
1339
1383
|
if ((0, types_1.isVariant)(side, 'long')) {
|
1340
1384
|
if (marketCorrelation) {
|
1341
|
-
return
|
1385
|
+
return zeroOraclePrice;
|
1342
1386
|
}
|
1343
|
-
maxProfitPrice =
|
1387
|
+
maxProfitPrice = OraclePrice_1.OraclePrice.from({
|
1388
|
+
price: entryPrice.price.add(priceDiffProfit.price),
|
1389
|
+
exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
|
1390
|
+
confidence: constants_1.BN_ZERO,
|
1391
|
+
timestamp: constants_1.BN_ZERO
|
1392
|
+
});
|
1344
1393
|
}
|
1345
1394
|
else {
|
1346
|
-
maxProfitPrice =
|
1395
|
+
maxProfitPrice = OraclePrice_1.OraclePrice.from({
|
1396
|
+
price: entryPrice.price.sub(priceDiffProfit.price),
|
1397
|
+
exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
|
1398
|
+
confidence: constants_1.BN_ZERO,
|
1399
|
+
timestamp: constants_1.BN_ZERO
|
1400
|
+
});
|
1347
1401
|
}
|
1348
|
-
return maxProfitPrice.isNeg() ?
|
1402
|
+
return maxProfitPrice.price.isNeg() ? zeroOraclePrice : maxProfitPrice;
|
1349
1403
|
};
|
1350
1404
|
this.getPnlSync = function (positionAccount, targetTokenPrice, targetTokenEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, delay, poolConfig) {
|
1351
1405
|
if (positionAccount.sizeUsd.isZero() || positionAccount.entryPrice.price.isZero()) {
|
@@ -1355,61 +1409,64 @@ var PerpetualsClient = (function () {
|
|
1355
1409
|
};
|
1356
1410
|
}
|
1357
1411
|
var side = poolConfig.getMarketConfigByPk(positionAccount.market).side;
|
1358
|
-
var
|
1412
|
+
var exitOraclePrice = _this.getExitOraclePriceSync(side, targetTokenPrice, targetTokenEmaPrice, targetCustodyAccount);
|
1359
1413
|
var collateralMinPrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
|
1360
|
-
var
|
1414
|
+
var priceDiffProfit, priceDiffLoss;
|
1361
1415
|
var positionEntryPrice = OraclePrice_1.OraclePrice.from({
|
1362
1416
|
price: positionAccount.entryPrice.price,
|
1363
1417
|
exponent: new anchor_1.BN(positionAccount.entryPrice.exponent),
|
1364
1418
|
confidence: constants_1.BN_ZERO,
|
1365
1419
|
timestamp: constants_1.BN_ZERO
|
1366
|
-
})
|
1367
|
-
|
1420
|
+
});
|
1421
|
+
if (!exitOraclePrice.exponent.eq(positionEntryPrice.exponent)) {
|
1422
|
+
throw new Error("exponent mistach");
|
1423
|
+
}
|
1368
1424
|
if ((0, types_1.isVariant)(side, 'long')) {
|
1369
|
-
if (
|
1425
|
+
if (exitOraclePrice.price.gt(positionEntryPrice.price)) {
|
1370
1426
|
if (currentTimestamp.gt(positionAccount.updateTime.add(delay))) {
|
1371
|
-
|
1372
|
-
|
1427
|
+
priceDiffProfit = new OraclePrice_1.OraclePrice({ price: exitOraclePrice.price.sub(positionEntryPrice.price), exponent: exitOraclePrice.exponent, confidence: exitOraclePrice.confidence, timestamp: constants_1.BN_ZERO });
|
1428
|
+
priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1373
1429
|
}
|
1374
1430
|
else {
|
1375
|
-
|
1376
|
-
|
1431
|
+
priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1432
|
+
priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1377
1433
|
}
|
1378
1434
|
}
|
1379
1435
|
else {
|
1380
|
-
|
1381
|
-
|
1436
|
+
priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1437
|
+
priceDiffLoss = new OraclePrice_1.OraclePrice({ price: positionEntryPrice.price.sub(exitOraclePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1382
1438
|
}
|
1383
1439
|
}
|
1384
1440
|
else {
|
1385
|
-
if (
|
1441
|
+
if (exitOraclePrice.price.lt(positionEntryPrice.price)) {
|
1386
1442
|
if (currentTimestamp.gt(positionAccount.updateTime.add(delay))) {
|
1387
|
-
|
1388
|
-
|
1443
|
+
priceDiffProfit = new OraclePrice_1.OraclePrice({ price: positionEntryPrice.price.sub(exitOraclePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1444
|
+
priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1389
1445
|
}
|
1390
1446
|
else {
|
1391
|
-
|
1392
|
-
|
1447
|
+
priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1448
|
+
priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1393
1449
|
}
|
1394
1450
|
}
|
1395
1451
|
else {
|
1396
|
-
|
1397
|
-
|
1452
|
+
priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1453
|
+
priceDiffLoss = new OraclePrice_1.OraclePrice({ price: exitOraclePrice.price.sub(positionEntryPrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
1398
1454
|
}
|
1399
1455
|
}
|
1400
1456
|
;
|
1401
|
-
|
1402
|
-
|
1403
|
-
|
1457
|
+
if (!priceDiffProfit.exponent.eq(priceDiffLoss.exponent)) {
|
1458
|
+
throw new Error("exponent mistach");
|
1459
|
+
}
|
1460
|
+
if (priceDiffProfit.price.gt(constants_1.BN_ZERO)) {
|
1404
1461
|
return {
|
1405
|
-
profitUsd: anchor_1.BN.min(
|
1462
|
+
profitUsd: anchor_1.BN.min(priceDiffProfit.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals), collateralMinPrice.getAssetAmountUsd(positionAccount.lockedAmount, positionAccount.lockedDecimals)),
|
1406
1463
|
lossUsd: constants_1.BN_ZERO,
|
1407
1464
|
};
|
1408
1465
|
}
|
1409
1466
|
else {
|
1410
1467
|
return {
|
1411
1468
|
profitUsd: constants_1.BN_ZERO,
|
1412
|
-
lossUsd:
|
1469
|
+
lossUsd: priceDiffLoss.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals),
|
1413
1470
|
};
|
1414
1471
|
}
|
1415
1472
|
};
|
@@ -1425,31 +1482,46 @@ var PerpetualsClient = (function () {
|
|
1425
1482
|
feeOut: constants_1.BN_ZERO,
|
1426
1483
|
};
|
1427
1484
|
}
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1485
|
+
var newInputTokenPrice, newInputTokenEmaPrice;
|
1486
|
+
var newOutputTokenPrice, newOutputTokenEmaPrice;
|
1487
|
+
if (inputTokenPrice.exponent.lte(outputTokenPrice.exponent)) {
|
1488
|
+
newInputTokenPrice = inputTokenPrice;
|
1489
|
+
newInputTokenEmaPrice = inputTokenEmaPrice;
|
1490
|
+
newOutputTokenPrice = outputTokenPrice.scale_to_exponent(inputTokenPrice.exponent);
|
1491
|
+
newOutputTokenEmaPrice = outputTokenEmaPrice.scale_to_exponent(inputTokenPrice.exponent);
|
1492
|
+
}
|
1493
|
+
else {
|
1494
|
+
newInputTokenPrice = inputTokenPrice.scale_to_exponent(outputTokenPrice.exponent);
|
1495
|
+
newInputTokenEmaPrice = inputTokenEmaPrice.scale_to_exponent(outputTokenPrice.exponent);
|
1496
|
+
newOutputTokenPrice = outputTokenPrice;
|
1497
|
+
newOutputTokenEmaPrice = outputTokenEmaPrice;
|
1498
|
+
}
|
1499
|
+
if (!newInputTokenPrice.exponent.eq(newOutputTokenPrice.exponent)) {
|
1500
|
+
throw "Exponents mistmatch ".concat(newInputTokenPrice.exponent.toNumber(), " != ").concat(newOutputTokenPrice.exponent.toNumber());
|
1431
1501
|
}
|
1432
|
-
var inputMinMaxPrice = _this.getMinAndMaxOraclePriceSync(
|
1433
|
-
var outputMinMaxPrice = _this.getMinAndMaxOraclePriceSync(
|
1502
|
+
var inputMinMaxPrice = _this.getMinAndMaxOraclePriceSync(newInputTokenPrice, newInputTokenEmaPrice, inputTokenCustodyAccount);
|
1503
|
+
var outputMinMaxPrice = _this.getMinAndMaxOraclePriceSync(newOutputTokenPrice, newOutputTokenEmaPrice, outputTokenCustodyAccount);
|
1434
1504
|
var pairPrice;
|
1435
|
-
var
|
1505
|
+
var inputTokenAmount;
|
1506
|
+
var outputTokenAmount;
|
1436
1507
|
var feeIn;
|
1437
1508
|
var feeOut;
|
1438
1509
|
if (amountIn.isZero()) {
|
1439
1510
|
if (inputTokenCustodyAccount.isStable &&
|
1440
1511
|
inputMinMaxPrice.min != inputMinMaxPrice.max &&
|
1441
|
-
|
1512
|
+
inputTokenCustodyAccount.depegAdjustment) {
|
1442
1513
|
pairPrice = outputMinMaxPrice.min.price;
|
1443
1514
|
}
|
1444
1515
|
else {
|
1445
1516
|
pairPrice = outputMinMaxPrice.min.price.mul(new anchor_1.BN(10).pow(outputMinMaxPrice.min.exponent)).div(inputMinMaxPrice.max.price);
|
1446
1517
|
}
|
1447
1518
|
var swapPrice = pairPrice.sub(pairPrice.mul(outputTokenCustodyAccount.pricing.swapSpread).div(new anchor_1.BN(constants_1.BPS_POWER)));
|
1448
|
-
|
1449
|
-
feeIn = _this.getFeeHelper(types_1.FeesAction.SwapIn,
|
1450
|
-
feeOut = _this.getFeeHelper(types_1.FeesAction.SwapOut, constants_1.BN_ZERO, amountOut, outputTokenCustodyAccount, outputMinMaxPrice.max
|
1519
|
+
inputTokenAmount = (0, utils_1.checkedDecimalMul)(amountOut, new anchor_1.BN(-1 * outputTokenCustodyAccount.decimals), swapPrice, inputMinMaxPrice.min.exponent, new anchor_1.BN(-1 * inputTokenCustodyAccount.decimals));
|
1520
|
+
feeIn = _this.getFeeHelper(types_1.FeesAction.SwapIn, inputTokenAmount, constants_1.BN_ZERO, inputTokenCustodyAccount, inputMinMaxPrice.max, poolAumUsdMax, poolAccount, poolConfig).feeAmount;
|
1521
|
+
feeOut = _this.getFeeHelper(types_1.FeesAction.SwapOut, constants_1.BN_ZERO, amountOut, outputTokenCustodyAccount, outputMinMaxPrice.max, poolAumUsdMax, poolAccount, poolConfig).feeAmount;
|
1522
|
+
var swapAmount = (0, utils_1.checkedDecimalMul)(amountOut.add(feeOut), new anchor_1.BN(-1 * outputTokenCustodyAccount.decimals), swapPrice, inputMinMaxPrice.min.exponent, new anchor_1.BN(-1 * inputTokenCustodyAccount.decimals)).add(feeIn);
|
1451
1523
|
return {
|
1452
|
-
minAmountIn: swapAmount
|
1524
|
+
minAmountIn: swapAmount,
|
1453
1525
|
minAmountOut: constants_1.BN_ZERO,
|
1454
1526
|
feeIn: feeIn,
|
1455
1527
|
feeOut: feeOut,
|
@@ -1458,19 +1530,20 @@ var PerpetualsClient = (function () {
|
|
1458
1530
|
else {
|
1459
1531
|
if (outputTokenCustodyAccount.isStable &&
|
1460
1532
|
outputMinMaxPrice.min != outputMinMaxPrice.max &&
|
1461
|
-
|
1533
|
+
outputTokenCustodyAccount.depegAdjustment) {
|
1462
1534
|
pairPrice = inputMinMaxPrice.min.price;
|
1463
1535
|
}
|
1464
1536
|
else {
|
1465
1537
|
pairPrice = inputMinMaxPrice.min.price.mul(new anchor_1.BN(10).pow(inputMinMaxPrice.min.exponent)).div(outputMinMaxPrice.max.price);
|
1466
1538
|
}
|
1467
1539
|
var swapPrice = pairPrice.sub(pairPrice.mul(inputTokenCustodyAccount.pricing.swapSpread).div(new anchor_1.BN(constants_1.BPS_POWER)));
|
1468
|
-
|
1469
|
-
feeIn = _this.getFeeHelper(types_1.FeesAction.SwapIn, amountIn, constants_1.BN_ZERO, inputTokenCustodyAccount, inputMinMaxPrice.max
|
1470
|
-
feeOut = _this.getFeeHelper(types_1.FeesAction.SwapOut, constants_1.BN_ZERO,
|
1540
|
+
outputTokenAmount = (0, utils_1.checkedDecimalMul)(amountIn, new anchor_1.BN(-1 * inputTokenCustodyAccount.decimals), swapPrice, inputMinMaxPrice.min.exponent, new anchor_1.BN(-1 * outputTokenCustodyAccount.decimals));
|
1541
|
+
feeIn = _this.getFeeHelper(types_1.FeesAction.SwapIn, amountIn, constants_1.BN_ZERO, inputTokenCustodyAccount, inputMinMaxPrice.max, poolAumUsdMax, poolAccount, poolConfig).feeAmount;
|
1542
|
+
feeOut = _this.getFeeHelper(types_1.FeesAction.SwapOut, constants_1.BN_ZERO, outputTokenAmount, outputTokenCustodyAccount, outputMinMaxPrice.max, poolAumUsdMax, poolAccount, poolConfig).feeAmount;
|
1543
|
+
var swapAmount = (0, utils_1.checkedDecimalMul)(amountIn.sub(feeIn), new anchor_1.BN(-1 * inputTokenCustodyAccount.decimals), swapPrice, inputMinMaxPrice.min.exponent, new anchor_1.BN(-1 * outputTokenCustodyAccount.decimals)).sub(feeOut);
|
1471
1544
|
return {
|
1472
1545
|
minAmountIn: constants_1.BN_ZERO,
|
1473
|
-
minAmountOut: swapAmount
|
1546
|
+
minAmountOut: swapAmount,
|
1474
1547
|
feeIn: feeIn,
|
1475
1548
|
feeOut: feeOut,
|
1476
1549
|
};
|
@@ -4077,9 +4150,8 @@ var PerpetualsClient = (function () {
|
|
4077
4150
|
this.getMarketPk(targetCustody, collateralCustody, side),
|
4078
4151
|
]).publicKey;
|
4079
4152
|
};
|
4080
|
-
PerpetualsClient.prototype.getNewRatioHelper = function (amountAdd, amountRemove, custodyAccount,
|
4153
|
+
PerpetualsClient.prototype.getNewRatioHelper = function (amountAdd, amountRemove, custodyAccount, maxPriceOracle, poolAumUsdMax) {
|
4081
4154
|
var newRatio = constants_1.BN_ZERO;
|
4082
|
-
var maxPriceOracle = OraclePrice_1.OraclePrice.from({ price: new anchor_1.BN(maxPrice), exponent: new anchor_1.BN(-1 * constants_1.USD_DECIMALS), confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
|
4083
4155
|
var tokenAumUsd = maxPriceOracle.getAssetAmountUsd(custodyAccount.assets.owned, custodyAccount.decimals);
|
4084
4156
|
if (amountAdd.gt(constants_1.BN_ZERO) && amountRemove.gt(constants_1.BN_ZERO)) {
|
4085
4157
|
throw new Error("cannot add and remove liquidity together");
|
@@ -4102,50 +4174,16 @@ var PerpetualsClient = (function () {
|
|
4102
4174
|
}
|
4103
4175
|
return newRatio;
|
4104
4176
|
};
|
4105
|
-
PerpetualsClient.prototype.getPriceAfterSlippageOld = function (isEntry, slippageBps, targetPrice, targetEmaPrice, side, custody) {
|
4106
|
-
if (isEntry) {
|
4107
|
-
var current_price_usd = this.getEntryPriceUsdSync(side, targetPrice, targetEmaPrice, custody);
|
4108
|
-
var spread_i = (0, utils_1.checkedDecimalCeilMul)(current_price_usd, new anchor_1.BN(-1 * constants_1.USD_DECIMALS), slippageBps, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), new anchor_1.BN(-1 * constants_1.USD_DECIMALS));
|
4109
|
-
if ((0, types_1.isVariant)(side, 'long')) {
|
4110
|
-
return { price: current_price_usd.add(spread_i), exponent: targetPrice.exponent.toNumber() };
|
4111
|
-
}
|
4112
|
-
else {
|
4113
|
-
if (spread_i.lt(current_price_usd)) {
|
4114
|
-
return { price: current_price_usd.sub(spread_i), exponent: targetPrice.exponent.toNumber() };
|
4115
|
-
}
|
4116
|
-
else {
|
4117
|
-
return { price: constants_1.BN_ZERO, exponent: targetPrice.exponent.toNumber() };
|
4118
|
-
}
|
4119
|
-
;
|
4120
|
-
}
|
4121
|
-
}
|
4122
|
-
else {
|
4123
|
-
var current_price = this.getExitPriceSync(side, targetPrice, targetEmaPrice, custody);
|
4124
|
-
var spread_i = (0, utils_1.checkedDecimalCeilMul)(current_price, new anchor_1.BN(-1 * constants_1.USD_DECIMALS), slippageBps, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), new anchor_1.BN(-1 * constants_1.USD_DECIMALS));
|
4125
|
-
if ((0, types_1.isVariant)(side, 'long')) {
|
4126
|
-
if (spread_i.lt(current_price)) {
|
4127
|
-
return { price: current_price.sub(spread_i), exponent: targetPrice.exponent.toNumber() };
|
4128
|
-
}
|
4129
|
-
else {
|
4130
|
-
return { price: constants_1.BN_ZERO, exponent: targetPrice.exponent.toNumber() };
|
4131
|
-
}
|
4132
|
-
;
|
4133
|
-
}
|
4134
|
-
else {
|
4135
|
-
return { price: current_price.add(spread_i), exponent: targetPrice.exponent.toNumber() };
|
4136
|
-
}
|
4137
|
-
}
|
4138
|
-
};
|
4139
4177
|
PerpetualsClient.prototype.getPriceAfterSlippage = function (isEntry, slippageBps, targetPrice, side) {
|
4140
4178
|
if (isEntry) {
|
4141
|
-
var
|
4142
|
-
var spread_i = (0, utils_1.checkedDecimalCeilMul)(
|
4179
|
+
var currentPrice = targetPrice.price;
|
4180
|
+
var spread_i = (0, utils_1.checkedDecimalCeilMul)(currentPrice, targetPrice.exponent, slippageBps, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), targetPrice.exponent);
|
4143
4181
|
if ((0, types_1.isVariant)(side, 'long')) {
|
4144
|
-
return { price:
|
4182
|
+
return { price: currentPrice.add(spread_i), exponent: targetPrice.exponent.toNumber() };
|
4145
4183
|
}
|
4146
4184
|
else {
|
4147
|
-
if (spread_i.lt(
|
4148
|
-
return { price:
|
4185
|
+
if (spread_i.lt(currentPrice)) {
|
4186
|
+
return { price: currentPrice.sub(spread_i), exponent: targetPrice.exponent.toNumber() };
|
4149
4187
|
}
|
4150
4188
|
else {
|
4151
4189
|
return { price: constants_1.BN_ZERO, exponent: targetPrice.exponent.toNumber() };
|
@@ -4155,7 +4193,7 @@ var PerpetualsClient = (function () {
|
|
4155
4193
|
}
|
4156
4194
|
else {
|
4157
4195
|
var current_price = targetPrice.price;
|
4158
|
-
var spread_i = (0, utils_1.checkedDecimalCeilMul)(current_price,
|
4196
|
+
var spread_i = (0, utils_1.checkedDecimalCeilMul)(current_price, targetPrice.exponent, slippageBps, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), targetPrice.exponent);
|
4159
4197
|
if ((0, types_1.isVariant)(side, 'long')) {
|
4160
4198
|
if (spread_i.lt(current_price)) {
|
4161
4199
|
return { price: current_price.sub(spread_i), exponent: targetPrice.exponent.toNumber() };
|