flash-sdk 9.0.0-alpha.6 → 9.0.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -638,24 +638,41 @@ var PerpetualsClient = (function () {
638
638
  var averageEntryPrice = totalValue.div(totalSize);
639
639
  return averageEntryPrice;
640
640
  };
641
- this.getLeverageSync = function (sizeUsd, collateralAmount, collateralMinOraclePrice, collateralTokenDecimals, pnlUsd) {
642
- var currentCollateralUsd = collateralMinOraclePrice.getAssetAmountUsd(collateralAmount, collateralTokenDecimals);
643
- var currentCollateralUsdIncludingPnl = currentCollateralUsd.add(pnlUsd);
644
- if (currentCollateralUsdIncludingPnl.gt(constants_1.BN_ZERO)) {
645
- return sizeUsd.mul(new anchor_1.BN(constants_1.BPS_POWER)).div(currentCollateralUsdIncludingPnl);
641
+ this.getLeverageContractHelper = function (positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, isInitial, poolConfig) {
642
+ var pnl = _this.getPnlSync(positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
643
+ var exitFee = _this.getExitFeeSync(positionAccount, targetCustodyAccount, collateralCustodyAccount, collateralPrice, collateralEmaPrice);
644
+ var lockFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionAccount, collateralCustodyAccount, currentTimestamp);
645
+ var unsettledFeesUsd = exitFee.exitFeeUsd.add(lockFeeUsd);
646
+ var lossUsd = pnl.lossUsd.add(unsettledFeesUsd);
647
+ var currentMarginUsd = constants_1.BN_ZERO;
648
+ if (isInitial) {
649
+ currentMarginUsd = positionAccount.collateralUsd.sub(lossUsd);
650
+ }
651
+ else {
652
+ currentMarginUsd = positionAccount.collateralUsd.add(pnl.profitUsd).sub(lossUsd);
653
+ }
654
+ if (currentMarginUsd.gt(constants_1.BN_ZERO)) {
655
+ return positionAccount.sizeUsd.mul(new anchor_1.BN(constants_1.BPS_POWER)).div(currentMarginUsd);
646
656
  }
647
657
  else {
648
658
  return new anchor_1.BN(Number.MAX_SAFE_INTEGER);
649
659
  }
650
660
  };
651
- this.getLeverageAtAmountEntryWithSwapSync = function (positionAccount, inputDeltaAmount, sizeDeltaAmount, side, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, swapPoolAumUsdMax, poolConfigPosition, poolConfigSwap, pnlUsd) {
661
+ this.getLeverageSync = function (positionAccount, finalCollateralUsd, finalSizeUsd, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, isInitial, poolConfig) {
662
+ positionAccount = positionAccount.clone();
663
+ positionAccount.sizeUsd = finalSizeUsd;
664
+ positionAccount.sizeAmount = targetPrice.getTokenAmount(finalSizeUsd, targetCustodyAccount.decimals);
665
+ positionAccount.collateralUsd = finalCollateralUsd;
666
+ return _this.getLeverageContractHelper(positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, isInitial, poolConfig);
667
+ };
668
+ this.getLeverageAtAmountEntryWithSwapSync = function (positionAccount, inputDeltaAmount, sizeDeltaAmount, side, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, swapPoolAumUsdMax, poolConfig, pnlUsd) {
652
669
  var finalCollateralAmount = constants_1.BN_ZERO;
653
670
  if (!inputDeltaAmount.isZero()) {
654
671
  if (inputTokenCustodyAccount.publicKey.equals(collateralTokenCustodyAccount.publicKey)) {
655
672
  finalCollateralAmount = inputDeltaAmount;
656
673
  }
657
674
  else {
658
- var swapAmountOut = _this.getSwapAmountAndFeesSync(inputDeltaAmount, constants_1.BN_ZERO, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, swapPoolAumUsdMax, poolConfigSwap).minAmountOut;
675
+ var swapAmountOut = _this.getSwapAmountAndFeesSync(inputDeltaAmount, constants_1.BN_ZERO, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, swapPoolAumUsdMax, poolConfig).minAmountOut;
659
676
  finalCollateralAmount = swapAmountOut;
660
677
  }
661
678
  }
@@ -681,9 +698,8 @@ var PerpetualsClient = (function () {
681
698
  entryOraclePrice.price = _this.getAveragePriceSync(positionEntryPrice.price, positionAccount.sizeAmount, entryOraclePrice.price, sizeDeltaAmount);
682
699
  }
683
700
  var collateralMinOraclePrice = _this.getMinAndMaxOraclePriceSync(collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount).min;
684
- var collateralAmount = positionAccount.collateralAmount.add(finalCollateralAmount);
685
- var currentCollateralUsd = collateralMinOraclePrice.getAssetAmountUsd(collateralAmount, collateralTokenCustodyAccount.decimals);
686
- var currentCollateralUsdIncludingPnl = currentCollateralUsd.add(pnlUsd).sub(openFeeUsd);
701
+ var finalCollateralUsd = collateralMinOraclePrice.getAssetAmountUsd(finalCollateralAmount, collateralTokenCustodyAccount.decimals);
702
+ var currentCollateralUsdIncludingPnl = finalCollateralUsd.add(pnlUsd).sub(openFeeUsd);
687
703
  var sizeAmount = positionAccount.sizeAmount.add(sizeDeltaAmount);
688
704
  var sizeAmountUsd = entryOraclePrice.getAssetAmountUsd(sizeAmount, targetTokenCustodyAccount.decimals);
689
705
  if (currentCollateralUsdIncludingPnl.gt(constants_1.BN_ZERO)) {
@@ -693,68 +709,6 @@ var PerpetualsClient = (function () {
693
709
  return new anchor_1.BN(Number.MAX_SAFE_INTEGER);
694
710
  }
695
711
  };
696
- this.getEntryPriceAndFeeSync = function (positionAccount, marketCorrelation, collateralDeltaAmount, sizeDeltaAmount, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, discountBps) {
697
- if (discountBps === void 0) { discountBps = constants_1.BN_ZERO; }
698
- if (collateralDeltaAmount.isNeg() || sizeDeltaAmount.isNeg()) {
699
- throw new Error("Delta Amounts cannot be negative.");
700
- }
701
- var sizeUsd = targetPrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
702
- var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetPrice, targetEmaPrice, targetCustodyAccount, sizeUsd);
703
- if (positionAccount === null) {
704
- var data = __assign({}, types_1.DEFAULT_POSITION);
705
- positionAccount = PositionAccount_1.PositionAccount.from(web3_js_1.PublicKey.default, data);
706
- var sizeUsd_1 = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
707
- positionAccount.sizeUsd = sizeUsd_1;
708
- positionAccount.sizeDecimals = targetCustodyAccount.decimals;
709
- positionAccount.collateralDecimals = collateralCustodyAccount.decimals;
710
- positionAccount.lockedDecimals = collateralCustodyAccount.decimals;
711
- }
712
- else {
713
- positionAccount = positionAccount.clone();
714
- var positionEntryPrice = OraclePrice_1.OraclePrice.from({
715
- price: positionAccount.entryPrice.price,
716
- exponent: new anchor_1.BN(positionAccount.entryPrice.exponent),
717
- confidence: constants_1.BN_ZERO,
718
- timestamp: constants_1.BN_ZERO
719
- });
720
- entryOraclePrice.price = _this.getAveragePriceSync(positionEntryPrice.price, positionAccount.sizeAmount, entryOraclePrice.price, sizeDeltaAmount);
721
- var sizeDeltaUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
722
- positionAccount.sizeUsd = positionAccount.sizeUsd.add(sizeDeltaUsd);
723
- }
724
- positionAccount.collateralAmount = positionAccount.collateralAmount.add(collateralDeltaAmount);
725
- positionAccount.sizeAmount = positionAccount.sizeAmount.add(sizeDeltaAmount);
726
- var lockFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionAccount, collateralCustodyAccount, currentTimestamp);
727
- var liquidationPrice = _this.getLiquidationPriceSync(positionAccount.collateralAmount, positionAccount.sizeAmount, entryOraclePrice, lockFeeUsd, marketCorrelation, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, positionAccount);
728
- var sizeAmountUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
729
- var collateralTokenMinOraclePrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
730
- var feeUsd = constants_1.BN_ZERO;
731
- var feeAmount = constants_1.BN_ZERO;
732
- var feeUsdAfterDiscount = constants_1.BN_ZERO;
733
- var feeAmountAfterDiscount = constants_1.BN_ZERO;
734
- if (positionAccount !== null && sizeDeltaAmount.isZero()) {
735
- }
736
- else {
737
- feeUsd = sizeAmountUsd.mul(targetCustodyAccount.fees.openPosition).div(new anchor_1.BN(constants_1.RATE_POWER));
738
- feeAmount = feeUsd.mul(new anchor_1.BN(Math.pow(10, collateralCustodyAccount.decimals))).div(collateralTokenMinOraclePrice.price);
739
- if (discountBps.gt(constants_1.BN_ZERO)) {
740
- feeUsdAfterDiscount = feeUsd.mul(discountBps).div(new anchor_1.BN(constants_1.BPS_POWER));
741
- feeUsdAfterDiscount = feeUsd.sub(feeUsdAfterDiscount);
742
- feeAmountAfterDiscount = feeUsdAfterDiscount.mul(new anchor_1.BN(Math.pow(10, collateralCustodyAccount.decimals))).div(collateralTokenMinOraclePrice.price);
743
- }
744
- else {
745
- feeUsdAfterDiscount = feeUsd;
746
- feeAmountAfterDiscount = feeAmount;
747
- }
748
- }
749
- return {
750
- entryOraclePrice: entryOraclePrice,
751
- feeUsd: feeUsd,
752
- feeAmount: feeAmount,
753
- feeUsdAfterDiscount: feeUsdAfterDiscount,
754
- feeAmountAfterDiscount: feeAmountAfterDiscount,
755
- liquidationPrice: liquidationPrice
756
- };
757
- };
758
712
  this.getEntryPriceAndFeeSyncV2 = function (positionAccount, marketCorrelation, collateralDeltaAmount, sizeDeltaAmount, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, discountBps, enableLogs) {
759
713
  if (discountBps === void 0) { discountBps = constants_1.BN_ZERO; }
760
714
  if (enableLogs === void 0) { enableLogs = false; }
@@ -766,8 +720,8 @@ var PerpetualsClient = (function () {
766
720
  if (positionAccount === null) {
767
721
  var data = __assign({}, types_1.DEFAULT_POSITION);
768
722
  positionAccount = PositionAccount_1.PositionAccount.from(web3_js_1.PublicKey.default, data);
769
- var sizeUsd_2 = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
770
- positionAccount.sizeUsd = sizeUsd_2;
723
+ var sizeUsd_1 = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
724
+ positionAccount.sizeUsd = sizeUsd_1;
771
725
  positionAccount.sizeDecimals = targetCustodyAccount.decimals;
772
726
  positionAccount.collateralDecimals = collateralCustodyAccount.decimals;
773
727
  positionAccount.lockedDecimals = collateralCustodyAccount.decimals;
@@ -784,12 +738,13 @@ var PerpetualsClient = (function () {
784
738
  var sizeDeltaUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
785
739
  positionAccount.sizeUsd = positionAccount.sizeUsd.add(sizeDeltaUsd);
786
740
  }
787
- positionAccount.collateralAmount = positionAccount.collateralAmount.add(collateralDeltaAmount);
741
+ var collateralMinMaxPrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount);
742
+ var collateralDeltaUsd = collateralMinMaxPrice.min.getAssetAmountUsd(collateralDeltaAmount, collateralCustodyAccount.decimals);
743
+ positionAccount.collateralUsd = positionAccount.collateralUsd.add(collateralDeltaUsd);
788
744
  positionAccount.sizeAmount = positionAccount.sizeAmount.add(sizeDeltaAmount);
789
- var lockFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionAccount, collateralCustodyAccount, currentTimestamp);
790
- var liquidationPrice = _this.getLiquidationPriceSync(positionAccount.collateralAmount, positionAccount.sizeAmount, entryOraclePrice, lockFeeUsd, marketCorrelation, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, positionAccount);
745
+ var lockAndUnsettledFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionAccount, collateralCustodyAccount, currentTimestamp);
746
+ var liquidationPrice = _this.getLiquidationPriceContractHelper(entryOraclePrice, lockAndUnsettledFeeUsd, side, targetCustodyAccount, positionAccount);
791
747
  var sizeAmountUsd = entryOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
792
- var collateralTokenMinOraclePrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
793
748
  var feeUsd = constants_1.BN_ZERO;
794
749
  var feeAmount = constants_1.BN_ZERO;
795
750
  var feeUsdAfterDiscount = constants_1.BN_ZERO;
@@ -798,11 +753,11 @@ var PerpetualsClient = (function () {
798
753
  }
799
754
  else {
800
755
  feeUsd = sizeAmountUsd.mul(targetCustodyAccount.fees.openPosition).div(new anchor_1.BN(constants_1.RATE_POWER));
801
- feeAmount = feeUsd.mul(new anchor_1.BN(Math.pow(10, collateralCustodyAccount.decimals))).div(collateralTokenMinOraclePrice.price);
756
+ feeAmount = feeUsd.mul(new anchor_1.BN(Math.pow(10, collateralCustodyAccount.decimals))).div(collateralMinMaxPrice.min.price);
802
757
  if (discountBps.gt(constants_1.BN_ZERO)) {
803
758
  feeUsdAfterDiscount = feeUsd.mul(discountBps).div(new anchor_1.BN(constants_1.BPS_POWER));
804
759
  feeUsdAfterDiscount = feeUsd.sub(feeUsdAfterDiscount);
805
- feeAmountAfterDiscount = feeUsdAfterDiscount.mul(new anchor_1.BN(Math.pow(10, collateralCustodyAccount.decimals))).div(collateralTokenMinOraclePrice.price);
760
+ feeAmountAfterDiscount = feeUsdAfterDiscount.mul(new anchor_1.BN(Math.pow(10, collateralCustodyAccount.decimals))).div(collateralMinMaxPrice.min.price);
806
761
  }
807
762
  else {
808
763
  feeUsdAfterDiscount = feeUsd;
@@ -861,25 +816,26 @@ var PerpetualsClient = (function () {
861
816
  if (collateralDeltaAmount.isNeg() || sizeDeltaAmount.isNeg()) {
862
817
  throw new Error("Delta Amounts cannot be negative ");
863
818
  }
864
- resultingPositionAccount.collateralAmount = resultingPositionAccount.collateralAmount.sub(collateralDeltaAmount);
865
- resultingPositionAccount.sizeAmount = resultingPositionAccount.sizeAmount.sub(sizeDeltaAmount);
866
819
  if (resultingPositionAccount.collateralAmount.isNeg() || resultingPositionAccount.sizeAmount.isNeg()) {
867
820
  throw new Error("cannot remove/close more than collateral/Size");
868
821
  }
869
822
  var sizeUsd = targetPrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
870
823
  var exitOraclePrice = _this.getExitOraclePriceSync(side, targetPrice, targetEmaPrice, targetCustodyAccount, sizeUsd);
871
- var _a = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount), collateralTokenMinOraclePrice = _a.min, collateralTokenMaxOraclePrice = _a.max;
824
+ var collateralMinMaxPrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount);
825
+ var collateralDeltaUsd = collateralMinMaxPrice.min.getAssetAmountUsd(collateralDeltaAmount, collateralCustodyAccount.decimals);
826
+ resultingPositionAccount.collateralUsd = resultingPositionAccount.collateralUsd.sub(collateralDeltaUsd);
827
+ resultingPositionAccount.sizeAmount = resultingPositionAccount.sizeAmount.sub(sizeDeltaAmount);
872
828
  var lockAndUnsettledFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(resultingPositionAccount, collateralCustodyAccount, currentTimestamp);
873
- var lockAndUnsettledFee = collateralTokenMinOraclePrice.getTokenAmount(lockAndUnsettledFeeUsd, collateralCustodyAccount.decimals);
829
+ var lockAndUnsettledFee = collateralMinMaxPrice.min.getTokenAmount(lockAndUnsettledFeeUsd, collateralCustodyAccount.decimals);
874
830
  var sizeAmountUsd = exitOraclePrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
875
831
  var exitFeeUsd = sizeAmountUsd.mul(targetCustodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
876
- var exitFeeAmount = collateralTokenMaxOraclePrice.getTokenAmount(exitFeeUsd, collateralCustodyAccount.decimals);
832
+ var exitFeeAmount = collateralMinMaxPrice.max.getTokenAmount(exitFeeUsd, collateralCustodyAccount.decimals);
877
833
  var exitFeeUsdAfterDiscount = constants_1.BN_ZERO;
878
834
  var exitFeeAmountAfterDiscount = constants_1.BN_ZERO;
879
835
  if (discountBps.gt(constants_1.BN_ZERO)) {
880
836
  exitFeeUsdAfterDiscount = exitFeeUsd.mul(discountBps).div(new anchor_1.BN(constants_1.BPS_POWER));
881
837
  exitFeeUsdAfterDiscount = exitFeeUsd.sub(exitFeeUsdAfterDiscount);
882
- exitFeeAmountAfterDiscount = collateralTokenMaxOraclePrice.getTokenAmount(exitFeeUsdAfterDiscount, collateralCustodyAccount.decimals);
838
+ exitFeeAmountAfterDiscount = collateralMinMaxPrice.max.getTokenAmount(exitFeeUsdAfterDiscount, collateralCustodyAccount.decimals);
883
839
  }
884
840
  else {
885
841
  exitFeeUsdAfterDiscount = exitFeeUsd;
@@ -889,7 +845,7 @@ var PerpetualsClient = (function () {
889
845
  price: positionAccount.entryPrice.price, exponent: new anchor_1.BN(positionAccount.entryPrice.exponent), confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO
890
846
  });
891
847
  resultingPositionAccount.sizeUsd = positionEntryOraclePrice.getAssetAmountUsd(resultingPositionAccount.sizeAmount, targetCustodyAccount.decimals);
892
- var liquidationPrice = _this.getLiquidationPriceSync(resultingPositionAccount.collateralAmount, resultingPositionAccount.sizeAmount, positionEntryOraclePrice, lockAndUnsettledFeeUsd, marketCorrelation, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, positionAccount);
848
+ var liquidationPrice = _this.getLiquidationPriceContractHelper(positionEntryOraclePrice, lockAndUnsettledFeeUsd, side, targetCustodyAccount, positionAccount);
893
849
  return {
894
850
  exitOraclePrice: exitOraclePrice,
895
851
  borrowFeeUsd: lockAndUnsettledFeeUsd,
@@ -902,9 +858,7 @@ var PerpetualsClient = (function () {
902
858
  };
903
859
  };
904
860
  this.getTradeSpread = function (targetCustodyAccount, sizeUsd) {
905
- if (targetCustodyAccount.pricing.tradeSpreadMax.sub(targetCustodyAccount.pricing.tradeSpreadMin).isZero()
906
- ||
907
- sizeUsd.isZero()) {
861
+ if (targetCustodyAccount.pricing.tradeSpreadMax.sub(targetCustodyAccount.pricing.tradeSpreadMin).isZero() || sizeUsd.isZero()) {
908
862
  return constants_1.BN_ZERO;
909
863
  }
910
864
  var slope = ((targetCustodyAccount.pricing.tradeSpreadMax.sub(targetCustodyAccount.pricing.tradeSpreadMin)).mul(new anchor_1.BN(Math.pow(10, (constants_1.RATE_DECIMALS + constants_1.BPS_DECIMALS)))))
@@ -923,21 +877,11 @@ var PerpetualsClient = (function () {
923
877
  var exitOraclePrice = OraclePrice_1.OraclePrice.from({ price: exitPriceBN, exponent: maxPrice.exponent, confidence: maxPrice.confidence, timestamp: maxPrice.timestamp });
924
878
  return exitOraclePrice;
925
879
  };
926
- this.getExitOraclePriceWithoutSpreadSync = function (side, targetPrice, targetEmaPrice, targetCustodyAccount) {
927
- var _a = _this.getMinAndMaxOraclePriceSync(targetPrice, targetEmaPrice, targetCustodyAccount), minPrice = _a.min, maxPrice = _a.max;
928
- if ((0, types_1.isVariant)(side, 'long')) {
929
- return minPrice;
930
- }
931
- else {
932
- return maxPrice;
933
- }
934
- };
935
880
  this.getSizeAmountFromLeverageAndCollateral = function (collateralAmtWithFee, leverage, marketToken, collateralToken, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, discountBps) {
936
881
  if (discountBps === void 0) { discountBps = constants_1.BN_ZERO; }
937
882
  var collateralTokenMinPrice = _this.getMinAndMaxPriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount).min;
938
883
  var collateralTokenMinPriceUi = new bignumber_js_1.default(collateralTokenMinPrice.toString()).dividedBy(Math.pow(10, constants_1.USD_DECIMALS));
939
- var collateralAmtMinUsdUi = new bignumber_js_1.default(collateralAmtWithFee.toString()).dividedBy(Math.pow(10, collateralToken.decimals))
940
- .multipliedBy(collateralTokenMinPriceUi);
884
+ var collateralAmtMinUsdUi = new bignumber_js_1.default(collateralAmtWithFee.toString()).dividedBy(Math.pow(10, collateralToken.decimals)).multipliedBy(collateralTokenMinPriceUi);
941
885
  var openPosFeeRateUi = new bignumber_js_1.default(targetCustodyAccount.fees.openPosition.toString()).dividedBy(Math.pow(10, constants_1.RATE_DECIMALS));
942
886
  if (!discountBps.isZero()) {
943
887
  var discountBpsUi = new bignumber_js_1.default(discountBps.toString()).dividedBy(Math.pow(10, constants_1.BPS_DECIMALS));
@@ -945,20 +889,20 @@ var PerpetualsClient = (function () {
945
889
  }
946
890
  var sizeUsdUi = collateralAmtMinUsdUi.multipliedBy(leverage)
947
891
  .dividedBy(new bignumber_js_1.default(1).plus((new bignumber_js_1.default(2).multipliedBy(openPosFeeRateUi)).multipliedBy(leverage)));
948
- var lockedUsd = (0, utils_1.uiDecimalsToNative)(sizeUsdUi.toString(), constants_1.USD_DECIMALS);
949
- var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetPrice, targetEmaPrice, targetCustodyAccount, lockedUsd);
892
+ var sizeUsd = (0, utils_1.uiDecimalsToNative)(sizeUsdUi.toString(), constants_1.USD_DECIMALS);
893
+ var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetPrice, targetEmaPrice, targetCustodyAccount, sizeUsd);
950
894
  var entryPriceUsdUi = new bignumber_js_1.default(entryOraclePrice.toUiPrice(constants_1.ORACLE_EXPONENT));
951
895
  var sizeAmountUi = sizeUsdUi.dividedBy(entryPriceUsdUi);
952
896
  return (0, utils_1.uiDecimalsToNative)(sizeAmountUi.toFixed(marketToken.decimals, bignumber_js_1.default.ROUND_DOWN), marketToken.decimals);
953
897
  };
954
- this.getSizeAmountWithSwapSync = function (amountIn, leverage, side, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, swapPoolAumUsdMax, poolConfigSwap, discountBps) {
898
+ this.getSizeAmountWithSwapSync = function (amountIn, leverage, side, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, swapPoolAumUsdMax, poolConfig, discountBps) {
955
899
  if (discountBps === void 0) { discountBps = constants_1.BN_ZERO; }
956
900
  var finalCollateralAmount = constants_1.BN_ZERO;
957
901
  if (inputTokenCustodyAccount.publicKey.equals(collateralTokenCustodyAccount.publicKey)) {
958
902
  finalCollateralAmount = amountIn;
959
903
  }
960
904
  else {
961
- var swapAmountOut = _this.getSwapAmountAndFeesSync(amountIn, constants_1.BN_ZERO, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, swapPoolAumUsdMax, poolConfigSwap).minAmountOut;
905
+ var swapAmountOut = _this.getSwapAmountAndFeesSync(amountIn, constants_1.BN_ZERO, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, swapPoolAumUsdMax, poolConfig).minAmountOut;
962
906
  finalCollateralAmount = swapAmountOut;
963
907
  }
964
908
  var collateralTokenMinPrice = _this.getMinAndMaxPriceSync(collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount).min;
@@ -972,8 +916,8 @@ var PerpetualsClient = (function () {
972
916
  }
973
917
  var sizeUsdUi = collateralAmtMinUsdUi.multipliedBy(leverage)
974
918
  .dividedBy(new bignumber_js_1.default(1).plus((new bignumber_js_1.default(2).multipliedBy(openPosFeeRateUi)).multipliedBy(leverage)));
975
- var lockedUsd = (0, utils_1.uiDecimalsToNative)(sizeUsdUi.toFixed(constants_1.USD_DECIMALS), constants_1.USD_DECIMALS);
976
- var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, lockedUsd);
919
+ var sizeUsd = (0, utils_1.uiDecimalsToNative)(sizeUsdUi.toFixed(constants_1.USD_DECIMALS), constants_1.USD_DECIMALS);
920
+ var entryOraclePrice = _this.getEntryPriceUsdSync(side, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, sizeUsd);
977
921
  var entryPriceUsdUi = new bignumber_js_1.default(entryOraclePrice.toUiPrice(constants_1.ORACLE_EXPONENT));
978
922
  var sizeAmountUi = sizeUsdUi.dividedBy(entryPriceUsdUi);
979
923
  return (0, utils_1.uiDecimalsToNative)(sizeAmountUi.toFixed(targetTokenCustodyAccount.decimals, bignumber_js_1.default.ROUND_DOWN), targetTokenCustodyAccount.decimals);
@@ -996,7 +940,7 @@ var PerpetualsClient = (function () {
996
940
  var collateralAmtWithFeeUi = collateralWithFeeUsdUi.dividedBy(collateralTokenMinPriceUi);
997
941
  return (0, utils_1.uiDecimalsToNative)(collateralAmtWithFeeUi.toFixed(collateralToken.decimals, bignumber_js_1.default.ROUND_DOWN), collateralToken.decimals);
998
942
  };
999
- this.getCollateralAmountWithSwapSync = function (sizeAmount, leverage, side, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, swapPoolAumUsdMax, poolConfigPosition, poolConfigSwap) {
943
+ this.getCollateralAmountWithSwapSync = function (sizeAmount, leverage, side, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount, targetTokenPrice, targetTokenEmaPrice, targetTokenCustodyAccount, swapPoolAumUsdMax, poolConfig) {
1000
944
  var collateralTokenMinPrice = _this.getMinAndMaxPriceSync(collateralTokenPrice, collateralTokenEmaPrice, collateralTokenCustodyAccount).min;
1001
945
  var collateralTokenMinPriceUi = new bignumber_js_1.default(collateralTokenMinPrice.toString()).dividedBy(Math.pow(10, constants_1.USD_DECIMALS));
1002
946
  var sizeUsd = targetTokenPrice.getAssetAmountUsd(sizeAmount, targetTokenCustodyAccount.decimals);
@@ -1013,7 +957,7 @@ var PerpetualsClient = (function () {
1013
957
  collateralInInputToken = collateralAmountWithFee;
1014
958
  }
1015
959
  else {
1016
- collateralInInputToken = _this.getSwapAmountAndFeesSync(constants_1.BN_ZERO, collateralAmountWithFee, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, swapPoolAumUsdMax, poolConfigSwap).minAmountIn;
960
+ collateralInInputToken = _this.getSwapAmountAndFeesSync(constants_1.BN_ZERO, collateralAmountWithFee, poolAccount, inputTokenPrice, inputTokenEmaPrice, inputTokenCustodyAccount, swapOutTokenPrice, swapOutTokenEmaPrice, swapOutTokenCustodyAccount, swapPoolAumUsdMax, poolConfig).minAmountIn;
1017
961
  }
1018
962
  return collateralInInputToken;
1019
963
  };
@@ -1039,10 +983,10 @@ var PerpetualsClient = (function () {
1039
983
  var decimalPower = new anchor_1.BN(Math.pow(10, targetCustodyAccount.decimals));
1040
984
  var closeRatio = (positionDelta.sizeAmount.mul(decimalPower)).div(positionAccount.sizeAmount);
1041
985
  positionDelta.sizeUsd = (positionAccount.sizeUsd.mul(closeRatio)).div(decimalPower);
1042
- positionDelta.unsettledFeesUsd = (positionAccount.unsettledFeesUsd.mul(closeRatio)).div(decimalPower);
1043
986
  positionDelta.lockedAmount = (positionAccount.lockedAmount.mul(closeRatio)).div(decimalPower);
1044
987
  positionDelta.lockedUsd = (positionAccount.lockedUsd.mul(closeRatio)).div(decimalPower);
1045
- positionDelta.collateralAmount = (positionAccount.collateralAmount.mul(closeRatio)).div(decimalPower);
988
+ positionDelta.collateralUsd = (positionAccount.collateralUsd.mul(closeRatio)).div(decimalPower);
989
+ positionDelta.unsettledFeesUsd = (positionAccount.unsettledFeesUsd.mul(closeRatio)).div(decimalPower);
1046
990
  positionDelta.degenSizeUsd = positionAccount.degenSizeUsd.mul(closeRatio).div(decimalPower);
1047
991
  var newPnl = _this.getPnlSync(positionDelta, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1048
992
  var exitFeeUsd = positionDelta.sizeUsd.mul(targetCustodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
@@ -1052,29 +996,29 @@ var PerpetualsClient = (function () {
1052
996
  }
1053
997
  var lockAndUnsettledFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionDelta, collateralCustodyAccount, currentTimestamp);
1054
998
  var totalFeesUsd = (exitFeeUsd.add(lockAndUnsettledFeeUsd));
1055
- var currentCollateralUsd = collateralMinMaxPrice.min.getAssetAmountUsd(positionDelta.collateralAmount, collateralCustodyAccount.decimals);
999
+ var currentCollateralUsd = positionDelta.collateralUsd;
1056
1000
  var liabilityUsd = newPnl.lossUsd.add(totalFeesUsd);
1057
- var assetsUsd = newPnl.profitUsd.add(currentCollateralUsd);
1058
- var closeAmount, feesAmount;
1001
+ var assetsUsd = anchor_1.BN.min(newPnl.profitUsd.add(currentCollateralUsd), collateralMinMaxPrice.max.getAssetAmountUsd(positionDelta.lockedAmount, collateralCustodyAccount.decimals));
1059
1002
  if (debugLogs) {
1060
1003
  console.log("assetsUsd.sub(liabilityUsd):", collateralCustodyAccount.decimals, assetsUsd.toString(), liabilityUsd.toString(), assetsUsd.sub(liabilityUsd).toString());
1061
1004
  }
1005
+ var closeAmountUsd, feesAmountUsd;
1062
1006
  if (assetsUsd.gte(liabilityUsd)) {
1063
- closeAmount = collateralMinMaxPrice.max.getTokenAmount(assetsUsd.sub(liabilityUsd), collateralCustodyAccount.decimals);
1064
- feesAmount = collateralMinMaxPrice.min.getTokenAmount(totalFeesUsd, collateralCustodyAccount.decimals);
1007
+ closeAmountUsd = assetsUsd.sub(liabilityUsd);
1008
+ feesAmountUsd = totalFeesUsd;
1065
1009
  }
1066
1010
  else {
1067
- closeAmount = constants_1.BN_ZERO;
1068
- feesAmount = collateralMinMaxPrice.min.getTokenAmount(assetsUsd.sub(newPnl.lossUsd), collateralCustodyAccount.decimals);
1011
+ closeAmountUsd = constants_1.BN_ZERO;
1012
+ feesAmountUsd = assetsUsd.sub(newPnl.lossUsd);
1069
1013
  }
1014
+ var closeAmount = collateralMinMaxPrice.max.getTokenAmount(closeAmountUsd, collateralCustodyAccount.decimals);
1070
1015
  var newPosition = PositionAccount_1.PositionAccount.from(positionAccount.publicKey, __assign({}, positionAccount));
1071
1016
  newPosition.sizeAmount = positionAccount.sizeAmount.sub(positionDelta.sizeAmount);
1072
1017
  newPosition.sizeUsd = positionAccount.sizeUsd.sub(positionDelta.sizeUsd);
1073
1018
  newPosition.lockedUsd = positionAccount.lockedUsd.sub(positionDelta.lockedUsd);
1074
1019
  newPosition.lockedAmount = positionAccount.lockedAmount.sub(positionDelta.lockedAmount);
1075
- newPosition.collateralAmount = positionAccount.collateralAmount.sub(positionDelta.collateralAmount);
1020
+ newPosition.collateralUsd = positionAccount.collateralUsd.sub(positionDelta.collateralUsd);
1076
1021
  newPosition.unsettledFeesUsd = positionAccount.unsettledFeesUsd.sub(positionDelta.unsettledFeesUsd);
1077
- newPosition.collateralUsd = collateralMinMaxPrice.min.getAssetAmountUsd(newPosition.collateralAmount, collateralCustodyAccount.decimals);
1078
1022
  newPosition.degenSizeUsd = positionAccount.degenSizeUsd.sub(positionDelta.degenSizeUsd);
1079
1023
  var feeUsdWithDiscount = constants_1.BN_ZERO;
1080
1024
  var feeUsd = sizeDeltaUsd.mul(targetCustodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
@@ -1089,8 +1033,8 @@ var PerpetualsClient = (function () {
1089
1033
  feeUsdWithDiscount = feeUsdWithDiscount.add(lockAndUnsettledFeeUsd);
1090
1034
  if (keepLevSame) {
1091
1035
  var collateralAmountReceived = closeAmount;
1092
- var collateralAmountRecievedUsd = collateralMinMaxPrice.min.getAssetAmountUsd(collateralAmountReceived, collateralCustodyAccount.decimals);
1093
- var _a = _this.getMaxWithdrawableAmountSyncInternal(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig, closeAmount), maxWithdrawableAmount = _a.maxWithdrawableAmount, diff = _a.diff;
1036
+ var collateralAmountRecievedUsd = closeAmountUsd;
1037
+ var _a = _this.getMaxWithdrawableAmountSyncInternal(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig, closeAmountUsd), maxWithdrawableAmount = _a.maxWithdrawableAmount, diff = _a.diff;
1094
1038
  if (debugLogs) {
1095
1039
  console.log("maxWithdrawableAmount ", maxWithdrawableAmount.toString(), keepLevSame);
1096
1040
  console.log("collateralAmountReceived ", collateralAmountReceived.toString(), keepLevSame);
@@ -1107,11 +1051,11 @@ var PerpetualsClient = (function () {
1107
1051
  collateralAmountRecievedUsd = collateralMinMaxPrice.min.getAssetAmountUsd(maxWithdrawableAmount, collateralCustodyAccount.decimals);
1108
1052
  }
1109
1053
  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 });
1110
- var finalInterestUsd = _this.getLockFeeAndUnsettledUsdForPosition(newPosition, collateralCustodyAccount, currentTimestamp);
1111
- var finalLiquidationPrice = _this.getLiquidationPriceSync(newPosition.collateralAmount, newPosition.sizeAmount, entryPrice, finalInterestUsd, marketCorrelation, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, newPosition);
1054
+ var lockAndUnsettledFeeUsdNew = _this.getLockFeeAndUnsettledUsdForPosition(newPosition, collateralCustodyAccount, currentTimestamp);
1055
+ var finalLiquidationPrice = _this.getLiquidationPriceContractHelper(entryPrice, lockAndUnsettledFeeUsdNew, side, targetCustodyAccount, newPosition);
1112
1056
  var finalPnl = _this.getPnlSync(newPosition, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1113
1057
  var finalPnlUsd = finalPnl.profitUsd.sub(finalPnl.lossUsd);
1114
- var newLev = _this.getLeverageSync(newPosition.sizeUsd, newPosition.collateralAmount, collateralMinMaxPrice.min, collateralCustodyAccount.decimals, constants_1.BN_ZERO);
1058
+ var newLev = _this.getLeverageSync(newPosition, newPosition.collateralUsd, newPosition.sizeUsd, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, false, poolConfig);
1115
1059
  return {
1116
1060
  newSizeUsd: newPosition.sizeUsd,
1117
1061
  feeUsd: feeUsd,
@@ -1129,8 +1073,8 @@ var PerpetualsClient = (function () {
1129
1073
  throw "only same leverage is supported for now";
1130
1074
  }
1131
1075
  };
1132
- this.getMaxWithdrawableAmountSyncInternal = function (positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig, closeAmount, errorBandwidthPercentageUi) {
1133
- if (closeAmount === void 0) { closeAmount = constants_1.BN_ZERO; }
1076
+ this.getMaxWithdrawableAmountSyncInternal = function (positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig, closeAmountUsd, errorBandwidthPercentageUi) {
1077
+ if (closeAmountUsd === void 0) { closeAmountUsd = constants_1.BN_ZERO; }
1134
1078
  if (errorBandwidthPercentageUi === void 0) { errorBandwidthPercentageUi = 5; }
1135
1079
  if (errorBandwidthPercentageUi > 100 || errorBandwidthPercentageUi < 0) {
1136
1080
  throw new Error("errorBandwidthPercentageUi cannot be >100 or <0");
@@ -1139,11 +1083,11 @@ var PerpetualsClient = (function () {
1139
1083
  (new anchor_1.BN(targetCustodyAccount.pricing.maxInitDegenLeverage)).mul(new anchor_1.BN(100 - errorBandwidthPercentageUi)).div(new anchor_1.BN(100))
1140
1084
  : (new anchor_1.BN(targetCustodyAccount.pricing.maxInitLeverage)).mul(new anchor_1.BN(100 - errorBandwidthPercentageUi)).div(new anchor_1.BN(100));
1141
1085
  var profitLoss = _this.getPnlSync(positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1142
- var _a = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount), collateralMinPrice = _a.min, collateralMaxPrice = _a.max;
1086
+ var collateralMinMaxPrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount);
1143
1087
  var exitFeeUsd = positionAccount.sizeUsd.mul(targetCustodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
1144
1088
  var lockAndUnsettledFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionAccount, collateralCustodyAccount, currentTimestamp);
1145
1089
  var lossUsd = profitLoss.lossUsd.add(exitFeeUsd).add(lockAndUnsettledFeeUsd);
1146
- var currentCollateralUsd = collateralMinPrice.getAssetAmountUsd(positionAccount.collateralAmount.add(closeAmount), collateralCustodyAccount.decimals);
1090
+ var currentCollateralUsd = positionAccount.collateralUsd.sub(closeAmountUsd);
1147
1091
  var availableInitMarginUsd = constants_1.BN_ZERO;
1148
1092
  if (profitLoss.lossUsd.lt(currentCollateralUsd)) {
1149
1093
  availableInitMarginUsd = currentCollateralUsd.sub(lossUsd);
@@ -1168,35 +1112,34 @@ var PerpetualsClient = (function () {
1168
1112
  return { maxWithdrawableAmount: constants_1.BN_ZERO, maxWithdrawableAmountUsd: constants_1.BN_ZERO, diff: constants_1.BN_ZERO };
1169
1113
  }
1170
1114
  else {
1171
- maxWithdrawableAmount = collateralMaxPrice.getTokenAmount(updatedMaxRemovableCollateralUsd, collateralCustodyAccount.decimals);
1115
+ maxWithdrawableAmount = collateralMinMaxPrice.max.getTokenAmount(updatedMaxRemovableCollateralUsd, collateralCustodyAccount.decimals);
1172
1116
  maxWithdrawableAmountUsd = updatedMaxRemovableCollateralUsd;
1173
1117
  }
1174
1118
  }
1175
1119
  else {
1176
- maxWithdrawableAmount = collateralMaxPrice.getTokenAmount(maxRemovableCollateralUsd, collateralCustodyAccount.decimals);
1120
+ maxWithdrawableAmount = collateralMinMaxPrice.max.getTokenAmount(maxRemovableCollateralUsd, collateralCustodyAccount.decimals);
1177
1121
  maxWithdrawableAmountUsd = maxRemovableCollateralUsd;
1178
1122
  }
1179
1123
  return { maxWithdrawableAmount: maxWithdrawableAmount, maxWithdrawableAmountUsd: maxWithdrawableAmountUsd, diff: diff };
1180
1124
  };
1181
- this.getFinalCloseAmountSync = function (positionAccount, marketCorrelation, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig) {
1125
+ this.getFinalCloseAmountUsdSync = function (positionAccount, marketCorrelation, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig) {
1182
1126
  var position = PositionAccount_1.PositionAccount.from(positionAccount.publicKey, __assign({}, positionAccount));
1183
1127
  var collateralMinMaxPrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount);
1184
- var collateralUsd = collateralMinMaxPrice.min.getAssetAmountUsd(position.collateralAmount, collateralCustodyAccount.decimals);
1185
1128
  var newPnl = _this.getPnlSync(position, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1186
1129
  var exitPriceAndFee = _this.getExitPriceAndFeeSync(positionAccount, marketCorrelation, positionAccount.collateralAmount, positionAccount.sizeAmount, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp);
1187
1130
  var totalFeesUsd = (exitPriceAndFee.exitFeeUsd.add(exitPriceAndFee.borrowFeeUsd));
1188
1131
  var liabilityUsd = newPnl.lossUsd.add(totalFeesUsd);
1189
- var assetsUsd = newPnl.profitUsd.add(collateralMinMaxPrice.min.getAssetAmountUsd(positionAccount.collateralAmount, positionAccount.collateralDecimals));
1190
- var closeAmount, feesAmount;
1132
+ var assetsUsd = anchor_1.BN.min(newPnl.profitUsd.add(positionAccount.collateralUsd), collateralMinMaxPrice.max.getAssetAmountUsd(positionAccount.lockedAmount, collateralCustodyAccount.decimals));
1133
+ var closeAmountUsd, feesAmountUsd;
1191
1134
  if (assetsUsd.gt(liabilityUsd)) {
1192
- closeAmount = collateralMinMaxPrice.max.getTokenAmount(assetsUsd.sub(liabilityUsd), position.collateralDecimals);
1193
- feesAmount = collateralMinMaxPrice.min.getTokenAmount(totalFeesUsd, positionAccount.collateralDecimals);
1135
+ closeAmountUsd = assetsUsd.sub(liabilityUsd);
1136
+ feesAmountUsd = totalFeesUsd;
1194
1137
  }
1195
1138
  else {
1196
- closeAmount = constants_1.BN_ZERO;
1197
- feesAmount = collateralMinMaxPrice.min.getTokenAmount(assetsUsd.sub(newPnl.lossUsd), positionAccount.collateralDecimals);
1139
+ closeAmountUsd = constants_1.BN_ZERO;
1140
+ feesAmountUsd = assetsUsd.sub(newPnl.lossUsd);
1198
1141
  }
1199
- return { closeAmount: closeAmount, feesAmount: feesAmount };
1142
+ return { closeAmountUsd: closeAmountUsd, feesAmountUsd: feesAmountUsd };
1200
1143
  };
1201
1144
  this.getMaxWithdrawableAmountSync = function (positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, poolConfig, errorBandwidthPercentageUi) {
1202
1145
  if (errorBandwidthPercentageUi === void 0) { errorBandwidthPercentageUi = 5; }
@@ -1212,11 +1155,11 @@ var PerpetualsClient = (function () {
1212
1155
  return { maxWithdrawableAmount: constants_1.BN_ZERO, maxWithdrawableAmountUsd: constants_1.BN_ZERO };
1213
1156
  }
1214
1157
  var profitLoss = _this.getPnlSync(positionAccount, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1215
- var _a = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount), collateralMinPrice = _a.min, collateralMaxPrice = _a.max;
1158
+ var collateralMinMaxPrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount);
1216
1159
  var exitFeeUsd = positionAccount.sizeUsd.mul(targetCustodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
1217
1160
  var lockAndUnsettledFeeUsd = _this.getLockFeeAndUnsettledUsdForPosition(positionAccount, collateralCustodyAccount, currentTimestamp);
1218
1161
  var lossUsd = profitLoss.lossUsd.add(exitFeeUsd).add(lockAndUnsettledFeeUsd);
1219
- var currentCollateralUsd = collateralMinPrice.getAssetAmountUsd(positionAccount.collateralAmount, collateralCustodyAccount.decimals);
1162
+ var currentCollateralUsd = positionAccount.collateralUsd;
1220
1163
  var availableInitMarginUsd = constants_1.BN_ZERO;
1221
1164
  if (profitLoss.lossUsd.lt(currentCollateralUsd)) {
1222
1165
  availableInitMarginUsd = currentCollateralUsd.sub(lossUsd);
@@ -1232,11 +1175,11 @@ var PerpetualsClient = (function () {
1232
1175
  var maxWithdrawableAmount;
1233
1176
  var maxWithdrawableAmountUsd = constants_1.BN_ZERO;
1234
1177
  if (maxRemoveableCollateralUsdAfterMinRequired.lt(maxRemovableCollateralUsd)) {
1235
- maxWithdrawableAmount = collateralMaxPrice.getTokenAmount(maxRemoveableCollateralUsdAfterMinRequired, collateralCustodyAccount.decimals);
1178
+ maxWithdrawableAmount = collateralMinMaxPrice.max.getTokenAmount(maxRemoveableCollateralUsdAfterMinRequired, collateralCustodyAccount.decimals);
1236
1179
  maxWithdrawableAmountUsd = maxRemoveableCollateralUsdAfterMinRequired;
1237
1180
  }
1238
1181
  else {
1239
- maxWithdrawableAmount = collateralMaxPrice.getTokenAmount(maxRemovableCollateralUsd, collateralCustodyAccount.decimals);
1182
+ maxWithdrawableAmount = collateralMinMaxPrice.max.getTokenAmount(maxRemovableCollateralUsd, collateralCustodyAccount.decimals);
1240
1183
  maxWithdrawableAmountUsd = maxRemovableCollateralUsd;
1241
1184
  }
1242
1185
  return { maxWithdrawableAmount: maxWithdrawableAmount, maxWithdrawableAmountUsd: maxWithdrawableAmountUsd };
@@ -1293,185 +1236,138 @@ var PerpetualsClient = (function () {
1293
1236
  var lockedUsd = (sideUsd.mul(maxPayOffBpsNew)).div(new anchor_1.BN(constants_1.BPS_POWER));
1294
1237
  return lockedUsd;
1295
1238
  };
1296
- this.getLiquidationPriceSync = function (collateralAmount, sizeAmount, entryOraclePrice, lockAndUnsettledFeeUsd, marketCorrelation, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, positionAccount) {
1239
+ this.getLiquidationPriceContractHelper = function (entryOraclePrice, lockAndUnsettledFeeUsd, side, targetCustodyAccount, positionAccount) {
1297
1240
  var zeroOraclePrice = OraclePrice_1.OraclePrice.from({
1298
1241
  price: constants_1.BN_ZERO,
1299
1242
  exponent: constants_1.BN_ZERO,
1300
1243
  confidence: constants_1.BN_ZERO,
1301
1244
  timestamp: constants_1.BN_ZERO
1302
1245
  });
1303
- if (collateralAmount.isZero() || sizeAmount.isZero()) {
1304
- return zeroOraclePrice;
1305
- }
1306
1246
  if (positionAccount.entryPrice.exponent && !entryOraclePrice.exponent.eq(new anchor_1.BN(positionAccount.entryPrice.exponent))) {
1307
1247
  throw new Error("Exponent mismatch : ".concat(positionAccount.entryPrice.exponent, " & ").concat(entryOraclePrice.exponent.toString(), " ").concat(entryOraclePrice === null || entryOraclePrice === void 0 ? void 0 : entryOraclePrice.toUiPrice(8)));
1308
1248
  }
1309
1249
  var exitFeeUsd = positionAccount.sizeUsd.mul(targetCustodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
1310
1250
  var unsettledLossUsd = exitFeeUsd.add(lockAndUnsettledFeeUsd);
1311
1251
  var liablitiesUsd = positionAccount.sizeUsd.mul(new anchor_1.BN(constants_1.BPS_POWER)).div(new anchor_1.BN(targetCustodyAccount.pricing.maxLeverage)).add(unsettledLossUsd);
1312
- var targetMinMaxPriceOracle = _this.getMinAndMaxOraclePriceSync(targetPrice, targetEmaPrice, targetCustodyAccount);
1313
- var collateralMinMaxPriceOracle = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount);
1314
1252
  var liquidationPrice;
1315
- if (marketCorrelation && (0, types_1.isVariant)(side, 'long')) {
1316
- var newCollateralAmount = void 0;
1317
- if (targetCustodyAccount.mint == collateralCustodyAccount.mint) {
1318
- newCollateralAmount = collateralAmount;
1253
+ if (positionAccount.collateralUsd.gte(liablitiesUsd)) {
1254
+ var priceDiffLossOracle = OraclePrice_1.OraclePrice.from({
1255
+ price: (positionAccount.collateralUsd.sub(liablitiesUsd)).mul(new anchor_1.BN(Math.pow(10, (positionAccount.sizeDecimals + 3))))
1256
+ .div(positionAccount.sizeAmount),
1257
+ exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
1258
+ confidence: constants_1.BN_ZERO,
1259
+ timestamp: constants_1.BN_ZERO
1260
+ }).scale_to_exponent(new anchor_1.BN(entryOraclePrice.exponent));
1261
+ if ((0, types_1.isVariant)(side, 'long')) {
1262
+ liquidationPrice = OraclePrice_1.OraclePrice.from({
1263
+ price: entryOraclePrice.price.sub(priceDiffLossOracle.price),
1264
+ exponent: new anchor_1.BN(entryOraclePrice.exponent),
1265
+ confidence: constants_1.BN_ZERO,
1266
+ timestamp: constants_1.BN_ZERO
1267
+ });
1319
1268
  }
1320
1269
  else {
1321
- var pairPrice = collateralMinMaxPriceOracle.min.price.mul(new anchor_1.BN(10).pow(collateralMinMaxPriceOracle.min.exponent)).div(targetMinMaxPriceOracle.max.price);
1322
- var swapPrice = pairPrice.sub(pairPrice.mul(collateralCustodyAccount.pricing.swapSpread).div(new anchor_1.BN(constants_1.BPS_POWER)));
1323
- newCollateralAmount = (0, utils_1.checkedDecimalMul)(collateralAmount, new anchor_1.BN(-1 * collateralCustodyAccount.decimals), swapPrice, collateralMinMaxPriceOracle.min.exponent, new anchor_1.BN(-1 * targetCustodyAccount.decimals));
1270
+ liquidationPrice = OraclePrice_1.OraclePrice.from({
1271
+ price: entryOraclePrice.price.add(priceDiffLossOracle.price),
1272
+ exponent: new anchor_1.BN(entryOraclePrice.exponent),
1273
+ confidence: constants_1.BN_ZERO,
1274
+ timestamp: constants_1.BN_ZERO
1275
+ });
1324
1276
  }
1325
- var lp = OraclePrice_1.OraclePrice.from({
1326
- price: (positionAccount.sizeUsd.add(liablitiesUsd)).mul(new anchor_1.BN(Math.pow(10, (positionAccount.sizeDecimals + 3))))
1327
- .div(sizeAmount.add(newCollateralAmount)),
1277
+ }
1278
+ else {
1279
+ var priceDiffProfitOracle = OraclePrice_1.OraclePrice.from({
1280
+ price: (liablitiesUsd.sub(positionAccount.collateralUsd)).mul(new anchor_1.BN(Math.pow(10, (positionAccount.sizeDecimals + 3))))
1281
+ .div(positionAccount.sizeAmount),
1328
1282
  exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
1329
1283
  confidence: constants_1.BN_ZERO,
1330
1284
  timestamp: constants_1.BN_ZERO
1331
- });
1332
- liquidationPrice = lp.scale_to_exponent(new anchor_1.BN(entryOraclePrice.exponent));
1333
- }
1334
- else {
1335
- var assetsUsd = collateralMinMaxPriceOracle.min.getAssetAmountUsd(collateralAmount, collateralCustodyAccount.decimals);
1336
- if (assetsUsd.gte(liablitiesUsd)) {
1337
- var priceDiffLossOracle = OraclePrice_1.OraclePrice.from({
1338
- price: (assetsUsd.sub(liablitiesUsd)).mul(new anchor_1.BN(Math.pow(10, (positionAccount.sizeDecimals + 3))))
1339
- .div(positionAccount.sizeAmount),
1340
- exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
1285
+ }).scale_to_exponent(new anchor_1.BN(entryOraclePrice.exponent));
1286
+ if ((0, types_1.isVariant)(side, 'long')) {
1287
+ liquidationPrice = OraclePrice_1.OraclePrice.from({
1288
+ price: entryOraclePrice.price.add(priceDiffProfitOracle.price),
1289
+ exponent: new anchor_1.BN(entryOraclePrice.exponent),
1341
1290
  confidence: constants_1.BN_ZERO,
1342
1291
  timestamp: constants_1.BN_ZERO
1343
- }).scale_to_exponent(new anchor_1.BN(entryOraclePrice.exponent));
1344
- if ((0, types_1.isVariant)(side, 'long')) {
1345
- liquidationPrice = OraclePrice_1.OraclePrice.from({
1346
- price: entryOraclePrice.price.sub(priceDiffLossOracle.price),
1347
- exponent: new anchor_1.BN(entryOraclePrice.exponent),
1348
- confidence: constants_1.BN_ZERO,
1349
- timestamp: constants_1.BN_ZERO
1350
- });
1351
- }
1352
- else {
1353
- liquidationPrice = OraclePrice_1.OraclePrice.from({
1354
- price: entryOraclePrice.price.add(priceDiffLossOracle.price),
1355
- exponent: new anchor_1.BN(entryOraclePrice.exponent),
1356
- confidence: constants_1.BN_ZERO,
1357
- timestamp: constants_1.BN_ZERO
1358
- });
1359
- }
1292
+ });
1360
1293
  }
1361
1294
  else {
1362
- var priceDiffProfitOracle = OraclePrice_1.OraclePrice.from({
1363
- price: (liablitiesUsd.sub(assetsUsd)).mul(new anchor_1.BN(Math.pow(10, (positionAccount.sizeDecimals + 3))))
1364
- .div(positionAccount.sizeAmount),
1365
- exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
1295
+ liquidationPrice = OraclePrice_1.OraclePrice.from({
1296
+ price: entryOraclePrice.price.sub(priceDiffProfitOracle.price),
1297
+ exponent: new anchor_1.BN(entryOraclePrice.exponent),
1366
1298
  confidence: constants_1.BN_ZERO,
1367
1299
  timestamp: constants_1.BN_ZERO
1368
- }).scale_to_exponent(new anchor_1.BN(entryOraclePrice.exponent));
1369
- if ((0, types_1.isVariant)(side, 'long')) {
1370
- liquidationPrice = OraclePrice_1.OraclePrice.from({
1371
- price: entryOraclePrice.price.add(priceDiffProfitOracle.price),
1372
- exponent: new anchor_1.BN(entryOraclePrice.exponent),
1373
- confidence: constants_1.BN_ZERO,
1374
- timestamp: constants_1.BN_ZERO
1375
- });
1376
- }
1377
- else {
1378
- liquidationPrice = OraclePrice_1.OraclePrice.from({
1379
- price: entryOraclePrice.price.sub(priceDiffProfitOracle.price),
1380
- exponent: new anchor_1.BN(entryOraclePrice.exponent),
1381
- confidence: constants_1.BN_ZERO,
1382
- timestamp: constants_1.BN_ZERO
1383
- });
1384
- }
1300
+ });
1385
1301
  }
1386
1302
  }
1387
1303
  return liquidationPrice.price.isNeg() ? zeroOraclePrice : liquidationPrice;
1388
1304
  };
1389
- this.getLiquidationPriceWithOrder = function (collateralAmount, collateralUsd, sizeAmount, sizeUsd, sizeDecimals, limitOraclePrice, marketCorrelation, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount) {
1305
+ this.getLiquidationPriceSync = function (collateralUsd, sizeAmount, entryOraclePrice, lockAndUnsettledFeeUsd, side, targetPrice, targetCustodyAccount, positionAccount) {
1306
+ var newPositionAccount = positionAccount.clone();
1307
+ newPositionAccount.sizeAmount = sizeAmount;
1308
+ newPositionAccount.sizeUsd = targetPrice.getAssetAmountUsd(sizeAmount, targetCustodyAccount.decimals);
1309
+ newPositionAccount.collateralUsd = collateralUsd;
1310
+ _this.getLiquidationPriceContractHelper(entryOraclePrice, lockAndUnsettledFeeUsd, side, targetCustodyAccount, newPositionAccount);
1311
+ };
1312
+ this.getLiquidationPriceWithOrder = function (collateralUsd, sizeAmount, sizeUsd, sizeDecimals, limitOraclePrice, side, targetCustodyAccount) {
1390
1313
  var zeroOraclePrice = OraclePrice_1.OraclePrice.from({
1391
1314
  price: constants_1.BN_ZERO,
1392
1315
  exponent: constants_1.BN_ZERO,
1393
1316
  confidence: constants_1.BN_ZERO,
1394
1317
  timestamp: constants_1.BN_ZERO
1395
1318
  });
1396
- if (collateralAmount.isZero() || sizeAmount.isZero()) {
1397
- return zeroOraclePrice;
1398
- }
1399
1319
  var exitFeeUsd = sizeUsd.mul(targetCustodyAccount.fees.closePosition).div(new anchor_1.BN(constants_1.RATE_POWER));
1400
1320
  var unsettledLossUsd = exitFeeUsd;
1401
1321
  var liablitiesUsd = sizeUsd.mul(new anchor_1.BN(constants_1.BPS_POWER)).div(new anchor_1.BN(targetCustodyAccount.pricing.maxLeverage)).add(unsettledLossUsd);
1402
- var targetMinMaxPriceOracle = _this.getMinAndMaxOraclePriceSync(targetPrice, targetEmaPrice, targetCustodyAccount);
1403
- var collateralMinMaxPriceOracle = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount);
1404
1322
  var liquidationPrice;
1405
- if (marketCorrelation && (0, types_1.isVariant)(side, 'long')) {
1406
- var newCollateralAmount = void 0;
1407
- if (targetCustodyAccount.mint == collateralCustodyAccount.mint) {
1408
- newCollateralAmount = collateralAmount;
1323
+ if (collateralUsd.gte(liablitiesUsd)) {
1324
+ var priceDiffLossOracle = OraclePrice_1.OraclePrice.from({
1325
+ price: (collateralUsd.sub(liablitiesUsd)).mul(new anchor_1.BN(Math.pow(10, (sizeDecimals + 3))))
1326
+ .div(sizeAmount),
1327
+ exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
1328
+ confidence: constants_1.BN_ZERO,
1329
+ timestamp: constants_1.BN_ZERO
1330
+ }).scale_to_exponent(new anchor_1.BN(limitOraclePrice.exponent));
1331
+ if ((0, types_1.isVariant)(side, 'long')) {
1332
+ liquidationPrice = OraclePrice_1.OraclePrice.from({
1333
+ price: limitOraclePrice.price.sub(priceDiffLossOracle.price),
1334
+ exponent: new anchor_1.BN(limitOraclePrice.exponent),
1335
+ confidence: constants_1.BN_ZERO,
1336
+ timestamp: constants_1.BN_ZERO
1337
+ });
1409
1338
  }
1410
1339
  else {
1411
- var pairPrice = collateralMinMaxPriceOracle.min.price.mul(new anchor_1.BN(10).pow(collateralMinMaxPriceOracle.min.exponent)).div(targetMinMaxPriceOracle.max.price);
1412
- var swapPrice = pairPrice.sub(pairPrice.mul(collateralCustodyAccount.pricing.swapSpread).div(new anchor_1.BN(constants_1.BPS_POWER)));
1413
- newCollateralAmount = (0, utils_1.checkedDecimalMul)(collateralAmount, new anchor_1.BN(-1 * collateralCustodyAccount.decimals), swapPrice, collateralMinMaxPriceOracle.min.exponent, new anchor_1.BN(-1 * targetCustodyAccount.decimals));
1340
+ liquidationPrice = OraclePrice_1.OraclePrice.from({
1341
+ price: limitOraclePrice.price.add(priceDiffLossOracle.price),
1342
+ exponent: new anchor_1.BN(limitOraclePrice.exponent),
1343
+ confidence: constants_1.BN_ZERO,
1344
+ timestamp: constants_1.BN_ZERO
1345
+ });
1414
1346
  }
1415
- var lp = OraclePrice_1.OraclePrice.from({
1416
- price: (sizeUsd.add(liablitiesUsd)).mul(new anchor_1.BN(Math.pow(10, (sizeDecimals + 3))))
1417
- .div(sizeAmount.add(newCollateralAmount)),
1347
+ }
1348
+ else {
1349
+ var priceDiffProfitOracle = OraclePrice_1.OraclePrice.from({
1350
+ price: (liablitiesUsd.sub(collateralUsd)).mul(new anchor_1.BN(Math.pow(10, (sizeDecimals + 3))))
1351
+ .div(sizeAmount),
1418
1352
  exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
1419
1353
  confidence: constants_1.BN_ZERO,
1420
1354
  timestamp: constants_1.BN_ZERO
1421
- });
1422
- liquidationPrice = lp.scale_to_exponent(new anchor_1.BN(limitOraclePrice.exponent));
1423
- }
1424
- else {
1425
- var assetsUsd = collateralUsd;
1426
- if (assetsUsd.gte(liablitiesUsd)) {
1427
- var priceDiffLossOracle = OraclePrice_1.OraclePrice.from({
1428
- price: (assetsUsd.sub(liablitiesUsd)).mul(new anchor_1.BN(Math.pow(10, (sizeDecimals + 3))))
1429
- .div(sizeAmount),
1430
- exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
1355
+ }).scale_to_exponent(new anchor_1.BN(limitOraclePrice.exponent));
1356
+ if ((0, types_1.isVariant)(side, 'long')) {
1357
+ liquidationPrice = OraclePrice_1.OraclePrice.from({
1358
+ price: limitOraclePrice.price.add(priceDiffProfitOracle.price),
1359
+ exponent: new anchor_1.BN(limitOraclePrice.exponent),
1431
1360
  confidence: constants_1.BN_ZERO,
1432
1361
  timestamp: constants_1.BN_ZERO
1433
- }).scale_to_exponent(new anchor_1.BN(limitOraclePrice.exponent));
1434
- if ((0, types_1.isVariant)(side, 'long')) {
1435
- liquidationPrice = OraclePrice_1.OraclePrice.from({
1436
- price: limitOraclePrice.price.sub(priceDiffLossOracle.price),
1437
- exponent: new anchor_1.BN(limitOraclePrice.exponent),
1438
- confidence: constants_1.BN_ZERO,
1439
- timestamp: constants_1.BN_ZERO
1440
- });
1441
- }
1442
- else {
1443
- liquidationPrice = OraclePrice_1.OraclePrice.from({
1444
- price: limitOraclePrice.price.add(priceDiffLossOracle.price),
1445
- exponent: new anchor_1.BN(limitOraclePrice.exponent),
1446
- confidence: constants_1.BN_ZERO,
1447
- timestamp: constants_1.BN_ZERO
1448
- });
1449
- }
1362
+ });
1450
1363
  }
1451
1364
  else {
1452
- var priceDiffProfitOracle = OraclePrice_1.OraclePrice.from({
1453
- price: (liablitiesUsd.sub(assetsUsd)).mul(new anchor_1.BN(Math.pow(10, (sizeDecimals + 3))))
1454
- .div(sizeAmount),
1455
- exponent: new anchor_1.BN(-1 * constants_1.RATE_DECIMALS),
1365
+ liquidationPrice = OraclePrice_1.OraclePrice.from({
1366
+ price: limitOraclePrice.price.sub(priceDiffProfitOracle.price),
1367
+ exponent: new anchor_1.BN(limitOraclePrice.exponent),
1456
1368
  confidence: constants_1.BN_ZERO,
1457
1369
  timestamp: constants_1.BN_ZERO
1458
- }).scale_to_exponent(new anchor_1.BN(limitOraclePrice.exponent));
1459
- if ((0, types_1.isVariant)(side, 'long')) {
1460
- liquidationPrice = OraclePrice_1.OraclePrice.from({
1461
- price: limitOraclePrice.price.add(priceDiffProfitOracle.price),
1462
- exponent: new anchor_1.BN(limitOraclePrice.exponent),
1463
- confidence: constants_1.BN_ZERO,
1464
- timestamp: constants_1.BN_ZERO
1465
- });
1466
- }
1467
- else {
1468
- liquidationPrice = OraclePrice_1.OraclePrice.from({
1469
- price: limitOraclePrice.price.sub(priceDiffProfitOracle.price),
1470
- exponent: new anchor_1.BN(limitOraclePrice.exponent),
1471
- confidence: constants_1.BN_ZERO,
1472
- timestamp: constants_1.BN_ZERO
1473
- });
1474
- }
1370
+ });
1475
1371
  }
1476
1372
  }
1477
1373
  return liquidationPrice.price.isNeg() ? zeroOraclePrice : liquidationPrice;
@@ -1547,8 +1443,8 @@ var PerpetualsClient = (function () {
1547
1443
  positionAccount.market = marketAccountPk;
1548
1444
  positionAccount.lockedUsd = targetTokenPrice.getAssetAmountUsd(positionAccount.sizeAmount, targetCustodyAccount.decimals);
1549
1445
  positionAccount.lockedAmount = collateralPrice.getTokenAmount(positionAccount.lockedUsd, collateralCustodyAccount.decimals);
1550
- positionAccount.collateralAmount = positionAccount.collateralAmount.add(collateralDeltaAmount);
1551
- positionAccount.collateralUsd = collateralPrice.getAssetAmountUsd(positionAccount.collateralAmount, collateralCustodyAccount.decimals);
1446
+ var collateralDeltaUsd = collateralPrice.getAssetAmountUsd(collateralDeltaAmount, collateralCustodyAccount.decimals);
1447
+ positionAccount.collateralUsd = positionAccount.collateralUsd.add(collateralDeltaUsd);
1552
1448
  var currentTime = new anchor_1.BN((0, utils_1.getUnixTs)());
1553
1449
  var pnl = _this.getPnlSync(positionAccount, userEntrytpSlOraclePrice, userEntrytpSlOraclePrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTime, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1554
1450
  var pnlUsd = pnl.profitUsd.sub(pnl.lossUsd);
@@ -1620,6 +1516,9 @@ var PerpetualsClient = (function () {
1620
1516
  });
1621
1517
  };
1622
1518
  this.getPnlSync = function (positionAccount, targetTokenPrice, targetTokenEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, delay, poolConfig) {
1519
+ return _this.getPnlContractHelper(positionAccount, targetTokenPrice, targetTokenEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, delay, poolConfig);
1520
+ };
1521
+ this.getPnlContractHelper = function (positionAccount, targetTokenPrice, targetTokenEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, delay, poolConfig) {
1623
1522
  if (positionAccount.sizeUsd.isZero() || positionAccount.entryPrice.price.isZero()) {
1624
1523
  return {
1625
1524
  profitUsd: constants_1.BN_ZERO,
@@ -1678,7 +1577,7 @@ var PerpetualsClient = (function () {
1678
1577
  }
1679
1578
  if (priceDiffProfit.price.gt(constants_1.BN_ZERO)) {
1680
1579
  return {
1681
- profitUsd: anchor_1.BN.min(priceDiffProfit.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals), collateralMinPrice.getAssetAmountUsd(positionAccount.lockedAmount, positionAccount.lockedDecimals)),
1580
+ profitUsd: priceDiffProfit.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals),
1682
1581
  lossUsd: constants_1.BN_ZERO,
1683
1582
  };
1684
1583
  }
@@ -1769,6 +1668,9 @@ var PerpetualsClient = (function () {
1769
1668
  }
1770
1669
  };
1771
1670
  this.getAssetsUnderManagementUsdSync = function (poolAccount, tokenPrices, tokenEmaPrices, custodies, markets, aumCalcMode, currentTime, poolConfig) {
1671
+ return _this.getAssetsUnderManagementUsdContractHelper(poolAccount, tokenPrices, tokenEmaPrices, custodies, markets, aumCalcMode, currentTime, poolConfig);
1672
+ };
1673
+ this.getAssetsUnderManagementUsdContractHelper = function (poolAccount, tokenPrices, tokenEmaPrices, custodies, markets, aumCalcMode, currentTime, poolConfig) {
1772
1674
  var poolAmountUsd = constants_1.BN_ZERO;
1773
1675
  for (var index = 0; index < custodies.length; index++) {
1774
1676
  if (custodies.length != poolAccount.custodies.length || !custodies[index].publicKey.equals(poolAccount.custodies[index])) {
@@ -1781,6 +1683,7 @@ var PerpetualsClient = (function () {
1781
1683
  var token_amount_usd = tokenMinMaxPrice.max.getAssetAmountUsd(custodies[index].assets.owned, custodies[index].decimals);
1782
1684
  poolAmountUsd = poolAmountUsd.add(token_amount_usd);
1783
1685
  }
1686
+ poolAmountUsd = poolAmountUsd.sub(poolAccount.feesObligationUsd.add(poolAccount.rebateObligationUsd));
1784
1687
  if (aumCalcMode === "includePnl") {
1785
1688
  var poolEquityUsd = poolAmountUsd;
1786
1689
  for (var index = 0; index < markets.length; index++) {
@@ -1790,11 +1693,12 @@ var PerpetualsClient = (function () {
1790
1693
  var targetCustodyId = poolAccount.getCustodyId(markets[index].targetCustody);
1791
1694
  var collateralCustodyId = poolAccount.getCustodyId(markets[index].collateralCustody);
1792
1695
  var position = markets[index].getCollectivePosition();
1696
+ poolEquityUsd = poolEquityUsd.sub(position.collateralUsd);
1793
1697
  var collectivePnl = _this.getPnlSync(position, tokenPrices[targetCustodyId], tokenEmaPrices[targetCustodyId], custodies[targetCustodyId], tokenPrices[collateralCustodyId], tokenEmaPrices[collateralCustodyId], custodies[collateralCustodyId], currentTime, custodies[targetCustodyId].pricing.delaySeconds, poolConfig);
1794
1698
  var collateralMinMaxPrice = _this.getMinAndMaxOraclePriceSync(tokenPrices[collateralCustodyId], tokenEmaPrices[collateralCustodyId], custodies[collateralCustodyId]);
1795
- var collectiveCollateralUsd = collateralMinMaxPrice.min.getAssetAmountUsd(position.collateralAmount, position.collateralDecimals);
1796
- var collectiveLossUsd = anchor_1.BN.min(collectivePnl.lossUsd, collectiveCollateralUsd);
1797
- poolEquityUsd = (poolEquityUsd.add(collectiveLossUsd)).sub(collectivePnl.profitUsd);
1699
+ var collectiveLossUsd = anchor_1.BN.min(collectivePnl.lossUsd, position.collateralUsd);
1700
+ var collectiveProfitUsd = anchor_1.BN.min(collectivePnl.profitUsd, collateralMinMaxPrice.max.getAssetAmountUsd(position.lockedAmount, custodies[collateralCustodyId].decimals).sub(position.collateralUsd));
1701
+ poolEquityUsd = (poolEquityUsd.add(collectiveLossUsd)).sub(collectiveProfitUsd);
1798
1702
  }
1799
1703
  return { poolAmountUsd: poolAmountUsd, poolEquityUsd: poolEquityUsd };
1800
1704
  }
@@ -1802,14 +1706,6 @@ var PerpetualsClient = (function () {
1802
1706
  return { poolAmountUsd: poolAmountUsd, poolEquityUsd: constants_1.BN_ZERO };
1803
1707
  }
1804
1708
  };
1805
- this.getNftFinalDiscount = function (perpetualsAccount, nftTradingAccount, currentTime) {
1806
- if (currentTime.sub(nftTradingAccount.timestamp).lt(constants_1.DAY_SECONDS) && nftTradingAccount.counter.gt(new anchor_1.BN(perpetualsAccount.tradeLimit))) {
1807
- return { discountBn: constants_1.BN_ZERO };
1808
- }
1809
- else {
1810
- return { discountBn: perpetualsAccount.tradingDiscount[nftTradingAccount.level - 1] };
1811
- }
1812
- };
1813
1709
  this.getFeeDiscount = function (perpetualsAccount, tokenStakeAccount, currentTime) {
1814
1710
  if (tokenStakeAccount.level === 0) {
1815
1711
  return { discountBn: constants_1.BN_ZERO };
@@ -1894,7 +1790,7 @@ var PerpetualsClient = (function () {
1894
1790
  });
1895
1791
  };
1896
1792
  this.getStakedLpTokenPrice = function (poolKey, POOL_CONFIG) { return __awaiter(_this, void 0, void 0, function () {
1897
- var backUpOracleInstructionPromise, custodies, custodyMetas, marketMetas, _i, custodies_1, token, _a, custodies_2, custody, _b, _c, market, transaction, backUpOracleInstruction, result, index, res;
1793
+ var backUpOracleInstructionPromise, custodies, custodyMetas, marketMetas, _i, custodies_1, token, _a, custodies_2, custody, _b, _c, market, transaction, backUpOracleInstruction, setCULimitIx, result, index, res;
1898
1794
  var _d;
1899
1795
  return __generator(this, function (_e) {
1900
1796
  switch (_e.label) {
@@ -1942,6 +1838,8 @@ var PerpetualsClient = (function () {
1942
1838
  return [4, backUpOracleInstructionPromise];
1943
1839
  case 2:
1944
1840
  backUpOracleInstruction = _e.sent();
1841
+ setCULimitIx = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 450000 });
1842
+ transaction.instructions.unshift(setCULimitIx);
1945
1843
  (_d = transaction.instructions).unshift.apply(_d, backUpOracleInstruction);
1946
1844
  return [4, this.viewHelper.simulateTransaction(transaction)];
1947
1845
  case 3:
@@ -2008,7 +1906,7 @@ var PerpetualsClient = (function () {
2008
1906
  args_1[_i - 4] = arguments[_i];
2009
1907
  }
2010
1908
  return __awaiter(_this, __spreadArray([amount_1, poolKey_1, depositCustodyKey_1, POOL_CONFIG_1], args_1, true), void 0, function (amount, poolKey, depositCustodyKey, POOL_CONFIG, userPublicKey, enableBackupOracle) {
2011
- var custodies, custodyMetas, marketMetas, _a, custodies_5, token, _b, custodies_6, custody, _c, _d, market, depositCustodyConfig, transaction, backUpOracleInstruction, result, index, res;
1909
+ var custodies, custodyMetas, marketMetas, _a, custodies_5, token, _b, custodies_6, custody, _c, _d, market, depositCustodyConfig, transaction, setCULimitIx, backUpOracleInstruction, result, index, res;
2012
1910
  var _e;
2013
1911
  if (userPublicKey === void 0) { userPublicKey = undefined; }
2014
1912
  if (enableBackupOracle === void 0) { enableBackupOracle = false; }
@@ -2059,6 +1957,8 @@ var PerpetualsClient = (function () {
2059
1957
  .transaction()];
2060
1958
  case 1:
2061
1959
  transaction = _f.sent();
1960
+ setCULimitIx = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 450000 });
1961
+ transaction.instructions.unshift(setCULimitIx);
2062
1962
  if (!enableBackupOracle) return [3, 3];
2063
1963
  return [4, (0, backupOracle_1.createBackupOracleInstruction)(POOL_CONFIG.poolAddress.toBase58(), true)];
2064
1964
  case 2:
@@ -2084,7 +1984,7 @@ var PerpetualsClient = (function () {
2084
1984
  args_1[_i - 4] = arguments[_i];
2085
1985
  }
2086
1986
  return __awaiter(_this, __spreadArray([amount_1, poolKey_1, removeTokenCustodyKey_1, POOL_CONFIG_1], args_1, true), void 0, function (amount, poolKey, removeTokenCustodyKey, POOL_CONFIG, userPublicKey, enableBackupOracle) {
2087
- var custodies, custodyMetas, marketMetas, _a, custodies_7, token, _b, custodies_8, custody, _c, _d, market, removeCustodyConfig, transaction, backUpOracleInstruction, result, index, res;
1987
+ var custodies, custodyMetas, marketMetas, _a, custodies_7, token, _b, custodies_8, custody, _c, _d, market, removeCustodyConfig, transaction, setCULimitIx, backUpOracleInstruction, result, index, res;
2088
1988
  var _e;
2089
1989
  if (userPublicKey === void 0) { userPublicKey = undefined; }
2090
1990
  if (enableBackupOracle === void 0) { enableBackupOracle = false; }
@@ -2135,6 +2035,8 @@ var PerpetualsClient = (function () {
2135
2035
  .transaction()];
2136
2036
  case 1:
2137
2037
  transaction = _f.sent();
2038
+ setCULimitIx = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 450000 });
2039
+ transaction.instructions.unshift(setCULimitIx);
2138
2040
  if (!enableBackupOracle) return [3, 3];
2139
2041
  return [4, (0, backupOracle_1.createBackupOracleInstruction)(POOL_CONFIG.poolAddress.toBase58(), true)];
2140
2042
  case 2:
@@ -2161,7 +2063,7 @@ var PerpetualsClient = (function () {
2161
2063
  });
2162
2064
  };
2163
2065
  this.getCompoundingLPTokenPrice = function (poolKey, POOL_CONFIG) { return __awaiter(_this, void 0, void 0, function () {
2164
- var backUpOracleInstructionPromise, custodies, custodyMetas, marketMetas, _i, custodies_9, token, _a, custodies_10, custody, _b, _c, market, backUpOracleInstruction, transaction, result, index, res;
2066
+ var backUpOracleInstructionPromise, custodies, custodyMetas, marketMetas, _i, custodies_9, token, _a, custodies_10, custody, _b, _c, market, backUpOracleInstruction, transaction, setCULimitIx, result, index, res;
2165
2067
  var _d;
2166
2068
  return __generator(this, function (_e) {
2167
2069
  switch (_e.label) {
@@ -2209,6 +2111,8 @@ var PerpetualsClient = (function () {
2209
2111
  .transaction()];
2210
2112
  case 2:
2211
2113
  transaction = _e.sent();
2114
+ setCULimitIx = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 450000 });
2115
+ transaction.instructions.unshift(setCULimitIx);
2212
2116
  (_d = transaction.instructions).unshift.apply(_d, backUpOracleInstruction);
2213
2117
  return [4, this.viewHelper.simulateTransaction(transaction)];
2214
2118
  case 3:
@@ -2225,7 +2129,7 @@ var PerpetualsClient = (function () {
2225
2129
  args_1[_i - 4] = arguments[_i];
2226
2130
  }
2227
2131
  return __awaiter(_this, __spreadArray([amount_1, poolKey_1, depositCustodyKey_1, POOL_CONFIG_1], args_1, true), void 0, function (amount, poolKey, depositCustodyKey, POOL_CONFIG, userPublicKey, enableBackupOracle) {
2228
- var custodies, custodyMetas, marketMetas, _a, custodies_11, token, _b, custodies_12, custody, _c, _d, market, depositCustodyConfig, rewardCustody, transaction, backUpOracleInstruction, result, index, res;
2132
+ var custodies, custodyMetas, marketMetas, _a, custodies_11, token, _b, custodies_12, custody, _c, _d, market, depositCustodyConfig, rewardCustody, transaction, setCULimitIx, backUpOracleInstruction, result, index, res;
2229
2133
  var _e;
2230
2134
  if (userPublicKey === void 0) { userPublicKey = undefined; }
2231
2135
  if (enableBackupOracle === void 0) { enableBackupOracle = false; }
@@ -2280,6 +2184,8 @@ var PerpetualsClient = (function () {
2280
2184
  .transaction()];
2281
2185
  case 1:
2282
2186
  transaction = _f.sent();
2187
+ setCULimitIx = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 450000 });
2188
+ transaction.instructions.unshift(setCULimitIx);
2283
2189
  if (!enableBackupOracle) return [3, 3];
2284
2190
  return [4, (0, backupOracle_1.createBackupOracleInstruction)(POOL_CONFIG.poolAddress.toBase58(), true)];
2285
2191
  case 2:
@@ -2305,7 +2211,7 @@ var PerpetualsClient = (function () {
2305
2211
  args_1[_i - 4] = arguments[_i];
2306
2212
  }
2307
2213
  return __awaiter(_this, __spreadArray([amount_1, poolKey_1, removeTokenCustodyKey_1, POOL_CONFIG_1], args_1, true), void 0, function (amount, poolKey, removeTokenCustodyKey, POOL_CONFIG, userPublicKey, enableBackupOracle) {
2308
- var custodies, custodyMetas, marketMetas, _a, custodies_13, token, _b, custodies_14, custody, _c, _d, market, removeCustodyConfig, rewardCustody, transaction, backUpOracleInstruction, result, index, res;
2214
+ var custodies, custodyMetas, marketMetas, _a, custodies_13, token, _b, custodies_14, custody, _c, _d, market, removeCustodyConfig, rewardCustody, transaction, setCULimitIx, backUpOracleInstruction, result, index, res;
2309
2215
  var _e;
2310
2216
  if (userPublicKey === void 0) { userPublicKey = undefined; }
2311
2217
  if (enableBackupOracle === void 0) { enableBackupOracle = false; }
@@ -2360,6 +2266,8 @@ var PerpetualsClient = (function () {
2360
2266
  .transaction()];
2361
2267
  case 1:
2362
2268
  transaction = _f.sent();
2269
+ setCULimitIx = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 450000 });
2270
+ transaction.instructions.unshift(setCULimitIx);
2363
2271
  if (!enableBackupOracle) return [3, 3];
2364
2272
  return [4, (0, backupOracle_1.createBackupOracleInstruction)(POOL_CONFIG.poolAddress.toBase58(), true)];
2365
2273
  case 2:
@@ -4076,119 +3984,8 @@ var PerpetualsClient = (function () {
4076
3984
  }
4077
3985
  });
4078
3986
  }); };
4079
- this.updateNftAccount = function (nftMint, updateReferer, updateBooster, flpStakeAccounts) { return __awaiter(_this, void 0, void 0, function () {
4080
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, nftTradingAccount, nftReferralAccount, nftTokenAccount, flpStakeAccountMetas, _i, flpStakeAccounts_1, flpStakeAccountPk, updateNftTradingAccountInstruction, err_8;
4081
- return __generator(this, function (_a) {
4082
- switch (_a.label) {
4083
- case 0:
4084
- publicKey = this.provider.wallet.publicKey;
4085
- preInstructions = [];
4086
- instructions = [];
4087
- postInstructions = [];
4088
- additionalSigners = [];
4089
- _a.label = 1;
4090
- case 1:
4091
- _a.trys.push([1, 4, , 5]);
4092
- nftTradingAccount = web3_js_1.PublicKey.findProgramAddressSync([
4093
- Buffer.from("trading"),
4094
- nftMint.toBuffer(),
4095
- ], this.programId)[0];
4096
- nftReferralAccount = web3_js_1.PublicKey.findProgramAddressSync([
4097
- Buffer.from("referral"),
4098
- publicKey.toBuffer(),
4099
- ], this.programId)[0];
4100
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(nftMint, publicKey, true)];
4101
- case 2:
4102
- nftTokenAccount = _a.sent();
4103
- flpStakeAccountMetas = [];
4104
- for (_i = 0, flpStakeAccounts_1 = flpStakeAccounts; _i < flpStakeAccounts_1.length; _i++) {
4105
- flpStakeAccountPk = flpStakeAccounts_1[_i];
4106
- flpStakeAccountMetas.push({
4107
- pubkey: flpStakeAccountPk,
4108
- isSigner: false,
4109
- isWritable: true,
4110
- });
4111
- }
4112
- return [4, this.program.methods
4113
- .updateTradingAccount({
4114
- updateReferer: updateReferer,
4115
- updateBooster: updateBooster
4116
- })
4117
- .accounts({
4118
- owner: publicKey,
4119
- feePayer: publicKey,
4120
- nftTokenAccount: nftTokenAccount,
4121
- referralAccount: nftReferralAccount,
4122
- tradingAccount: nftTradingAccount
4123
- })
4124
- .instruction()];
4125
- case 3:
4126
- updateNftTradingAccountInstruction = _a.sent();
4127
- instructions.push(updateNftTradingAccountInstruction);
4128
- return [3, 5];
4129
- case 4:
4130
- err_8 = _a.sent();
4131
- console.log("perpClient updateNftAccount error:: ", err_8);
4132
- throw err_8;
4133
- case 5: return [2, {
4134
- instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4135
- additionalSigners: additionalSigners
4136
- }];
4137
- }
4138
- });
4139
- }); };
4140
- this.levelUp = function (poolConfig, nftMint, authorizationRulesAccount) { return __awaiter(_this, void 0, void 0, function () {
4141
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, nftTradingAccount, metadataAccount, levelUpInstruction, err_9;
4142
- return __generator(this, function (_a) {
4143
- switch (_a.label) {
4144
- case 0:
4145
- publicKey = this.provider.wallet.publicKey;
4146
- preInstructions = [];
4147
- instructions = [];
4148
- postInstructions = [];
4149
- additionalSigners = [];
4150
- _a.label = 1;
4151
- case 1:
4152
- _a.trys.push([1, 3, , 4]);
4153
- nftTradingAccount = web3_js_1.PublicKey.findProgramAddressSync([
4154
- Buffer.from("trading"),
4155
- nftMint.toBuffer(),
4156
- ], this.programId)[0];
4157
- metadataAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
4158
- return [4, this.program.methods
4159
- .levelUp({})
4160
- .accounts({
4161
- owner: publicKey,
4162
- perpetuals: this.perpetuals.publicKey,
4163
- pool: poolConfig.poolAddress,
4164
- metadataAccount: metadataAccount,
4165
- nftMint: nftMint,
4166
- metadataProgram: constants_1.METAPLEX_PROGRAM_ID,
4167
- tradingAccount: nftTradingAccount,
4168
- transferAuthority: this.authority.publicKey,
4169
- authorizationRulesAccount: authorizationRulesAccount,
4170
- authorizationRulesProgram: new web3_js_1.PublicKey('auth9SigNpDKz4sJJ1DfCTuZrZNSAgh9sFD3rboVmgg'),
4171
- systemProgram: web3_js_1.SystemProgram.programId,
4172
- ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY
4173
- })
4174
- .instruction()];
4175
- case 2:
4176
- levelUpInstruction = _a.sent();
4177
- instructions.push(levelUpInstruction);
4178
- return [3, 4];
4179
- case 3:
4180
- err_9 = _a.sent();
4181
- console.log("perpClient levelUp error:: ", err_9);
4182
- throw err_9;
4183
- case 4: return [2, {
4184
- instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4185
- additionalSigners: additionalSigners
4186
- }];
4187
- }
4188
- });
4189
- }); };
4190
3987
  this.depositStake = function (owner, feePayer, depositAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4191
- var preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, depositStakeInstruction, err_10;
3988
+ var preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, depositStakeInstruction, err_8;
4192
3989
  return __generator(this, function (_a) {
4193
3990
  switch (_a.label) {
4194
3991
  case 0:
@@ -4230,9 +4027,9 @@ var PerpetualsClient = (function () {
4230
4027
  instructions.push(depositStakeInstruction);
4231
4028
  return [3, 5];
4232
4029
  case 4:
4233
- err_10 = _a.sent();
4234
- console.log("perpClient depositStaking error:: ", err_10);
4235
- throw err_10;
4030
+ err_8 = _a.sent();
4031
+ console.log("perpClient depositStaking error:: ", err_8);
4032
+ throw err_8;
4236
4033
  case 5: return [2, {
4237
4034
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4238
4035
  additionalSigners: additionalSigners
@@ -4241,7 +4038,7 @@ var PerpetualsClient = (function () {
4241
4038
  });
4242
4039
  }); };
4243
4040
  this.refreshStakeWithAllFlpStakeAccounts = function (rewardSymbol, poolConfig, flpStakeAccountPks) { return __awaiter(_this, void 0, void 0, function () {
4244
- var rewardCustodyMint, rewardCustodyConfig, pool, feeDistributionTokenAccount, custodyAccountMetas, _i, _a, custody, maxFlpStakeAccountPkLength, flpStakeAccountMetas, _b, flpStakeAccountPks_1, flpStakeAccountPk, refreshStakeInstruction, err_11;
4041
+ var rewardCustodyMint, rewardCustodyConfig, pool, feeDistributionTokenAccount, custodyAccountMetas, _i, _a, custody, maxFlpStakeAccountPkLength, flpStakeAccountMetas, _b, flpStakeAccountPks_1, flpStakeAccountPk, refreshStakeInstruction, err_9;
4245
4042
  return __generator(this, function (_c) {
4246
4043
  switch (_c.label) {
4247
4044
  case 0:
@@ -4288,9 +4085,9 @@ var PerpetualsClient = (function () {
4288
4085
  refreshStakeInstruction = _c.sent();
4289
4086
  return [2, refreshStakeInstruction];
4290
4087
  case 2:
4291
- err_11 = _c.sent();
4292
- console.log("perpClient refreshStaking error:: ", err_11);
4293
- throw err_11;
4088
+ err_9 = _c.sent();
4089
+ console.log("perpClient refreshStaking error:: ", err_9);
4090
+ throw err_9;
4294
4091
  case 3: return [2];
4295
4092
  }
4296
4093
  });
@@ -4301,7 +4098,7 @@ var PerpetualsClient = (function () {
4301
4098
  args_1[_i - 3] = arguments[_i];
4302
4099
  }
4303
4100
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, flpStakeAccountPk_1], args_1, true), void 0, function (rewardSymbol, poolConfig, flpStakeAccountPk, userPublicKey) {
4304
- var publicKey, rewardCustodyMint, rewardCustodyConfig, pool, feeDistributionTokenAccount, custodyAccountMetas, _a, _b, custody, stakeAccountMetas, tokenStakeAccount, refreshStakeInstruction, err_12;
4101
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, pool, feeDistributionTokenAccount, custodyAccountMetas, _a, _b, custody, stakeAccountMetas, tokenStakeAccount, refreshStakeInstruction, err_10;
4305
4102
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4306
4103
  return __generator(this, function (_c) {
4307
4104
  switch (_c.label) {
@@ -4349,9 +4146,9 @@ var PerpetualsClient = (function () {
4349
4146
  refreshStakeInstruction = _c.sent();
4350
4147
  return [2, refreshStakeInstruction];
4351
4148
  case 2:
4352
- err_12 = _c.sent();
4353
- console.log("perpClient refreshStaking error:: ", err_12);
4354
- throw err_12;
4149
+ err_10 = _c.sent();
4150
+ console.log("perpClient refreshStaking error:: ", err_10);
4151
+ throw err_10;
4355
4152
  case 3: return [2];
4356
4153
  }
4357
4154
  });
@@ -4363,7 +4160,7 @@ var PerpetualsClient = (function () {
4363
4160
  args_1[_i - 3] = arguments[_i];
4364
4161
  }
4365
4162
  return __awaiter(_this, __spreadArray([rewardSymbol_1, unstakeAmount_1, poolConfig_1], args_1, true), void 0, function (rewardSymbol, unstakeAmount, poolConfig, userPublicKey) {
4366
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyConfig, pool, flpStakeAccount, tokenStakeAccount, tokenStakeAccounts, _a, unstakeInstantInstruction, err_13;
4163
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyConfig, pool, flpStakeAccount, tokenStakeAccount, tokenStakeAccounts, _a, unstakeInstantInstruction, err_11;
4367
4164
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4368
4165
  return __generator(this, function (_b) {
4369
4166
  switch (_b.label) {
@@ -4415,9 +4212,9 @@ var PerpetualsClient = (function () {
4415
4212
  instructions.push(unstakeInstantInstruction);
4416
4213
  return [3, 6];
4417
4214
  case 5:
4418
- err_13 = _b.sent();
4419
- console.log("perpClient unstakeInstant error:: ", err_13);
4420
- throw err_13;
4215
+ err_11 = _b.sent();
4216
+ console.log("perpClient unstakeInstant error:: ", err_11);
4217
+ throw err_11;
4421
4218
  case 6: return [2, {
4422
4219
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4423
4220
  additionalSigners: additionalSigners
@@ -4427,7 +4224,7 @@ var PerpetualsClient = (function () {
4427
4224
  });
4428
4225
  };
4429
4226
  this.setFeeShareBps = function (poolConfig, flpStakeAccountPks) { return __awaiter(_this, void 0, void 0, function () {
4430
- var publicKey, pool, custodyAccountMetas, _i, _a, custody, maxFlpStakeAccountPkLength, flpStakeAccountMetas, _b, flpStakeAccountPks_2, flpStakeAccountPk, refreshStakeInstruction, err_14;
4227
+ var publicKey, pool, custodyAccountMetas, _i, _a, custody, maxFlpStakeAccountPkLength, flpStakeAccountMetas, _b, flpStakeAccountPks_2, flpStakeAccountPk, refreshStakeInstruction, err_12;
4431
4228
  return __generator(this, function (_c) {
4432
4229
  switch (_c.label) {
4433
4230
  case 0:
@@ -4471,15 +4268,15 @@ var PerpetualsClient = (function () {
4471
4268
  refreshStakeInstruction = _c.sent();
4472
4269
  return [2, refreshStakeInstruction];
4473
4270
  case 2:
4474
- err_14 = _c.sent();
4475
- console.log("perpClient refreshStaking error:: ", err_14);
4476
- throw err_14;
4271
+ err_12 = _c.sent();
4272
+ console.log("perpClient refreshStaking error:: ", err_12);
4273
+ throw err_12;
4477
4274
  case 3: return [2];
4478
4275
  }
4479
4276
  });
4480
4277
  }); };
4481
4278
  this.unstakeRequest = function (unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4482
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_15;
4279
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_13;
4483
4280
  return __generator(this, function (_a) {
4484
4281
  switch (_a.label) {
4485
4282
  case 0:
@@ -4513,9 +4310,9 @@ var PerpetualsClient = (function () {
4513
4310
  instructions.push(unstakeRequestInstruction);
4514
4311
  return [3, 4];
4515
4312
  case 3:
4516
- err_15 = _a.sent();
4517
- console.log("perpClient unstakeRequest error:: ", err_15);
4518
- throw err_15;
4313
+ err_13 = _a.sent();
4314
+ console.log("perpClient unstakeRequest error:: ", err_13);
4315
+ throw err_13;
4519
4316
  case 4: return [2, {
4520
4317
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4521
4318
  additionalSigners: additionalSigners
@@ -4529,7 +4326,7 @@ var PerpetualsClient = (function () {
4529
4326
  args_1[_i - 1] = arguments[_i];
4530
4327
  }
4531
4328
  return __awaiter(_this, __spreadArray([poolConfig_1], args_1, true), void 0, function (poolConfig, pendingActivation, deactivated, createUserLPTA, userPublicKey) {
4532
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, pool, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, _a, withdrawStakeInstruction, err_16;
4329
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, pool, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, _a, withdrawStakeInstruction, err_14;
4533
4330
  if (pendingActivation === void 0) { pendingActivation = true; }
4534
4331
  if (deactivated === void 0) { deactivated = true; }
4535
4332
  if (createUserLPTA === void 0) { createUserLPTA = true; }
@@ -4585,9 +4382,9 @@ var PerpetualsClient = (function () {
4585
4382
  instructions.push(withdrawStakeInstruction);
4586
4383
  return [3, 6];
4587
4384
  case 5:
4588
- err_16 = _b.sent();
4589
- console.log("perpClient withdrawStake error:: ", err_16);
4590
- throw err_16;
4385
+ err_14 = _b.sent();
4386
+ console.log("perpClient withdrawStake error:: ", err_14);
4387
+ throw err_14;
4591
4388
  case 6: return [2, {
4592
4389
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4593
4390
  additionalSigners: additionalSigners
@@ -4602,7 +4399,7 @@ var PerpetualsClient = (function () {
4602
4399
  args_1[_i - 3] = arguments[_i];
4603
4400
  }
4604
4401
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4605
- var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_17;
4402
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4606
4403
  if (createUserATA === void 0) { createUserATA = true; }
4607
4404
  return __generator(this, function (_b) {
4608
4405
  switch (_b.label) {
@@ -4663,9 +4460,9 @@ var PerpetualsClient = (function () {
4663
4460
  instructions.push(withdrawStakeInstruction);
4664
4461
  return [3, 6];
4665
4462
  case 5:
4666
- err_17 = _b.sent();
4667
- console.log("perpClient withdrawStake error:: ", err_17);
4668
- throw err_17;
4463
+ err_15 = _b.sent();
4464
+ console.log("perpClient withdrawStake error:: ", err_15);
4465
+ throw err_15;
4669
4466
  case 6: return [2, {
4670
4467
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4671
4468
  additionalSigners: additionalSigners
@@ -4680,7 +4477,7 @@ var PerpetualsClient = (function () {
4680
4477
  args_1[_i - 5] = arguments[_i];
4681
4478
  }
4682
4479
  return __awaiter(_this, __spreadArray([amountIn_1, minCompoundingAmountOut_1, inTokenSymbol_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amountIn, minCompoundingAmountOut, inTokenSymbol, rewardTokenMint, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
4683
- var publicKey, preInstructions, instructions, additionalSigners, postInstructions, rewardCustody, inCustodyConfig, lpTokenMint, compoundingTokenMint, wrappedSolAccount, lpTokenAccount, compoundingTokenAccount, fundingAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, lamports, unWrappedSolBalance, _e, addCompoundingLiquidity, err_18;
4480
+ var publicKey, preInstructions, instructions, additionalSigners, postInstructions, rewardCustody, inCustodyConfig, lpTokenMint, compoundingTokenMint, wrappedSolAccount, lpTokenAccount, compoundingTokenAccount, fundingAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, lamports, unWrappedSolBalance, _e, addCompoundingLiquidity, err_16;
4684
4481
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
4685
4482
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4686
4483
  if (userPublicKey === void 0) { userPublicKey = undefined; }
@@ -4808,8 +4605,8 @@ var PerpetualsClient = (function () {
4808
4605
  instructions.push(addCompoundingLiquidity);
4809
4606
  return [3, 10];
4810
4607
  case 9:
4811
- err_18 = _f.sent();
4812
- console.log("perpClient addCompoundingLiquidity error:: ", err_18);
4608
+ err_16 = _f.sent();
4609
+ console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4813
4610
  return [3, 10];
4814
4611
  case 10: return [2, {
4815
4612
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
@@ -4825,7 +4622,7 @@ var PerpetualsClient = (function () {
4825
4622
  args_1[_i - 5] = arguments[_i];
4826
4623
  }
4827
4624
  return __awaiter(_this, __spreadArray([compoundingAmountIn_1, minAmountOut_1, outTokenSymbol_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (compoundingAmountIn, minAmountOut, outTokenSymbol, rewardTokenMint, poolConfig, createUserATA, ephemeralSignerPubkey, userPublicKey) {
4828
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, outCustodyConfig, lpTokenMint, compoundingTokenMint, lamports, _a, custodyAccountMetas, custodyOracleAccountMetas, markets, _b, _c, custody, _d, _e, market, compoundingTokenAccount, removeCompoundingLiquidity, err_19;
4625
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, outCustodyConfig, lpTokenMint, compoundingTokenMint, lamports, _a, custodyAccountMetas, custodyOracleAccountMetas, markets, _b, _c, custody, _d, _e, market, compoundingTokenAccount, removeCompoundingLiquidity, err_17;
4829
4626
  if (createUserATA === void 0) { createUserATA = true; }
4830
4627
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4831
4628
  if (userPublicKey === void 0) { userPublicKey = undefined; }
@@ -4937,8 +4734,8 @@ var PerpetualsClient = (function () {
4937
4734
  instructions.push(removeCompoundingLiquidity);
4938
4735
  return [3, 8];
4939
4736
  case 7:
4940
- err_19 = _f.sent();
4941
- console.log("perpClient removeCompoundingLiquidity error:: ", err_19);
4737
+ err_17 = _f.sent();
4738
+ console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
4942
4739
  return [3, 8];
4943
4740
  case 8: return [2, {
4944
4741
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
@@ -4954,7 +4751,7 @@ var PerpetualsClient = (function () {
4954
4751
  args_1[_i - 3] = arguments[_i];
4955
4752
  }
4956
4753
  return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA) {
4957
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, _a, flpStakeAccount, tokenStakeAccount, tokenStakeAccounts, _b, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _c, _d, custody, _e, _f, market, migrateStake, err_20;
4754
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, _a, flpStakeAccount, tokenStakeAccount, tokenStakeAccounts, _b, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _c, _d, custody, _e, _f, market, migrateStake, err_18;
4958
4755
  if (createUserATA === void 0) { createUserATA = true; }
4959
4756
  return __generator(this, function (_g) {
4960
4757
  switch (_g.label) {
@@ -5052,8 +4849,8 @@ var PerpetualsClient = (function () {
5052
4849
  instructions.push(migrateStake);
5053
4850
  return [3, 8];
5054
4851
  case 7:
5055
- err_20 = _g.sent();
5056
- console.log("perpClient migrateStake error:: ", err_20);
4852
+ err_18 = _g.sent();
4853
+ console.log("perpClient migrateStake error:: ", err_18);
5057
4854
  return [3, 8];
5058
4855
  case 8: return [2, {
5059
4856
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
@@ -5064,7 +4861,7 @@ var PerpetualsClient = (function () {
5064
4861
  });
5065
4862
  };
5066
4863
  this.migrateFlp = function (amount, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5067
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_21;
4864
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_19;
5068
4865
  return __generator(this, function (_d) {
5069
4866
  switch (_d.label) {
5070
4867
  case 0:
@@ -5136,8 +4933,8 @@ var PerpetualsClient = (function () {
5136
4933
  instructions.push(migrateFlp);
5137
4934
  return [3, 4];
5138
4935
  case 3:
5139
- err_21 = _d.sent();
5140
- console.log("perpClient migrateFlp error:: ", err_21);
4936
+ err_19 = _d.sent();
4937
+ console.log("perpClient migrateFlp error:: ", err_19);
5141
4938
  return [3, 4];
5142
4939
  case 4: return [2, {
5143
4940
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
@@ -5152,7 +4949,7 @@ var PerpetualsClient = (function () {
5152
4949
  args_1[_i - 1] = arguments[_i];
5153
4950
  }
5154
4951
  return __awaiter(_this, __spreadArray([poolConfig_1], args_1, true), void 0, function (poolConfig, rewardTokenSymbol) {
5155
- var instructions, additionalSigners, rewardCustody, lpTokenMint, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, compoundingFee, err_22;
4952
+ var instructions, additionalSigners, rewardCustody, lpTokenMint, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, compoundingFee, err_20;
5156
4953
  if (rewardTokenSymbol === void 0) { rewardTokenSymbol = 'USDC'; }
5157
4954
  return __generator(this, function (_e) {
5158
4955
  switch (_e.label) {
@@ -5210,8 +5007,8 @@ var PerpetualsClient = (function () {
5210
5007
  instructions.push(compoundingFee);
5211
5008
  return [3, 4];
5212
5009
  case 3:
5213
- err_22 = _e.sent();
5214
- console.log("perpClient compoundingFee error:: ", err_22);
5010
+ err_20 = _e.sent();
5011
+ console.log("perpClient compoundingFee error:: ", err_20);
5215
5012
  return [3, 4];
5216
5013
  case 4: return [2, {
5217
5014
  instructions: __spreadArray([], instructions, true),
@@ -5221,84 +5018,61 @@ var PerpetualsClient = (function () {
5221
5018
  });
5222
5019
  });
5223
5020
  };
5224
- this.burnAndClaim = function (owner, nftMint, poolConfig, createAta) { return __awaiter(_this, void 0, void 0, function () {
5225
- var preInstructions, instructions, postInstructions, additionalSigners, userTokenAccount, _a, nftTokenAccount, nftTradingAccount, metadataAccount, collectionMetadata, edition, tokenRecord, burnAndClaimInstruction, err_23;
5226
- return __generator(this, function (_b) {
5227
- switch (_b.label) {
5021
+ this.depositTokenStake = function (owner, feePayer, depositAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5022
+ var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, depositTokenStakeInstruction, err_21;
5023
+ return __generator(this, function (_a) {
5024
+ switch (_a.label) {
5228
5025
  case 0:
5229
5026
  preInstructions = [];
5230
5027
  instructions = [];
5231
5028
  postInstructions = [];
5232
5029
  additionalSigners = [];
5233
- _b.label = 1;
5030
+ _a.label = 1;
5234
5031
  case 1:
5235
- _b.trys.push([1, 7, , 8]);
5236
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, owner, true)];
5237
- case 2:
5238
- userTokenAccount = _b.sent();
5239
- _a = createAta;
5240
- if (!_a) return [3, 4];
5032
+ _a.trys.push([1, 4, , 5]);
5033
+ tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5034
+ userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5241
5035
  return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5242
- case 3:
5243
- _a = !(_b.sent());
5244
- _b.label = 4;
5245
- case 4:
5246
- if (_a) {
5247
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(owner, userTokenAccount, owner, poolConfig.tokenMint));
5036
+ case 2:
5037
+ if (!(_a.sent())) {
5038
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5248
5039
  }
5249
- return [4, (0, spl_token_1.getAssociatedTokenAddress)(nftMint, owner, true)];
5250
- case 5:
5251
- nftTokenAccount = _b.sent();
5252
- nftTradingAccount = web3_js_1.PublicKey.findProgramAddressSync([
5253
- Buffer.from("trading"),
5254
- nftMint.toBuffer(),
5255
- ], this.programId)[0];
5256
- metadataAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
5257
- collectionMetadata = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), poolConfig.nftCollectionAddress.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
5258
- edition = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer(), Buffer.from("edition")], constants_1.METAPLEX_PROGRAM_ID)[0];
5259
- tokenRecord = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer(), Buffer.from("token_record"), nftTokenAccount.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
5260
5040
  return [4, this.program.methods
5261
- .burnAndClaim({})
5041
+ .depositTokenStake({
5042
+ depositAmount: depositAmount
5043
+ })
5262
5044
  .accounts({
5263
5045
  owner: owner,
5264
- receivingTokenAccount: userTokenAccount,
5046
+ feePayer: feePayer,
5047
+ fundingTokenAccount: userTokenAccount,
5265
5048
  perpetuals: this.perpetuals.publicKey,
5266
5049
  tokenVault: poolConfig.tokenVault,
5267
5050
  tokenVaultTokenAccount: poolConfig.tokenVaultTokenAccount,
5268
- metadataAccount: metadataAccount,
5269
- collectionMetadata: collectionMetadata,
5270
- edition: edition,
5271
- tokenRecord: tokenRecord,
5272
- tradingAccount: nftTradingAccount,
5273
- transferAuthority: poolConfig.transferAuthority,
5274
- metadataProgram: constants_1.METAPLEX_PROGRAM_ID,
5275
- nftMint: nftMint,
5276
- nftTokenAccount: nftTokenAccount,
5051
+ tokenStakeAccount: tokenStakeAccount,
5277
5052
  systemProgram: web3_js_1.SystemProgram.programId,
5278
5053
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
5279
- ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
5280
5054
  eventAuthority: this.eventAuthority.publicKey,
5281
5055
  program: this.programId,
5282
- receivingTokenMint: poolConfig.tokenMint,
5056
+ tokenMint: poolConfig.tokenMint,
5283
5057
  })
5284
5058
  .instruction()];
5285
- case 6:
5286
- burnAndClaimInstruction = _b.sent();
5287
- instructions.push(burnAndClaimInstruction);
5288
- return [3, 8];
5289
- case 7:
5290
- err_23 = _b.sent();
5291
- console.log("perpClient burnAndClaimInstruction error:: ", err_23);
5292
- throw err_23;
5293
- case 8: return [2, {
5059
+ case 3:
5060
+ depositTokenStakeInstruction = _a.sent();
5061
+ instructions.push(depositTokenStakeInstruction);
5062
+ return [3, 5];
5063
+ case 4:
5064
+ err_21 = _a.sent();
5065
+ console.log("perpClient depositStakingInstruction error:: ", err_21);
5066
+ throw err_21;
5067
+ case 5: return [2, {
5294
5068
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5295
5069
  additionalSigners: additionalSigners
5296
5070
  }];
5297
5071
  }
5298
5072
  });
5299
5073
  }); };
5300
- this.burnAndStake = function (owner, feePayer, nftMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5301
- var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, nftTokenAccount, nftTradingAccount, metadataAccount, collectionMetadata, edition, tokenRecord, burnAndStakeInstruction, err_24;
5074
+ this.unstakeTokenRequest = function (owner, unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5075
+ var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, unstakeTokenRequestInstruction, err_22;
5302
5076
  return __generator(this, function (_a) {
5303
5077
  switch (_a.label) {
5304
5078
  case 0:
@@ -5310,125 +5084,10 @@ var PerpetualsClient = (function () {
5310
5084
  case 1:
5311
5085
  _a.trys.push([1, 3, , 4]);
5312
5086
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5313
- nftTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(nftMint, owner, true);
5314
- nftTradingAccount = web3_js_1.PublicKey.findProgramAddressSync([
5315
- Buffer.from("trading"),
5316
- nftMint.toBuffer(),
5317
- ], this.programId)[0];
5318
- metadataAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
5319
- collectionMetadata = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), poolConfig.nftCollectionAddress.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
5320
- edition = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer(), Buffer.from("edition")], constants_1.METAPLEX_PROGRAM_ID)[0];
5321
- tokenRecord = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer(), Buffer.from("token_record"), nftTokenAccount.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
5322
5087
  return [4, this.program.methods
5323
- .burnAndStake({})
5324
- .accounts({
5325
- owner: owner,
5326
- feePayer: feePayer,
5327
- perpetuals: this.perpetuals.publicKey,
5328
- tokenVault: poolConfig.tokenVault,
5329
- tokenVaultTokenAccount: poolConfig.tokenVaultTokenAccount,
5330
- tokenStakeAccount: tokenStakeAccount,
5331
- metadataAccount: metadataAccount,
5332
- collectionMetadata: collectionMetadata,
5333
- edition: edition,
5334
- tokenRecord: tokenRecord,
5335
- tradingAccount: nftTradingAccount,
5336
- transferAuthority: poolConfig.transferAuthority,
5337
- metadataProgram: constants_1.METAPLEX_PROGRAM_ID,
5338
- nftMint: nftMint,
5339
- nftTokenAccount: nftTokenAccount,
5340
- systemProgram: web3_js_1.SystemProgram.programId,
5341
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
5342
- ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
5343
- eventAuthority: this.eventAuthority.publicKey,
5344
- program: this.programId
5345
- })
5346
- .instruction()];
5347
- case 2:
5348
- burnAndStakeInstruction = _a.sent();
5349
- instructions.push(burnAndStakeInstruction);
5350
- return [3, 4];
5351
- case 3:
5352
- err_24 = _a.sent();
5353
- console.log("perpClient burnAndStakeInstruction error:: ", err_24);
5354
- throw err_24;
5355
- case 4: return [2, {
5356
- instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5357
- additionalSigners: additionalSigners
5358
- }];
5359
- }
5360
- });
5361
- }); };
5362
- this.depositTokenStake = function (owner, feePayer, depositAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5363
- var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, depositTokenStakeInstruction, err_25;
5364
- return __generator(this, function (_a) {
5365
- switch (_a.label) {
5366
- case 0:
5367
- preInstructions = [];
5368
- instructions = [];
5369
- postInstructions = [];
5370
- additionalSigners = [];
5371
- _a.label = 1;
5372
- case 1:
5373
- _a.trys.push([1, 4, , 5]);
5374
- tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5375
- userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5376
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5377
- case 2:
5378
- if (!(_a.sent())) {
5379
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5380
- }
5381
- return [4, this.program.methods
5382
- .depositTokenStake({
5383
- depositAmount: depositAmount
5384
- })
5385
- .accounts({
5386
- owner: owner,
5387
- feePayer: feePayer,
5388
- fundingTokenAccount: userTokenAccount,
5389
- perpetuals: this.perpetuals.publicKey,
5390
- tokenVault: poolConfig.tokenVault,
5391
- tokenVaultTokenAccount: poolConfig.tokenVaultTokenAccount,
5392
- tokenStakeAccount: tokenStakeAccount,
5393
- systemProgram: web3_js_1.SystemProgram.programId,
5394
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
5395
- eventAuthority: this.eventAuthority.publicKey,
5396
- program: this.programId,
5397
- tokenMint: poolConfig.tokenMint,
5398
- })
5399
- .instruction()];
5400
- case 3:
5401
- depositTokenStakeInstruction = _a.sent();
5402
- instructions.push(depositTokenStakeInstruction);
5403
- return [3, 5];
5404
- case 4:
5405
- err_25 = _a.sent();
5406
- console.log("perpClient depositStakingInstruction error:: ", err_25);
5407
- throw err_25;
5408
- case 5: return [2, {
5409
- instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5410
- additionalSigners: additionalSigners
5411
- }];
5412
- }
5413
- });
5414
- }); };
5415
- this.unstakeTokenRequest = function (owner, unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5416
- var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, unstakeTokenRequestInstruction, err_26;
5417
- return __generator(this, function (_a) {
5418
- switch (_a.label) {
5419
- case 0:
5420
- preInstructions = [];
5421
- instructions = [];
5422
- postInstructions = [];
5423
- additionalSigners = [];
5424
- _a.label = 1;
5425
- case 1:
5426
- _a.trys.push([1, 3, , 4]);
5427
- tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5428
- return [4, this.program.methods
5429
- .unstakeTokenRequest({
5430
- unstakeAmount: unstakeAmount
5431
- })
5088
+ .unstakeTokenRequest({
5089
+ unstakeAmount: unstakeAmount
5090
+ })
5432
5091
  .accounts({
5433
5092
  owner: owner,
5434
5093
  tokenVault: poolConfig.tokenVault,
@@ -5442,9 +5101,9 @@ var PerpetualsClient = (function () {
5442
5101
  instructions.push(unstakeTokenRequestInstruction);
5443
5102
  return [3, 4];
5444
5103
  case 3:
5445
- err_26 = _a.sent();
5446
- console.log("perpClient unstakeTokenRequestInstruction error:: ", err_26);
5447
- throw err_26;
5104
+ err_22 = _a.sent();
5105
+ console.log("perpClient unstakeTokenRequestInstruction error:: ", err_22);
5106
+ throw err_22;
5448
5107
  case 4: return [2, {
5449
5108
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5450
5109
  additionalSigners: additionalSigners
@@ -5453,7 +5112,7 @@ var PerpetualsClient = (function () {
5453
5112
  });
5454
5113
  }); };
5455
5114
  this.unstakeTokenInstant = function (owner, unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5456
- var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, unstakeTokenInstantInstruction, err_27;
5115
+ var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, unstakeTokenInstantInstruction, err_23;
5457
5116
  return __generator(this, function (_a) {
5458
5117
  switch (_a.label) {
5459
5118
  case 0:
@@ -5494,9 +5153,9 @@ var PerpetualsClient = (function () {
5494
5153
  instructions.push(unstakeTokenInstantInstruction);
5495
5154
  return [3, 5];
5496
5155
  case 4:
5497
- err_27 = _a.sent();
5498
- console.log("perpClient unstakeTokenInstantInstruction error:: ", err_27);
5499
- throw err_27;
5156
+ err_23 = _a.sent();
5157
+ console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5158
+ throw err_23;
5500
5159
  case 5: return [2, {
5501
5160
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5502
5161
  additionalSigners: additionalSigners
@@ -5505,7 +5164,7 @@ var PerpetualsClient = (function () {
5505
5164
  });
5506
5165
  }); };
5507
5166
  this.withdrawToken = function (owner, withdrawRequestId, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5508
- var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, withdrawTokenInstruction, err_28;
5167
+ var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, withdrawTokenInstruction, err_24;
5509
5168
  return __generator(this, function (_a) {
5510
5169
  switch (_a.label) {
5511
5170
  case 0:
@@ -5546,9 +5205,9 @@ var PerpetualsClient = (function () {
5546
5205
  instructions.push(withdrawTokenInstruction);
5547
5206
  return [3, 5];
5548
5207
  case 4:
5549
- err_28 = _a.sent();
5550
- console.log("perpClient withdrawTokenInstruction error:: ", err_28);
5551
- throw err_28;
5208
+ err_24 = _a.sent();
5209
+ console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5210
+ throw err_24;
5552
5211
  case 5: return [2, {
5553
5212
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5554
5213
  additionalSigners: additionalSigners
@@ -5557,7 +5216,7 @@ var PerpetualsClient = (function () {
5557
5216
  });
5558
5217
  }); };
5559
5218
  this.cancelUnstakeRequest = function (owner, withdrawRequestId, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5560
- var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, cancelUnstakeRequestInstruction, err_29;
5219
+ var preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, cancelUnstakeRequestInstruction, err_25;
5561
5220
  return __generator(this, function (_a) {
5562
5221
  switch (_a.label) {
5563
5222
  case 0:
@@ -5586,9 +5245,9 @@ var PerpetualsClient = (function () {
5586
5245
  instructions.push(cancelUnstakeRequestInstruction);
5587
5246
  return [3, 4];
5588
5247
  case 3:
5589
- err_29 = _a.sent();
5590
- console.log("perpClient cancelUnstakeRequestInstruction error:: ", err_29);
5591
- throw err_29;
5248
+ err_25 = _a.sent();
5249
+ console.log("perpClient cancelUnstakeRequestInstruction error:: ", err_25);
5250
+ throw err_25;
5592
5251
  case 4: return [2, {
5593
5252
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5594
5253
  additionalSigners: additionalSigners
@@ -5602,7 +5261,7 @@ var PerpetualsClient = (function () {
5602
5261
  args_1[_i - 2] = arguments[_i];
5603
5262
  }
5604
5263
  return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA) {
5605
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_30;
5264
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_26;
5606
5265
  if (createUserATA === void 0) { createUserATA = true; }
5607
5266
  return __generator(this, function (_b) {
5608
5267
  switch (_b.label) {
@@ -5648,9 +5307,9 @@ var PerpetualsClient = (function () {
5648
5307
  instructions.push(collectTokenRewardInstruction);
5649
5308
  return [3, 6];
5650
5309
  case 5:
5651
- err_30 = _b.sent();
5652
- console.log("perpClient collectTokenRewardInstruction error:: ", err_30);
5653
- throw err_30;
5310
+ err_26 = _b.sent();
5311
+ console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5312
+ throw err_26;
5654
5313
  case 6: return [2, {
5655
5314
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5656
5315
  additionalSigners: additionalSigners
@@ -5665,7 +5324,7 @@ var PerpetualsClient = (function () {
5665
5324
  args_1[_i - 3] = arguments[_i];
5666
5325
  }
5667
5326
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5668
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_31;
5327
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_27;
5669
5328
  if (createUserATA === void 0) { createUserATA = true; }
5670
5329
  return __generator(this, function (_b) {
5671
5330
  switch (_b.label) {
@@ -5712,250 +5371,9 @@ var PerpetualsClient = (function () {
5712
5371
  instructions.push(collectRevenueInstruction);
5713
5372
  return [3, 6];
5714
5373
  case 5:
5715
- err_31 = _b.sent();
5716
- console.log("perpClient collectRevenueInstruction error:: ", err_31);
5717
- throw err_31;
5718
- case 6: return [2, {
5719
- instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5720
- additionalSigners: additionalSigners
5721
- }];
5722
- }
5723
- });
5724
- });
5725
- };
5726
- this.initRewardVault = function (nftCount, rewardSymbol, collectionMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5727
- var publicKey, rewardCustodyMint, instructions, additionalSigners, fbNftProgramData, rewardVault, rewardTokenAccount, nftTransferAuthority, initRewardVault, err_32;
5728
- return __generator(this, function (_a) {
5729
- switch (_a.label) {
5730
- case 0:
5731
- publicKey = this.provider.wallet.publicKey;
5732
- rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5733
- instructions = [];
5734
- additionalSigners = [];
5735
- _a.label = 1;
5736
- case 1:
5737
- _a.trys.push([1, 3, , 4]);
5738
- fbNftProgramData = web3_js_1.PublicKey.findProgramAddressSync([this.programFbnftReward.programId.toBuffer()], new web3_js_1.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111"))[0];
5739
- rewardVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_vault")], this.programFbnftReward.programId)[0];
5740
- rewardTokenAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_token_account")], this.programFbnftReward.programId)[0];
5741
- nftTransferAuthority = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("transfer_authority")], this.programFbnftReward.programId)[0];
5742
- return [4, this.programFbnftReward.methods
5743
- .initRewardVault({
5744
- nftCount: nftCount
5745
- })
5746
- .accounts({
5747
- admin: publicKey,
5748
- transferAuthority: nftTransferAuthority,
5749
- rewardVault: rewardVault,
5750
- rewardMint: rewardCustodyMint,
5751
- rewardTokenAccount: rewardTokenAccount,
5752
- collectionMint: collectionMint,
5753
- programData: fbNftProgramData,
5754
- systemProgram: web3_js_1.SystemProgram.programId,
5755
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
5756
- rent: web3_js_1.SYSVAR_RENT_PUBKEY
5757
- })
5758
- .instruction()];
5759
- case 2:
5760
- initRewardVault = _a.sent();
5761
- instructions.push(initRewardVault);
5762
- return [3, 4];
5763
- case 3:
5764
- err_32 = _a.sent();
5765
- console.log("perpClient InitRewardVault error:: ", err_32);
5766
- throw err_32;
5767
- case 4: return [2, {
5768
- instructions: __spreadArray([], instructions, true),
5769
- additionalSigners: additionalSigners
5770
- }];
5771
- }
5772
- });
5773
- }); };
5774
- this.distributeReward = function (rewardAmount, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5775
- var publicKey, rewardCustodyMint, instructions, additionalSigners, fundingAccount, rewardVault, rewardTokenAccount, distributeReward, err_33;
5776
- return __generator(this, function (_a) {
5777
- switch (_a.label) {
5778
- case 0:
5779
- publicKey = this.provider.wallet.publicKey;
5780
- rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5781
- instructions = [];
5782
- additionalSigners = [];
5783
- _a.label = 1;
5784
- case 1:
5785
- _a.trys.push([1, 3, , 4]);
5786
- fundingAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
5787
- rewardVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_vault")], this.programFbnftReward.programId)[0];
5788
- rewardTokenAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_token_account")], this.programFbnftReward.programId)[0];
5789
- return [4, this.programFbnftReward.methods
5790
- .distributeRewards({
5791
- rewardAmount: rewardAmount
5792
- })
5793
- .accounts({
5794
- admin: publicKey,
5795
- fundingAccount: fundingAccount,
5796
- rewardVault: rewardVault,
5797
- rewardTokenAccount: rewardTokenAccount,
5798
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
5799
- })
5800
- .instruction()];
5801
- case 2:
5802
- distributeReward = _a.sent();
5803
- instructions.push(distributeReward);
5804
- return [3, 4];
5805
- case 3:
5806
- err_33 = _a.sent();
5807
- console.log("perpClient distributeReward error:: ", err_33);
5808
- throw err_33;
5809
- case 4: return [2, {
5810
- instructions: __spreadArray([], instructions, true),
5811
- additionalSigners: additionalSigners
5812
- }];
5813
- }
5814
- });
5815
- }); };
5816
- this.collectNftReward = function (rewardSymbol_1, poolConfig_1, nftMint_1) {
5817
- var args_1 = [];
5818
- for (var _i = 3; _i < arguments.length; _i++) {
5819
- args_1[_i - 3] = arguments[_i];
5820
- }
5821
- return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, nftMint_1], args_1, true), void 0, function (rewardSymbol, poolConfig, nftMint, createUserATA) {
5822
- var publicKey, rewardToken, rewardCustodyMint, instructions, additionalSigners, nftTokenAccount, metadataAccount, receivingTokenAccount, _a, rewardRecord, rewardVault, rewardTokenAccount, nftTransferAuthority, collectNftReward, err_34;
5823
- if (createUserATA === void 0) { createUserATA = true; }
5824
- return __generator(this, function (_b) {
5825
- switch (_b.label) {
5826
- case 0:
5827
- publicKey = this.provider.wallet.publicKey;
5828
- rewardToken = poolConfig.getTokenFromSymbol(rewardSymbol);
5829
- rewardCustodyMint = rewardToken.mintKey;
5830
- instructions = [];
5831
- additionalSigners = [];
5832
- _b.label = 1;
5833
- case 1:
5834
- _b.trys.push([1, 5, , 6]);
5835
- nftTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(nftMint, publicKey, true);
5836
- metadataAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("metadata"), constants_1.METAPLEX_PROGRAM_ID.toBuffer(), nftMint.toBuffer()], constants_1.METAPLEX_PROGRAM_ID)[0];
5837
- receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true, rewardToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
5838
- _a = createUserATA;
5839
- if (!_a) return [3, 3];
5840
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
5841
- case 2:
5842
- _a = !(_b.sent());
5843
- _b.label = 3;
5844
- case 3:
5845
- if (_a) {
5846
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint, rewardToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
5847
- }
5848
- rewardRecord = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_record"), nftMint.toBuffer()], this.programFbnftReward.programId)[0];
5849
- rewardVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_vault")], this.programFbnftReward.programId)[0];
5850
- rewardTokenAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_token_account")], this.programFbnftReward.programId)[0];
5851
- nftTransferAuthority = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("transfer_authority")], this.programFbnftReward.programId)[0];
5852
- return [4, this.programFbnftReward.methods
5853
- .collectReward()
5854
- .accounts({
5855
- owner: publicKey,
5856
- feePayer: publicKey,
5857
- nftMint: nftMint,
5858
- nftTokenAccount: nftTokenAccount,
5859
- metadataAccount: metadataAccount,
5860
- receivingAccount: receivingTokenAccount,
5861
- rewardRecord: rewardRecord,
5862
- rewardVault: rewardVault,
5863
- rewardTokenAccount: rewardTokenAccount,
5864
- transferAuthority: nftTransferAuthority,
5865
- systemProgram: web3_js_1.SystemProgram.programId,
5866
- tokenProgram: rewardToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID,
5867
- })
5868
- .instruction()];
5869
- case 4:
5870
- collectNftReward = _b.sent();
5871
- instructions.push(collectNftReward);
5872
- return [3, 6];
5873
- case 5:
5874
- err_34 = _b.sent();
5875
- throw err_34;
5876
- case 6: return [2, {
5877
- instructions: __spreadArray([], instructions, true),
5878
- additionalSigners: additionalSigners
5879
- }];
5880
- }
5881
- });
5882
- });
5883
- };
5884
- this.collectAndDistributeFee = function (rewardSymbol_1, poolConfig_1) {
5885
- var args_1 = [];
5886
- for (var _i = 2; _i < arguments.length; _i++) {
5887
- args_1[_i - 2] = arguments[_i];
5888
- }
5889
- return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (rewardSymbol, poolConfig, createUserATA, nftTradingAccount) {
5890
- var publicKey, rewardToken, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tradingAccount, rewardVault, rewardTokenAccount, withdrawStakeInstruction, err_35;
5891
- if (createUserATA === void 0) { createUserATA = true; }
5892
- return __generator(this, function (_b) {
5893
- switch (_b.label) {
5894
- case 0:
5895
- publicKey = this.provider.wallet.publicKey;
5896
- rewardToken = poolConfig.getTokenFromSymbol(rewardSymbol);
5897
- rewardCustodyMint = rewardToken.mintKey;
5898
- rewardCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(rewardToken.mintKey); });
5899
- preInstructions = [];
5900
- instructions = [];
5901
- postInstructions = [];
5902
- additionalSigners = [];
5903
- _b.label = 1;
5904
- case 1:
5905
- _b.trys.push([1, 5, , 6]);
5906
- pool = poolConfig.poolAddress;
5907
- flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
5908
- receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true, rewardToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
5909
- _a = createUserATA;
5910
- if (!_a) return [3, 3];
5911
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
5912
- case 2:
5913
- _a = !(_b.sent());
5914
- _b.label = 3;
5915
- case 3:
5916
- if (_a) {
5917
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint));
5918
- }
5919
- tradingAccount = [];
5920
- if (nftTradingAccount) {
5921
- tradingAccount.push({
5922
- pubkey: nftTradingAccount,
5923
- isSigner: false,
5924
- isWritable: true,
5925
- });
5926
- }
5927
- rewardVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_vault")], this.programFbnftReward.programId)[0];
5928
- rewardTokenAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("reward_token_account")], this.programFbnftReward.programId)[0];
5929
- return [4, this.programPerpComposability.methods
5930
- .collectAndDistributeFee()
5931
- .accounts({
5932
- perpProgram: this.programId,
5933
- owner: publicKey,
5934
- receivingTokenAccount: receivingTokenAccount,
5935
- transferAuthority: poolConfig.transferAuthority,
5936
- perpetuals: this.perpetuals.publicKey,
5937
- pool: pool,
5938
- feeCustody: rewardCustodyConfig.custodyAccount,
5939
- flpStakeAccount: flpStakeAccount,
5940
- feeCustodyTokenAccount: rewardCustodyConfig.tokenAccount,
5941
- systemProgram: web3_js_1.SystemProgram.programId,
5942
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
5943
- eventAuthority: this.eventAuthority.publicKey,
5944
- ixSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
5945
- fbnftRewardsProgram: this.programFbnftReward.programId,
5946
- rewardVault: rewardVault,
5947
- rewardTokenAccount: rewardTokenAccount
5948
- })
5949
- .remainingAccounts(tradingAccount)
5950
- .instruction()];
5951
- case 4:
5952
- withdrawStakeInstruction = _b.sent();
5953
- instructions.push(withdrawStakeInstruction);
5954
- return [3, 6];
5955
- case 5:
5956
- err_35 = _b.sent();
5957
- console.log("perpClient withdrawStake error:: ", err_35);
5958
- throw err_35;
5374
+ err_27 = _b.sent();
5375
+ console.log("perpClient collectRevenueInstruction error:: ", err_27);
5376
+ throw err_27;
5959
5377
  case 6: return [2, {
5960
5378
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5961
5379
  additionalSigners: additionalSigners
@@ -5970,7 +5388,7 @@ var PerpetualsClient = (function () {
5970
5388
  args_1[_i - 11] = arguments[_i];
5971
5389
  }
5972
5390
  return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, reserveSymbol_1, receiveSymbol_1, side_1, limitPrice_1, reserveAmount_1, sizeAmount_1, stopLossPrice_1, takeProfitPrice_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, reserveSymbol, receiveSymbol, side, limitPrice, reserveAmount, sizeAmount, stopLossPrice, takeProfitPrice, poolConfig, skipBalanceChecks, ephemeralSignerPubkey) {
5973
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_36;
5391
+ var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_28;
5974
5392
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5975
5393
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5976
5394
  return __generator(this, function (_c) {
@@ -6076,9 +5494,9 @@ var PerpetualsClient = (function () {
6076
5494
  instructions.push(placeLimitOrder);
6077
5495
  return [3, 10];
6078
5496
  case 9:
6079
- err_36 = _c.sent();
6080
- console.log("perpClient placeLimitOrder error:: ", err_36);
6081
- throw err_36;
5497
+ err_28 = _c.sent();
5498
+ console.log("perpClient placeLimitOrder error:: ", err_28);
5499
+ throw err_28;
6082
5500
  case 10: return [2, {
6083
5501
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6084
5502
  additionalSigners: additionalSigners
@@ -6093,7 +5511,7 @@ var PerpetualsClient = (function () {
6093
5511
  args_1[_i - 11] = arguments[_i];
6094
5512
  }
6095
5513
  return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, reserveSymbol_1, receiveSymbol_1, side_1, orderId_1, limitPrice_1, sizeAmount_1, stopLossPrice_1, takeProfitPrice_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, reserveSymbol, receiveSymbol, side, orderId, limitPrice, sizeAmount, stopLossPrice, takeProfitPrice, poolConfig, createUserATA, ephemeralSignerPubkey) {
6096
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_37;
5514
+ var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_29;
6097
5515
  if (createUserATA === void 0) { createUserATA = true; }
6098
5516
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6099
5517
  return __generator(this, function (_b) {
@@ -6184,9 +5602,9 @@ var PerpetualsClient = (function () {
6184
5602
  instructions.push(editLimitOrder);
6185
5603
  return [3, 8];
6186
5604
  case 7:
6187
- err_37 = _b.sent();
6188
- console.log("perpClient editLimitOrder error:: ", err_37);
6189
- throw err_37;
5605
+ err_29 = _b.sent();
5606
+ console.log("perpClient editLimitOrder error:: ", err_29);
5607
+ throw err_29;
6190
5608
  case 8: return [2, {
6191
5609
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6192
5610
  additionalSigners: additionalSigners
@@ -6201,7 +5619,7 @@ var PerpetualsClient = (function () {
6201
5619
  args_1[_i - 7] = arguments[_i];
6202
5620
  }
6203
5621
  return __awaiter(_this, __spreadArray([userPubkey_1, targetSymbol_1, collateralSymbol_1, side_1, orderId_1, poolConfig_1, privilege_1], args_1, true), void 0, function (userPubkey, targetSymbol, collateralSymbol, side, orderId, poolConfig, privilege, tokenStakeAccount, userReferralAccount) {
6204
- var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeLimitOrder, err_38;
5622
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeLimitOrder, err_30;
6205
5623
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6206
5624
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6207
5625
  return __generator(this, function (_a) {
@@ -6253,9 +5671,9 @@ var PerpetualsClient = (function () {
6253
5671
  instructions.push(executeLimitOrder);
6254
5672
  return [3, 4];
6255
5673
  case 3:
6256
- err_38 = _a.sent();
6257
- console.log("perpClient executeLimitOrder error:: ", err_38);
6258
- throw err_38;
5674
+ err_30 = _a.sent();
5675
+ console.log("perpClient executeLimitOrder error:: ", err_30);
5676
+ throw err_30;
6259
5677
  case 4: return [2, {
6260
5678
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6261
5679
  additionalSigners: additionalSigners
@@ -6270,7 +5688,7 @@ var PerpetualsClient = (function () {
6270
5688
  args_1[_i - 8] = arguments[_i];
6271
5689
  }
6272
5690
  return __awaiter(_this, __spreadArray([userPubkey_1, targetSymbol_1, collateralSymbol_1, reserveSymbol_1, side_1, orderId_1, poolConfig_1, privilege_1], args_1, true), void 0, function (userPubkey, targetSymbol, collateralSymbol, reserveSymbol, side, orderId, poolConfig, privilege, tokenStakeAccount, userReferralAccount) {
6273
- var publicKey, targetCustodyConfig, collateralCustodyConfig, reserveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeLimitWithSwap, err_39;
5691
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, reserveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeLimitWithSwap, err_31;
6274
5692
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6275
5693
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6276
5694
  return __generator(this, function (_a) {
@@ -6325,9 +5743,9 @@ var PerpetualsClient = (function () {
6325
5743
  instructions.push(executeLimitWithSwap);
6326
5744
  return [3, 4];
6327
5745
  case 3:
6328
- err_39 = _a.sent();
6329
- console.log("perpClient executeLimitWithSwap error:: ", err_39);
6330
- throw err_39;
5746
+ err_31 = _a.sent();
5747
+ console.log("perpClient executeLimitWithSwap error:: ", err_31);
5748
+ throw err_31;
6331
5749
  case 4: return [2, {
6332
5750
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6333
5751
  additionalSigners: additionalSigners
@@ -6337,7 +5755,7 @@ var PerpetualsClient = (function () {
6337
5755
  });
6338
5756
  };
6339
5757
  this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6340
- var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_40;
5758
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_32;
6341
5759
  return __generator(this, function (_a) {
6342
5760
  switch (_a.label) {
6343
5761
  case 0:
@@ -6385,9 +5803,9 @@ var PerpetualsClient = (function () {
6385
5803
  instructions.push(placeTriggerOrder);
6386
5804
  return [3, 4];
6387
5805
  case 3:
6388
- err_40 = _a.sent();
6389
- console.log("perpClient placeTriggerOrder error:: ", err_40);
6390
- throw err_40;
5806
+ err_32 = _a.sent();
5807
+ console.log("perpClient placeTriggerOrder error:: ", err_32);
5808
+ throw err_32;
6391
5809
  case 4: return [2, {
6392
5810
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6393
5811
  additionalSigners: additionalSigners
@@ -6396,7 +5814,7 @@ var PerpetualsClient = (function () {
6396
5814
  });
6397
5815
  }); };
6398
5816
  this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6399
- var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_41;
5817
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_33;
6400
5818
  return __generator(this, function (_a) {
6401
5819
  switch (_a.label) {
6402
5820
  case 0:
@@ -6443,9 +5861,9 @@ var PerpetualsClient = (function () {
6443
5861
  instructions.push(editTriggerOrder);
6444
5862
  return [3, 4];
6445
5863
  case 3:
6446
- err_41 = _a.sent();
6447
- console.log("perpClient editTriggerOrder error:: ", err_41);
6448
- throw err_41;
5864
+ err_33 = _a.sent();
5865
+ console.log("perpClient editTriggerOrder error:: ", err_33);
5866
+ throw err_33;
6449
5867
  case 4: return [2, {
6450
5868
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6451
5869
  additionalSigners: additionalSigners
@@ -6454,7 +5872,7 @@ var PerpetualsClient = (function () {
6454
5872
  });
6455
5873
  }); };
6456
5874
  this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6457
- var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_42;
5875
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_34;
6458
5876
  return __generator(this, function (_a) {
6459
5877
  switch (_a.label) {
6460
5878
  case 0:
@@ -6487,9 +5905,9 @@ var PerpetualsClient = (function () {
6487
5905
  instructions.push(cancelTriggerOrder);
6488
5906
  return [3, 4];
6489
5907
  case 3:
6490
- err_42 = _a.sent();
6491
- console.log("perpClient cancelTriggerOrder error:: ", err_42);
6492
- throw err_42;
5908
+ err_34 = _a.sent();
5909
+ console.log("perpClient cancelTriggerOrder error:: ", err_34);
5910
+ throw err_34;
6493
5911
  case 4: return [2, {
6494
5912
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6495
5913
  additionalSigners: additionalSigners
@@ -6498,7 +5916,7 @@ var PerpetualsClient = (function () {
6498
5916
  });
6499
5917
  }); };
6500
5918
  this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6501
- var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_43;
5919
+ var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_35;
6502
5920
  return __generator(this, function (_a) {
6503
5921
  switch (_a.label) {
6504
5922
  case 0:
@@ -6529,9 +5947,9 @@ var PerpetualsClient = (function () {
6529
5947
  instructions.push(cancelAllTriggerOrders);
6530
5948
  return [3, 4];
6531
5949
  case 3:
6532
- err_43 = _a.sent();
6533
- console.log("perpClient cancelAllTriggerOrders error:: ", err_43);
6534
- throw err_43;
5950
+ err_35 = _a.sent();
5951
+ console.log("perpClient cancelAllTriggerOrders error:: ", err_35);
5952
+ throw err_35;
6535
5953
  case 4: return [2, {
6536
5954
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6537
5955
  additionalSigners: additionalSigners
@@ -6545,7 +5963,7 @@ var PerpetualsClient = (function () {
6545
5963
  args_1[_i - 9] = arguments[_i];
6546
5964
  }
6547
5965
  return __awaiter(_this, __spreadArray([owner_1, targetSymbol_1, collateralSymbol_1, receivingSymbol_1, side_1, orderId_1, isStopLoss_1, privilege_1, poolConfig_1], args_1, true), void 0, function (owner, targetSymbol, collateralSymbol, receivingSymbol, side, orderId, isStopLoss, privilege, poolConfig, createUserATA, ephemeralSignerPubkey, tokenStakeAccount, userReferralAccount) {
6548
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, userReceivingTokenAccount, userReceivingTokenAccountCollateral, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, collateralToken, receivingToken, _a, _b, positionAccount, orderAccount, custodyAccountMetas, custodyOracleAccountMetas, _c, _d, custody, executeTriggerWithSwap, err_44;
5966
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, userReceivingTokenAccount, userReceivingTokenAccountCollateral, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, collateralToken, receivingToken, _a, _b, positionAccount, orderAccount, custodyAccountMetas, custodyOracleAccountMetas, _c, _d, custody, executeTriggerWithSwap, err_36;
6549
5967
  if (createUserATA === void 0) { createUserATA = true; }
6550
5968
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6551
5969
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
@@ -6651,9 +6069,9 @@ var PerpetualsClient = (function () {
6651
6069
  instructions.push(executeTriggerWithSwap);
6652
6070
  return [3, 10];
6653
6071
  case 9:
6654
- err_44 = _e.sent();
6655
- console.log("perpClient executeTriggerWithSwap error:: ", err_44);
6656
- throw err_44;
6072
+ err_36 = _e.sent();
6073
+ console.log("perpClient executeTriggerWithSwap error:: ", err_36);
6074
+ throw err_36;
6657
6075
  case 10: return [2, {
6658
6076
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6659
6077
  additionalSigners: additionalSigners
@@ -6668,7 +6086,7 @@ var PerpetualsClient = (function () {
6668
6086
  args_1[_i - 8] = arguments[_i];
6669
6087
  }
6670
6088
  return __awaiter(_this, __spreadArray([owner_1, targetSymbol_1, collateralSymbol_1, side_1, orderId_1, isStopLoss_1, privilege_1, poolConfig_1], args_1, true), void 0, function (owner, targetSymbol, collateralSymbol, side, orderId, isStopLoss, privilege, poolConfig, createUserATA, ephemeralSignerPubkey, tokenStakeAccount, userReferralAccount) {
6671
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_45;
6089
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_37;
6672
6090
  if (createUserATA === void 0) { createUserATA = true; }
6673
6091
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6674
6092
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
@@ -6739,9 +6157,9 @@ var PerpetualsClient = (function () {
6739
6157
  instructions.push(executeTriggerOrder);
6740
6158
  return [3, 8];
6741
6159
  case 7:
6742
- err_45 = _b.sent();
6743
- console.log("perpClient executeTriggerOrder error:: ", err_45);
6744
- throw err_45;
6160
+ err_37 = _b.sent();
6161
+ console.log("perpClient executeTriggerOrder error:: ", err_37);
6162
+ throw err_37;
6745
6163
  case 8: return [2, {
6746
6164
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6747
6165
  additionalSigners: additionalSigners
@@ -6756,7 +6174,7 @@ var PerpetualsClient = (function () {
6756
6174
  args_1[_i - 5] = arguments[_i];
6757
6175
  }
6758
6176
  return __awaiter(_this, __spreadArray([userInputTokenSymbol_1, userOutputTokenSymbol_1, amountIn_1, minAmountOut_1, poolConfig_1], args_1, true), void 0, function (userInputTokenSymbol, userOutputTokenSymbol, amountIn, minAmountOut, poolConfig, useFeesPool, createUserATA, unWrapSol, skipBalanceChecks, ephemeralSignerPubkey) {
6759
- var userInputCustodyConfig, userOutputCustodyConfig, publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userOutputTokenAccount, userInputTokenAccount, wsolAssociatedTokenAccount, wsolATAExist, unWrappedSolBalance, _a, wsolAssociatedTokenAccount, closeWsolATAIns, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, lamports, _d, custodyAccountMetas, custodyOracleAccountMetas, _e, _f, custody, params, inx, closeWsolATAIns, err_46;
6177
+ var userInputCustodyConfig, userOutputCustodyConfig, publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userOutputTokenAccount, userInputTokenAccount, wsolAssociatedTokenAccount, wsolATAExist, unWrappedSolBalance, _a, wsolAssociatedTokenAccount, closeWsolATAIns, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, lamports, _d, custodyAccountMetas, custodyOracleAccountMetas, _e, _f, custody, params, inx, closeWsolATAIns, err_38;
6760
6178
  if (useFeesPool === void 0) { useFeesPool = false; }
6761
6179
  if (createUserATA === void 0) { createUserATA = true; }
6762
6180
  if (unWrapSol === void 0) { unWrapSol = false; }
@@ -6961,9 +6379,9 @@ var PerpetualsClient = (function () {
6961
6379
  }
6962
6380
  return [3, 20];
6963
6381
  case 19:
6964
- err_46 = _g.sent();
6965
- console.error("perpClient Swap error:: ", err_46);
6966
- throw err_46;
6382
+ err_38 = _g.sent();
6383
+ console.error("perpClient Swap error:: ", err_38);
6384
+ throw err_38;
6967
6385
  case 20: return [2, {
6968
6386
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6969
6387
  additionalSigners: additionalSigners
@@ -6973,7 +6391,7 @@ var PerpetualsClient = (function () {
6973
6391
  });
6974
6392
  };
6975
6393
  this.swapFeeInternal = function (rewardTokenSymbol, swapTokenSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6976
- var rewardCustody, custody, publicKey, preInstructions, instructions, postInstructions, additionalSigners, custodyAccountMetas, custodyOracleAccountMetas, _i, _a, custody_1, params, inx, err_47;
6394
+ var rewardCustody, custody, publicKey, preInstructions, instructions, postInstructions, additionalSigners, custodyAccountMetas, custodyOracleAccountMetas, _i, _a, custody_1, params, inx, err_39;
6977
6395
  return __generator(this, function (_b) {
6978
6396
  switch (_b.label) {
6979
6397
  case 0:
@@ -7029,9 +6447,9 @@ var PerpetualsClient = (function () {
7029
6447
  instructions.push(inx);
7030
6448
  return [3, 4];
7031
6449
  case 3:
7032
- err_47 = _b.sent();
7033
- console.error("perpClient Swap error:: ", err_47);
7034
- throw err_47;
6450
+ err_39 = _b.sent();
6451
+ console.error("perpClient Swap error:: ", err_39);
6452
+ throw err_39;
7035
6453
  case 4: return [2, {
7036
6454
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7037
6455
  additionalSigners: additionalSigners
@@ -7040,7 +6458,7 @@ var PerpetualsClient = (function () {
7040
6458
  });
7041
6459
  }); };
7042
6460
  this.setLpTokenPrice = function (poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7043
- var instructions, additionalSigners, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, setLpTokenPriceInstruction, err_48;
6461
+ var instructions, additionalSigners, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, setLpTokenPriceInstruction, err_40;
7044
6462
  return __generator(this, function (_d) {
7045
6463
  switch (_d.label) {
7046
6464
  case 0:
@@ -7088,9 +6506,9 @@ var PerpetualsClient = (function () {
7088
6506
  instructions.push(setLpTokenPriceInstruction);
7089
6507
  return [3, 4];
7090
6508
  case 3:
7091
- err_48 = _d.sent();
7092
- console.log("perpClient setLpTokenPriceInstruction error:: ", err_48);
7093
- throw err_48;
6509
+ err_40 = _d.sent();
6510
+ console.log("perpClient setLpTokenPriceInstruction error:: ", err_40);
6511
+ throw err_40;
7094
6512
  case 4: return [2, {
7095
6513
  instructions: __spreadArray([], instructions, true),
7096
6514
  additionalSigners: additionalSigners
@@ -7138,7 +6556,7 @@ var PerpetualsClient = (function () {
7138
6556
  });
7139
6557
  }); };
7140
6558
  this.setAdminSigners = function (admins, minSignatures) { return __awaiter(_this, void 0, void 0, function () {
7141
- var adminMetas, _i, admins_2, admin, err_49;
6559
+ var adminMetas, _i, admins_2, admin, err_41;
7142
6560
  return __generator(this, function (_a) {
7143
6561
  switch (_a.label) {
7144
6562
  case 0:
@@ -7168,11 +6586,11 @@ var PerpetualsClient = (function () {
7168
6586
  _a.sent();
7169
6587
  return [3, 4];
7170
6588
  case 3:
7171
- err_49 = _a.sent();
6589
+ err_41 = _a.sent();
7172
6590
  if (this.printErrors) {
7173
- console.error("setAdminSigners err:", err_49);
6591
+ console.error("setAdminSigners err:", err_41);
7174
6592
  }
7175
- throw err_49;
6593
+ throw err_41;
7176
6594
  case 4: return [2];
7177
6595
  }
7178
6596
  });
@@ -7361,7 +6779,7 @@ var PerpetualsClient = (function () {
7361
6779
  });
7362
6780
  }); };
7363
6781
  this.protocolWithdrawFees = function (rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7364
- var publicKey, custodyConfig, receivingTokenAccount, instructions, additionalSigners, withdrawFeesIx, err_50;
6782
+ var publicKey, custodyConfig, receivingTokenAccount, instructions, additionalSigners, withdrawFeesIx, err_42;
7365
6783
  return __generator(this, function (_a) {
7366
6784
  switch (_a.label) {
7367
6785
  case 0:
@@ -7394,9 +6812,9 @@ var PerpetualsClient = (function () {
7394
6812
  instructions.push(withdrawFeesIx);
7395
6813
  return [3, 5];
7396
6814
  case 4:
7397
- err_50 = _a.sent();
7398
- console.log("perpClient setPool error:: ", err_50);
7399
- throw err_50;
6815
+ err_42 = _a.sent();
6816
+ console.log("perpClient setPool error:: ", err_42);
6817
+ throw err_42;
7400
6818
  case 5: return [2, {
7401
6819
  instructions: __spreadArray([], instructions, true),
7402
6820
  additionalSigners: additionalSigners
@@ -7405,7 +6823,7 @@ var PerpetualsClient = (function () {
7405
6823
  });
7406
6824
  }); };
7407
6825
  this.moveProtocolFees = function (rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7408
- var publicKey, custodyConfig, instructions, additionalSigners, moveProtocolFeesIx, err_51;
6826
+ var publicKey, custodyConfig, instructions, additionalSigners, moveProtocolFeesIx, err_43;
7409
6827
  return __generator(this, function (_a) {
7410
6828
  switch (_a.label) {
7411
6829
  case 0:
@@ -7439,9 +6857,9 @@ var PerpetualsClient = (function () {
7439
6857
  instructions.push(moveProtocolFeesIx);
7440
6858
  return [3, 4];
7441
6859
  case 3:
7442
- err_51 = _a.sent();
7443
- console.log("perpClient setPool error:: ", err_51);
7444
- throw err_51;
6860
+ err_43 = _a.sent();
6861
+ console.log("perpClient setPool error:: ", err_43);
6862
+ throw err_43;
7445
6863
  case 4: return [2, {
7446
6864
  instructions: __spreadArray([], instructions, true),
7447
6865
  additionalSigners: additionalSigners
@@ -7450,7 +6868,7 @@ var PerpetualsClient = (function () {
7450
6868
  });
7451
6869
  }); };
7452
6870
  this.setProtocolFeeShareBps = function (feeShareBps, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7453
- var publicKey, setProtocolFeeShareBpsIx, err_52;
6871
+ var publicKey, setProtocolFeeShareBpsIx, err_44;
7454
6872
  return __generator(this, function (_a) {
7455
6873
  switch (_a.label) {
7456
6874
  case 0:
@@ -7470,15 +6888,15 @@ var PerpetualsClient = (function () {
7470
6888
  setProtocolFeeShareBpsIx = _a.sent();
7471
6889
  return [2, setProtocolFeeShareBpsIx];
7472
6890
  case 2:
7473
- err_52 = _a.sent();
7474
- console.log("perpClient setProtocolFeeShareBpsIx error:: ", err_52);
7475
- throw err_52;
6891
+ err_44 = _a.sent();
6892
+ console.log("perpClient setProtocolFeeShareBpsIx error:: ", err_44);
6893
+ throw err_44;
7476
6894
  case 3: return [2];
7477
6895
  }
7478
6896
  });
7479
6897
  }); };
7480
6898
  this.setPermissions = function (permissions) { return __awaiter(_this, void 0, void 0, function () {
7481
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setPermissionsInstruction, err_53;
6899
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setPermissionsInstruction, err_45;
7482
6900
  return __generator(this, function (_a) {
7483
6901
  switch (_a.label) {
7484
6902
  case 0:
@@ -7505,9 +6923,9 @@ var PerpetualsClient = (function () {
7505
6923
  instructions.push(setPermissionsInstruction);
7506
6924
  return [3, 4];
7507
6925
  case 3:
7508
- err_53 = _a.sent();
7509
- console.log("perpClient setPool error:: ", err_53);
7510
- throw err_53;
6926
+ err_45 = _a.sent();
6927
+ console.log("perpClient setPool error:: ", err_45);
6928
+ throw err_45;
7511
6929
  case 4: return [2, {
7512
6930
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7513
6931
  additionalSigners: additionalSigners
@@ -7516,7 +6934,7 @@ var PerpetualsClient = (function () {
7516
6934
  });
7517
6935
  }); };
7518
6936
  this.reimburse = function (tokenMint, amountIn, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7519
- var custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, instructions, additionalSigners, custodyConfig, reimburse, _d, _e, err_54;
6937
+ var custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, instructions, additionalSigners, custodyConfig, reimburse, _d, _e, err_46;
7520
6938
  var _f;
7521
6939
  return __generator(this, function (_g) {
7522
6940
  switch (_g.label) {
@@ -7577,9 +6995,9 @@ var PerpetualsClient = (function () {
7577
6995
  instructions.push(reimburse);
7578
6996
  return [3, 5];
7579
6997
  case 4:
7580
- err_54 = _g.sent();
7581
- console.log("perpClient setPool error:: ", err_54);
7582
- throw err_54;
6998
+ err_46 = _g.sent();
6999
+ console.log("perpClient setPool error:: ", err_46);
7000
+ throw err_46;
7583
7001
  case 5: return [2, {
7584
7002
  instructions: __spreadArray([], instructions, true),
7585
7003
  additionalSigners: additionalSigners
@@ -7587,8 +7005,8 @@ var PerpetualsClient = (function () {
7587
7005
  }
7588
7006
  });
7589
7007
  }); };
7590
- this.setInternalOraclePrice = function (tokenMint, price, expo, conf, ema, publishTime, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7591
- var instructions, additionalSigners, custodyConfig, setInternalOraclePrice, err_55;
7008
+ this.setInternalOraclePrice = function (tokenMint, useCurrentTime, price, expo, conf, ema, publishTime, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7009
+ var instructions, additionalSigners, custodyConfig, setInternalOraclePrice, err_47;
7592
7010
  return __generator(this, function (_a) {
7593
7011
  switch (_a.label) {
7594
7012
  case 0:
@@ -7600,6 +7018,7 @@ var PerpetualsClient = (function () {
7600
7018
  _a.trys.push([1, 3, , 4]);
7601
7019
  return [4, this.program.methods
7602
7020
  .setInternalOraclePrice({
7021
+ useCurrentTime: useCurrentTime,
7603
7022
  price: price,
7604
7023
  expo: expo,
7605
7024
  conf: conf,
@@ -7621,9 +7040,9 @@ var PerpetualsClient = (function () {
7621
7040
  instructions.push(setInternalOraclePrice);
7622
7041
  return [3, 4];
7623
7042
  case 3:
7624
- err_55 = _a.sent();
7625
- console.log("perpClient setInternalOracleAccount error:: ", err_55);
7626
- throw err_55;
7043
+ err_47 = _a.sent();
7044
+ console.log("perpClient setInternalOracleAccount error:: ", err_47);
7045
+ throw err_47;
7627
7046
  case 4: return [2, {
7628
7047
  instructions: __spreadArray([], instructions, true),
7629
7048
  additionalSigners: additionalSigners
@@ -7631,8 +7050,8 @@ var PerpetualsClient = (function () {
7631
7050
  }
7632
7051
  });
7633
7052
  }); };
7634
- this.setInternalOraclePriceBatch = function (tokenMintList, tokenInternalPrices, POOL_CONFIGS) { return __awaiter(_this, void 0, void 0, function () {
7635
- var ALL_CUSTODY_CONFIGS, accountMetas, _loop_1, _i, tokenMintList_1, tokenMint, instructions, additionalSigners, setInternalOraclePrice, err_56;
7053
+ this.setInternalOraclePriceBatch = function (useCurrentTime, tokenMintList, tokenInternalPrices, POOL_CONFIGS) { return __awaiter(_this, void 0, void 0, function () {
7054
+ var ALL_CUSTODY_CONFIGS, accountMetas, _loop_1, _i, tokenMintList_1, tokenMint, instructions, additionalSigners, setInternalOraclePrice, err_48;
7636
7055
  return __generator(this, function (_a) {
7637
7056
  switch (_a.label) {
7638
7057
  case 0:
@@ -7670,7 +7089,8 @@ var PerpetualsClient = (function () {
7670
7089
  _a.trys.push([1, 3, , 4]);
7671
7090
  return [4, this.program.methods
7672
7091
  .setInternalCurrentPrice({
7673
- prices: tokenInternalPrices
7092
+ prices: tokenInternalPrices,
7093
+ useCurrentTime: useCurrentTime ? 1 : 0
7674
7094
  })
7675
7095
  .accounts({
7676
7096
  authority: POOL_CONFIGS[0].backupOracle,
@@ -7682,9 +7102,9 @@ var PerpetualsClient = (function () {
7682
7102
  instructions.push(setInternalOraclePrice);
7683
7103
  return [3, 4];
7684
7104
  case 3:
7685
- err_56 = _a.sent();
7686
- console.log("perpClient setInternalOracleAccount error:: ", err_56);
7687
- throw err_56;
7105
+ err_48 = _a.sent();
7106
+ console.log("perpClient setInternalOracleAccount error:: ", err_48);
7107
+ throw err_48;
7688
7108
  case 4: return [2, {
7689
7109
  instructions: __spreadArray([], instructions, true),
7690
7110
  additionalSigners: additionalSigners
@@ -7693,7 +7113,7 @@ var PerpetualsClient = (function () {
7693
7113
  });
7694
7114
  }); };
7695
7115
  this.setInternalOracleEmaPriceBatch = function (tokenMintList, tokenInternalEmaPrices, POOL_CONFIGS) { return __awaiter(_this, void 0, void 0, function () {
7696
- var ALL_CUSTODY_CONFIGS, accountMetas, _loop_2, _i, tokenMintList_2, tokenMint, instructions, additionalSigners, setInternalOraclePrice, err_57;
7116
+ var ALL_CUSTODY_CONFIGS, accountMetas, _loop_2, _i, tokenMintList_2, tokenMint, instructions, additionalSigners, setInternalOraclePrice, err_49;
7697
7117
  return __generator(this, function (_a) {
7698
7118
  switch (_a.label) {
7699
7119
  case 0:
@@ -7704,21 +7124,11 @@ var PerpetualsClient = (function () {
7704
7124
  accountMetas = [];
7705
7125
  _loop_2 = function (tokenMint) {
7706
7126
  var custody = ALL_CUSTODY_CONFIGS.find(function (i) { return i.mintKey.equals(tokenMint); });
7707
- accountMetas.push({
7708
- pubkey: custody.custodyAccount,
7709
- isSigner: false,
7710
- isWritable: false,
7711
- });
7712
7127
  accountMetas.push({
7713
7128
  pubkey: custody.intOracleAccount,
7714
7129
  isSigner: false,
7715
7130
  isWritable: true,
7716
7131
  });
7717
- accountMetas.push({
7718
- pubkey: custody.extOracleAccount,
7719
- isSigner: false,
7720
- isWritable: false,
7721
- });
7722
7132
  };
7723
7133
  for (_i = 0, tokenMintList_2 = tokenMintList; _i < tokenMintList_2.length; _i++) {
7724
7134
  tokenMint = tokenMintList_2[_i];
@@ -7743,9 +7153,9 @@ var PerpetualsClient = (function () {
7743
7153
  instructions.push(setInternalOraclePrice);
7744
7154
  return [3, 4];
7745
7155
  case 3:
7746
- err_57 = _a.sent();
7747
- console.log("perpClient setInternalOracleAccount error:: ", err_57);
7748
- throw err_57;
7156
+ err_49 = _a.sent();
7157
+ console.log("perpClient setInternalOracleAccount error:: ", err_49);
7158
+ throw err_49;
7749
7159
  case 4: return [2, {
7750
7160
  instructions: __spreadArray([], instructions, true),
7751
7161
  additionalSigners: additionalSigners
@@ -7754,7 +7164,7 @@ var PerpetualsClient = (function () {
7754
7164
  });
7755
7165
  }); };
7756
7166
  this.renameFlp = function (flag, lpTokenName, lpTokenSymbol, lpTokenUri, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7757
- var publicKey, instructions, additionalSigners, lpTokenMint, lpMetadataAccount, renameFlp, err_58;
7167
+ var publicKey, instructions, additionalSigners, lpTokenMint, lpMetadataAccount, renameFlp, err_50;
7758
7168
  return __generator(this, function (_a) {
7759
7169
  switch (_a.label) {
7760
7170
  case 0:
@@ -7792,8 +7202,8 @@ var PerpetualsClient = (function () {
7792
7202
  instructions.push(renameFlp);
7793
7203
  return [3, 4];
7794
7204
  case 3:
7795
- err_58 = _a.sent();
7796
- console.log("perpClient renameFlp error:: ", err_58);
7205
+ err_50 = _a.sent();
7206
+ console.log("perpClient renameFlp error:: ", err_50);
7797
7207
  return [3, 4];
7798
7208
  case 4: return [2, {
7799
7209
  instructions: __spreadArray([], instructions, true),
@@ -7803,7 +7213,7 @@ var PerpetualsClient = (function () {
7803
7213
  });
7804
7214
  }); };
7805
7215
  this.initStake = function (stakingFeeShareBps, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7806
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, stakedLpTokenAccount, rewardCustodyConfig, initStakeInstruction, err_59;
7216
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, stakedLpTokenAccount, rewardCustodyConfig, initStakeInstruction, err_51;
7807
7217
  return __generator(this, function (_a) {
7808
7218
  switch (_a.label) {
7809
7219
  case 0:
@@ -7841,9 +7251,9 @@ var PerpetualsClient = (function () {
7841
7251
  instructions.push(initStakeInstruction);
7842
7252
  return [3, 4];
7843
7253
  case 3:
7844
- err_59 = _a.sent();
7845
- console.log("perpClient InitStaking error:: ", err_59);
7846
- throw err_59;
7254
+ err_51 = _a.sent();
7255
+ console.log("perpClient InitStaking error:: ", err_51);
7256
+ throw err_51;
7847
7257
  case 4: return [2, {
7848
7258
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7849
7259
  additionalSigners: additionalSigners
@@ -7852,7 +7262,7 @@ var PerpetualsClient = (function () {
7852
7262
  });
7853
7263
  }); };
7854
7264
  this.initCompounding = function (feeShareBps, metadataTitle, metadataSymbol, metadataUri, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7855
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyConfig, compoundingTokenMint, compoundingVault, metadataAccount, initCompoundingInstruction, err_60;
7265
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyConfig, compoundingTokenMint, compoundingVault, metadataAccount, initCompoundingInstruction, err_52;
7856
7266
  return __generator(this, function (_a) {
7857
7267
  switch (_a.label) {
7858
7268
  case 0:
@@ -7897,9 +7307,9 @@ var PerpetualsClient = (function () {
7897
7307
  instructions.push(initCompoundingInstruction);
7898
7308
  return [3, 4];
7899
7309
  case 3:
7900
- err_60 = _a.sent();
7901
- console.log("perpClient initCompounding error:: ", err_60);
7902
- throw err_60;
7310
+ err_52 = _a.sent();
7311
+ console.log("perpClient initCompounding error:: ", err_52);
7312
+ throw err_52;
7903
7313
  case 4: return [2, {
7904
7314
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7905
7315
  additionalSigners: additionalSigners
@@ -7908,7 +7318,7 @@ var PerpetualsClient = (function () {
7908
7318
  });
7909
7319
  }); };
7910
7320
  this.initTokenVault = function (token_permissions, tokens_to_distribute, withdrawTimeLimit, withdrawInstantFee, stakeLevel, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7911
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenMint, fundingTokenAccount, initTokenVaultInstruction, err_61;
7321
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenMint, fundingTokenAccount, initTokenVaultInstruction, err_53;
7912
7322
  return __generator(this, function (_a) {
7913
7323
  switch (_a.label) {
7914
7324
  case 0:
@@ -7949,9 +7359,9 @@ var PerpetualsClient = (function () {
7949
7359
  instructions.push(initTokenVaultInstruction);
7950
7360
  return [3, 4];
7951
7361
  case 3:
7952
- err_61 = _a.sent();
7953
- console.log("perpClient InitTokenVaultInstruction error:: ", err_61);
7954
- throw err_61;
7362
+ err_53 = _a.sent();
7363
+ console.log("perpClient InitTokenVaultInstruction error:: ", err_53);
7364
+ throw err_53;
7955
7365
  case 4: return [2, {
7956
7366
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7957
7367
  additionalSigners: additionalSigners
@@ -7960,7 +7370,7 @@ var PerpetualsClient = (function () {
7960
7370
  });
7961
7371
  }); };
7962
7372
  this.setTokenVaultConfig = function (token_permissions, withdrawTimeLimit, withdrawInstantFee, stakeLevel, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7963
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setTokenVaultConfigInstruction, err_62;
7373
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setTokenVaultConfigInstruction, err_54;
7964
7374
  return __generator(this, function (_a) {
7965
7375
  switch (_a.label) {
7966
7376
  case 0:
@@ -7991,9 +7401,9 @@ var PerpetualsClient = (function () {
7991
7401
  instructions.push(setTokenVaultConfigInstruction);
7992
7402
  return [3, 4];
7993
7403
  case 3:
7994
- err_62 = _a.sent();
7995
- console.log("perpClient setTokenVaultConfigInstruction error:: ", err_62);
7996
- throw err_62;
7404
+ err_54 = _a.sent();
7405
+ console.log("perpClient setTokenVaultConfigInstruction error:: ", err_54);
7406
+ throw err_54;
7997
7407
  case 4: return [2, {
7998
7408
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7999
7409
  additionalSigners: additionalSigners
@@ -8002,7 +7412,7 @@ var PerpetualsClient = (function () {
8002
7412
  });
8003
7413
  }); };
8004
7414
  this.withdrawInstantFee = function (poolConfig) { return __awaiter(_this, void 0, void 0, function () {
8005
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, receivingTokenAccount, withdrawInstantFeeInstruction, err_63;
7415
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, receivingTokenAccount, withdrawInstantFeeInstruction, err_55;
8006
7416
  return __generator(this, function (_a) {
8007
7417
  switch (_a.label) {
8008
7418
  case 0:
@@ -8041,9 +7451,9 @@ var PerpetualsClient = (function () {
8041
7451
  instructions.push(withdrawInstantFeeInstruction);
8042
7452
  return [3, 6];
8043
7453
  case 5:
8044
- err_63 = _a.sent();
8045
- console.log("perpClient withdrawInstantFeeInstruction error:: ", err_63);
8046
- throw err_63;
7454
+ err_55 = _a.sent();
7455
+ console.log("perpClient withdrawInstantFeeInstruction error:: ", err_55);
7456
+ throw err_55;
8047
7457
  case 6: return [2, {
8048
7458
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
8049
7459
  additionalSigners: additionalSigners
@@ -8052,7 +7462,7 @@ var PerpetualsClient = (function () {
8052
7462
  });
8053
7463
  }); };
8054
7464
  this.initRevenueTokenAccount = function (feeShareBps, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
8055
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, initRevenueTokenAccountInstruction, err_64;
7465
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, initRevenueTokenAccountInstruction, err_56;
8056
7466
  return __generator(this, function (_a) {
8057
7467
  switch (_a.label) {
8058
7468
  case 0:
@@ -8089,9 +7499,9 @@ var PerpetualsClient = (function () {
8089
7499
  instructions.push(initRevenueTokenAccountInstruction);
8090
7500
  return [3, 4];
8091
7501
  case 3:
8092
- err_64 = _a.sent();
8093
- console.log("perpClient initRevenueTokenAccountInstruction error:: ", err_64);
8094
- throw err_64;
7502
+ err_56 = _a.sent();
7503
+ console.log("perpClient initRevenueTokenAccountInstruction error:: ", err_56);
7504
+ throw err_56;
8095
7505
  case 4: return [2, {
8096
7506
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
8097
7507
  additionalSigners: additionalSigners
@@ -8100,7 +7510,7 @@ var PerpetualsClient = (function () {
8100
7510
  });
8101
7511
  }); };
8102
7512
  this.distributeTokenReward = function (amount, epochCount, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
8103
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, fundingTokenAccount, revenueFundingTokenAccount, distributeTokenRewardInstruction, err_65;
7513
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, fundingTokenAccount, revenueFundingTokenAccount, distributeTokenRewardInstruction, err_57;
8104
7514
  return __generator(this, function (_a) {
8105
7515
  switch (_a.label) {
8106
7516
  case 0:
@@ -8139,9 +7549,9 @@ var PerpetualsClient = (function () {
8139
7549
  instructions.push(distributeTokenRewardInstruction);
8140
7550
  return [3, 4];
8141
7551
  case 3:
8142
- err_65 = _a.sent();
8143
- console.log("perpClient distributeTokenRewardInstruction error:: ", err_65);
8144
- throw err_65;
7552
+ err_57 = _a.sent();
7553
+ console.log("perpClient distributeTokenRewardInstruction error:: ", err_57);
7554
+ throw err_57;
8145
7555
  case 4: return [2, {
8146
7556
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
8147
7557
  additionalSigners: additionalSigners
@@ -8150,7 +7560,7 @@ var PerpetualsClient = (function () {
8150
7560
  });
8151
7561
  }); };
8152
7562
  this.setTokenStakeLevel = function (owner, stakeLevel) { return __awaiter(_this, void 0, void 0, function () {
8153
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, setTokenStakeLevelInstruction, err_66;
7563
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, setTokenStakeLevelInstruction, err_58;
8154
7564
  return __generator(this, function (_a) {
8155
7565
  switch (_a.label) {
8156
7566
  case 0:
@@ -8178,9 +7588,9 @@ var PerpetualsClient = (function () {
8178
7588
  instructions.push(setTokenStakeLevelInstruction);
8179
7589
  return [3, 4];
8180
7590
  case 3:
8181
- err_66 = _a.sent();
8182
- console.log("perpClient setTokenStakeLevelInstruction error:: ", err_66);
8183
- throw err_66;
7591
+ err_58 = _a.sent();
7592
+ console.log("perpClient setTokenStakeLevelInstruction error:: ", err_58);
7593
+ throw err_58;
8184
7594
  case 4: return [2, {
8185
7595
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
8186
7596
  additionalSigners: additionalSigners
@@ -8189,7 +7599,7 @@ var PerpetualsClient = (function () {
8189
7599
  });
8190
7600
  }); };
8191
7601
  this.setTokenReward = function (owner, amount, epochCount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
8192
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, setTokenRewardInstruction, err_67;
7602
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, setTokenRewardInstruction, err_59;
8193
7603
  return __generator(this, function (_a) {
8194
7604
  switch (_a.label) {
8195
7605
  case 0:
@@ -8221,9 +7631,49 @@ var PerpetualsClient = (function () {
8221
7631
  instructions.push(setTokenRewardInstruction);
8222
7632
  return [3, 4];
8223
7633
  case 3:
8224
- err_67 = _a.sent();
8225
- console.log("perpClient setTokenRewardInstruction error:: ", err_67);
8226
- throw err_67;
7634
+ err_59 = _a.sent();
7635
+ console.log("perpClient setTokenRewardInstruction error:: ", err_59);
7636
+ throw err_59;
7637
+ case 4: return [2, {
7638
+ instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7639
+ additionalSigners: additionalSigners
7640
+ }];
7641
+ }
7642
+ });
7643
+ }); };
7644
+ this.resizeInternalOracle = function (extOracle, tokenMint, intOracleAccount) { return __awaiter(_this, void 0, void 0, function () {
7645
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, resizeInternalOracleInstruction, err_60;
7646
+ return __generator(this, function (_a) {
7647
+ switch (_a.label) {
7648
+ case 0:
7649
+ publicKey = this.provider.wallet.publicKey;
7650
+ preInstructions = [];
7651
+ instructions = [];
7652
+ postInstructions = [];
7653
+ additionalSigners = [];
7654
+ _a.label = 1;
7655
+ case 1:
7656
+ _a.trys.push([1, 3, , 4]);
7657
+ return [4, this.program.methods
7658
+ .resizeInternalOracle({
7659
+ extOracle: extOracle
7660
+ })
7661
+ .accounts({
7662
+ admin: publicKey,
7663
+ multisig: this.multisig.publicKey,
7664
+ custodyTokenMint: tokenMint,
7665
+ intOracleAccount: intOracleAccount,
7666
+ systemProgram: web3_js_1.SystemProgram.programId,
7667
+ })
7668
+ .instruction()];
7669
+ case 2:
7670
+ resizeInternalOracleInstruction = _a.sent();
7671
+ instructions.push(resizeInternalOracleInstruction);
7672
+ return [3, 4];
7673
+ case 3:
7674
+ err_60 = _a.sent();
7675
+ console.log("perpClient resizeInternalOracleInstruction error:: ", err_60);
7676
+ throw err_60;
8227
7677
  case 4: return [2, {
8228
7678
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
8229
7679
  additionalSigners: additionalSigners