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