flash-sdk 11.9.3-alpha.0 → 11.10.1

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.
@@ -701,7 +701,7 @@ var PerpetualsClient = (function () {
701
701
  currentMarginUsd = positionAccount.collateralUsd.sub(lossUsd);
702
702
  }
703
703
  else {
704
- currentMarginUsd = positionAccount.collateralUsd.add(pnl.profitUsd).sub(lossUsd);
704
+ currentMarginUsd = positionAccount.collateralUsd.add(pnl.netProfitUsd).sub(lossUsd);
705
705
  }
706
706
  if (currentMarginUsd.gt(constants_1.BN_ZERO)) {
707
707
  return positionAccount.sizeUsd.mul(new anchor_1.BN(constants_1.BPS_POWER)).div(currentMarginUsd);
@@ -875,7 +875,7 @@ var PerpetualsClient = (function () {
875
875
  if (collateralDeltaAmount.isNeg() || sizeDeltaAmount.isNeg()) {
876
876
  throw new Error("Delta Amounts cannot be negative ");
877
877
  }
878
- if (resultingPositionAccount.collateralAmount.isNeg() || resultingPositionAccount.sizeAmount.isNeg()) {
878
+ if (resultingPositionAccount.collateralUsd.isNeg() || resultingPositionAccount.sizeAmount.isNeg()) {
879
879
  throw new Error("cannot remove/close more than collateral/Size");
880
880
  }
881
881
  var sizeUsd = targetPrice.getAssetAmountUsd(sizeDeltaAmount, targetCustodyAccount.decimals);
@@ -1056,7 +1056,7 @@ var PerpetualsClient = (function () {
1056
1056
  var totalFeesUsd = (exitFeeUsd.add(lockAndUnsettledFeeUsd));
1057
1057
  var currentCollateralUsd = positionDelta.collateralUsd;
1058
1058
  var liabilityUsd = newPnl.lossUsd.add(totalFeesUsd);
1059
- var assetsUsd = anchor_1.BN.min(newPnl.profitUsd.add(currentCollateralUsd), collateralMinMaxPrice.max.getAssetAmountUsd(positionDelta.lockedAmount, collateralCustodyAccount.decimals));
1059
+ var assetsUsd = anchor_1.BN.min(newPnl.netProfitUsd.add(currentCollateralUsd), collateralMinMaxPrice.max.getAssetAmountUsd(positionDelta.lockedAmount, collateralCustodyAccount.decimals));
1060
1060
  if (debugLogs) {
1061
1061
  console.log("assetsUsd.sub(liabilityUsd):", collateralCustodyAccount.decimals, assetsUsd.toString(), liabilityUsd.toString(), assetsUsd.sub(liabilityUsd).toString());
1062
1062
  }
@@ -1190,10 +1190,11 @@ var PerpetualsClient = (function () {
1190
1190
  var position = PositionAccount_1.PositionAccount.from(positionAccount.publicKey, __assign({}, positionAccount));
1191
1191
  var collateralMinMaxPrice = _this.getMinAndMaxOraclePriceSync(collateralPrice, collateralEmaPrice, collateralCustodyAccount);
1192
1192
  var newPnl = _this.getPnlSync(position, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1193
- var exitPriceAndFee = _this.getExitPriceAndFeeSync(positionAccount, marketCorrelation, positionAccount.collateralAmount, positionAccount.sizeAmount, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp);
1193
+ var collateralAmount = collateralPrice.getTokenAmount(positionAccount.collateralUsd, collateralCustodyAccount.decimals);
1194
+ var exitPriceAndFee = _this.getExitPriceAndFeeSync(positionAccount, marketCorrelation, collateralAmount, positionAccount.sizeAmount, side, targetPrice, targetEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp);
1194
1195
  var totalFeesUsd = (exitPriceAndFee.exitFeeUsd.add(exitPriceAndFee.borrowFeeUsd));
1195
1196
  var liabilityUsd = newPnl.lossUsd.add(totalFeesUsd);
1196
- var assetsUsd = anchor_1.BN.min(newPnl.profitUsd.add(positionAccount.collateralUsd), collateralMinMaxPrice.max.getAssetAmountUsd(positionAccount.lockedAmount, collateralCustodyAccount.decimals));
1197
+ var assetsUsd = anchor_1.BN.min(newPnl.netProfitUsd.add(positionAccount.collateralUsd), collateralMinMaxPrice.max.getAssetAmountUsd(positionAccount.lockedAmount, collateralCustodyAccount.decimals));
1197
1198
  var closeAmountUsd, feesAmountUsd;
1198
1199
  if (assetsUsd.gt(liabilityUsd)) {
1199
1200
  closeAmountUsd = assetsUsd.sub(liabilityUsd);
@@ -1609,6 +1610,9 @@ var PerpetualsClient = (function () {
1609
1610
  return {
1610
1611
  profitUsd: constants_1.BN_ZERO,
1611
1612
  lossUsd: constants_1.BN_ZERO,
1613
+ priceImpactUsd: constants_1.BN_ZERO,
1614
+ maxPriceImpactUsd: constants_1.BN_ZERO,
1615
+ netProfitUsd: constants_1.BN_ZERO
1612
1616
  };
1613
1617
  }
1614
1618
  var side = poolConfig.getMarketConfigByPk(positionAccount.market).side;
@@ -1672,16 +1676,28 @@ var PerpetualsClient = (function () {
1672
1676
  if (!priceDiffProfit.exponent.eq(priceDiffLoss.exponent)) {
1673
1677
  throw new Error("exponent mistach");
1674
1678
  }
1679
+ var maxPriceImpactUsd = positionAccount.priceImpactUsd;
1675
1680
  if (priceDiffProfit.price.gt(constants_1.BN_ZERO)) {
1681
+ var grossProfitUsd = priceDiffProfit.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals);
1682
+ var priceImpactUsd = positionAccount.priceImpactUsd.gt(constants_1.BN_ZERO)
1683
+ ? anchor_1.BN.min(positionAccount.priceImpactUsd, grossProfitUsd)
1684
+ : constants_1.BN_ZERO;
1685
+ var netProfitUsd = grossProfitUsd.sub(priceImpactUsd);
1676
1686
  return {
1677
- profitUsd: priceDiffProfit.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals),
1687
+ profitUsd: grossProfitUsd,
1678
1688
  lossUsd: constants_1.BN_ZERO,
1689
+ priceImpactUsd: priceImpactUsd,
1690
+ maxPriceImpactUsd: maxPriceImpactUsd,
1691
+ netProfitUsd: netProfitUsd,
1679
1692
  };
1680
1693
  }
1681
1694
  else {
1682
1695
  return {
1683
1696
  profitUsd: constants_1.BN_ZERO,
1684
1697
  lossUsd: priceDiffLoss.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals),
1698
+ priceImpactUsd: constants_1.BN_ZERO,
1699
+ maxPriceImpactUsd: maxPriceImpactUsd,
1700
+ netProfitUsd: constants_1.BN_ZERO,
1685
1701
  };
1686
1702
  }
1687
1703
  };
@@ -1694,7 +1710,7 @@ var PerpetualsClient = (function () {
1694
1710
  timestamp: constants_1.BN_ZERO
1695
1711
  });
1696
1712
  return {
1697
- pnl: { profitUsd: constants_1.BN_ZERO, lossUsd: constants_1.BN_ZERO },
1713
+ pnl: { profitUsd: constants_1.BN_ZERO, lossUsd: constants_1.BN_ZERO, priceImpactUsd: constants_1.BN_ZERO, maxPriceImpactUsd: constants_1.BN_ZERO, netProfitUsd: constants_1.BN_ZERO },
1698
1714
  leverage: constants_1.BN_ZERO,
1699
1715
  liquidationPrice: zeroOraclePrice,
1700
1716
  fees: { exitFeeUsd: constants_1.BN_ZERO, exitFeeAmount: constants_1.BN_ZERO, lockAndUnsettledFeeUsd: constants_1.BN_ZERO }
@@ -1714,68 +1730,18 @@ var PerpetualsClient = (function () {
1714
1730
  if (!exitOraclePrice.exponent.eq(entryOraclePrice.exponent)) {
1715
1731
  throw new Error("exponent mismatch");
1716
1732
  }
1717
- var priceDiffProfit, priceDiffLoss;
1718
- var delay = targetCustodyAccount.pricing.delaySeconds;
1719
- if ((0, types_1.isVariant)(side, 'long')) {
1720
- if (exitOraclePrice.price.gt(entryOraclePrice.price)) {
1721
- if (currentTimestamp.gt(positionAccount.updateTime.add(delay))) {
1722
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: exitOraclePrice.price.sub(entryOraclePrice.price), exponent: exitOraclePrice.exponent, confidence: exitOraclePrice.confidence, timestamp: constants_1.BN_ZERO });
1723
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1724
- }
1725
- else {
1726
- if (positionAccount.referencePrice.price.gt(entryOraclePrice.price)) {
1727
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: positionAccount.referencePrice.price.sub(entryOraclePrice.price), exponent: entryOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1728
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1729
- }
1730
- else {
1731
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1732
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1733
- }
1734
- }
1735
- }
1736
- else {
1737
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1738
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: entryOraclePrice.price.sub(exitOraclePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1739
- }
1740
- }
1741
- else {
1742
- if (exitOraclePrice.price.lt(entryOraclePrice.price)) {
1743
- if (currentTimestamp.gt(positionAccount.updateTime.add(delay))) {
1744
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: entryOraclePrice.price.sub(exitOraclePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1745
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1746
- }
1747
- else {
1748
- if (entryOraclePrice.price.gt(positionAccount.referencePrice.price)) {
1749
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: entryOraclePrice.price.sub(positionAccount.referencePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1750
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1751
- }
1752
- else {
1753
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1754
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1755
- }
1756
- }
1757
- }
1758
- else {
1759
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1760
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: exitOraclePrice.price.sub(entryOraclePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1761
- }
1762
- }
1763
- if (priceDiffProfit.price.gt(constants_1.BN_ZERO)) {
1764
- pnl = {
1765
- profitUsd: priceDiffProfit.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals),
1766
- lossUsd: constants_1.BN_ZERO,
1767
- };
1768
- }
1769
- else {
1770
- pnl = {
1771
- profitUsd: constants_1.BN_ZERO,
1772
- lossUsd: priceDiffLoss.getAssetAmountUsd(positionAccount.sizeAmount, positionAccount.sizeDecimals),
1773
- };
1774
- }
1733
+ var pnlResult = _this.getPnlContractHelper(positionAccount, targetTokenPrice, targetTokenEmaPrice, targetCustodyAccount, collateralPrice, collateralEmaPrice, collateralCustodyAccount, currentTimestamp, targetCustodyAccount.pricing.delaySeconds, poolConfig);
1734
+ pnl = {
1735
+ profitUsd: pnlResult.profitUsd,
1736
+ lossUsd: pnlResult.lossUsd,
1737
+ priceImpactUsd: pnlResult.priceImpactUsd,
1738
+ maxPriceImpactUsd: pnlResult.maxPriceImpactUsd,
1739
+ netProfitUsd: pnlResult.netProfitUsd
1740
+ };
1775
1741
  var liquidationPrice = _this.getLiquidationPriceContractHelper(entryOraclePrice, lockAndUnsettledFeeUsd, side, targetCustodyAccount, positionAccount);
1776
1742
  var unsettledFeesUsd = exitFeeUsd.add(lockAndUnsettledFeeUsd);
1777
1743
  var lossUsd = pnl.lossUsd.add(unsettledFeesUsd);
1778
- var currentMarginUsd = positionAccount.collateralUsd.add(pnl.profitUsd).sub(lossUsd);
1744
+ var currentMarginUsd = positionAccount.collateralUsd.add(pnl.netProfitUsd).sub(lossUsd);
1779
1745
  var leverage;
1780
1746
  if (currentMarginUsd.gt(constants_1.BN_ZERO)) {
1781
1747
  leverage = positionAccount.sizeUsd.mul(new anchor_1.BN(constants_1.BPS_POWER)).div(currentMarginUsd);
@@ -2728,7 +2694,7 @@ var PerpetualsClient = (function () {
2728
2694
  for (var _i = 8; _i < arguments.length; _i++) {
2729
2695
  args_1[_i - 8] = arguments[_i];
2730
2696
  }
2731
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, priceWithSlippage_1, collateralWithfee_1, size_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, priceWithSlippage, collateralWithfee, size, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
2697
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, priceWithSlippage_1, collateralWithfee_1, size_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, priceWithSlippage, collateralWithfee, size, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey) {
2732
2698
  var publicKey, targetCustodyConfig, collateralCustodyConfig, collateralToken, marketAccount, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
2733
2699
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2734
2700
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2737,7 +2703,7 @@ var PerpetualsClient = (function () {
2737
2703
  return __generator(this, function (_c) {
2738
2704
  switch (_c.label) {
2739
2705
  case 0:
2740
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2706
+ publicKey = this.provider.wallet.publicKey;
2741
2707
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
2742
2708
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2743
2709
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
@@ -2842,48 +2808,59 @@ var PerpetualsClient = (function () {
2842
2808
  for (var _i = 6; _i < arguments.length; _i++) {
2843
2809
  args_1[_i - 6] = arguments[_i];
2844
2810
  }
2845
- return __awaiter(_this, __spreadArray([marketSymbol_1, collateralSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (marketSymbol, collateralSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey, userPublicKey) {
2846
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2811
+ return __awaiter(_this, __spreadArray([marketSymbol_1, collateralSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (marketSymbol, collateralSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey) {
2812
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2847
2813
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2848
2814
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
2849
2815
  if (createUserATA === void 0) { createUserATA = true; }
2850
2816
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
2851
2817
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2852
- return __generator(this, function (_a) {
2853
- switch (_a.label) {
2818
+ return __generator(this, function (_b) {
2819
+ switch (_b.label) {
2854
2820
  case 0:
2855
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2821
+ console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
2822
+ publicKey = this.provider.wallet.publicKey;
2856
2823
  preInstructions = [];
2857
2824
  instructions = [];
2858
2825
  postInstructions = [];
2859
2826
  additionalSigners = [];
2860
- _a.label = 1;
2827
+ _b.label = 1;
2861
2828
  case 1:
2862
- _a.trys.push([1, 3, , 4]);
2863
- if (collateralSymbol == 'SOL') {
2864
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
2865
- if (!ephemeralSignerPubkey) {
2866
- wrappedSolAccount = new web3_js_1.Keypair();
2867
- additionalSigners.push(wrappedSolAccount);
2868
- }
2869
- preInstructions = [
2870
- web3_js_1.SystemProgram.createAccount({
2871
- fromPubkey: publicKey,
2872
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2873
- lamports: lamports,
2874
- space: 165,
2875
- programId: spl_token_1.TOKEN_PROGRAM_ID,
2876
- }),
2877
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2878
- ];
2879
- postInstructions = [
2880
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2881
- ];
2829
+ _b.trys.push([1, 7, , 8]);
2830
+ if (!(collateralSymbol == 'SOL')) return [3, 2];
2831
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
2832
+ if (!ephemeralSignerPubkey) {
2833
+ wrappedSolAccount = new web3_js_1.Keypair();
2834
+ additionalSigners.push(wrappedSolAccount);
2882
2835
  }
2883
- else {
2884
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
2885
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
2836
+ preInstructions = [
2837
+ web3_js_1.SystemProgram.createAccount({
2838
+ fromPubkey: publicKey,
2839
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2840
+ lamports: lamports,
2841
+ space: 165,
2842
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
2843
+ }),
2844
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2845
+ ];
2846
+ postInstructions = [
2847
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2848
+ ];
2849
+ return [3, 5];
2850
+ case 2:
2851
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
2852
+ _a = createUserATA;
2853
+ if (!_a) return [3, 4];
2854
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
2855
+ case 3:
2856
+ _a = !(_b.sent());
2857
+ _b.label = 4;
2858
+ case 4:
2859
+ if (_a) {
2860
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
2886
2861
  }
2862
+ _b.label = 5;
2863
+ case 5:
2887
2864
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2888
2865
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
2889
2866
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -2915,19 +2892,19 @@ var PerpetualsClient = (function () {
2915
2892
  })
2916
2893
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
2917
2894
  .instruction()];
2918
- case 2:
2919
- instruction = _a.sent();
2895
+ case 6:
2896
+ instruction = _b.sent();
2920
2897
  instructions.push(instruction);
2921
2898
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
2922
2899
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
2923
2900
  postInstructions.push(closeWsolATAIns);
2924
2901
  }
2925
- return [3, 4];
2926
- case 3:
2927
- error_1 = _a.sent();
2902
+ return [3, 8];
2903
+ case 7:
2904
+ error_1 = _b.sent();
2928
2905
  console.error("perpclient closePosition error:", error_1);
2929
2906
  throw error_1;
2930
- case 4: return [2, {
2907
+ case 8: return [2, {
2931
2908
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
2932
2909
  additionalSigners: additionalSigners
2933
2910
  }];
@@ -2940,7 +2917,7 @@ var PerpetualsClient = (function () {
2940
2917
  for (var _i = 9; _i < arguments.length; _i++) {
2941
2918
  args_1[_i - 9] = arguments[_i];
2942
2919
  }
2943
- return __awaiter(_this, __spreadArray([targetTokenSymbol_1, collateralTokenSymbol_1, userInputTokenSymbol_1, amountIn_1, priceWithSlippage_1, sizeAmount_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetTokenSymbol, collateralTokenSymbol, userInputTokenSymbol, amountIn, priceWithSlippage, sizeAmount, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
2920
+ return __awaiter(_this, __spreadArray([targetTokenSymbol_1, collateralTokenSymbol_1, userInputTokenSymbol_1, amountIn_1, priceWithSlippage_1, sizeAmount_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetTokenSymbol, collateralTokenSymbol, userInputTokenSymbol, amountIn, priceWithSlippage, sizeAmount, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey) {
2944
2921
  var publicKey, userInputCustodyConfig, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, targetToken, userInputTokenAccount, userInputToken, lamports, unWrappedSolBalance, _a, userOutputTokenAccount, tokenAccountBalance, _b, userOutputTokenAccount, inx, err_3;
2945
2922
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2946
2923
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2949,7 +2926,7 @@ var PerpetualsClient = (function () {
2949
2926
  return __generator(this, function (_c) {
2950
2927
  switch (_c.label) {
2951
2928
  case 0:
2952
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2929
+ publicKey = this.provider.wallet.publicKey;
2953
2930
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
2954
2931
  if (!userInputCustodyConfig) {
2955
2932
  throw "userInputCustodyConfig not found";
@@ -2973,7 +2950,7 @@ var PerpetualsClient = (function () {
2973
2950
  additionalSigners = [];
2974
2951
  targetToken = poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol);
2975
2952
  userInputToken = poolConfig.getTokenFromSymbol(userInputTokenSymbol);
2976
- if (!(userInputTokenSymbol == 'SOL')) return [3, 3];
2953
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 5];
2977
2954
  console.log("inputSymbol === SOL", userInputTokenSymbol);
2978
2955
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
2979
2956
  if (!!skipBalanceChecks) return [3, 2];
@@ -3003,33 +2980,41 @@ var PerpetualsClient = (function () {
3003
2980
  postInstructions = [
3004
2981
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3005
2982
  ];
3006
- if (!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) {
3007
- userOutputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3008
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3009
- }
3010
- return [3, 7];
2983
+ if (!!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) return [3, 4];
2984
+ userOutputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
2985
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3011
2986
  case 3:
2987
+ if (!(_c.sent())) {
2988
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey));
2989
+ }
2990
+ _c.label = 4;
2991
+ case 4: return [3, 10];
2992
+ case 5:
3012
2993
  userInputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(userInputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3013
- if (!!skipBalanceChecks) return [3, 6];
2994
+ if (!!skipBalanceChecks) return [3, 8];
3014
2995
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3015
- case 4:
2996
+ case 6:
3016
2997
  if (!(_c.sent())) {
3017
2998
  throw "Insufficient Funds , Token Account doesn't exist";
3018
2999
  }
3019
3000
  _b = anchor_1.BN.bind;
3020
3001
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3021
- case 5:
3002
+ case 7:
3022
3003
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
3023
3004
  if (tokenAccountBalance.lt(amountIn)) {
3024
3005
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3025
3006
  }
3026
- _c.label = 6;
3027
- case 6:
3007
+ _c.label = 8;
3008
+ case 8:
3028
3009
  userOutputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3029
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3030
- _c.label = 7;
3031
- case 7:
3032
- _c.trys.push([7, 9, , 10]);
3010
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3011
+ case 9:
3012
+ if (!(_c.sent())) {
3013
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3014
+ }
3015
+ _c.label = 10;
3016
+ case 10:
3017
+ _c.trys.push([10, 12, , 13]);
3033
3018
  return [4, this.program.methods
3034
3019
  .swapAndOpen({
3035
3020
  amountIn: amountIn,
@@ -3065,15 +3050,15 @@ var PerpetualsClient = (function () {
3065
3050
  })
3066
3051
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3067
3052
  .instruction()];
3068
- case 8:
3053
+ case 11:
3069
3054
  inx = _c.sent();
3070
3055
  instructions.push(inx);
3071
- return [3, 10];
3072
- case 9:
3056
+ return [3, 13];
3057
+ case 12:
3073
3058
  err_3 = _c.sent();
3074
3059
  console.error("perpClient SwapAndOpen error:: ", err_3);
3075
3060
  throw err_3;
3076
- case 10: return [2, {
3061
+ case 13: return [2, {
3077
3062
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3078
3063
  additionalSigners: additionalSigners
3079
3064
  }];
@@ -3086,7 +3071,7 @@ var PerpetualsClient = (function () {
3086
3071
  for (var _i = 7; _i < arguments.length; _i++) {
3087
3072
  args_1[_i - 7] = arguments[_i];
3088
3073
  }
3089
- return __awaiter(_this, __spreadArray([targetTokenSymbol_1, userOutputTokenSymbol_1, collateralTokenSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetTokenSymbol, userOutputTokenSymbol, collateralTokenSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, ephemeralSignerPubkey, userPublicKey) {
3074
+ return __awaiter(_this, __spreadArray([targetTokenSymbol_1, userOutputTokenSymbol_1, collateralTokenSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetTokenSymbol, userOutputTokenSymbol, collateralTokenSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, ephemeralSignerPubkey) {
3090
3075
  var publicKey, userOutputCustodyConfig, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, collateralToken, userOutputToken, lamports, userCollateralTokenAccount, inx, err_4;
3091
3076
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3092
3077
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -3094,7 +3079,7 @@ var PerpetualsClient = (function () {
3094
3079
  return __generator(this, function (_a) {
3095
3080
  switch (_a.label) {
3096
3081
  case 0:
3097
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3082
+ publicKey = this.provider.wallet.publicKey;
3098
3083
  userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey); });
3099
3084
  if (!userOutputCustodyConfig) {
3100
3085
  throw "userOutputCustodyConfig not found";
@@ -3118,37 +3103,46 @@ var PerpetualsClient = (function () {
3118
3103
  additionalSigners = [];
3119
3104
  collateralToken = poolConfig.getTokenFromSymbol(collateralTokenSymbol);
3120
3105
  userOutputToken = poolConfig.getTokenFromSymbol(userOutputTokenSymbol);
3121
- if (userOutputTokenSymbol == 'SOL') {
3122
- console.log("outputSymbol === SOL", userOutputTokenSymbol);
3123
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
3124
- if (!ephemeralSignerPubkey) {
3125
- wrappedSolAccount = new web3_js_1.Keypair();
3126
- additionalSigners.push(wrappedSolAccount);
3127
- }
3128
- preInstructions = [
3129
- web3_js_1.SystemProgram.createAccount({
3130
- fromPubkey: publicKey,
3131
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3132
- lamports: lamports,
3133
- space: 165,
3134
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3135
- }),
3136
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3137
- ];
3138
- postInstructions = [
3139
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3140
- ];
3106
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 1];
3107
+ console.log("outputSymbol === SOL", userOutputTokenSymbol);
3108
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
3109
+ if (!ephemeralSignerPubkey) {
3110
+ wrappedSolAccount = new web3_js_1.Keypair();
3141
3111
  additionalSigners.push(wrappedSolAccount);
3142
3112
  }
3143
- else {
3144
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(userOutputToken.mintKey, publicKey, true, userOutputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3145
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, userOutputToken.mintKey, userOutputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3113
+ preInstructions = [
3114
+ web3_js_1.SystemProgram.createAccount({
3115
+ fromPubkey: publicKey,
3116
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3117
+ lamports: lamports,
3118
+ space: 165,
3119
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3120
+ }),
3121
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3122
+ ];
3123
+ postInstructions = [
3124
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3125
+ ];
3126
+ additionalSigners.push(wrappedSolAccount);
3127
+ return [3, 3];
3128
+ case 1:
3129
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(userOutputToken.mintKey, publicKey, true, userOutputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3130
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3131
+ case 2:
3132
+ if (!(_a.sent())) {
3133
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, userOutputToken.mintKey, userOutputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3146
3134
  }
3135
+ _a.label = 3;
3136
+ case 3:
3147
3137
  userCollateralTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralToken.mintKey, publicKey, true, collateralToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3148
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, collateralToken.mintKey, collateralToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3149
- _a.label = 1;
3150
- case 1:
3151
- _a.trys.push([1, 3, , 4]);
3138
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3139
+ case 4:
3140
+ if (!(_a.sent())) {
3141
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userCollateralTokenAccount, publicKey, collateralToken.mintKey, collateralToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3142
+ }
3143
+ _a.label = 5;
3144
+ case 5:
3145
+ _a.trys.push([5, 7, , 8]);
3152
3146
  return [4, this.program.methods
3153
3147
  .closeAndSwap({
3154
3148
  priceWithSlippage: priceWithSlippage,
@@ -3182,15 +3176,15 @@ var PerpetualsClient = (function () {
3182
3176
  })
3183
3177
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3184
3178
  .instruction()];
3185
- case 2:
3179
+ case 6:
3186
3180
  inx = _a.sent();
3187
3181
  instructions.push(inx);
3188
- return [3, 4];
3189
- case 3:
3182
+ return [3, 8];
3183
+ case 7:
3190
3184
  err_4 = _a.sent();
3191
3185
  console.error("perpClient CloseAndSwap error:: ", err_4);
3192
3186
  throw err_4;
3193
- case 4: return [2, {
3187
+ case 8: return [2, {
3194
3188
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3195
3189
  additionalSigners: additionalSigners
3196
3190
  }];
@@ -3203,14 +3197,14 @@ var PerpetualsClient = (function () {
3203
3197
  for (var _i = 6; _i < arguments.length; _i++) {
3204
3198
  args_1[_i - 6] = arguments[_i];
3205
3199
  }
3206
- return __awaiter(_this, __spreadArray([collateralWithFee_1, targetSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (collateralWithFee, targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
3200
+ return __awaiter(_this, __spreadArray([collateralWithFee_1, targetSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (collateralWithFee, targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey) {
3207
3201
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, instruction;
3208
3202
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3209
3203
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3210
3204
  return __generator(this, function (_c) {
3211
3205
  switch (_c.label) {
3212
3206
  case 0:
3213
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3207
+ publicKey = this.provider.wallet.publicKey;
3214
3208
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3215
3209
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3216
3210
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -3306,14 +3300,14 @@ var PerpetualsClient = (function () {
3306
3300
  for (var _i = 7; _i < arguments.length; _i++) {
3307
3301
  args_1[_i - 7] = arguments[_i];
3308
3302
  }
3309
- return __awaiter(_this, __spreadArray([targetSymbol_1, inputSymbol_1, collateralSymbol_1, amountIn_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, inputSymbol, collateralSymbol, amountIn, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
3303
+ return __awaiter(_this, __spreadArray([targetSymbol_1, inputSymbol_1, collateralSymbol_1, amountIn_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, inputSymbol, collateralSymbol, amountIn, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey) {
3310
3304
  var publicKey, collateralCustodyConfig, targetCustodyConfig, inputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userCollateralTokenAccount, marketAccount, instruction;
3311
3305
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3312
3306
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3313
3307
  return __generator(this, function (_c) {
3314
3308
  switch (_c.label) {
3315
3309
  case 0:
3316
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3310
+ publicKey = this.provider.wallet.publicKey;
3317
3311
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3318
3312
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3319
3313
  inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inputSymbol).mintKey); });
@@ -3377,7 +3371,11 @@ var PerpetualsClient = (function () {
3377
3371
  _c.label = 6;
3378
3372
  case 6:
3379
3373
  userCollateralTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralCustodyConfig.mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3380
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, collateralCustodyConfig.mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3374
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3375
+ case 7:
3376
+ if (!(_c.sent())) {
3377
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userCollateralTokenAccount, publicKey, collateralCustodyConfig.mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3378
+ }
3381
3379
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3382
3380
  return [4, this.program.methods.swapAndAddCollateral({
3383
3381
  amountIn: amountIn,
@@ -3405,7 +3403,7 @@ var PerpetualsClient = (function () {
3405
3403
  fundingMint: poolConfig.getTokenFromSymbol(inputSymbol).mintKey,
3406
3404
  })
3407
3405
  .instruction()];
3408
- case 7:
3406
+ case 8:
3409
3407
  instruction = _c.sent();
3410
3408
  instructions.push(instruction);
3411
3409
  return [2, {
@@ -3421,15 +3419,15 @@ var PerpetualsClient = (function () {
3421
3419
  for (var _i = 6; _i < arguments.length; _i++) {
3422
3420
  args_1[_i - 6] = arguments[_i];
3423
3421
  }
3424
- return __awaiter(_this, __spreadArray([collateralDeltaUsd_1, marketSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (collateralDeltaUsd, marketSymbol, collateralSymbol, side, positionPubKey, poolConfig, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey, userPublicKey) {
3425
- var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, marketAccount, instruction, closeWsolATAIns, error_2;
3422
+ return __awaiter(_this, __spreadArray([collateralDeltaUsd_1, marketSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (collateralDeltaUsd, marketSymbol, collateralSymbol, side, positionPubKey, poolConfig, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey) {
3423
+ var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, marketAccount, instruction, closeWsolATAIns, error_2;
3426
3424
  if (createUserATA === void 0) { createUserATA = true; }
3427
3425
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3428
3426
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3429
- return __generator(this, function (_a) {
3430
- switch (_a.label) {
3427
+ return __generator(this, function (_b) {
3428
+ switch (_b.label) {
3431
3429
  case 0:
3432
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3430
+ publicKey = this.provider.wallet.publicKey;
3433
3431
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3434
3432
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3435
3433
  });
@@ -3443,35 +3441,45 @@ var PerpetualsClient = (function () {
3443
3441
  instructions = [];
3444
3442
  postInstructions = [];
3445
3443
  additionalSigners = [];
3446
- _a.label = 1;
3444
+ _b.label = 1;
3447
3445
  case 1:
3448
- _a.trys.push([1, 3, , 4]);
3446
+ _b.trys.push([1, 7, , 8]);
3449
3447
  console.log("removeCollateral -- collateralSymbol:", collateralSymbol);
3450
- if (collateralSymbol == 'SOL') {
3451
- console.log("remove collateral in SOL ...create WSOL temp and close it ");
3452
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3453
- if (!ephemeralSignerPubkey) {
3454
- wrappedSolAccount = new web3_js_1.Keypair();
3455
- additionalSigners.push(wrappedSolAccount);
3456
- }
3457
- preInstructions = [
3458
- web3_js_1.SystemProgram.createAccount({
3459
- fromPubkey: publicKey,
3460
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3461
- lamports: lamports,
3462
- space: 165,
3463
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3464
- }),
3465
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3466
- ];
3467
- postInstructions = [
3468
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3469
- ];
3448
+ if (!(collateralSymbol == 'SOL')) return [3, 2];
3449
+ console.log("remove collateral in SOL ...create WSOL temp and close it ");
3450
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3451
+ if (!ephemeralSignerPubkey) {
3452
+ wrappedSolAccount = new web3_js_1.Keypair();
3453
+ additionalSigners.push(wrappedSolAccount);
3470
3454
  }
3471
- else {
3472
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3473
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3455
+ preInstructions = [
3456
+ web3_js_1.SystemProgram.createAccount({
3457
+ fromPubkey: publicKey,
3458
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3459
+ lamports: lamports,
3460
+ space: 165,
3461
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3462
+ }),
3463
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3464
+ ];
3465
+ postInstructions = [
3466
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3467
+ ];
3468
+ return [3, 5];
3469
+ case 2:
3470
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3471
+ _a = createUserATA;
3472
+ if (!_a) return [3, 4];
3473
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3474
+ case 3:
3475
+ _a = !(_b.sent());
3476
+ _b.label = 4;
3477
+ case 4:
3478
+ if (_a) {
3479
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3474
3480
  }
3481
+ _b.label = 5;
3482
+ case 5:
3475
3483
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3476
3484
  return [4, this.program.methods
3477
3485
  .removeCollateral({
@@ -3497,19 +3505,19 @@ var PerpetualsClient = (function () {
3497
3505
  receivingMint: collateralCustodyConfig.mintKey
3498
3506
  })
3499
3507
  .instruction()];
3500
- case 2:
3501
- instruction = _a.sent();
3508
+ case 6:
3509
+ instruction = _b.sent();
3502
3510
  instructions.push(instruction);
3503
3511
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
3504
3512
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
3505
3513
  postInstructions.push(closeWsolATAIns);
3506
3514
  }
3507
- return [3, 4];
3508
- case 3:
3509
- error_2 = _a.sent();
3515
+ return [3, 8];
3516
+ case 7:
3517
+ error_2 = _b.sent();
3510
3518
  console.error("perpclient removeCollateral error:", error_2);
3511
3519
  throw error_2;
3512
- case 4: return [2, {
3520
+ case 8: return [2, {
3513
3521
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3514
3522
  additionalSigners: additionalSigners
3515
3523
  }];
@@ -3522,13 +3530,13 @@ var PerpetualsClient = (function () {
3522
3530
  for (var _i = 6; _i < arguments.length; _i++) {
3523
3531
  args_1[_i - 6] = arguments[_i];
3524
3532
  }
3525
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, outputSymbol_1, collateralDeltaUsd_1, side_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, outputSymbol, collateralDeltaUsd, side, poolConfig, ephemeralSignerPubkey, userPublicKey) {
3533
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, outputSymbol_1, collateralDeltaUsd_1, side_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, outputSymbol, collateralDeltaUsd, side, poolConfig, ephemeralSignerPubkey) {
3526
3534
  var publicKey, targetCustodyConfig, collateralCustodyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, instruction;
3527
3535
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3528
3536
  return __generator(this, function (_a) {
3529
3537
  switch (_a.label) {
3530
3538
  case 0:
3531
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3539
+ publicKey = this.provider.wallet.publicKey;
3532
3540
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3533
3541
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3534
3542
  outputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outputSymbol).mintKey); });
@@ -3539,33 +3547,42 @@ var PerpetualsClient = (function () {
3539
3547
  instructions = [];
3540
3548
  postInstructions = [];
3541
3549
  additionalSigners = [];
3542
- if (outputSymbol == 'SOL') {
3543
- console.log("outputSymbol === SOL", outputSymbol);
3544
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3545
- if (!ephemeralSignerPubkey) {
3546
- wrappedSolAccount = new web3_js_1.Keypair();
3547
- additionalSigners.push(wrappedSolAccount);
3548
- }
3549
- preInstructions = [
3550
- web3_js_1.SystemProgram.createAccount({
3551
- fromPubkey: publicKey,
3552
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3553
- lamports: lamports,
3554
- space: 165,
3555
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3556
- }),
3557
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3558
- ];
3559
- postInstructions = [
3560
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3561
- ];
3550
+ if (!(outputSymbol == 'SOL')) return [3, 1];
3551
+ console.log("outputSymbol === SOL", outputSymbol);
3552
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3553
+ if (!ephemeralSignerPubkey) {
3554
+ wrappedSolAccount = new web3_js_1.Keypair();
3555
+ additionalSigners.push(wrappedSolAccount);
3562
3556
  }
3563
- else {
3564
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(outputSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(outputSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3565
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(outputSymbol).mintKey, poolConfig.getTokenFromSymbol(outputSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3557
+ preInstructions = [
3558
+ web3_js_1.SystemProgram.createAccount({
3559
+ fromPubkey: publicKey,
3560
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3561
+ lamports: lamports,
3562
+ space: 165,
3563
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3564
+ }),
3565
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3566
+ ];
3567
+ postInstructions = [
3568
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3569
+ ];
3570
+ return [3, 3];
3571
+ case 1:
3572
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(outputSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(outputSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3573
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3574
+ case 2:
3575
+ if (!(_a.sent())) {
3576
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(outputSymbol).mintKey, poolConfig.getTokenFromSymbol(outputSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3566
3577
  }
3578
+ _a.label = 3;
3579
+ case 3:
3567
3580
  userCollateralTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3568
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3581
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3582
+ case 4:
3583
+ if (!(_a.sent())) {
3584
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userCollateralTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3585
+ }
3569
3586
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3570
3587
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3571
3588
  return [4, this.program.methods
@@ -3599,7 +3616,7 @@ var PerpetualsClient = (function () {
3599
3616
  collateralTokenProgram: poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID
3600
3617
  })
3601
3618
  .instruction()];
3602
- case 1:
3619
+ case 5:
3603
3620
  instruction = _a.sent();
3604
3621
  instructions.push(instruction);
3605
3622
  return [2, {
@@ -3615,14 +3632,14 @@ var PerpetualsClient = (function () {
3615
3632
  for (var _i = 8; _i < arguments.length; _i++) {
3616
3633
  args_1[_i - 8] = arguments[_i];
3617
3634
  }
3618
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, positionPubKey_1, side_1, poolConfig_1, priceWithSlippage_1, sizeDelta_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, positionPubKey, side, poolConfig, priceWithSlippage, sizeDelta, privilege, tokenStakeAccount, userReferralAccount, userPublicKey) {
3635
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, positionPubKey_1, side_1, poolConfig_1, priceWithSlippage_1, sizeDelta_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, positionPubKey, side, poolConfig, priceWithSlippage, sizeDelta, privilege, tokenStakeAccount, userReferralAccount) {
3619
3636
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3620
3637
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3621
3638
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3622
3639
  return __generator(this, function (_a) {
3623
3640
  switch (_a.label) {
3624
3641
  case 0:
3625
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3642
+ publicKey = this.provider.wallet.publicKey;
3626
3643
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3627
3644
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3628
3645
  });
@@ -3679,14 +3696,14 @@ var PerpetualsClient = (function () {
3679
3696
  for (var _i = 8; _i < arguments.length; _i++) {
3680
3697
  args_1[_i - 8] = arguments[_i];
3681
3698
  }
3682
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1, priceWithSlippage_1, sizeDelta_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, priceWithSlippage, sizeDelta, privilege, tokenStakeAccount, userReferralAccount, userPublicKey) {
3699
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1, priceWithSlippage_1, sizeDelta_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, priceWithSlippage, sizeDelta, privilege, tokenStakeAccount, userReferralAccount) {
3683
3700
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3684
3701
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3685
3702
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3686
3703
  return __generator(this, function (_a) {
3687
3704
  switch (_a.label) {
3688
3705
  case 0:
3689
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3706
+ publicKey = this.provider.wallet.publicKey;
3690
3707
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3691
3708
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3692
3709
  });
@@ -3743,7 +3760,7 @@ var PerpetualsClient = (function () {
3743
3760
  for (var _i = 4; _i < arguments.length; _i++) {
3744
3761
  args_1[_i - 4] = arguments[_i];
3745
3762
  }
3746
- return __awaiter(_this, __spreadArray([payTokenSymbol_1, tokenAmountIn_1, minLpAmountOut_1, poolConfig_1], args_1, true), void 0, function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, isWhitelistedUser, userPublicKey) {
3763
+ return __awaiter(_this, __spreadArray([payTokenSymbol_1, tokenAmountIn_1, minLpAmountOut_1, poolConfig_1], args_1, true), void 0, function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, isWhitelistedUser) {
3747
3764
  var publicKey, payTokenCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, payToken, userPayingTokenAccount, lpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, lamports, unWrappedSolBalance, _e, tokenAccountBalance, _f, whitelistPda, whitelistMeta, instruction, err_5;
3748
3765
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3749
3766
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
@@ -3751,7 +3768,7 @@ var PerpetualsClient = (function () {
3751
3768
  return __generator(this, function (_g) {
3752
3769
  switch (_g.label) {
3753
3770
  case 0:
3754
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3771
+ publicKey = this.provider.wallet.publicKey;
3755
3772
  payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
3756
3773
  if (!payTokenCustodyConfig) {
3757
3774
  throw "payTokenCustodyConfig not found";
@@ -3763,7 +3780,7 @@ var PerpetualsClient = (function () {
3763
3780
  payToken = poolConfig.getTokenFromSymbol(payTokenSymbol);
3764
3781
  _g.label = 1;
3765
3782
  case 1:
3766
- _g.trys.push([1, 9, , 10]);
3783
+ _g.trys.push([1, 10, , 11]);
3767
3784
  userPayingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(payTokenCustodyConfig.mintKey, publicKey, true, payToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3768
3785
  lpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
3769
3786
  custodyAccountMetas = [];
@@ -3790,20 +3807,24 @@ var PerpetualsClient = (function () {
3790
3807
  isWritable: false,
3791
3808
  });
3792
3809
  }
3793
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3794
- if (!(payTokenSymbol == 'SOL')) return [3, 4];
3810
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3811
+ case 2:
3812
+ if (!(_g.sent())) {
3813
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3814
+ }
3815
+ if (!(payTokenSymbol == 'SOL')) return [3, 5];
3795
3816
  console.log("payTokenSymbol === SOL", payTokenSymbol);
3796
3817
  lamports = tokenAmountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3797
- if (!!skipBalanceChecks) return [3, 3];
3818
+ if (!!skipBalanceChecks) return [3, 4];
3798
3819
  _e = anchor_1.BN.bind;
3799
3820
  return [4, this.provider.connection.getBalance(publicKey)];
3800
- case 2:
3821
+ case 3:
3801
3822
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _g.sent()]))();
3802
3823
  if (unWrappedSolBalance.lt(lamports)) {
3803
3824
  throw "Insufficient SOL Funds";
3804
3825
  }
3805
- _g.label = 3;
3806
- case 3:
3826
+ _g.label = 4;
3827
+ case 4:
3807
3828
  if (!ephemeralSignerPubkey) {
3808
3829
  wrappedSolAccount = new web3_js_1.Keypair();
3809
3830
  additionalSigners.push(wrappedSolAccount);
@@ -3821,23 +3842,23 @@ var PerpetualsClient = (function () {
3821
3842
  postInstructions = [
3822
3843
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3823
3844
  ];
3824
- return [3, 7];
3825
- case 4:
3826
- if (!!skipBalanceChecks) return [3, 7];
3827
- return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3845
+ return [3, 8];
3828
3846
  case 5:
3847
+ if (!!skipBalanceChecks) return [3, 8];
3848
+ return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3849
+ case 6:
3829
3850
  if (!(_g.sent())) {
3830
3851
  throw "Insufficient Funds , token Account doesn't exist";
3831
3852
  }
3832
3853
  _f = anchor_1.BN.bind;
3833
3854
  return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
3834
- case 6:
3855
+ case 7:
3835
3856
  tokenAccountBalance = new (_f.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3836
3857
  if (tokenAccountBalance.lt(tokenAmountIn)) {
3837
3858
  throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
3838
3859
  }
3839
- _g.label = 7;
3840
- case 7:
3860
+ _g.label = 8;
3861
+ case 8:
3841
3862
  whitelistPda = this.findProgramAddress("whitelist", [publicKey]).publicKey;
3842
3863
  whitelistMeta = {
3843
3864
  pubkey: whitelistPda,
@@ -3869,15 +3890,15 @@ var PerpetualsClient = (function () {
3869
3890
  })
3870
3891
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3871
3892
  .instruction()];
3872
- case 8:
3893
+ case 9:
3873
3894
  instruction = _g.sent();
3874
3895
  instructions.push(instruction);
3875
- return [3, 10];
3876
- case 9:
3896
+ return [3, 11];
3897
+ case 10:
3877
3898
  err_5 = _g.sent();
3878
3899
  console.error("perpClient addLiquidity error:: ", err_5);
3879
3900
  throw err_5;
3880
- case 10: return [2, {
3901
+ case 11: return [2, {
3881
3902
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3882
3903
  additionalSigners: additionalSigners
3883
3904
  }];
@@ -3910,7 +3931,7 @@ var PerpetualsClient = (function () {
3910
3931
  inputToken = poolConfig.getTokenFromSymbol(inputSymbol);
3911
3932
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
3912
3933
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
3913
- if (!(inputSymbol == 'SOL')) return [3, 3];
3934
+ if (!(inputSymbol == 'SOL')) return [3, 4];
3914
3935
  console.log("inputSymbol === SOL", inputSymbol);
3915
3936
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3916
3937
  console.log("lamports:", lamports.toNumber());
@@ -3923,8 +3944,11 @@ var PerpetualsClient = (function () {
3923
3944
  throw "Insufficient SOL Funds";
3924
3945
  }
3925
3946
  _g.label = 2;
3926
- case 2:
3927
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3947
+ case 2: return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3948
+ case 3:
3949
+ if (!(_g.sent())) {
3950
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3951
+ }
3928
3952
  if (!ephemeralSignerPubkey) {
3929
3953
  wrappedSolAccount = new web3_js_1.Keypair();
3930
3954
  additionalSigners.push(wrappedSolAccount);
@@ -3942,24 +3966,24 @@ var PerpetualsClient = (function () {
3942
3966
  postInstructions = [
3943
3967
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3944
3968
  ];
3945
- return [3, 6];
3946
- case 3:
3969
+ return [3, 7];
3970
+ case 4:
3947
3971
  userInputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(inputCustodyConfig.mintKey, publicKey, true, inputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3948
- if (!!skipBalanceChecks) return [3, 6];
3972
+ if (!!skipBalanceChecks) return [3, 7];
3949
3973
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3950
- case 4:
3974
+ case 5:
3951
3975
  if (!(_g.sent())) {
3952
3976
  throw "Insufficient Funds , token Account doesn't exist";
3953
3977
  }
3954
3978
  _b = anchor_1.BN.bind;
3955
3979
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3956
- case 5:
3980
+ case 6:
3957
3981
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3958
3982
  if (tokenAccountBalance.lt(amountIn)) {
3959
3983
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3960
3984
  }
3961
- _g.label = 6;
3962
- case 6:
3985
+ _g.label = 7;
3986
+ case 7:
3963
3987
  custodyAccountMetas = [];
3964
3988
  custodyOracleAccountMetas = [];
3965
3989
  markets = [];
@@ -4016,7 +4040,7 @@ var PerpetualsClient = (function () {
4016
4040
  })
4017
4041
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4018
4042
  .instruction()];
4019
- case 7:
4043
+ case 8:
4020
4044
  instruction = _g.sent();
4021
4045
  instructions.push(instruction);
4022
4046
  return [2, {
@@ -4499,12 +4523,12 @@ var PerpetualsClient = (function () {
4499
4523
  }
4500
4524
  });
4501
4525
  }); };
4502
- this.unstakeRequest = function (unstakeAmount, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
4526
+ this.unstakeRequest = function (unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4503
4527
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_13;
4504
4528
  return __generator(this, function (_a) {
4505
4529
  switch (_a.label) {
4506
4530
  case 0:
4507
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4531
+ publicKey = this.provider.wallet.publicKey;
4508
4532
  preInstructions = [];
4509
4533
  instructions = [];
4510
4534
  postInstructions = [];
@@ -4623,10 +4647,10 @@ var PerpetualsClient = (function () {
4623
4647
  args_1[_i - 3] = arguments[_i];
4624
4648
  }
4625
4649
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4626
- var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4650
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4627
4651
  if (createUserATA === void 0) { createUserATA = true; }
4628
- return __generator(this, function (_a) {
4629
- switch (_a.label) {
4652
+ return __generator(this, function (_b) {
4653
+ switch (_b.label) {
4630
4654
  case 0:
4631
4655
  publicKey = this.provider.wallet.publicKey;
4632
4656
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
@@ -4635,13 +4659,22 @@ var PerpetualsClient = (function () {
4635
4659
  instructions = [];
4636
4660
  postInstructions = [];
4637
4661
  additionalSigners = [];
4638
- _a.label = 1;
4662
+ _b.label = 1;
4639
4663
  case 1:
4640
- _a.trys.push([1, 3, , 4]);
4664
+ _b.trys.push([1, 5, , 6]);
4641
4665
  pool = poolConfig.poolAddress;
4642
4666
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4643
4667
  receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
4644
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
4668
+ _a = createUserATA;
4669
+ if (!_a) return [3, 3];
4670
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
4671
+ case 2:
4672
+ _a = !(_b.sent());
4673
+ _b.label = 3;
4674
+ case 3:
4675
+ if (_a) {
4676
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint));
4677
+ }
4645
4678
  tokenStakeAccounts = [];
4646
4679
  if (tokenStakeAccount) {
4647
4680
  tokenStakeAccounts.push({
@@ -4670,15 +4703,15 @@ var PerpetualsClient = (function () {
4670
4703
  })
4671
4704
  .remainingAccounts(__spreadArray([], tokenStakeAccounts, true))
4672
4705
  .instruction()];
4673
- case 2:
4674
- withdrawStakeInstruction = _a.sent();
4706
+ case 4:
4707
+ withdrawStakeInstruction = _b.sent();
4675
4708
  instructions.push(withdrawStakeInstruction);
4676
- return [3, 4];
4677
- case 3:
4678
- err_15 = _a.sent();
4709
+ return [3, 6];
4710
+ case 5:
4711
+ err_15 = _b.sent();
4679
4712
  console.log("perpClient withdrawStake error:: ", err_15);
4680
4713
  throw err_15;
4681
- case 4: return [2, {
4714
+ case 6: return [2, {
4682
4715
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4683
4716
  additionalSigners: additionalSigners
4684
4717
  }];
@@ -4737,21 +4770,29 @@ var PerpetualsClient = (function () {
4737
4770
  isWritable: false,
4738
4771
  });
4739
4772
  }
4740
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4741
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4742
- if (!(inTokenSymbol == 'SOL')) return [3, 3];
4773
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
4774
+ case 1:
4775
+ if (!(_f.sent())) {
4776
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4777
+ }
4778
+ return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
4779
+ case 2:
4780
+ if (!(_f.sent())) {
4781
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
4782
+ }
4783
+ if (!(inTokenSymbol == 'SOL')) return [3, 5];
4743
4784
  console.log("inTokenSymbol === SOL", inTokenSymbol);
4744
4785
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
4745
- if (!!skipBalanceChecks) return [3, 2];
4786
+ if (!!skipBalanceChecks) return [3, 4];
4746
4787
  _e = anchor_1.BN.bind;
4747
4788
  return [4, this.provider.connection.getBalance(publicKey)];
4748
- case 1:
4789
+ case 3:
4749
4790
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _f.sent()]))();
4750
4791
  if (unWrappedSolBalance.lt(lamports)) {
4751
4792
  throw "Insufficient SOL Funds";
4752
4793
  }
4753
- _f.label = 2;
4754
- case 2:
4794
+ _f.label = 4;
4795
+ case 4:
4755
4796
  if (!ephemeralSignerPubkey) {
4756
4797
  wrappedSolAccount = new web3_js_1.Keypair();
4757
4798
  additionalSigners.push(wrappedSolAccount);
@@ -4769,17 +4810,17 @@ var PerpetualsClient = (function () {
4769
4810
  postInstructions = [
4770
4811
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4771
4812
  ];
4772
- return [3, 5];
4773
- case 3:
4774
- if (!!skipBalanceChecks) return [3, 5];
4813
+ return [3, 7];
4814
+ case 5:
4815
+ if (!!skipBalanceChecks) return [3, 7];
4775
4816
  return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
4776
- case 4:
4817
+ case 6:
4777
4818
  if (!(_f.sent())) {
4778
4819
  throw "Insufficient Funds , token Account doesn't exist";
4779
4820
  }
4780
- _f.label = 5;
4781
- case 5:
4782
- _f.trys.push([5, 7, , 8]);
4821
+ _f.label = 7;
4822
+ case 7:
4823
+ _f.trys.push([7, 9, , 10]);
4783
4824
  if (enableHeapSizeIx) {
4784
4825
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4785
4826
  bytes: 64 * 1024,
@@ -4821,15 +4862,15 @@ var PerpetualsClient = (function () {
4821
4862
  })
4822
4863
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4823
4864
  .instruction()];
4824
- case 6:
4865
+ case 8:
4825
4866
  addCompoundingLiquidity = _f.sent();
4826
4867
  instructions.push(addCompoundingLiquidity);
4827
- return [3, 8];
4828
- case 7:
4868
+ return [3, 10];
4869
+ case 9:
4829
4870
  err_16 = _f.sent();
4830
4871
  console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4831
- return [3, 8];
4832
- case 8: return [2, {
4872
+ return [3, 10];
4873
+ case 10: return [2, {
4833
4874
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4834
4875
  additionalSigners: additionalSigners
4835
4876
  }];
@@ -4843,14 +4884,14 @@ var PerpetualsClient = (function () {
4843
4884
  args_1[_i - 5] = arguments[_i];
4844
4885
  }
4845
4886
  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, enableHeapSizeIx, isWhitelistedUser) {
4846
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, outCustodyConfig, lpTokenMint, compoundingTokenMint, lamports, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, compoundingTokenAccount, heapSizeIx, whitelistPda, whitelistMeta, removeCompoundingLiquidity, err_17;
4887
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, outCustodyConfig, lpTokenMint, compoundingTokenMint, lamports, _a, custodyAccountMetas, custodyOracleAccountMetas, markets, _b, _c, custody, _d, _e, market, compoundingTokenAccount, heapSizeIx, whitelistPda, whitelistMeta, removeCompoundingLiquidity, err_17;
4847
4888
  if (createUserATA === void 0) { createUserATA = true; }
4848
4889
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4849
4890
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4850
4891
  if (enableHeapSizeIx === void 0) { enableHeapSizeIx = true; }
4851
4892
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
4852
- return __generator(this, function (_e) {
4853
- switch (_e.label) {
4893
+ return __generator(this, function (_f) {
4894
+ switch (_f.label) {
4854
4895
  case 0:
4855
4896
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4856
4897
  preInstructions = [];
@@ -4861,35 +4902,45 @@ var PerpetualsClient = (function () {
4861
4902
  outCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outTokenSymbol).mintKey); });
4862
4903
  lpTokenMint = poolConfig.stakedLpTokenMint;
4863
4904
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4864
- if (outCustodyConfig.symbol == 'SOL') {
4865
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4866
- if (!ephemeralSignerPubkey) {
4867
- wrappedSolAccount = new web3_js_1.Keypair();
4868
- additionalSigners.push(wrappedSolAccount);
4869
- }
4870
- preInstructions = [
4871
- web3_js_1.SystemProgram.createAccount({
4872
- fromPubkey: publicKey,
4873
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4874
- lamports: lamports,
4875
- space: 165,
4876
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4877
- }),
4878
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4879
- ];
4880
- postInstructions = [
4881
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4882
- ];
4905
+ if (!(outCustodyConfig.symbol == 'SOL')) return [3, 1];
4906
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
4907
+ if (!ephemeralSignerPubkey) {
4908
+ wrappedSolAccount = new web3_js_1.Keypair();
4909
+ additionalSigners.push(wrappedSolAccount);
4883
4910
  }
4884
- else {
4885
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(outCustodyConfig.mintKey, publicKey, true, poolConfig.getTokenFromSymbol(outTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
4886
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, outCustodyConfig.mintKey, poolConfig.getTokenFromSymbol(outTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
4911
+ preInstructions = [
4912
+ web3_js_1.SystemProgram.createAccount({
4913
+ fromPubkey: publicKey,
4914
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4915
+ lamports: lamports,
4916
+ space: 165,
4917
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
4918
+ }),
4919
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4920
+ ];
4921
+ postInstructions = [
4922
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4923
+ ];
4924
+ return [3, 4];
4925
+ case 1:
4926
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(outCustodyConfig.mintKey, publicKey, true, poolConfig.getTokenFromSymbol(outTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
4927
+ _a = createUserATA;
4928
+ if (!_a) return [3, 3];
4929
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
4930
+ case 2:
4931
+ _a = !(_f.sent());
4932
+ _f.label = 3;
4933
+ case 3:
4934
+ if (_a) {
4935
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, outCustodyConfig.mintKey, poolConfig.getTokenFromSymbol(outTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
4887
4936
  }
4937
+ _f.label = 4;
4938
+ case 4:
4888
4939
  custodyAccountMetas = [];
4889
4940
  custodyOracleAccountMetas = [];
4890
4941
  markets = [];
4891
- for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
4892
- custody = _b[_a];
4942
+ for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4943
+ custody = _c[_b];
4893
4944
  custodyAccountMetas.push({
4894
4945
  pubkey: custody.custodyAccount,
4895
4946
  isSigner: false,
@@ -4901,8 +4952,8 @@ var PerpetualsClient = (function () {
4901
4952
  isWritable: false,
4902
4953
  });
4903
4954
  }
4904
- for (_c = 0, _d = poolConfig.markets; _c < _d.length; _c++) {
4905
- market = _d[_c];
4955
+ for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4956
+ market = _e[_d];
4906
4957
  markets.push({
4907
4958
  pubkey: market.marketAccount,
4908
4959
  isSigner: false,
@@ -4910,9 +4961,9 @@ var PerpetualsClient = (function () {
4910
4961
  });
4911
4962
  }
4912
4963
  compoundingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4913
- _e.label = 1;
4914
- case 1:
4915
- _e.trys.push([1, 3, , 4]);
4964
+ _f.label = 5;
4965
+ case 5:
4966
+ _f.trys.push([5, 7, , 8]);
4916
4967
  if (enableHeapSizeIx) {
4917
4968
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4918
4969
  bytes: 64 * 1024,
@@ -4954,15 +5005,15 @@ var PerpetualsClient = (function () {
4954
5005
  })
4955
5006
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4956
5007
  .instruction()];
4957
- case 2:
4958
- removeCompoundingLiquidity = _e.sent();
5008
+ case 6:
5009
+ removeCompoundingLiquidity = _f.sent();
4959
5010
  instructions.push(removeCompoundingLiquidity);
4960
- return [3, 4];
4961
- case 3:
4962
- err_17 = _e.sent();
5011
+ return [3, 8];
5012
+ case 7:
5013
+ err_17 = _f.sent();
4963
5014
  console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
4964
- return [3, 4];
4965
- case 4: return [2, {
5015
+ return [3, 8];
5016
+ case 8: return [2, {
4966
5017
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4967
5018
  additionalSigners: additionalSigners
4968
5019
  }];
@@ -4975,13 +5026,13 @@ var PerpetualsClient = (function () {
4975
5026
  for (var _i = 3; _i < arguments.length; _i++) {
4976
5027
  args_1[_i - 3] = arguments[_i];
4977
5028
  }
4978
- return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA, userPublicKey) {
4979
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, tokenStakeAccount, tokenStakeAccounts, _a, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _b, _c, custody, _d, _e, market, migrateStake, err_18;
5029
+ return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA) {
5030
+ 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;
4980
5031
  if (createUserATA === void 0) { createUserATA = true; }
4981
- return __generator(this, function (_f) {
4982
- switch (_f.label) {
5032
+ return __generator(this, function (_g) {
5033
+ switch (_g.label) {
4983
5034
  case 0:
4984
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5035
+ publicKey = this.provider.wallet.publicKey;
4985
5036
  preInstructions = [];
4986
5037
  instructions = [];
4987
5038
  postInstructions = [];
@@ -4990,18 +5041,27 @@ var PerpetualsClient = (function () {
4990
5041
  lpTokenMint = poolConfig.stakedLpTokenMint;
4991
5042
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4992
5043
  compoudingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4993
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4994
- flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
4995
- tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
4996
- tokenStakeAccounts = [];
4997
- _a = tokenStakeAccount;
5044
+ _a = createUserATA;
4998
5045
  if (!_a) return [3, 2];
4999
- return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5046
+ return [4, (0, utils_1.checkIfAccountExists)(compoudingTokenAccount, this.provider.connection)];
5000
5047
  case 1:
5001
- _a = (_f.sent());
5002
- _f.label = 2;
5048
+ _a = !(_g.sent());
5049
+ _g.label = 2;
5003
5050
  case 2:
5004
5051
  if (_a) {
5052
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint));
5053
+ }
5054
+ flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
5055
+ tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
5056
+ tokenStakeAccounts = [];
5057
+ _b = tokenStakeAccount;
5058
+ if (!_b) return [3, 4];
5059
+ return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5060
+ case 3:
5061
+ _b = (_g.sent());
5062
+ _g.label = 4;
5063
+ case 4:
5064
+ if (_b) {
5005
5065
  tokenStakeAccounts.push({
5006
5066
  pubkey: tokenStakeAccount,
5007
5067
  isSigner: false,
@@ -5012,8 +5072,8 @@ var PerpetualsClient = (function () {
5012
5072
  custodyAccountMetas = [];
5013
5073
  custodyOracleAccountMetas = [];
5014
5074
  markets = [];
5015
- for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
5016
- custody = _c[_b];
5075
+ for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
5076
+ custody = _d[_c];
5017
5077
  custodyAccountMetas.push({
5018
5078
  pubkey: custody.custodyAccount,
5019
5079
  isSigner: false,
@@ -5025,17 +5085,17 @@ var PerpetualsClient = (function () {
5025
5085
  isWritable: false,
5026
5086
  });
5027
5087
  }
5028
- for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
5029
- market = _e[_d];
5088
+ for (_e = 0, _f = poolConfig.markets; _e < _f.length; _e++) {
5089
+ market = _f[_e];
5030
5090
  markets.push({
5031
5091
  pubkey: market.marketAccount,
5032
5092
  isSigner: false,
5033
5093
  isWritable: false,
5034
5094
  });
5035
5095
  }
5036
- _f.label = 3;
5037
- case 3:
5038
- _f.trys.push([3, 5, , 6]);
5096
+ _g.label = 5;
5097
+ case 5:
5098
+ _g.trys.push([5, 7, , 8]);
5039
5099
  return [4, this.program.methods
5040
5100
  .migrateStake({
5041
5101
  amount: amount
@@ -5060,15 +5120,15 @@ var PerpetualsClient = (function () {
5060
5120
  })
5061
5121
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), tokenStakeAccounts, true))
5062
5122
  .instruction()];
5063
- case 4:
5064
- migrateStake = _f.sent();
5123
+ case 6:
5124
+ migrateStake = _g.sent();
5065
5125
  instructions.push(migrateStake);
5066
- return [3, 6];
5067
- case 5:
5068
- err_18 = _f.sent();
5126
+ return [3, 8];
5127
+ case 7:
5128
+ err_18 = _g.sent();
5069
5129
  console.log("perpClient migrateStake error:: ", err_18);
5070
- return [3, 6];
5071
- case 6: return [2, {
5130
+ return [3, 8];
5131
+ case 8: return [2, {
5072
5132
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5073
5133
  additionalSigners: additionalSigners
5074
5134
  }];
@@ -5076,12 +5136,12 @@ var PerpetualsClient = (function () {
5076
5136
  });
5077
5137
  });
5078
5138
  };
5079
- this.migrateFlp = function (amount, rewardTokenMint, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
5139
+ this.migrateFlp = function (amount, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5080
5140
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_19;
5081
5141
  return __generator(this, function (_d) {
5082
5142
  switch (_d.label) {
5083
5143
  case 0:
5084
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5144
+ publicKey = this.provider.wallet.publicKey;
5085
5145
  preInstructions = [];
5086
5146
  instructions = [];
5087
5147
  postInstructions = [];
@@ -5245,10 +5305,14 @@ var PerpetualsClient = (function () {
5245
5305
  additionalSigners = [];
5246
5306
  _a.label = 1;
5247
5307
  case 1:
5248
- _a.trys.push([1, 3, , 4]);
5308
+ _a.trys.push([1, 4, , 5]);
5249
5309
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5250
5310
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5251
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5311
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5312
+ case 2:
5313
+ if (!(_a.sent())) {
5314
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5315
+ }
5252
5316
  return [4, this.program.methods
5253
5317
  .depositTokenStake({
5254
5318
  depositAmount: depositAmount
@@ -5268,15 +5332,15 @@ var PerpetualsClient = (function () {
5268
5332
  tokenMint: poolConfig.tokenMint,
5269
5333
  })
5270
5334
  .instruction()];
5271
- case 2:
5335
+ case 3:
5272
5336
  depositTokenStakeInstruction = _a.sent();
5273
5337
  instructions.push(depositTokenStakeInstruction);
5274
- return [3, 4];
5275
- case 3:
5338
+ return [3, 5];
5339
+ case 4:
5276
5340
  err_21 = _a.sent();
5277
5341
  console.log("perpClient depositStakingInstruction error:: ", err_21);
5278
5342
  throw err_21;
5279
- case 4: return [2, {
5343
+ case 5: return [2, {
5280
5344
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5281
5345
  additionalSigners: additionalSigners
5282
5346
  }];
@@ -5334,10 +5398,14 @@ var PerpetualsClient = (function () {
5334
5398
  additionalSigners = [];
5335
5399
  _a.label = 1;
5336
5400
  case 1:
5337
- _a.trys.push([1, 3, , 4]);
5401
+ _a.trys.push([1, 4, , 5]);
5338
5402
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5339
5403
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5340
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5404
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5405
+ case 2:
5406
+ if (!(_a.sent())) {
5407
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5408
+ }
5341
5409
  return [4, this.program.methods
5342
5410
  .unstakeTokenInstant({
5343
5411
  unstakeAmount: unstakeAmount
@@ -5356,15 +5424,15 @@ var PerpetualsClient = (function () {
5356
5424
  tokenMint: poolConfig.tokenMint,
5357
5425
  })
5358
5426
  .instruction()];
5359
- case 2:
5427
+ case 3:
5360
5428
  unstakeTokenInstantInstruction = _a.sent();
5361
5429
  instructions.push(unstakeTokenInstantInstruction);
5362
- return [3, 4];
5363
- case 3:
5430
+ return [3, 5];
5431
+ case 4:
5364
5432
  err_23 = _a.sent();
5365
5433
  console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5366
5434
  throw err_23;
5367
- case 4: return [2, {
5435
+ case 5: return [2, {
5368
5436
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5369
5437
  additionalSigners: additionalSigners
5370
5438
  }];
@@ -5382,10 +5450,14 @@ var PerpetualsClient = (function () {
5382
5450
  additionalSigners = [];
5383
5451
  _a.label = 1;
5384
5452
  case 1:
5385
- _a.trys.push([1, 3, , 4]);
5453
+ _a.trys.push([1, 4, , 5]);
5386
5454
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5387
5455
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5388
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5456
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5457
+ case 2:
5458
+ if (!(_a.sent())) {
5459
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5460
+ }
5389
5461
  return [4, this.program.methods
5390
5462
  .withdrawToken({
5391
5463
  withdrawRequestId: withdrawRequestId
@@ -5404,15 +5476,15 @@ var PerpetualsClient = (function () {
5404
5476
  tokenMint: poolConfig.tokenMint,
5405
5477
  })
5406
5478
  .instruction()];
5407
- case 2:
5479
+ case 3:
5408
5480
  withdrawTokenInstruction = _a.sent();
5409
5481
  instructions.push(withdrawTokenInstruction);
5410
- return [3, 4];
5411
- case 3:
5482
+ return [3, 5];
5483
+ case 4:
5412
5484
  err_24 = _a.sent();
5413
5485
  console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5414
5486
  throw err_24;
5415
- case 4: return [2, {
5487
+ case 5: return [2, {
5416
5488
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5417
5489
  additionalSigners: additionalSigners
5418
5490
  }];
@@ -5464,23 +5536,32 @@ var PerpetualsClient = (function () {
5464
5536
  for (var _i = 2; _i < arguments.length; _i++) {
5465
5537
  args_1[_i - 2] = arguments[_i];
5466
5538
  }
5467
- return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA, userPublicKey) {
5468
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, collectTokenRewardInstruction, err_26;
5539
+ return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA) {
5540
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_26;
5469
5541
  if (createUserATA === void 0) { createUserATA = true; }
5470
- return __generator(this, function (_a) {
5471
- switch (_a.label) {
5542
+ return __generator(this, function (_b) {
5543
+ switch (_b.label) {
5472
5544
  case 0:
5473
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5545
+ publicKey = this.provider.wallet.publicKey;
5474
5546
  preInstructions = [];
5475
5547
  instructions = [];
5476
5548
  postInstructions = [];
5477
5549
  additionalSigners = [];
5478
- _a.label = 1;
5550
+ _b.label = 1;
5479
5551
  case 1:
5480
- _a.trys.push([1, 3, , 4]);
5552
+ _b.trys.push([1, 5, , 6]);
5481
5553
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5482
5554
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5483
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5555
+ _a = createUserATA;
5556
+ if (!_a) return [3, 3];
5557
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5558
+ case 2:
5559
+ _a = !(_b.sent());
5560
+ _b.label = 3;
5561
+ case 3:
5562
+ if (_a) {
5563
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint));
5564
+ }
5484
5565
  return [4, this.program.methods
5485
5566
  .collectTokenReward({})
5486
5567
  .accounts({
@@ -5497,15 +5578,15 @@ var PerpetualsClient = (function () {
5497
5578
  tokenMint: poolConfig.tokenMint,
5498
5579
  })
5499
5580
  .instruction()];
5500
- case 2:
5501
- collectTokenRewardInstruction = _a.sent();
5581
+ case 4:
5582
+ collectTokenRewardInstruction = _b.sent();
5502
5583
  instructions.push(collectTokenRewardInstruction);
5503
- return [3, 4];
5504
- case 3:
5505
- err_26 = _a.sent();
5584
+ return [3, 6];
5585
+ case 5:
5586
+ err_26 = _b.sent();
5506
5587
  console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5507
5588
  throw err_26;
5508
- case 4: return [2, {
5589
+ case 6: return [2, {
5509
5590
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5510
5591
  additionalSigners: additionalSigners
5511
5592
  }];
@@ -5519,23 +5600,32 @@ var PerpetualsClient = (function () {
5519
5600
  args_1[_i - 3] = arguments[_i];
5520
5601
  }
5521
5602
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5522
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, collectRevenueInstruction, err_27;
5603
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_27;
5523
5604
  if (createUserATA === void 0) { createUserATA = true; }
5524
- return __generator(this, function (_a) {
5525
- switch (_a.label) {
5605
+ return __generator(this, function (_b) {
5606
+ switch (_b.label) {
5526
5607
  case 0:
5527
5608
  publicKey = this.provider.wallet.publicKey;
5528
5609
  preInstructions = [];
5529
5610
  instructions = [];
5530
5611
  postInstructions = [];
5531
5612
  additionalSigners = [];
5532
- _a.label = 1;
5613
+ _b.label = 1;
5533
5614
  case 1:
5534
- _a.trys.push([1, 3, , 4]);
5615
+ _b.trys.push([1, 5, , 6]);
5535
5616
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5536
5617
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5537
5618
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, owner, true);
5538
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
5619
+ _a = createUserATA;
5620
+ if (!_a) return [3, 3];
5621
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5622
+ case 2:
5623
+ _a = !(_b.sent());
5624
+ _b.label = 3;
5625
+ case 3:
5626
+ if (_a) {
5627
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint));
5628
+ }
5539
5629
  return [4, this.program.methods
5540
5630
  .collectRevenue({})
5541
5631
  .accounts({
@@ -5552,15 +5642,15 @@ var PerpetualsClient = (function () {
5552
5642
  receivingTokenMint: rewardCustodyMint,
5553
5643
  })
5554
5644
  .instruction()];
5555
- case 2:
5556
- collectRevenueInstruction = _a.sent();
5645
+ case 4:
5646
+ collectRevenueInstruction = _b.sent();
5557
5647
  instructions.push(collectRevenueInstruction);
5558
- return [3, 4];
5559
- case 3:
5560
- err_27 = _a.sent();
5648
+ return [3, 6];
5649
+ case 5:
5650
+ err_27 = _b.sent();
5561
5651
  console.log("perpClient collectRevenueInstruction error:: ", err_27);
5562
5652
  throw err_27;
5563
- case 4: return [2, {
5653
+ case 6: return [2, {
5564
5654
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5565
5655
  additionalSigners: additionalSigners
5566
5656
  }];
@@ -5574,23 +5664,32 @@ var PerpetualsClient = (function () {
5574
5664
  args_1[_i - 3] = arguments[_i];
5575
5665
  }
5576
5666
  return __awaiter(_this, __spreadArray([owner_1, rebateSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rebateSymbol, poolConfig, createUserATA) {
5577
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, collectRebateInstruction, err_28;
5667
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, _a, collectRebateInstruction, err_28;
5578
5668
  if (createUserATA === void 0) { createUserATA = true; }
5579
- return __generator(this, function (_a) {
5580
- switch (_a.label) {
5669
+ return __generator(this, function (_b) {
5670
+ switch (_b.label) {
5581
5671
  case 0:
5582
5672
  publicKey = this.provider.wallet.publicKey;
5583
5673
  preInstructions = [];
5584
5674
  instructions = [];
5585
5675
  postInstructions = [];
5586
5676
  additionalSigners = [];
5587
- _a.label = 1;
5677
+ _b.label = 1;
5588
5678
  case 1:
5589
- _a.trys.push([1, 3, , 4]);
5679
+ _b.trys.push([1, 5, , 6]);
5590
5680
  rebateMint = poolConfig.getTokenFromSymbol(rebateSymbol).mintKey;
5591
5681
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5592
5682
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rebateMint, owner, true);
5593
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rebateMint, spl_token_1.TOKEN_PROGRAM_ID));
5683
+ _a = createUserATA;
5684
+ if (!_a) return [3, 3];
5685
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5686
+ case 2:
5687
+ _a = !(_b.sent());
5688
+ _b.label = 3;
5689
+ case 3:
5690
+ if (_a) {
5691
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rebateMint));
5692
+ }
5594
5693
  return [4, this.program.methods
5595
5694
  .collectRebate()
5596
5695
  .accounts({
@@ -5607,15 +5706,15 @@ var PerpetualsClient = (function () {
5607
5706
  receivingTokenMint: rebateMint,
5608
5707
  })
5609
5708
  .instruction()];
5610
- case 2:
5611
- collectRebateInstruction = _a.sent();
5709
+ case 4:
5710
+ collectRebateInstruction = _b.sent();
5612
5711
  instructions.push(collectRebateInstruction);
5613
- return [3, 4];
5614
- case 3:
5615
- err_28 = _a.sent();
5712
+ return [3, 6];
5713
+ case 5:
5714
+ err_28 = _b.sent();
5616
5715
  console.log("perpClient collectRebateInstruction error:: ", err_28);
5617
5716
  throw err_28;
5618
- case 4: return [2, {
5717
+ case 6: return [2, {
5619
5718
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5620
5719
  additionalSigners: additionalSigners
5621
5720
  }];
@@ -5675,14 +5774,14 @@ var PerpetualsClient = (function () {
5675
5774
  for (var _i = 11; _i < arguments.length; _i++) {
5676
5775
  args_1[_i - 11] = arguments[_i];
5677
5776
  }
5678
- 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, userPublicKey) {
5777
+ 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) {
5679
5778
  var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_30;
5680
5779
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5681
5780
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5682
5781
  return __generator(this, function (_c) {
5683
5782
  switch (_c.label) {
5684
5783
  case 0:
5685
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5784
+ publicKey = this.provider.wallet.publicKey;
5686
5785
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5687
5786
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5688
5787
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5798,14 +5897,14 @@ var PerpetualsClient = (function () {
5798
5897
  for (var _i = 11; _i < arguments.length; _i++) {
5799
5898
  args_1[_i - 11] = arguments[_i];
5800
5899
  }
5801
- 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, userPublicKey) {
5802
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, positionAccount, orderAccount, editLimitOrder, err_31;
5900
+ 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) {
5901
+ var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_31;
5803
5902
  if (createUserATA === void 0) { createUserATA = true; }
5804
5903
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5805
- return __generator(this, function (_a) {
5806
- switch (_a.label) {
5904
+ return __generator(this, function (_b) {
5905
+ switch (_b.label) {
5807
5906
  case 0:
5808
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5907
+ publicKey = this.provider.wallet.publicKey;
5809
5908
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5810
5909
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5811
5910
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5815,33 +5914,43 @@ var PerpetualsClient = (function () {
5815
5914
  instructions = [];
5816
5915
  postInstructions = [];
5817
5916
  additionalSigners = [];
5818
- _a.label = 1;
5917
+ _b.label = 1;
5819
5918
  case 1:
5820
- _a.trys.push([1, 3, , 4]);
5821
- if (reserveSymbol == 'SOL') {
5822
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
5823
- if (!ephemeralSignerPubkey) {
5824
- wrappedSolAccount = new web3_js_1.Keypair();
5825
- additionalSigners.push(wrappedSolAccount);
5826
- }
5827
- preInstructions = [
5828
- web3_js_1.SystemProgram.createAccount({
5829
- fromPubkey: publicKey,
5830
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5831
- lamports: lamports,
5832
- space: 165,
5833
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5834
- }),
5835
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5836
- ];
5837
- postInstructions = [
5838
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5839
- ];
5919
+ _b.trys.push([1, 7, , 8]);
5920
+ if (!(reserveSymbol == 'SOL')) return [3, 2];
5921
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
5922
+ if (!ephemeralSignerPubkey) {
5923
+ wrappedSolAccount = new web3_js_1.Keypair();
5924
+ additionalSigners.push(wrappedSolAccount);
5840
5925
  }
5841
- else {
5842
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(reserveSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
5843
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(reserveSymbol).mintKey, poolConfig.getTokenFromSymbol(reserveSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
5926
+ preInstructions = [
5927
+ web3_js_1.SystemProgram.createAccount({
5928
+ fromPubkey: publicKey,
5929
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5930
+ lamports: lamports,
5931
+ space: 165,
5932
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
5933
+ }),
5934
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5935
+ ];
5936
+ postInstructions = [
5937
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5938
+ ];
5939
+ return [3, 5];
5940
+ case 2:
5941
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(reserveSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
5942
+ _a = createUserATA;
5943
+ if (!_a) return [3, 4];
5944
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
5945
+ case 3:
5946
+ _a = !(_b.sent());
5947
+ _b.label = 4;
5948
+ case 4:
5949
+ if (_a) {
5950
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(reserveSymbol).mintKey, poolConfig.getTokenFromSymbol(reserveSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
5844
5951
  }
5952
+ _b.label = 5;
5953
+ case 5:
5845
5954
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
5846
5955
  orderAccount = poolConfig.getOrderFromMarketPk(publicKey, marketAccount);
5847
5956
  return [4, this.program.methods
@@ -5875,15 +5984,15 @@ var PerpetualsClient = (function () {
5875
5984
  receivingMint: poolConfig.getTokenFromSymbol(reserveSymbol).mintKey
5876
5985
  })
5877
5986
  .instruction()];
5878
- case 2:
5879
- editLimitOrder = _a.sent();
5987
+ case 6:
5988
+ editLimitOrder = _b.sent();
5880
5989
  instructions.push(editLimitOrder);
5881
- return [3, 4];
5882
- case 3:
5883
- err_31 = _a.sent();
5990
+ return [3, 8];
5991
+ case 7:
5992
+ err_31 = _b.sent();
5884
5993
  console.log("perpClient editLimitOrder error:: ", err_31);
5885
5994
  throw err_31;
5886
- case 4: return [2, {
5995
+ case 8: return [2, {
5887
5996
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5888
5997
  additionalSigners: additionalSigners
5889
5998
  }];
@@ -6032,12 +6141,12 @@ var PerpetualsClient = (function () {
6032
6141
  });
6033
6142
  });
6034
6143
  };
6035
- this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6144
+ this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6036
6145
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_34;
6037
6146
  return __generator(this, function (_a) {
6038
6147
  switch (_a.label) {
6039
6148
  case 0:
6040
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6149
+ publicKey = this.provider.wallet.publicKey;
6041
6150
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6042
6151
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6043
6152
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6091,12 +6200,12 @@ var PerpetualsClient = (function () {
6091
6200
  }
6092
6201
  });
6093
6202
  }); };
6094
- this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6203
+ this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6095
6204
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_35;
6096
6205
  return __generator(this, function (_a) {
6097
6206
  switch (_a.label) {
6098
6207
  case 0:
6099
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6208
+ publicKey = this.provider.wallet.publicKey;
6100
6209
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6101
6210
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6102
6211
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6149,12 +6258,12 @@ var PerpetualsClient = (function () {
6149
6258
  }
6150
6259
  });
6151
6260
  }); };
6152
- this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6261
+ this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6153
6262
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_36;
6154
6263
  return __generator(this, function (_a) {
6155
6264
  switch (_a.label) {
6156
6265
  case 0:
6157
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6266
+ publicKey = this.provider.wallet.publicKey;
6158
6267
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6159
6268
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6160
6269
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6193,12 +6302,12 @@ var PerpetualsClient = (function () {
6193
6302
  }
6194
6303
  });
6195
6304
  }); };
6196
- this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6305
+ this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6197
6306
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_37;
6198
6307
  return __generator(this, function (_a) {
6199
6308
  switch (_a.label) {
6200
6309
  case 0:
6201
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6310
+ publicKey = this.provider.wallet.publicKey;
6202
6311
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6203
6312
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6204
6313
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6241,13 +6350,13 @@ var PerpetualsClient = (function () {
6241
6350
  args_1[_i - 9] = arguments[_i];
6242
6351
  }
6243
6352
  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) {
6244
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, userReceivingTokenAccount, userReceivingTokenAccountCollateral, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, collateralToken, receivingToken, positionAccount, orderAccount, custodyAccountMetas, custodyOracleAccountMetas, _a, _b, custody, executeTriggerWithSwap, err_38;
6353
+ 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_38;
6245
6354
  if (createUserATA === void 0) { createUserATA = true; }
6246
6355
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6247
6356
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6248
6357
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6249
- return __generator(this, function (_c) {
6250
- switch (_c.label) {
6358
+ return __generator(this, function (_e) {
6359
+ switch (_e.label) {
6251
6360
  case 0:
6252
6361
  payerPubkey = this.provider.wallet.publicKey;
6253
6362
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6260,23 +6369,42 @@ var PerpetualsClient = (function () {
6260
6369
  additionalSigners = [];
6261
6370
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
6262
6371
  receivingToken = poolConfig.getTokenFromSymbol(receivingSymbol);
6263
- _c.label = 1;
6372
+ _e.label = 1;
6264
6373
  case 1:
6265
- _c.trys.push([1, 3, , 4]);
6266
- if (false) {
6374
+ _e.trys.push([1, 9, , 10]);
6375
+ if (!false) return [3, 2];
6376
+ return [3, 7];
6377
+ case 2:
6378
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, owner, true, receivingToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6379
+ _a = createUserATA;
6380
+ if (!_a) return [3, 4];
6381
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6382
+ case 3:
6383
+ _a = !(_e.sent());
6384
+ _e.label = 4;
6385
+ case 4:
6386
+ if (_a) {
6387
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey));
6267
6388
  }
6268
- else {
6269
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, owner, true, receivingToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6270
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, spl_token_1.TOKEN_PROGRAM_ID));
6271
- userReceivingTokenAccountCollateral = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, owner, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6272
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6389
+ userReceivingTokenAccountCollateral = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, owner, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6390
+ _b = createUserATA;
6391
+ if (!_b) return [3, 6];
6392
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccountCollateral, this.provider.connection)];
6393
+ case 5:
6394
+ _b = !(_e.sent());
6395
+ _e.label = 6;
6396
+ case 6:
6397
+ if (_b) {
6398
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
6273
6399
  }
6400
+ _e.label = 7;
6401
+ case 7:
6274
6402
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6275
6403
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6276
6404
  custodyAccountMetas = [];
6277
6405
  custodyOracleAccountMetas = [];
6278
- for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
6279
- custody = _b[_a];
6406
+ for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
6407
+ custody = _d[_c];
6280
6408
  custodyAccountMetas.push({
6281
6409
  pubkey: custody.custodyAccount,
6282
6410
  isSigner: false,
@@ -6323,15 +6451,15 @@ var PerpetualsClient = (function () {
6323
6451
  })
6324
6452
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6325
6453
  .instruction()];
6326
- case 2:
6327
- executeTriggerWithSwap = _c.sent();
6454
+ case 8:
6455
+ executeTriggerWithSwap = _e.sent();
6328
6456
  instructions.push(executeTriggerWithSwap);
6329
- return [3, 4];
6330
- case 3:
6331
- err_38 = _c.sent();
6457
+ return [3, 10];
6458
+ case 9:
6459
+ err_38 = _e.sent();
6332
6460
  console.log("perpClient executeTriggerWithSwap error:: ", err_38);
6333
6461
  throw err_38;
6334
- case 4: return [2, {
6462
+ case 10: return [2, {
6335
6463
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6336
6464
  additionalSigners: additionalSigners
6337
6465
  }];
@@ -6345,13 +6473,13 @@ var PerpetualsClient = (function () {
6345
6473
  args_1[_i - 8] = arguments[_i];
6346
6474
  }
6347
6475
  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) {
6348
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeTriggerOrder, err_39;
6476
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_39;
6349
6477
  if (createUserATA === void 0) { createUserATA = true; }
6350
6478
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6351
6479
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6352
6480
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6353
- return __generator(this, function (_a) {
6354
- switch (_a.label) {
6481
+ return __generator(this, function (_b) {
6482
+ switch (_b.label) {
6355
6483
  case 0:
6356
6484
  payerPubkey = this.provider.wallet.publicKey;
6357
6485
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6361,15 +6489,25 @@ var PerpetualsClient = (function () {
6361
6489
  instructions = [];
6362
6490
  postInstructions = [];
6363
6491
  additionalSigners = [];
6364
- _a.label = 1;
6492
+ _b.label = 1;
6365
6493
  case 1:
6366
- _a.trys.push([1, 3, , 4]);
6367
- if (false) {
6368
- }
6369
- else {
6370
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, owner, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6371
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6494
+ _b.trys.push([1, 7, , 8]);
6495
+ if (!false) return [3, 2];
6496
+ return [3, 5];
6497
+ case 2:
6498
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, owner, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6499
+ _a = createUserATA;
6500
+ if (!_a) return [3, 4];
6501
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6502
+ case 3:
6503
+ _a = !(_b.sent());
6504
+ _b.label = 4;
6505
+ case 4:
6506
+ if (_a) {
6507
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6372
6508
  }
6509
+ _b.label = 5;
6510
+ case 5:
6373
6511
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6374
6512
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6375
6513
  return [4, this.program.methods
@@ -6401,15 +6539,15 @@ var PerpetualsClient = (function () {
6401
6539
  })
6402
6540
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6403
6541
  .instruction()];
6404
- case 2:
6405
- executeTriggerOrder = _a.sent();
6542
+ case 6:
6543
+ executeTriggerOrder = _b.sent();
6406
6544
  instructions.push(executeTriggerOrder);
6407
- return [3, 4];
6408
- case 3:
6409
- err_39 = _a.sent();
6545
+ return [3, 8];
6546
+ case 7:
6547
+ err_39 = _b.sent();
6410
6548
  console.log("perpClient executeTriggerOrder error:: ", err_39);
6411
6549
  throw err_39;
6412
- case 4: return [2, {
6550
+ case 8: return [2, {
6413
6551
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6414
6552
  additionalSigners: additionalSigners
6415
6553
  }];
@@ -6422,16 +6560,16 @@ var PerpetualsClient = (function () {
6422
6560
  for (var _i = 5; _i < arguments.length; _i++) {
6423
6561
  args_1[_i - 5] = arguments[_i];
6424
6562
  }
6425
- 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, isWhitelistedUser, userPublicKey) {
6426
- var userInputCustodyConfig, userOutputCustodyConfig, publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userOutputTokenAccount, userInputTokenAccount, wsolAssociatedTokenAccount, unWrappedSolBalance, _a, wsolAssociatedTokenAccount, closeWsolATAIns, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, lamports, custodyAccountMetas, custodyOracleAccountMetas, _d, _e, custody, whitelistPda, whitelistMeta, params, inx, closeWsolATAIns, err_40;
6563
+ 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, isWhitelistedUser) {
6564
+ 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, whitelistPda, whitelistMeta, params, inx, closeWsolATAIns, err_40;
6427
6565
  if (useFeesPool === void 0) { useFeesPool = false; }
6428
6566
  if (createUserATA === void 0) { createUserATA = true; }
6429
6567
  if (unWrapSol === void 0) { unWrapSol = false; }
6430
6568
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
6431
6569
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6432
6570
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
6433
- return __generator(this, function (_f) {
6434
- switch (_f.label) {
6571
+ return __generator(this, function (_g) {
6572
+ switch (_g.label) {
6435
6573
  case 0:
6436
6574
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
6437
6575
  if (!userInputCustodyConfig) {
@@ -6441,26 +6579,31 @@ var PerpetualsClient = (function () {
6441
6579
  if (!userOutputCustodyConfig) {
6442
6580
  throw "userOutputCustodyConfig not found";
6443
6581
  }
6444
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6582
+ publicKey = this.provider.wallet.publicKey;
6445
6583
  preInstructions = [];
6446
6584
  instructions = [];
6447
6585
  postInstructions = [];
6448
6586
  additionalSigners = [];
6449
- if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
6587
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 5];
6450
6588
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
6451
6589
  case 1:
6452
- wsolAssociatedTokenAccount = _f.sent();
6453
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID));
6454
- if (!!skipBalanceChecks) return [3, 3];
6590
+ wsolAssociatedTokenAccount = _g.sent();
6591
+ return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
6592
+ case 2:
6593
+ wsolATAExist = _g.sent();
6594
+ if (!wsolATAExist) {
6595
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
6596
+ }
6597
+ if (!!skipBalanceChecks) return [3, 4];
6455
6598
  _a = anchor_1.BN.bind;
6456
6599
  return [4, this.provider.connection.getBalance(publicKey)];
6457
- case 2:
6458
- unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
6600
+ case 3:
6601
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _g.sent()]))();
6459
6602
  if (unWrappedSolBalance.lt(amountIn)) {
6460
6603
  throw "Insufficient SOL Funds";
6461
6604
  }
6462
- _f.label = 3;
6463
- case 3:
6605
+ _g.label = 4;
6606
+ case 4:
6464
6607
  instructions.push(web3_js_1.SystemProgram.transfer({
6465
6608
  fromPubkey: publicKey,
6466
6609
  toPubkey: wsolAssociatedTokenAccount,
@@ -6470,7 +6613,7 @@ var PerpetualsClient = (function () {
6470
6613
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6471
6614
  additionalSigners: additionalSigners
6472
6615
  }];
6473
- case 4:
6616
+ case 5:
6474
6617
  if (userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL') {
6475
6618
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
6476
6619
  wsolAssociatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, publicKey, true);
@@ -6481,20 +6624,20 @@ var PerpetualsClient = (function () {
6481
6624
  additionalSigners: additionalSigners
6482
6625
  }];
6483
6626
  }
6484
- _f.label = 5;
6485
- case 5:
6486
- _f.trys.push([5, 16, , 17]);
6487
- if (!(userInputTokenSymbol == 'SOL')) return [3, 8];
6627
+ _g.label = 6;
6628
+ case 6:
6629
+ _g.trys.push([6, 19, , 20]);
6630
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
6488
6631
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
6489
6632
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
6490
- case 6:
6491
- accCreationLamports = (_f.sent());
6633
+ case 7:
6634
+ accCreationLamports = (_g.sent());
6492
6635
  console.log("accCreationLamports:", accCreationLamports);
6493
6636
  lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
6494
6637
  _b = anchor_1.BN.bind;
6495
6638
  return [4, this.provider.connection.getBalance(publicKey)];
6496
- case 7:
6497
- unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
6639
+ case 8:
6640
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _g.sent()]))();
6498
6641
  if (unWrappedSolBalance.lt(amountIn)) {
6499
6642
  throw "Insufficient SOL Funds";
6500
6643
  }
@@ -6516,25 +6659,25 @@ var PerpetualsClient = (function () {
6516
6659
  postInstructions = [
6517
6660
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6518
6661
  ];
6519
- return [3, 11];
6520
- case 8:
6662
+ return [3, 12];
6663
+ case 9:
6521
6664
  userInputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(userInputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6522
6665
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
6523
- case 9:
6524
- if (!(_f.sent())) {
6666
+ case 10:
6667
+ if (!(_g.sent())) {
6525
6668
  throw "Insufficient Funds , Token Account doesn't exist";
6526
6669
  }
6527
- if (!!skipBalanceChecks) return [3, 11];
6670
+ if (!!skipBalanceChecks) return [3, 12];
6528
6671
  _c = anchor_1.BN.bind;
6529
6672
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
6530
- case 10:
6531
- tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
6673
+ case 11:
6674
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
6532
6675
  if (tokenAccountBalance.lt(amountIn)) {
6533
6676
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
6534
6677
  }
6535
- _f.label = 11;
6536
- case 11:
6537
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 12];
6678
+ _g.label = 12;
6679
+ case 12:
6680
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 13];
6538
6681
  lamports = (this.minimumBalanceForRentExemptAccountLamports);
6539
6682
  if (!ephemeralSignerPubkey) {
6540
6683
  wrappedSolAccount = new web3_js_1.Keypair();
@@ -6554,17 +6697,26 @@ var PerpetualsClient = (function () {
6554
6697
  postInstructions = [
6555
6698
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6556
6699
  ];
6557
- return [3, 14];
6558
- case 12: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID)];
6559
- case 13:
6560
- userOutputTokenAccount = _f.sent();
6561
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6562
- _f.label = 14;
6700
+ return [3, 17];
6701
+ case 13: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID)];
6563
6702
  case 14:
6703
+ userOutputTokenAccount = _g.sent();
6704
+ _d = createUserATA;
6705
+ if (!_d) return [3, 16];
6706
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
6707
+ case 15:
6708
+ _d = !(_g.sent());
6709
+ _g.label = 16;
6710
+ case 16:
6711
+ if (_d) {
6712
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6713
+ }
6714
+ _g.label = 17;
6715
+ case 17:
6564
6716
  custodyAccountMetas = [];
6565
6717
  custodyOracleAccountMetas = [];
6566
- for (_d = 0, _e = poolConfig.custodies; _d < _e.length; _d++) {
6567
- custody = _e[_d];
6718
+ for (_e = 0, _f = poolConfig.custodies; _e < _f.length; _e++) {
6719
+ custody = _f[_e];
6568
6720
  custodyAccountMetas.push({
6569
6721
  pubkey: custody.custodyAccount,
6570
6722
  isSigner: false,
@@ -6612,19 +6764,19 @@ var PerpetualsClient = (function () {
6612
6764
  })
6613
6765
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
6614
6766
  .instruction()];
6615
- case 15:
6616
- inx = _f.sent();
6767
+ case 18:
6768
+ inx = _g.sent();
6617
6769
  instructions.push(inx);
6618
6770
  if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
6619
6771
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
6620
6772
  instructions.push(closeWsolATAIns);
6621
6773
  }
6622
- return [3, 17];
6623
- case 16:
6624
- err_40 = _f.sent();
6774
+ return [3, 20];
6775
+ case 19:
6776
+ err_40 = _g.sent();
6625
6777
  console.error("perpClient Swap error:: ", err_40);
6626
6778
  throw err_40;
6627
- case 17: return [2, {
6779
+ case 20: return [2, {
6628
6780
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6629
6781
  additionalSigners: additionalSigners
6630
6782
  }];
@@ -7407,8 +7559,44 @@ var PerpetualsClient = (function () {
7407
7559
  }
7408
7560
  });
7409
7561
  }); };
7562
+ this.setPositionPriceImpact = function (positionPubkey, priceImpactUsd, penaltyAuthority) { return __awaiter(_this, void 0, void 0, function () {
7563
+ var instructions, additionalSigners, setPositionPriceImpactIx, err_52;
7564
+ return __generator(this, function (_a) {
7565
+ switch (_a.label) {
7566
+ case 0:
7567
+ instructions = [];
7568
+ additionalSigners = [];
7569
+ _a.label = 1;
7570
+ case 1:
7571
+ _a.trys.push([1, 3, , 4]);
7572
+ return [4, this.program.methods
7573
+ .setPositionPriceImpact({
7574
+ priceImpactUsd: priceImpactUsd,
7575
+ })
7576
+ .accounts({
7577
+ authority: penaltyAuthority,
7578
+ position: positionPubkey,
7579
+ eventAuthority: this.eventAuthority.publicKey,
7580
+ program: this.program.programId,
7581
+ })
7582
+ .instruction()];
7583
+ case 2:
7584
+ setPositionPriceImpactIx = _a.sent();
7585
+ instructions.push(setPositionPriceImpactIx);
7586
+ return [3, 4];
7587
+ case 3:
7588
+ err_52 = _a.sent();
7589
+ console.log("perpClient setPositionPriceImpact error:: ", err_52);
7590
+ throw err_52;
7591
+ case 4: return [2, {
7592
+ instructions: __spreadArray([], instructions, true),
7593
+ additionalSigners: additionalSigners
7594
+ }];
7595
+ }
7596
+ });
7597
+ }); };
7410
7598
  this.renameFlp = function (flag, lpTokenName, lpTokenSymbol, lpTokenUri, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7411
- var publicKey, instructions, additionalSigners, lpTokenMint, lpMetadataAccount, renameFlp, err_52;
7599
+ var publicKey, instructions, additionalSigners, lpTokenMint, lpMetadataAccount, renameFlp, err_53;
7412
7600
  return __generator(this, function (_a) {
7413
7601
  switch (_a.label) {
7414
7602
  case 0:
@@ -7446,8 +7634,8 @@ var PerpetualsClient = (function () {
7446
7634
  instructions.push(renameFlp);
7447
7635
  return [3, 4];
7448
7636
  case 3:
7449
- err_52 = _a.sent();
7450
- console.log("perpClient renameFlp error:: ", err_52);
7637
+ err_53 = _a.sent();
7638
+ console.log("perpClient renameFlp error:: ", err_53);
7451
7639
  return [3, 4];
7452
7640
  case 4: return [2, {
7453
7641
  instructions: __spreadArray([], instructions, true),
@@ -7457,7 +7645,7 @@ var PerpetualsClient = (function () {
7457
7645
  });
7458
7646
  }); };
7459
7647
  this.initStake = function (stakingFeeShareBps, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7460
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, stakedLpTokenAccount, rewardCustodyConfig, initStakeInstruction, err_53;
7648
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, stakedLpTokenAccount, rewardCustodyConfig, initStakeInstruction, err_54;
7461
7649
  return __generator(this, function (_a) {
7462
7650
  switch (_a.label) {
7463
7651
  case 0:
@@ -7495,9 +7683,9 @@ var PerpetualsClient = (function () {
7495
7683
  instructions.push(initStakeInstruction);
7496
7684
  return [3, 4];
7497
7685
  case 3:
7498
- err_53 = _a.sent();
7499
- console.log("perpClient InitStaking error:: ", err_53);
7500
- throw err_53;
7686
+ err_54 = _a.sent();
7687
+ console.log("perpClient InitStaking error:: ", err_54);
7688
+ throw err_54;
7501
7689
  case 4: return [2, {
7502
7690
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7503
7691
  additionalSigners: additionalSigners
@@ -7506,7 +7694,7 @@ var PerpetualsClient = (function () {
7506
7694
  });
7507
7695
  }); };
7508
7696
  this.initCompounding = function (feeShareBps, metadataTitle, metadataSymbol, metadataUri, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7509
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyConfig, compoundingTokenMint, compoundingVault, metadataAccount, initCompoundingInstruction, err_54;
7697
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyConfig, compoundingTokenMint, compoundingVault, metadataAccount, initCompoundingInstruction, err_55;
7510
7698
  return __generator(this, function (_a) {
7511
7699
  switch (_a.label) {
7512
7700
  case 0:
@@ -7550,9 +7738,9 @@ var PerpetualsClient = (function () {
7550
7738
  instructions.push(initCompoundingInstruction);
7551
7739
  return [3, 4];
7552
7740
  case 3:
7553
- err_54 = _a.sent();
7554
- console.log("perpClient initCompounding error:: ", err_54);
7555
- throw err_54;
7741
+ err_55 = _a.sent();
7742
+ console.log("perpClient initCompounding error:: ", err_55);
7743
+ throw err_55;
7556
7744
  case 4: return [2, {
7557
7745
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7558
7746
  additionalSigners: additionalSigners
@@ -7561,7 +7749,7 @@ var PerpetualsClient = (function () {
7561
7749
  });
7562
7750
  }); };
7563
7751
  this.initTokenVault = function (token_permissions, tokens_to_distribute, withdrawTimeLimit, withdrawInstantFee, stakeLevel, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7564
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenMint, fundingTokenAccount, initTokenVaultInstruction, err_55;
7752
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenMint, fundingTokenAccount, initTokenVaultInstruction, err_56;
7565
7753
  return __generator(this, function (_a) {
7566
7754
  switch (_a.label) {
7567
7755
  case 0:
@@ -7602,9 +7790,9 @@ var PerpetualsClient = (function () {
7602
7790
  instructions.push(initTokenVaultInstruction);
7603
7791
  return [3, 4];
7604
7792
  case 3:
7605
- err_55 = _a.sent();
7606
- console.log("perpClient InitTokenVaultInstruction error:: ", err_55);
7607
- throw err_55;
7793
+ err_56 = _a.sent();
7794
+ console.log("perpClient InitTokenVaultInstruction error:: ", err_56);
7795
+ throw err_56;
7608
7796
  case 4: return [2, {
7609
7797
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7610
7798
  additionalSigners: additionalSigners
@@ -7613,7 +7801,7 @@ var PerpetualsClient = (function () {
7613
7801
  });
7614
7802
  }); };
7615
7803
  this.setTokenVaultConfig = function (token_permissions, withdrawTimeLimit, withdrawInstantFee, stakeLevel, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7616
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setTokenVaultConfigInstruction, err_56;
7804
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, setTokenVaultConfigInstruction, err_57;
7617
7805
  return __generator(this, function (_a) {
7618
7806
  switch (_a.label) {
7619
7807
  case 0:
@@ -7644,9 +7832,9 @@ var PerpetualsClient = (function () {
7644
7832
  instructions.push(setTokenVaultConfigInstruction);
7645
7833
  return [3, 4];
7646
7834
  case 3:
7647
- err_56 = _a.sent();
7648
- console.log("perpClient setTokenVaultConfigInstruction error:: ", err_56);
7649
- throw err_56;
7835
+ err_57 = _a.sent();
7836
+ console.log("perpClient setTokenVaultConfigInstruction error:: ", err_57);
7837
+ throw err_57;
7650
7838
  case 4: return [2, {
7651
7839
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7652
7840
  additionalSigners: additionalSigners
@@ -7655,7 +7843,7 @@ var PerpetualsClient = (function () {
7655
7843
  });
7656
7844
  }); };
7657
7845
  this.withdrawInstantFee = function (poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7658
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, receivingTokenAccount, withdrawInstantFeeInstruction, err_57;
7846
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, receivingTokenAccount, withdrawInstantFeeInstruction, err_58;
7659
7847
  return __generator(this, function (_a) {
7660
7848
  switch (_a.label) {
7661
7849
  case 0:
@@ -7666,11 +7854,15 @@ var PerpetualsClient = (function () {
7666
7854
  additionalSigners = [];
7667
7855
  _a.label = 1;
7668
7856
  case 1:
7669
- _a.trys.push([1, 4, , 5]);
7857
+ _a.trys.push([1, 5, , 6]);
7670
7858
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7671
7859
  case 2:
7672
7860
  receivingTokenAccount = _a.sent();
7673
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7861
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7862
+ case 3:
7863
+ if (!(_a.sent())) {
7864
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7865
+ }
7674
7866
  return [4, this.program.methods
7675
7867
  .withdrawInstantFees({})
7676
7868
  .accounts({
@@ -7685,15 +7877,15 @@ var PerpetualsClient = (function () {
7685
7877
  receivingTokenMint: poolConfig.tokenMint,
7686
7878
  })
7687
7879
  .instruction()];
7688
- case 3:
7880
+ case 4:
7689
7881
  withdrawInstantFeeInstruction = _a.sent();
7690
7882
  instructions.push(withdrawInstantFeeInstruction);
7691
- return [3, 5];
7692
- case 4:
7693
- err_57 = _a.sent();
7694
- console.log("perpClient withdrawInstantFeeInstruction error:: ", err_57);
7695
- throw err_57;
7696
- case 5: return [2, {
7883
+ return [3, 6];
7884
+ case 5:
7885
+ err_58 = _a.sent();
7886
+ console.log("perpClient withdrawInstantFeeInstruction error:: ", err_58);
7887
+ throw err_58;
7888
+ case 6: return [2, {
7697
7889
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7698
7890
  additionalSigners: additionalSigners
7699
7891
  }];
@@ -7701,7 +7893,7 @@ var PerpetualsClient = (function () {
7701
7893
  });
7702
7894
  }); };
7703
7895
  this.withdrawUnclaimedTokens = function (poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7704
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, receivingTokenAccount, withdrawUnclaimedTokensInstruction, err_58;
7896
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, receivingTokenAccount, withdrawUnclaimedTokensInstruction, err_59;
7705
7897
  return __generator(this, function (_a) {
7706
7898
  switch (_a.label) {
7707
7899
  case 0:
@@ -7712,11 +7904,15 @@ var PerpetualsClient = (function () {
7712
7904
  additionalSigners = [];
7713
7905
  _a.label = 1;
7714
7906
  case 1:
7715
- _a.trys.push([1, 4, , 5]);
7907
+ _a.trys.push([1, 5, , 6]);
7716
7908
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7717
7909
  case 2:
7718
7910
  receivingTokenAccount = _a.sent();
7719
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7911
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7912
+ case 3:
7913
+ if (!(_a.sent())) {
7914
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7915
+ }
7720
7916
  return [4, this.program.methods
7721
7917
  .withdrawUnclaimedTokens({})
7722
7918
  .accounts({
@@ -7731,15 +7927,15 @@ var PerpetualsClient = (function () {
7731
7927
  receivingTokenMint: poolConfig.tokenMint,
7732
7928
  })
7733
7929
  .instruction()];
7734
- case 3:
7930
+ case 4:
7735
7931
  withdrawUnclaimedTokensInstruction = _a.sent();
7736
7932
  instructions.push(withdrawUnclaimedTokensInstruction);
7737
- return [3, 5];
7738
- case 4:
7739
- err_58 = _a.sent();
7740
- console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_58);
7741
- throw err_58;
7742
- case 5: return [2, {
7933
+ return [3, 6];
7934
+ case 5:
7935
+ err_59 = _a.sent();
7936
+ console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_59);
7937
+ throw err_59;
7938
+ case 6: return [2, {
7743
7939
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7744
7940
  additionalSigners: additionalSigners
7745
7941
  }];
@@ -7747,7 +7943,7 @@ var PerpetualsClient = (function () {
7747
7943
  });
7748
7944
  }); };
7749
7945
  this.initRevenueTokenAccount = function (feeShareBps, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7750
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, initRevenueTokenAccountInstruction, err_59;
7946
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, initRevenueTokenAccountInstruction, err_60;
7751
7947
  return __generator(this, function (_a) {
7752
7948
  switch (_a.label) {
7753
7949
  case 0:
@@ -7784,9 +7980,9 @@ var PerpetualsClient = (function () {
7784
7980
  instructions.push(initRevenueTokenAccountInstruction);
7785
7981
  return [3, 4];
7786
7982
  case 3:
7787
- err_59 = _a.sent();
7788
- console.log("perpClient initRevenueTokenAccountInstruction error:: ", err_59);
7789
- throw err_59;
7983
+ err_60 = _a.sent();
7984
+ console.log("perpClient initRevenueTokenAccountInstruction error:: ", err_60);
7985
+ throw err_60;
7790
7986
  case 4: return [2, {
7791
7987
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7792
7988
  additionalSigners: additionalSigners
@@ -7795,7 +7991,7 @@ var PerpetualsClient = (function () {
7795
7991
  });
7796
7992
  }); };
7797
7993
  this.initRebateVault = function (allowRebatePayout, rebateSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7798
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateCustodyMint, initRebateVaultInstruction, err_60;
7994
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateCustodyMint, initRebateVaultInstruction, err_61;
7799
7995
  return __generator(this, function (_a) {
7800
7996
  switch (_a.label) {
7801
7997
  case 0:
@@ -7830,9 +8026,9 @@ var PerpetualsClient = (function () {
7830
8026
  instructions.push(initRebateVaultInstruction);
7831
8027
  return [3, 4];
7832
8028
  case 3:
7833
- err_60 = _a.sent();
7834
- console.log("perpClient initRebateVaultInstruction error:: ", err_60);
7835
- throw err_60;
8029
+ err_61 = _a.sent();
8030
+ console.log("perpClient initRebateVaultInstruction error:: ", err_61);
8031
+ throw err_61;
7836
8032
  case 4: return [2, {
7837
8033
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7838
8034
  additionalSigners: additionalSigners
@@ -7841,7 +8037,7 @@ var PerpetualsClient = (function () {
7841
8037
  });
7842
8038
  }); };
7843
8039
  this.distributeTokenReward = function (amount, epochCount, rewardSymbol, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7844
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, fundingTokenAccount, revenueFundingTokenAccount, distributeTokenRewardInstruction, err_61;
8040
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, fundingTokenAccount, revenueFundingTokenAccount, distributeTokenRewardInstruction, err_62;
7845
8041
  return __generator(this, function (_a) {
7846
8042
  switch (_a.label) {
7847
8043
  case 0:
@@ -7880,9 +8076,9 @@ var PerpetualsClient = (function () {
7880
8076
  instructions.push(distributeTokenRewardInstruction);
7881
8077
  return [3, 4];
7882
8078
  case 3:
7883
- err_61 = _a.sent();
7884
- console.log("perpClient distributeTokenRewardInstruction error:: ", err_61);
7885
- throw err_61;
8079
+ err_62 = _a.sent();
8080
+ console.log("perpClient distributeTokenRewardInstruction error:: ", err_62);
8081
+ throw err_62;
7886
8082
  case 4: return [2, {
7887
8083
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7888
8084
  additionalSigners: additionalSigners
@@ -7891,7 +8087,7 @@ var PerpetualsClient = (function () {
7891
8087
  });
7892
8088
  }); };
7893
8089
  this.setTokenStakeLevel = function (owner, stakeLevel) { return __awaiter(_this, void 0, void 0, function () {
7894
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, setTokenStakeLevelInstruction, err_62;
8090
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, setTokenStakeLevelInstruction, err_63;
7895
8091
  return __generator(this, function (_a) {
7896
8092
  switch (_a.label) {
7897
8093
  case 0:
@@ -7919,9 +8115,9 @@ var PerpetualsClient = (function () {
7919
8115
  instructions.push(setTokenStakeLevelInstruction);
7920
8116
  return [3, 4];
7921
8117
  case 3:
7922
- err_62 = _a.sent();
7923
- console.log("perpClient setTokenStakeLevelInstruction error:: ", err_62);
7924
- throw err_62;
8118
+ err_63 = _a.sent();
8119
+ console.log("perpClient setTokenStakeLevelInstruction error:: ", err_63);
8120
+ throw err_63;
7925
8121
  case 4: return [2, {
7926
8122
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7927
8123
  additionalSigners: additionalSigners
@@ -7930,7 +8126,7 @@ var PerpetualsClient = (function () {
7930
8126
  });
7931
8127
  }); };
7932
8128
  this.setTokenReward = function (owner, amount, epochCount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
7933
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, setTokenRewardInstruction, err_63;
8129
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, setTokenRewardInstruction, err_64;
7934
8130
  return __generator(this, function (_a) {
7935
8131
  switch (_a.label) {
7936
8132
  case 0:
@@ -7962,9 +8158,9 @@ var PerpetualsClient = (function () {
7962
8158
  instructions.push(setTokenRewardInstruction);
7963
8159
  return [3, 4];
7964
8160
  case 3:
7965
- err_63 = _a.sent();
7966
- console.log("perpClient setTokenRewardInstruction error:: ", err_63);
7967
- throw err_63;
8161
+ err_64 = _a.sent();
8162
+ console.log("perpClient setTokenRewardInstruction error:: ", err_64);
8163
+ throw err_64;
7968
8164
  case 4: return [2, {
7969
8165
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7970
8166
  additionalSigners: additionalSigners
@@ -7973,7 +8169,7 @@ var PerpetualsClient = (function () {
7973
8169
  });
7974
8170
  }); };
7975
8171
  this.resizeInternalOracle = function (extOracle, tokenMint, intOracleAccount) { return __awaiter(_this, void 0, void 0, function () {
7976
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, resizeInternalOracleInstruction, err_64;
8172
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, resizeInternalOracleInstruction, err_65;
7977
8173
  return __generator(this, function (_a) {
7978
8174
  switch (_a.label) {
7979
8175
  case 0:
@@ -8002,9 +8198,9 @@ var PerpetualsClient = (function () {
8002
8198
  instructions.push(resizeInternalOracleInstruction);
8003
8199
  return [3, 4];
8004
8200
  case 3:
8005
- err_64 = _a.sent();
8006
- console.log("perpClient resizeInternalOracleInstruction error:: ", err_64);
8007
- throw err_64;
8201
+ err_65 = _a.sent();
8202
+ console.log("perpClient resizeInternalOracleInstruction error:: ", err_65);
8203
+ throw err_65;
8008
8204
  case 4: return [2, {
8009
8205
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
8010
8206
  additionalSigners: additionalSigners
@@ -8013,7 +8209,7 @@ var PerpetualsClient = (function () {
8013
8209
  });
8014
8210
  }); };
8015
8211
  this.createWhitelist = function (isSwapFeeExempt, isDepositFeeExempt, isWithdrawalFeeExempt, poolAddress, owner) { return __awaiter(_this, void 0, void 0, function () {
8016
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, whitelist, createWhitelistInstruction, err_65;
8212
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, whitelist, createWhitelistInstruction, err_66;
8017
8213
  return __generator(this, function (_a) {
8018
8214
  switch (_a.label) {
8019
8215
  case 0:
@@ -8046,9 +8242,9 @@ var PerpetualsClient = (function () {
8046
8242
  instructions.push(createWhitelistInstruction);
8047
8243
  return [3, 4];
8048
8244
  case 3:
8049
- err_65 = _a.sent();
8050
- console.log("perpClient createWhitelistInstruction error:: ", err_65);
8051
- throw err_65;
8245
+ err_66 = _a.sent();
8246
+ console.log("perpClient createWhitelistInstruction error:: ", err_66);
8247
+ throw err_66;
8052
8248
  case 4: return [2, {
8053
8249
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
8054
8250
  additionalSigners: additionalSigners
@@ -8057,7 +8253,7 @@ var PerpetualsClient = (function () {
8057
8253
  });
8058
8254
  }); };
8059
8255
  this.setWhitelistConfig = function (isSwapFeeExempt, isDepositFeeExempt, isWithdrawalFeeExempt, poolAddress, owner) { return __awaiter(_this, void 0, void 0, function () {
8060
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, whitelist, setWhitelistConfigInstruction, err_66;
8256
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, whitelist, setWhitelistConfigInstruction, err_67;
8061
8257
  return __generator(this, function (_a) {
8062
8258
  switch (_a.label) {
8063
8259
  case 0:
@@ -8090,9 +8286,9 @@ var PerpetualsClient = (function () {
8090
8286
  instructions.push(setWhitelistConfigInstruction);
8091
8287
  return [3, 4];
8092
8288
  case 3:
8093
- err_66 = _a.sent();
8094
- console.log("perpClient setWhitelistConfigInstruction error:: ", err_66);
8095
- throw err_66;
8289
+ err_67 = _a.sent();
8290
+ console.log("perpClient setWhitelistConfigInstruction error:: ", err_67);
8291
+ throw err_67;
8096
8292
  case 4: return [2, {
8097
8293
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
8098
8294
  additionalSigners: additionalSigners