flash-sdk 11.8.0-alpha.0 → 11.8.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.
@@ -1631,8 +1631,14 @@ var PerpetualsClient = (function () {
1631
1631
  priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1632
1632
  }
1633
1633
  else {
1634
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1635
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1634
+ if (positionAccount.referencePrice.price.gt(positionEntryPrice.price)) {
1635
+ priceDiffProfit = new OraclePrice_1.OraclePrice({ price: positionAccount.referencePrice.price.sub(positionEntryPrice.price), exponent: positionEntryPrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1636
+ priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1637
+ }
1638
+ else {
1639
+ priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1640
+ priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1641
+ }
1636
1642
  }
1637
1643
  }
1638
1644
  else {
@@ -1647,8 +1653,14 @@ var PerpetualsClient = (function () {
1647
1653
  priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1648
1654
  }
1649
1655
  else {
1650
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1651
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1656
+ if (positionEntryPrice.price.gt(positionAccount.referencePrice.price)) {
1657
+ priceDiffProfit = new OraclePrice_1.OraclePrice({ price: positionEntryPrice.price.sub(positionAccount.referencePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1658
+ priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1659
+ }
1660
+ else {
1661
+ priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1662
+ priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1663
+ }
1652
1664
  }
1653
1665
  }
1654
1666
  else {
@@ -1711,8 +1723,14 @@ var PerpetualsClient = (function () {
1711
1723
  priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1712
1724
  }
1713
1725
  else {
1714
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1715
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
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
+ }
1716
1734
  }
1717
1735
  }
1718
1736
  else {
@@ -1727,8 +1745,14 @@ var PerpetualsClient = (function () {
1727
1745
  priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1728
1746
  }
1729
1747
  else {
1730
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1731
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
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
+ }
1732
1756
  }
1733
1757
  }
1734
1758
  else {
@@ -2704,7 +2728,7 @@ var PerpetualsClient = (function () {
2704
2728
  for (var _i = 8; _i < arguments.length; _i++) {
2705
2729
  args_1[_i - 8] = arguments[_i];
2706
2730
  }
2707
- 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) {
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) {
2708
2732
  var publicKey, targetCustodyConfig, collateralCustodyConfig, collateralToken, marketAccount, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
2709
2733
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2710
2734
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2713,7 +2737,7 @@ var PerpetualsClient = (function () {
2713
2737
  return __generator(this, function (_c) {
2714
2738
  switch (_c.label) {
2715
2739
  case 0:
2716
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2740
+ publicKey = this.provider.wallet.publicKey;
2717
2741
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
2718
2742
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2719
2743
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
@@ -2818,48 +2842,59 @@ var PerpetualsClient = (function () {
2818
2842
  for (var _i = 6; _i < arguments.length; _i++) {
2819
2843
  args_1[_i - 6] = arguments[_i];
2820
2844
  }
2821
- 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) {
2822
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
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) {
2846
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2823
2847
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2824
2848
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
2825
2849
  if (createUserATA === void 0) { createUserATA = true; }
2826
2850
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
2827
2851
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2828
- return __generator(this, function (_a) {
2829
- switch (_a.label) {
2852
+ return __generator(this, function (_b) {
2853
+ switch (_b.label) {
2830
2854
  case 0:
2831
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2855
+ console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
2856
+ publicKey = this.provider.wallet.publicKey;
2832
2857
  preInstructions = [];
2833
2858
  instructions = [];
2834
2859
  postInstructions = [];
2835
2860
  additionalSigners = [];
2836
- _a.label = 1;
2861
+ _b.label = 1;
2837
2862
  case 1:
2838
- _a.trys.push([1, 3, , 4]);
2839
- if (collateralSymbol == 'SOL') {
2840
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
2841
- if (!ephemeralSignerPubkey) {
2842
- wrappedSolAccount = new web3_js_1.Keypair();
2843
- additionalSigners.push(wrappedSolAccount);
2844
- }
2845
- preInstructions = [
2846
- web3_js_1.SystemProgram.createAccount({
2847
- fromPubkey: publicKey,
2848
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2849
- lamports: lamports,
2850
- space: 165,
2851
- programId: spl_token_1.TOKEN_PROGRAM_ID,
2852
- }),
2853
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2854
- ];
2855
- postInstructions = [
2856
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2857
- ];
2863
+ _b.trys.push([1, 7, , 8]);
2864
+ if (!(collateralSymbol == 'SOL')) return [3, 2];
2865
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
2866
+ if (!ephemeralSignerPubkey) {
2867
+ wrappedSolAccount = new web3_js_1.Keypair();
2868
+ additionalSigners.push(wrappedSolAccount);
2858
2869
  }
2859
- else {
2860
- 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);
2861
- 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));
2870
+ preInstructions = [
2871
+ web3_js_1.SystemProgram.createAccount({
2872
+ fromPubkey: publicKey,
2873
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2874
+ lamports: lamports,
2875
+ space: 165,
2876
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
2877
+ }),
2878
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2879
+ ];
2880
+ postInstructions = [
2881
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2882
+ ];
2883
+ return [3, 5];
2884
+ case 2:
2885
+ 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);
2886
+ _a = createUserATA;
2887
+ if (!_a) return [3, 4];
2888
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
2889
+ case 3:
2890
+ _a = !(_b.sent());
2891
+ _b.label = 4;
2892
+ case 4:
2893
+ if (_a) {
2894
+ 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));
2862
2895
  }
2896
+ _b.label = 5;
2897
+ case 5:
2863
2898
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2864
2899
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
2865
2900
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -2891,19 +2926,19 @@ var PerpetualsClient = (function () {
2891
2926
  })
2892
2927
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
2893
2928
  .instruction()];
2894
- case 2:
2895
- instruction = _a.sent();
2929
+ case 6:
2930
+ instruction = _b.sent();
2896
2931
  instructions.push(instruction);
2897
2932
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
2898
2933
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
2899
2934
  postInstructions.push(closeWsolATAIns);
2900
2935
  }
2901
- return [3, 4];
2902
- case 3:
2903
- error_1 = _a.sent();
2936
+ return [3, 8];
2937
+ case 7:
2938
+ error_1 = _b.sent();
2904
2939
  console.error("perpclient closePosition error:", error_1);
2905
2940
  throw error_1;
2906
- case 4: return [2, {
2941
+ case 8: return [2, {
2907
2942
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
2908
2943
  additionalSigners: additionalSigners
2909
2944
  }];
@@ -2916,7 +2951,7 @@ var PerpetualsClient = (function () {
2916
2951
  for (var _i = 9; _i < arguments.length; _i++) {
2917
2952
  args_1[_i - 9] = arguments[_i];
2918
2953
  }
2919
- 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) {
2954
+ 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) {
2920
2955
  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;
2921
2956
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2922
2957
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2925,7 +2960,7 @@ var PerpetualsClient = (function () {
2925
2960
  return __generator(this, function (_c) {
2926
2961
  switch (_c.label) {
2927
2962
  case 0:
2928
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2963
+ publicKey = this.provider.wallet.publicKey;
2929
2964
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
2930
2965
  if (!userInputCustodyConfig) {
2931
2966
  throw "userInputCustodyConfig not found";
@@ -2949,7 +2984,7 @@ var PerpetualsClient = (function () {
2949
2984
  additionalSigners = [];
2950
2985
  targetToken = poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol);
2951
2986
  userInputToken = poolConfig.getTokenFromSymbol(userInputTokenSymbol);
2952
- if (!(userInputTokenSymbol == 'SOL')) return [3, 3];
2987
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 5];
2953
2988
  console.log("inputSymbol === SOL", userInputTokenSymbol);
2954
2989
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
2955
2990
  if (!!skipBalanceChecks) return [3, 2];
@@ -2979,33 +3014,41 @@ var PerpetualsClient = (function () {
2979
3014
  postInstructions = [
2980
3015
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2981
3016
  ];
2982
- if (!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) {
2983
- 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);
2984
- 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));
2985
- }
2986
- return [3, 7];
3017
+ if (!!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) return [3, 4];
3018
+ 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);
3019
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
2987
3020
  case 3:
3021
+ if (!(_c.sent())) {
3022
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey));
3023
+ }
3024
+ _c.label = 4;
3025
+ case 4: return [3, 10];
3026
+ case 5:
2988
3027
  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);
2989
- if (!!skipBalanceChecks) return [3, 6];
3028
+ if (!!skipBalanceChecks) return [3, 8];
2990
3029
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
2991
- case 4:
3030
+ case 6:
2992
3031
  if (!(_c.sent())) {
2993
3032
  throw "Insufficient Funds , Token Account doesn't exist";
2994
3033
  }
2995
3034
  _b = anchor_1.BN.bind;
2996
3035
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
2997
- case 5:
3036
+ case 7:
2998
3037
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
2999
3038
  if (tokenAccountBalance.lt(amountIn)) {
3000
3039
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3001
3040
  }
3002
- _c.label = 6;
3003
- case 6:
3041
+ _c.label = 8;
3042
+ case 8:
3004
3043
  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);
3005
- 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));
3006
- _c.label = 7;
3007
- case 7:
3008
- _c.trys.push([7, 9, , 10]);
3044
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3045
+ case 9:
3046
+ if (!(_c.sent())) {
3047
+ 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));
3048
+ }
3049
+ _c.label = 10;
3050
+ case 10:
3051
+ _c.trys.push([10, 12, , 13]);
3009
3052
  return [4, this.program.methods
3010
3053
  .swapAndOpen({
3011
3054
  amountIn: amountIn,
@@ -3041,15 +3084,15 @@ var PerpetualsClient = (function () {
3041
3084
  })
3042
3085
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3043
3086
  .instruction()];
3044
- case 8:
3087
+ case 11:
3045
3088
  inx = _c.sent();
3046
3089
  instructions.push(inx);
3047
- return [3, 10];
3048
- case 9:
3090
+ return [3, 13];
3091
+ case 12:
3049
3092
  err_3 = _c.sent();
3050
3093
  console.error("perpClient SwapAndOpen error:: ", err_3);
3051
3094
  throw err_3;
3052
- case 10: return [2, {
3095
+ case 13: return [2, {
3053
3096
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3054
3097
  additionalSigners: additionalSigners
3055
3098
  }];
@@ -3062,7 +3105,7 @@ var PerpetualsClient = (function () {
3062
3105
  for (var _i = 7; _i < arguments.length; _i++) {
3063
3106
  args_1[_i - 7] = arguments[_i];
3064
3107
  }
3065
- 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) {
3108
+ 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) {
3066
3109
  var publicKey, userOutputCustodyConfig, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, collateralToken, userOutputToken, lamports, userCollateralTokenAccount, inx, err_4;
3067
3110
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3068
3111
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -3070,7 +3113,7 @@ var PerpetualsClient = (function () {
3070
3113
  return __generator(this, function (_a) {
3071
3114
  switch (_a.label) {
3072
3115
  case 0:
3073
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3116
+ publicKey = this.provider.wallet.publicKey;
3074
3117
  userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey); });
3075
3118
  if (!userOutputCustodyConfig) {
3076
3119
  throw "userOutputCustodyConfig not found";
@@ -3094,37 +3137,46 @@ var PerpetualsClient = (function () {
3094
3137
  additionalSigners = [];
3095
3138
  collateralToken = poolConfig.getTokenFromSymbol(collateralTokenSymbol);
3096
3139
  userOutputToken = poolConfig.getTokenFromSymbol(userOutputTokenSymbol);
3097
- if (userOutputTokenSymbol == 'SOL') {
3098
- console.log("outputSymbol === SOL", userOutputTokenSymbol);
3099
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
3100
- if (!ephemeralSignerPubkey) {
3101
- wrappedSolAccount = new web3_js_1.Keypair();
3102
- additionalSigners.push(wrappedSolAccount);
3103
- }
3104
- preInstructions = [
3105
- web3_js_1.SystemProgram.createAccount({
3106
- fromPubkey: publicKey,
3107
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3108
- lamports: lamports,
3109
- space: 165,
3110
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3111
- }),
3112
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3113
- ];
3114
- postInstructions = [
3115
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3116
- ];
3140
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 1];
3141
+ console.log("outputSymbol === SOL", userOutputTokenSymbol);
3142
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
3143
+ if (!ephemeralSignerPubkey) {
3144
+ wrappedSolAccount = new web3_js_1.Keypair();
3117
3145
  additionalSigners.push(wrappedSolAccount);
3118
3146
  }
3119
- else {
3120
- 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);
3121
- 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));
3147
+ preInstructions = [
3148
+ web3_js_1.SystemProgram.createAccount({
3149
+ fromPubkey: publicKey,
3150
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3151
+ lamports: lamports,
3152
+ space: 165,
3153
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3154
+ }),
3155
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3156
+ ];
3157
+ postInstructions = [
3158
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3159
+ ];
3160
+ additionalSigners.push(wrappedSolAccount);
3161
+ return [3, 3];
3162
+ case 1:
3163
+ 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);
3164
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3165
+ case 2:
3166
+ if (!(_a.sent())) {
3167
+ 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));
3122
3168
  }
3169
+ _a.label = 3;
3170
+ case 3:
3123
3171
  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);
3124
- 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));
3125
- _a.label = 1;
3126
- case 1:
3127
- _a.trys.push([1, 3, , 4]);
3172
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3173
+ case 4:
3174
+ if (!(_a.sent())) {
3175
+ 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));
3176
+ }
3177
+ _a.label = 5;
3178
+ case 5:
3179
+ _a.trys.push([5, 7, , 8]);
3128
3180
  return [4, this.program.methods
3129
3181
  .closeAndSwap({
3130
3182
  priceWithSlippage: priceWithSlippage,
@@ -3158,15 +3210,15 @@ var PerpetualsClient = (function () {
3158
3210
  })
3159
3211
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3160
3212
  .instruction()];
3161
- case 2:
3213
+ case 6:
3162
3214
  inx = _a.sent();
3163
3215
  instructions.push(inx);
3164
- return [3, 4];
3165
- case 3:
3216
+ return [3, 8];
3217
+ case 7:
3166
3218
  err_4 = _a.sent();
3167
3219
  console.error("perpClient CloseAndSwap error:: ", err_4);
3168
3220
  throw err_4;
3169
- case 4: return [2, {
3221
+ case 8: return [2, {
3170
3222
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3171
3223
  additionalSigners: additionalSigners
3172
3224
  }];
@@ -3179,14 +3231,14 @@ var PerpetualsClient = (function () {
3179
3231
  for (var _i = 6; _i < arguments.length; _i++) {
3180
3232
  args_1[_i - 6] = arguments[_i];
3181
3233
  }
3182
- 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) {
3234
+ 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) {
3183
3235
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, instruction;
3184
3236
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3185
3237
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3186
3238
  return __generator(this, function (_c) {
3187
3239
  switch (_c.label) {
3188
3240
  case 0:
3189
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3241
+ publicKey = this.provider.wallet.publicKey;
3190
3242
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3191
3243
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3192
3244
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -3282,14 +3334,14 @@ var PerpetualsClient = (function () {
3282
3334
  for (var _i = 7; _i < arguments.length; _i++) {
3283
3335
  args_1[_i - 7] = arguments[_i];
3284
3336
  }
3285
- 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) {
3337
+ 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) {
3286
3338
  var publicKey, collateralCustodyConfig, targetCustodyConfig, inputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userCollateralTokenAccount, marketAccount, instruction;
3287
3339
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3288
3340
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3289
3341
  return __generator(this, function (_c) {
3290
3342
  switch (_c.label) {
3291
3343
  case 0:
3292
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3344
+ publicKey = this.provider.wallet.publicKey;
3293
3345
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3294
3346
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3295
3347
  inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inputSymbol).mintKey); });
@@ -3353,7 +3405,11 @@ var PerpetualsClient = (function () {
3353
3405
  _c.label = 6;
3354
3406
  case 6:
3355
3407
  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);
3356
- 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));
3408
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3409
+ case 7:
3410
+ if (!(_c.sent())) {
3411
+ 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));
3412
+ }
3357
3413
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3358
3414
  return [4, this.program.methods.swapAndAddCollateral({
3359
3415
  amountIn: amountIn,
@@ -3381,7 +3437,7 @@ var PerpetualsClient = (function () {
3381
3437
  fundingMint: poolConfig.getTokenFromSymbol(inputSymbol).mintKey,
3382
3438
  })
3383
3439
  .instruction()];
3384
- case 7:
3440
+ case 8:
3385
3441
  instruction = _c.sent();
3386
3442
  instructions.push(instruction);
3387
3443
  return [2, {
@@ -3397,15 +3453,15 @@ var PerpetualsClient = (function () {
3397
3453
  for (var _i = 6; _i < arguments.length; _i++) {
3398
3454
  args_1[_i - 6] = arguments[_i];
3399
3455
  }
3400
- 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) {
3401
- var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, marketAccount, instruction, closeWsolATAIns, error_2;
3456
+ 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) {
3457
+ var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, marketAccount, instruction, closeWsolATAIns, error_2;
3402
3458
  if (createUserATA === void 0) { createUserATA = true; }
3403
3459
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3404
3460
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3405
- return __generator(this, function (_a) {
3406
- switch (_a.label) {
3461
+ return __generator(this, function (_b) {
3462
+ switch (_b.label) {
3407
3463
  case 0:
3408
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3464
+ publicKey = this.provider.wallet.publicKey;
3409
3465
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3410
3466
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3411
3467
  });
@@ -3419,35 +3475,45 @@ var PerpetualsClient = (function () {
3419
3475
  instructions = [];
3420
3476
  postInstructions = [];
3421
3477
  additionalSigners = [];
3422
- _a.label = 1;
3478
+ _b.label = 1;
3423
3479
  case 1:
3424
- _a.trys.push([1, 3, , 4]);
3480
+ _b.trys.push([1, 7, , 8]);
3425
3481
  console.log("removeCollateral -- collateralSymbol:", collateralSymbol);
3426
- if (collateralSymbol == 'SOL') {
3427
- console.log("remove collateral in SOL ...create WSOL temp and close it ");
3428
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3429
- if (!ephemeralSignerPubkey) {
3430
- wrappedSolAccount = new web3_js_1.Keypair();
3431
- additionalSigners.push(wrappedSolAccount);
3432
- }
3433
- preInstructions = [
3434
- web3_js_1.SystemProgram.createAccount({
3435
- fromPubkey: publicKey,
3436
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3437
- lamports: lamports,
3438
- space: 165,
3439
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3440
- }),
3441
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3442
- ];
3443
- postInstructions = [
3444
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3445
- ];
3482
+ if (!(collateralSymbol == 'SOL')) return [3, 2];
3483
+ console.log("remove collateral in SOL ...create WSOL temp and close it ");
3484
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3485
+ if (!ephemeralSignerPubkey) {
3486
+ wrappedSolAccount = new web3_js_1.Keypair();
3487
+ additionalSigners.push(wrappedSolAccount);
3446
3488
  }
3447
- else {
3448
- 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);
3449
- 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));
3489
+ preInstructions = [
3490
+ web3_js_1.SystemProgram.createAccount({
3491
+ fromPubkey: publicKey,
3492
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3493
+ lamports: lamports,
3494
+ space: 165,
3495
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3496
+ }),
3497
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3498
+ ];
3499
+ postInstructions = [
3500
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3501
+ ];
3502
+ return [3, 5];
3503
+ case 2:
3504
+ 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);
3505
+ _a = createUserATA;
3506
+ if (!_a) return [3, 4];
3507
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3508
+ case 3:
3509
+ _a = !(_b.sent());
3510
+ _b.label = 4;
3511
+ case 4:
3512
+ if (_a) {
3513
+ 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));
3450
3514
  }
3515
+ _b.label = 5;
3516
+ case 5:
3451
3517
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3452
3518
  return [4, this.program.methods
3453
3519
  .removeCollateral({
@@ -3473,19 +3539,19 @@ var PerpetualsClient = (function () {
3473
3539
  receivingMint: collateralCustodyConfig.mintKey
3474
3540
  })
3475
3541
  .instruction()];
3476
- case 2:
3477
- instruction = _a.sent();
3542
+ case 6:
3543
+ instruction = _b.sent();
3478
3544
  instructions.push(instruction);
3479
3545
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
3480
3546
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
3481
3547
  postInstructions.push(closeWsolATAIns);
3482
3548
  }
3483
- return [3, 4];
3484
- case 3:
3485
- error_2 = _a.sent();
3549
+ return [3, 8];
3550
+ case 7:
3551
+ error_2 = _b.sent();
3486
3552
  console.error("perpclient removeCollateral error:", error_2);
3487
3553
  throw error_2;
3488
- case 4: return [2, {
3554
+ case 8: return [2, {
3489
3555
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3490
3556
  additionalSigners: additionalSigners
3491
3557
  }];
@@ -3498,13 +3564,13 @@ var PerpetualsClient = (function () {
3498
3564
  for (var _i = 6; _i < arguments.length; _i++) {
3499
3565
  args_1[_i - 6] = arguments[_i];
3500
3566
  }
3501
- 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) {
3567
+ 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) {
3502
3568
  var publicKey, targetCustodyConfig, collateralCustodyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, instruction;
3503
3569
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3504
3570
  return __generator(this, function (_a) {
3505
3571
  switch (_a.label) {
3506
3572
  case 0:
3507
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3573
+ publicKey = this.provider.wallet.publicKey;
3508
3574
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3509
3575
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3510
3576
  outputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outputSymbol).mintKey); });
@@ -3515,33 +3581,42 @@ var PerpetualsClient = (function () {
3515
3581
  instructions = [];
3516
3582
  postInstructions = [];
3517
3583
  additionalSigners = [];
3518
- if (outputSymbol == 'SOL') {
3519
- console.log("outputSymbol === SOL", outputSymbol);
3520
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3521
- if (!ephemeralSignerPubkey) {
3522
- wrappedSolAccount = new web3_js_1.Keypair();
3523
- additionalSigners.push(wrappedSolAccount);
3524
- }
3525
- preInstructions = [
3526
- web3_js_1.SystemProgram.createAccount({
3527
- fromPubkey: publicKey,
3528
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3529
- lamports: lamports,
3530
- space: 165,
3531
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3532
- }),
3533
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3534
- ];
3535
- postInstructions = [
3536
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3537
- ];
3584
+ if (!(outputSymbol == 'SOL')) return [3, 1];
3585
+ console.log("outputSymbol === SOL", outputSymbol);
3586
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3587
+ if (!ephemeralSignerPubkey) {
3588
+ wrappedSolAccount = new web3_js_1.Keypair();
3589
+ additionalSigners.push(wrappedSolAccount);
3538
3590
  }
3539
- else {
3540
- 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);
3541
- 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));
3591
+ preInstructions = [
3592
+ web3_js_1.SystemProgram.createAccount({
3593
+ fromPubkey: publicKey,
3594
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3595
+ lamports: lamports,
3596
+ space: 165,
3597
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3598
+ }),
3599
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3600
+ ];
3601
+ postInstructions = [
3602
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3603
+ ];
3604
+ return [3, 3];
3605
+ case 1:
3606
+ 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);
3607
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3608
+ case 2:
3609
+ if (!(_a.sent())) {
3610
+ 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));
3542
3611
  }
3612
+ _a.label = 3;
3613
+ case 3:
3543
3614
  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);
3544
- 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));
3615
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3616
+ case 4:
3617
+ if (!(_a.sent())) {
3618
+ 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));
3619
+ }
3545
3620
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3546
3621
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3547
3622
  return [4, this.program.methods
@@ -3575,7 +3650,7 @@ var PerpetualsClient = (function () {
3575
3650
  collateralTokenProgram: poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID
3576
3651
  })
3577
3652
  .instruction()];
3578
- case 1:
3653
+ case 5:
3579
3654
  instruction = _a.sent();
3580
3655
  instructions.push(instruction);
3581
3656
  return [2, {
@@ -3591,14 +3666,14 @@ var PerpetualsClient = (function () {
3591
3666
  for (var _i = 8; _i < arguments.length; _i++) {
3592
3667
  args_1[_i - 8] = arguments[_i];
3593
3668
  }
3594
- 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) {
3669
+ 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) {
3595
3670
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3596
3671
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3597
3672
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3598
3673
  return __generator(this, function (_a) {
3599
3674
  switch (_a.label) {
3600
3675
  case 0:
3601
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3676
+ publicKey = this.provider.wallet.publicKey;
3602
3677
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3603
3678
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3604
3679
  });
@@ -3655,14 +3730,14 @@ var PerpetualsClient = (function () {
3655
3730
  for (var _i = 8; _i < arguments.length; _i++) {
3656
3731
  args_1[_i - 8] = arguments[_i];
3657
3732
  }
3658
- 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) {
3733
+ 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) {
3659
3734
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3660
3735
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3661
3736
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3662
3737
  return __generator(this, function (_a) {
3663
3738
  switch (_a.label) {
3664
3739
  case 0:
3665
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3740
+ publicKey = this.provider.wallet.publicKey;
3666
3741
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3667
3742
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3668
3743
  });
@@ -3719,7 +3794,7 @@ var PerpetualsClient = (function () {
3719
3794
  for (var _i = 4; _i < arguments.length; _i++) {
3720
3795
  args_1[_i - 4] = arguments[_i];
3721
3796
  }
3722
- 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) {
3797
+ 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) {
3723
3798
  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;
3724
3799
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3725
3800
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
@@ -3727,7 +3802,7 @@ var PerpetualsClient = (function () {
3727
3802
  return __generator(this, function (_g) {
3728
3803
  switch (_g.label) {
3729
3804
  case 0:
3730
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3805
+ publicKey = this.provider.wallet.publicKey;
3731
3806
  payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
3732
3807
  if (!payTokenCustodyConfig) {
3733
3808
  throw "payTokenCustodyConfig not found";
@@ -3739,7 +3814,7 @@ var PerpetualsClient = (function () {
3739
3814
  payToken = poolConfig.getTokenFromSymbol(payTokenSymbol);
3740
3815
  _g.label = 1;
3741
3816
  case 1:
3742
- _g.trys.push([1, 9, , 10]);
3817
+ _g.trys.push([1, 10, , 11]);
3743
3818
  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);
3744
3819
  lpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
3745
3820
  custodyAccountMetas = [];
@@ -3766,20 +3841,24 @@ var PerpetualsClient = (function () {
3766
3841
  isWritable: false,
3767
3842
  });
3768
3843
  }
3769
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3770
- if (!(payTokenSymbol == 'SOL')) return [3, 4];
3844
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3845
+ case 2:
3846
+ if (!(_g.sent())) {
3847
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3848
+ }
3849
+ if (!(payTokenSymbol == 'SOL')) return [3, 5];
3771
3850
  console.log("payTokenSymbol === SOL", payTokenSymbol);
3772
3851
  lamports = tokenAmountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3773
- if (!!skipBalanceChecks) return [3, 3];
3852
+ if (!!skipBalanceChecks) return [3, 4];
3774
3853
  _e = anchor_1.BN.bind;
3775
3854
  return [4, this.provider.connection.getBalance(publicKey)];
3776
- case 2:
3855
+ case 3:
3777
3856
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _g.sent()]))();
3778
3857
  if (unWrappedSolBalance.lt(lamports)) {
3779
3858
  throw "Insufficient SOL Funds";
3780
3859
  }
3781
- _g.label = 3;
3782
- case 3:
3860
+ _g.label = 4;
3861
+ case 4:
3783
3862
  if (!ephemeralSignerPubkey) {
3784
3863
  wrappedSolAccount = new web3_js_1.Keypair();
3785
3864
  additionalSigners.push(wrappedSolAccount);
@@ -3797,23 +3876,23 @@ var PerpetualsClient = (function () {
3797
3876
  postInstructions = [
3798
3877
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3799
3878
  ];
3800
- return [3, 7];
3801
- case 4:
3802
- if (!!skipBalanceChecks) return [3, 7];
3803
- return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3879
+ return [3, 8];
3804
3880
  case 5:
3881
+ if (!!skipBalanceChecks) return [3, 8];
3882
+ return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3883
+ case 6:
3805
3884
  if (!(_g.sent())) {
3806
3885
  throw "Insufficient Funds , token Account doesn't exist";
3807
3886
  }
3808
3887
  _f = anchor_1.BN.bind;
3809
3888
  return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
3810
- case 6:
3889
+ case 7:
3811
3890
  tokenAccountBalance = new (_f.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3812
3891
  if (tokenAccountBalance.lt(tokenAmountIn)) {
3813
3892
  throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
3814
3893
  }
3815
- _g.label = 7;
3816
- case 7:
3894
+ _g.label = 8;
3895
+ case 8:
3817
3896
  whitelistPda = this.findProgramAddress("whitelist", [publicKey]).publicKey;
3818
3897
  whitelistMeta = {
3819
3898
  pubkey: whitelistPda,
@@ -3845,15 +3924,15 @@ var PerpetualsClient = (function () {
3845
3924
  })
3846
3925
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3847
3926
  .instruction()];
3848
- case 8:
3927
+ case 9:
3849
3928
  instruction = _g.sent();
3850
3929
  instructions.push(instruction);
3851
- return [3, 10];
3852
- case 9:
3930
+ return [3, 11];
3931
+ case 10:
3853
3932
  err_5 = _g.sent();
3854
3933
  console.error("perpClient addLiquidity error:: ", err_5);
3855
3934
  throw err_5;
3856
- case 10: return [2, {
3935
+ case 11: return [2, {
3857
3936
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3858
3937
  additionalSigners: additionalSigners
3859
3938
  }];
@@ -3886,7 +3965,7 @@ var PerpetualsClient = (function () {
3886
3965
  inputToken = poolConfig.getTokenFromSymbol(inputSymbol);
3887
3966
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
3888
3967
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
3889
- if (!(inputSymbol == 'SOL')) return [3, 3];
3968
+ if (!(inputSymbol == 'SOL')) return [3, 4];
3890
3969
  console.log("inputSymbol === SOL", inputSymbol);
3891
3970
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3892
3971
  console.log("lamports:", lamports.toNumber());
@@ -3899,8 +3978,11 @@ var PerpetualsClient = (function () {
3899
3978
  throw "Insufficient SOL Funds";
3900
3979
  }
3901
3980
  _g.label = 2;
3902
- case 2:
3903
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3981
+ case 2: return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3982
+ case 3:
3983
+ if (!(_g.sent())) {
3984
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3985
+ }
3904
3986
  if (!ephemeralSignerPubkey) {
3905
3987
  wrappedSolAccount = new web3_js_1.Keypair();
3906
3988
  additionalSigners.push(wrappedSolAccount);
@@ -3918,24 +4000,24 @@ var PerpetualsClient = (function () {
3918
4000
  postInstructions = [
3919
4001
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3920
4002
  ];
3921
- return [3, 6];
3922
- case 3:
4003
+ return [3, 7];
4004
+ case 4:
3923
4005
  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);
3924
- if (!!skipBalanceChecks) return [3, 6];
4006
+ if (!!skipBalanceChecks) return [3, 7];
3925
4007
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3926
- case 4:
4008
+ case 5:
3927
4009
  if (!(_g.sent())) {
3928
4010
  throw "Insufficient Funds , token Account doesn't exist";
3929
4011
  }
3930
4012
  _b = anchor_1.BN.bind;
3931
4013
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3932
- case 5:
4014
+ case 6:
3933
4015
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3934
4016
  if (tokenAccountBalance.lt(amountIn)) {
3935
4017
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3936
4018
  }
3937
- _g.label = 6;
3938
- case 6:
4019
+ _g.label = 7;
4020
+ case 7:
3939
4021
  custodyAccountMetas = [];
3940
4022
  custodyOracleAccountMetas = [];
3941
4023
  markets = [];
@@ -3992,7 +4074,7 @@ var PerpetualsClient = (function () {
3992
4074
  })
3993
4075
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3994
4076
  .instruction()];
3995
- case 7:
4077
+ case 8:
3996
4078
  instruction = _g.sent();
3997
4079
  instructions.push(instruction);
3998
4080
  return [2, {
@@ -4475,12 +4557,12 @@ var PerpetualsClient = (function () {
4475
4557
  }
4476
4558
  });
4477
4559
  }); };
4478
- this.unstakeRequest = function (unstakeAmount, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
4560
+ this.unstakeRequest = function (unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4479
4561
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_13;
4480
4562
  return __generator(this, function (_a) {
4481
4563
  switch (_a.label) {
4482
4564
  case 0:
4483
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4565
+ publicKey = this.provider.wallet.publicKey;
4484
4566
  preInstructions = [];
4485
4567
  instructions = [];
4486
4568
  postInstructions = [];
@@ -4599,10 +4681,10 @@ var PerpetualsClient = (function () {
4599
4681
  args_1[_i - 3] = arguments[_i];
4600
4682
  }
4601
4683
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4602
- var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4684
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4603
4685
  if (createUserATA === void 0) { createUserATA = true; }
4604
- return __generator(this, function (_a) {
4605
- switch (_a.label) {
4686
+ return __generator(this, function (_b) {
4687
+ switch (_b.label) {
4606
4688
  case 0:
4607
4689
  publicKey = this.provider.wallet.publicKey;
4608
4690
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
@@ -4611,13 +4693,22 @@ var PerpetualsClient = (function () {
4611
4693
  instructions = [];
4612
4694
  postInstructions = [];
4613
4695
  additionalSigners = [];
4614
- _a.label = 1;
4696
+ _b.label = 1;
4615
4697
  case 1:
4616
- _a.trys.push([1, 3, , 4]);
4698
+ _b.trys.push([1, 5, , 6]);
4617
4699
  pool = poolConfig.poolAddress;
4618
4700
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4619
4701
  receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
4620
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
4702
+ _a = createUserATA;
4703
+ if (!_a) return [3, 3];
4704
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
4705
+ case 2:
4706
+ _a = !(_b.sent());
4707
+ _b.label = 3;
4708
+ case 3:
4709
+ if (_a) {
4710
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint));
4711
+ }
4621
4712
  tokenStakeAccounts = [];
4622
4713
  if (tokenStakeAccount) {
4623
4714
  tokenStakeAccounts.push({
@@ -4646,15 +4737,15 @@ var PerpetualsClient = (function () {
4646
4737
  })
4647
4738
  .remainingAccounts(__spreadArray([], tokenStakeAccounts, true))
4648
4739
  .instruction()];
4649
- case 2:
4650
- withdrawStakeInstruction = _a.sent();
4740
+ case 4:
4741
+ withdrawStakeInstruction = _b.sent();
4651
4742
  instructions.push(withdrawStakeInstruction);
4652
- return [3, 4];
4653
- case 3:
4654
- err_15 = _a.sent();
4743
+ return [3, 6];
4744
+ case 5:
4745
+ err_15 = _b.sent();
4655
4746
  console.log("perpClient withdrawStake error:: ", err_15);
4656
4747
  throw err_15;
4657
- case 4: return [2, {
4748
+ case 6: return [2, {
4658
4749
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4659
4750
  additionalSigners: additionalSigners
4660
4751
  }];
@@ -4713,21 +4804,29 @@ var PerpetualsClient = (function () {
4713
4804
  isWritable: false,
4714
4805
  });
4715
4806
  }
4716
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4717
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4718
- if (!(inTokenSymbol == 'SOL')) return [3, 3];
4807
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
4808
+ case 1:
4809
+ if (!(_f.sent())) {
4810
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4811
+ }
4812
+ return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
4813
+ case 2:
4814
+ if (!(_f.sent())) {
4815
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
4816
+ }
4817
+ if (!(inTokenSymbol == 'SOL')) return [3, 5];
4719
4818
  console.log("inTokenSymbol === SOL", inTokenSymbol);
4720
4819
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
4721
- if (!!skipBalanceChecks) return [3, 2];
4820
+ if (!!skipBalanceChecks) return [3, 4];
4722
4821
  _e = anchor_1.BN.bind;
4723
4822
  return [4, this.provider.connection.getBalance(publicKey)];
4724
- case 1:
4823
+ case 3:
4725
4824
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _f.sent()]))();
4726
4825
  if (unWrappedSolBalance.lt(lamports)) {
4727
4826
  throw "Insufficient SOL Funds";
4728
4827
  }
4729
- _f.label = 2;
4730
- case 2:
4828
+ _f.label = 4;
4829
+ case 4:
4731
4830
  if (!ephemeralSignerPubkey) {
4732
4831
  wrappedSolAccount = new web3_js_1.Keypair();
4733
4832
  additionalSigners.push(wrappedSolAccount);
@@ -4745,17 +4844,17 @@ var PerpetualsClient = (function () {
4745
4844
  postInstructions = [
4746
4845
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4747
4846
  ];
4748
- return [3, 5];
4749
- case 3:
4750
- if (!!skipBalanceChecks) return [3, 5];
4847
+ return [3, 7];
4848
+ case 5:
4849
+ if (!!skipBalanceChecks) return [3, 7];
4751
4850
  return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
4752
- case 4:
4851
+ case 6:
4753
4852
  if (!(_f.sent())) {
4754
4853
  throw "Insufficient Funds , token Account doesn't exist";
4755
4854
  }
4756
- _f.label = 5;
4757
- case 5:
4758
- _f.trys.push([5, 7, , 8]);
4855
+ _f.label = 7;
4856
+ case 7:
4857
+ _f.trys.push([7, 9, , 10]);
4759
4858
  if (enableHeapSizeIx) {
4760
4859
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4761
4860
  bytes: 64 * 1024,
@@ -4797,15 +4896,15 @@ var PerpetualsClient = (function () {
4797
4896
  })
4798
4897
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4799
4898
  .instruction()];
4800
- case 6:
4899
+ case 8:
4801
4900
  addCompoundingLiquidity = _f.sent();
4802
4901
  instructions.push(addCompoundingLiquidity);
4803
- return [3, 8];
4804
- case 7:
4902
+ return [3, 10];
4903
+ case 9:
4805
4904
  err_16 = _f.sent();
4806
4905
  console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4807
- return [3, 8];
4808
- case 8: return [2, {
4906
+ return [3, 10];
4907
+ case 10: return [2, {
4809
4908
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4810
4909
  additionalSigners: additionalSigners
4811
4910
  }];
@@ -4819,14 +4918,14 @@ var PerpetualsClient = (function () {
4819
4918
  args_1[_i - 5] = arguments[_i];
4820
4919
  }
4821
4920
  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) {
4822
- 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;
4921
+ 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;
4823
4922
  if (createUserATA === void 0) { createUserATA = true; }
4824
4923
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4825
4924
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4826
4925
  if (enableHeapSizeIx === void 0) { enableHeapSizeIx = true; }
4827
4926
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
4828
- return __generator(this, function (_e) {
4829
- switch (_e.label) {
4927
+ return __generator(this, function (_f) {
4928
+ switch (_f.label) {
4830
4929
  case 0:
4831
4930
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4832
4931
  preInstructions = [];
@@ -4837,35 +4936,45 @@ var PerpetualsClient = (function () {
4837
4936
  outCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outTokenSymbol).mintKey); });
4838
4937
  lpTokenMint = poolConfig.stakedLpTokenMint;
4839
4938
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4840
- if (outCustodyConfig.symbol == 'SOL') {
4841
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4842
- if (!ephemeralSignerPubkey) {
4843
- wrappedSolAccount = new web3_js_1.Keypair();
4844
- additionalSigners.push(wrappedSolAccount);
4845
- }
4846
- preInstructions = [
4847
- web3_js_1.SystemProgram.createAccount({
4848
- fromPubkey: publicKey,
4849
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4850
- lamports: lamports,
4851
- space: 165,
4852
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4853
- }),
4854
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4855
- ];
4856
- postInstructions = [
4857
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4858
- ];
4939
+ if (!(outCustodyConfig.symbol == 'SOL')) return [3, 1];
4940
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
4941
+ if (!ephemeralSignerPubkey) {
4942
+ wrappedSolAccount = new web3_js_1.Keypair();
4943
+ additionalSigners.push(wrappedSolAccount);
4859
4944
  }
4860
- else {
4861
- 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);
4862
- 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));
4945
+ preInstructions = [
4946
+ web3_js_1.SystemProgram.createAccount({
4947
+ fromPubkey: publicKey,
4948
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4949
+ lamports: lamports,
4950
+ space: 165,
4951
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
4952
+ }),
4953
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4954
+ ];
4955
+ postInstructions = [
4956
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4957
+ ];
4958
+ return [3, 4];
4959
+ case 1:
4960
+ 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);
4961
+ _a = createUserATA;
4962
+ if (!_a) return [3, 3];
4963
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
4964
+ case 2:
4965
+ _a = !(_f.sent());
4966
+ _f.label = 3;
4967
+ case 3:
4968
+ if (_a) {
4969
+ 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));
4863
4970
  }
4971
+ _f.label = 4;
4972
+ case 4:
4864
4973
  custodyAccountMetas = [];
4865
4974
  custodyOracleAccountMetas = [];
4866
4975
  markets = [];
4867
- for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
4868
- custody = _b[_a];
4976
+ for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4977
+ custody = _c[_b];
4869
4978
  custodyAccountMetas.push({
4870
4979
  pubkey: custody.custodyAccount,
4871
4980
  isSigner: false,
@@ -4877,8 +4986,8 @@ var PerpetualsClient = (function () {
4877
4986
  isWritable: false,
4878
4987
  });
4879
4988
  }
4880
- for (_c = 0, _d = poolConfig.markets; _c < _d.length; _c++) {
4881
- market = _d[_c];
4989
+ for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4990
+ market = _e[_d];
4882
4991
  markets.push({
4883
4992
  pubkey: market.marketAccount,
4884
4993
  isSigner: false,
@@ -4886,9 +4995,9 @@ var PerpetualsClient = (function () {
4886
4995
  });
4887
4996
  }
4888
4997
  compoundingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4889
- _e.label = 1;
4890
- case 1:
4891
- _e.trys.push([1, 3, , 4]);
4998
+ _f.label = 5;
4999
+ case 5:
5000
+ _f.trys.push([5, 7, , 8]);
4892
5001
  if (enableHeapSizeIx) {
4893
5002
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4894
5003
  bytes: 64 * 1024,
@@ -4930,15 +5039,15 @@ var PerpetualsClient = (function () {
4930
5039
  })
4931
5040
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4932
5041
  .instruction()];
4933
- case 2:
4934
- removeCompoundingLiquidity = _e.sent();
5042
+ case 6:
5043
+ removeCompoundingLiquidity = _f.sent();
4935
5044
  instructions.push(removeCompoundingLiquidity);
4936
- return [3, 4];
4937
- case 3:
4938
- err_17 = _e.sent();
5045
+ return [3, 8];
5046
+ case 7:
5047
+ err_17 = _f.sent();
4939
5048
  console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
4940
- return [3, 4];
4941
- case 4: return [2, {
5049
+ return [3, 8];
5050
+ case 8: return [2, {
4942
5051
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4943
5052
  additionalSigners: additionalSigners
4944
5053
  }];
@@ -4951,13 +5060,13 @@ var PerpetualsClient = (function () {
4951
5060
  for (var _i = 3; _i < arguments.length; _i++) {
4952
5061
  args_1[_i - 3] = arguments[_i];
4953
5062
  }
4954
- return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA, userPublicKey) {
4955
- 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;
5063
+ return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA) {
5064
+ 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;
4956
5065
  if (createUserATA === void 0) { createUserATA = true; }
4957
- return __generator(this, function (_f) {
4958
- switch (_f.label) {
5066
+ return __generator(this, function (_g) {
5067
+ switch (_g.label) {
4959
5068
  case 0:
4960
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5069
+ publicKey = this.provider.wallet.publicKey;
4961
5070
  preInstructions = [];
4962
5071
  instructions = [];
4963
5072
  postInstructions = [];
@@ -4966,18 +5075,27 @@ var PerpetualsClient = (function () {
4966
5075
  lpTokenMint = poolConfig.stakedLpTokenMint;
4967
5076
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4968
5077
  compoudingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4969
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4970
- flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
4971
- tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
4972
- tokenStakeAccounts = [];
4973
- _a = tokenStakeAccount;
5078
+ _a = createUserATA;
4974
5079
  if (!_a) return [3, 2];
4975
- return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5080
+ return [4, (0, utils_1.checkIfAccountExists)(compoudingTokenAccount, this.provider.connection)];
4976
5081
  case 1:
4977
- _a = (_f.sent());
4978
- _f.label = 2;
5082
+ _a = !(_g.sent());
5083
+ _g.label = 2;
4979
5084
  case 2:
4980
5085
  if (_a) {
5086
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint));
5087
+ }
5088
+ flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
5089
+ tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
5090
+ tokenStakeAccounts = [];
5091
+ _b = tokenStakeAccount;
5092
+ if (!_b) return [3, 4];
5093
+ return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5094
+ case 3:
5095
+ _b = (_g.sent());
5096
+ _g.label = 4;
5097
+ case 4:
5098
+ if (_b) {
4981
5099
  tokenStakeAccounts.push({
4982
5100
  pubkey: tokenStakeAccount,
4983
5101
  isSigner: false,
@@ -4988,8 +5106,8 @@ var PerpetualsClient = (function () {
4988
5106
  custodyAccountMetas = [];
4989
5107
  custodyOracleAccountMetas = [];
4990
5108
  markets = [];
4991
- for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4992
- custody = _c[_b];
5109
+ for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
5110
+ custody = _d[_c];
4993
5111
  custodyAccountMetas.push({
4994
5112
  pubkey: custody.custodyAccount,
4995
5113
  isSigner: false,
@@ -5001,17 +5119,17 @@ var PerpetualsClient = (function () {
5001
5119
  isWritable: false,
5002
5120
  });
5003
5121
  }
5004
- for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
5005
- market = _e[_d];
5122
+ for (_e = 0, _f = poolConfig.markets; _e < _f.length; _e++) {
5123
+ market = _f[_e];
5006
5124
  markets.push({
5007
5125
  pubkey: market.marketAccount,
5008
5126
  isSigner: false,
5009
5127
  isWritable: false,
5010
5128
  });
5011
5129
  }
5012
- _f.label = 3;
5013
- case 3:
5014
- _f.trys.push([3, 5, , 6]);
5130
+ _g.label = 5;
5131
+ case 5:
5132
+ _g.trys.push([5, 7, , 8]);
5015
5133
  return [4, this.program.methods
5016
5134
  .migrateStake({
5017
5135
  amount: amount
@@ -5036,15 +5154,15 @@ var PerpetualsClient = (function () {
5036
5154
  })
5037
5155
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), tokenStakeAccounts, true))
5038
5156
  .instruction()];
5039
- case 4:
5040
- migrateStake = _f.sent();
5157
+ case 6:
5158
+ migrateStake = _g.sent();
5041
5159
  instructions.push(migrateStake);
5042
- return [3, 6];
5043
- case 5:
5044
- err_18 = _f.sent();
5160
+ return [3, 8];
5161
+ case 7:
5162
+ err_18 = _g.sent();
5045
5163
  console.log("perpClient migrateStake error:: ", err_18);
5046
- return [3, 6];
5047
- case 6: return [2, {
5164
+ return [3, 8];
5165
+ case 8: return [2, {
5048
5166
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5049
5167
  additionalSigners: additionalSigners
5050
5168
  }];
@@ -5052,12 +5170,12 @@ var PerpetualsClient = (function () {
5052
5170
  });
5053
5171
  });
5054
5172
  };
5055
- this.migrateFlp = function (amount, rewardTokenMint, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
5173
+ this.migrateFlp = function (amount, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5056
5174
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_19;
5057
5175
  return __generator(this, function (_d) {
5058
5176
  switch (_d.label) {
5059
5177
  case 0:
5060
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5178
+ publicKey = this.provider.wallet.publicKey;
5061
5179
  preInstructions = [];
5062
5180
  instructions = [];
5063
5181
  postInstructions = [];
@@ -5221,10 +5339,14 @@ var PerpetualsClient = (function () {
5221
5339
  additionalSigners = [];
5222
5340
  _a.label = 1;
5223
5341
  case 1:
5224
- _a.trys.push([1, 3, , 4]);
5342
+ _a.trys.push([1, 4, , 5]);
5225
5343
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5226
5344
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5227
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5345
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5346
+ case 2:
5347
+ if (!(_a.sent())) {
5348
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5349
+ }
5228
5350
  return [4, this.program.methods
5229
5351
  .depositTokenStake({
5230
5352
  depositAmount: depositAmount
@@ -5244,15 +5366,15 @@ var PerpetualsClient = (function () {
5244
5366
  tokenMint: poolConfig.tokenMint,
5245
5367
  })
5246
5368
  .instruction()];
5247
- case 2:
5369
+ case 3:
5248
5370
  depositTokenStakeInstruction = _a.sent();
5249
5371
  instructions.push(depositTokenStakeInstruction);
5250
- return [3, 4];
5251
- case 3:
5372
+ return [3, 5];
5373
+ case 4:
5252
5374
  err_21 = _a.sent();
5253
5375
  console.log("perpClient depositStakingInstruction error:: ", err_21);
5254
5376
  throw err_21;
5255
- case 4: return [2, {
5377
+ case 5: return [2, {
5256
5378
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5257
5379
  additionalSigners: additionalSigners
5258
5380
  }];
@@ -5310,10 +5432,14 @@ var PerpetualsClient = (function () {
5310
5432
  additionalSigners = [];
5311
5433
  _a.label = 1;
5312
5434
  case 1:
5313
- _a.trys.push([1, 3, , 4]);
5435
+ _a.trys.push([1, 4, , 5]);
5314
5436
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5315
5437
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5316
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5438
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5439
+ case 2:
5440
+ if (!(_a.sent())) {
5441
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5442
+ }
5317
5443
  return [4, this.program.methods
5318
5444
  .unstakeTokenInstant({
5319
5445
  unstakeAmount: unstakeAmount
@@ -5332,15 +5458,15 @@ var PerpetualsClient = (function () {
5332
5458
  tokenMint: poolConfig.tokenMint,
5333
5459
  })
5334
5460
  .instruction()];
5335
- case 2:
5461
+ case 3:
5336
5462
  unstakeTokenInstantInstruction = _a.sent();
5337
5463
  instructions.push(unstakeTokenInstantInstruction);
5338
- return [3, 4];
5339
- case 3:
5464
+ return [3, 5];
5465
+ case 4:
5340
5466
  err_23 = _a.sent();
5341
5467
  console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5342
5468
  throw err_23;
5343
- case 4: return [2, {
5469
+ case 5: return [2, {
5344
5470
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5345
5471
  additionalSigners: additionalSigners
5346
5472
  }];
@@ -5358,10 +5484,14 @@ var PerpetualsClient = (function () {
5358
5484
  additionalSigners = [];
5359
5485
  _a.label = 1;
5360
5486
  case 1:
5361
- _a.trys.push([1, 3, , 4]);
5487
+ _a.trys.push([1, 4, , 5]);
5362
5488
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5363
5489
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5364
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5490
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5491
+ case 2:
5492
+ if (!(_a.sent())) {
5493
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5494
+ }
5365
5495
  return [4, this.program.methods
5366
5496
  .withdrawToken({
5367
5497
  withdrawRequestId: withdrawRequestId
@@ -5380,15 +5510,15 @@ var PerpetualsClient = (function () {
5380
5510
  tokenMint: poolConfig.tokenMint,
5381
5511
  })
5382
5512
  .instruction()];
5383
- case 2:
5513
+ case 3:
5384
5514
  withdrawTokenInstruction = _a.sent();
5385
5515
  instructions.push(withdrawTokenInstruction);
5386
- return [3, 4];
5387
- case 3:
5516
+ return [3, 5];
5517
+ case 4:
5388
5518
  err_24 = _a.sent();
5389
5519
  console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5390
5520
  throw err_24;
5391
- case 4: return [2, {
5521
+ case 5: return [2, {
5392
5522
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5393
5523
  additionalSigners: additionalSigners
5394
5524
  }];
@@ -5440,23 +5570,32 @@ var PerpetualsClient = (function () {
5440
5570
  for (var _i = 2; _i < arguments.length; _i++) {
5441
5571
  args_1[_i - 2] = arguments[_i];
5442
5572
  }
5443
- return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA, userPublicKey) {
5444
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, collectTokenRewardInstruction, err_26;
5573
+ return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA) {
5574
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_26;
5445
5575
  if (createUserATA === void 0) { createUserATA = true; }
5446
- return __generator(this, function (_a) {
5447
- switch (_a.label) {
5576
+ return __generator(this, function (_b) {
5577
+ switch (_b.label) {
5448
5578
  case 0:
5449
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5579
+ publicKey = this.provider.wallet.publicKey;
5450
5580
  preInstructions = [];
5451
5581
  instructions = [];
5452
5582
  postInstructions = [];
5453
5583
  additionalSigners = [];
5454
- _a.label = 1;
5584
+ _b.label = 1;
5455
5585
  case 1:
5456
- _a.trys.push([1, 3, , 4]);
5586
+ _b.trys.push([1, 5, , 6]);
5457
5587
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5458
5588
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5459
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5589
+ _a = createUserATA;
5590
+ if (!_a) return [3, 3];
5591
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5592
+ case 2:
5593
+ _a = !(_b.sent());
5594
+ _b.label = 3;
5595
+ case 3:
5596
+ if (_a) {
5597
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint));
5598
+ }
5460
5599
  return [4, this.program.methods
5461
5600
  .collectTokenReward({})
5462
5601
  .accounts({
@@ -5473,15 +5612,15 @@ var PerpetualsClient = (function () {
5473
5612
  tokenMint: poolConfig.tokenMint,
5474
5613
  })
5475
5614
  .instruction()];
5476
- case 2:
5477
- collectTokenRewardInstruction = _a.sent();
5615
+ case 4:
5616
+ collectTokenRewardInstruction = _b.sent();
5478
5617
  instructions.push(collectTokenRewardInstruction);
5479
- return [3, 4];
5480
- case 3:
5481
- err_26 = _a.sent();
5618
+ return [3, 6];
5619
+ case 5:
5620
+ err_26 = _b.sent();
5482
5621
  console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5483
5622
  throw err_26;
5484
- case 4: return [2, {
5623
+ case 6: return [2, {
5485
5624
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5486
5625
  additionalSigners: additionalSigners
5487
5626
  }];
@@ -5495,23 +5634,32 @@ var PerpetualsClient = (function () {
5495
5634
  args_1[_i - 3] = arguments[_i];
5496
5635
  }
5497
5636
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5498
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, collectRevenueInstruction, err_27;
5637
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_27;
5499
5638
  if (createUserATA === void 0) { createUserATA = true; }
5500
- return __generator(this, function (_a) {
5501
- switch (_a.label) {
5639
+ return __generator(this, function (_b) {
5640
+ switch (_b.label) {
5502
5641
  case 0:
5503
5642
  publicKey = this.provider.wallet.publicKey;
5504
5643
  preInstructions = [];
5505
5644
  instructions = [];
5506
5645
  postInstructions = [];
5507
5646
  additionalSigners = [];
5508
- _a.label = 1;
5647
+ _b.label = 1;
5509
5648
  case 1:
5510
- _a.trys.push([1, 3, , 4]);
5649
+ _b.trys.push([1, 5, , 6]);
5511
5650
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5512
5651
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5513
5652
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, owner, true);
5514
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
5653
+ _a = createUserATA;
5654
+ if (!_a) return [3, 3];
5655
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5656
+ case 2:
5657
+ _a = !(_b.sent());
5658
+ _b.label = 3;
5659
+ case 3:
5660
+ if (_a) {
5661
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint));
5662
+ }
5515
5663
  return [4, this.program.methods
5516
5664
  .collectRevenue({})
5517
5665
  .accounts({
@@ -5528,15 +5676,15 @@ var PerpetualsClient = (function () {
5528
5676
  receivingTokenMint: rewardCustodyMint,
5529
5677
  })
5530
5678
  .instruction()];
5531
- case 2:
5532
- collectRevenueInstruction = _a.sent();
5679
+ case 4:
5680
+ collectRevenueInstruction = _b.sent();
5533
5681
  instructions.push(collectRevenueInstruction);
5534
- return [3, 4];
5535
- case 3:
5536
- err_27 = _a.sent();
5682
+ return [3, 6];
5683
+ case 5:
5684
+ err_27 = _b.sent();
5537
5685
  console.log("perpClient collectRevenueInstruction error:: ", err_27);
5538
5686
  throw err_27;
5539
- case 4: return [2, {
5687
+ case 6: return [2, {
5540
5688
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5541
5689
  additionalSigners: additionalSigners
5542
5690
  }];
@@ -5550,23 +5698,32 @@ var PerpetualsClient = (function () {
5550
5698
  args_1[_i - 3] = arguments[_i];
5551
5699
  }
5552
5700
  return __awaiter(_this, __spreadArray([owner_1, rebateSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rebateSymbol, poolConfig, createUserATA) {
5553
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, collectRebateInstruction, err_28;
5701
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, _a, collectRebateInstruction, err_28;
5554
5702
  if (createUserATA === void 0) { createUserATA = true; }
5555
- return __generator(this, function (_a) {
5556
- switch (_a.label) {
5703
+ return __generator(this, function (_b) {
5704
+ switch (_b.label) {
5557
5705
  case 0:
5558
5706
  publicKey = this.provider.wallet.publicKey;
5559
5707
  preInstructions = [];
5560
5708
  instructions = [];
5561
5709
  postInstructions = [];
5562
5710
  additionalSigners = [];
5563
- _a.label = 1;
5711
+ _b.label = 1;
5564
5712
  case 1:
5565
- _a.trys.push([1, 3, , 4]);
5713
+ _b.trys.push([1, 5, , 6]);
5566
5714
  rebateMint = poolConfig.getTokenFromSymbol(rebateSymbol).mintKey;
5567
5715
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5568
5716
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rebateMint, owner, true);
5569
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rebateMint, spl_token_1.TOKEN_PROGRAM_ID));
5717
+ _a = createUserATA;
5718
+ if (!_a) return [3, 3];
5719
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5720
+ case 2:
5721
+ _a = !(_b.sent());
5722
+ _b.label = 3;
5723
+ case 3:
5724
+ if (_a) {
5725
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rebateMint));
5726
+ }
5570
5727
  return [4, this.program.methods
5571
5728
  .collectRebate()
5572
5729
  .accounts({
@@ -5583,15 +5740,15 @@ var PerpetualsClient = (function () {
5583
5740
  receivingTokenMint: rebateMint,
5584
5741
  })
5585
5742
  .instruction()];
5586
- case 2:
5587
- collectRebateInstruction = _a.sent();
5743
+ case 4:
5744
+ collectRebateInstruction = _b.sent();
5588
5745
  instructions.push(collectRebateInstruction);
5589
- return [3, 4];
5590
- case 3:
5591
- err_28 = _a.sent();
5746
+ return [3, 6];
5747
+ case 5:
5748
+ err_28 = _b.sent();
5592
5749
  console.log("perpClient collectRebateInstruction error:: ", err_28);
5593
5750
  throw err_28;
5594
- case 4: return [2, {
5751
+ case 6: return [2, {
5595
5752
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5596
5753
  additionalSigners: additionalSigners
5597
5754
  }];
@@ -5651,14 +5808,14 @@ var PerpetualsClient = (function () {
5651
5808
  for (var _i = 11; _i < arguments.length; _i++) {
5652
5809
  args_1[_i - 11] = arguments[_i];
5653
5810
  }
5654
- 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) {
5811
+ 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) {
5655
5812
  var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_30;
5656
5813
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5657
5814
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5658
5815
  return __generator(this, function (_c) {
5659
5816
  switch (_c.label) {
5660
5817
  case 0:
5661
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5818
+ publicKey = this.provider.wallet.publicKey;
5662
5819
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5663
5820
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5664
5821
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5774,14 +5931,14 @@ var PerpetualsClient = (function () {
5774
5931
  for (var _i = 11; _i < arguments.length; _i++) {
5775
5932
  args_1[_i - 11] = arguments[_i];
5776
5933
  }
5777
- 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) {
5778
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, positionAccount, orderAccount, editLimitOrder, err_31;
5934
+ 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) {
5935
+ var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_31;
5779
5936
  if (createUserATA === void 0) { createUserATA = true; }
5780
5937
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5781
- return __generator(this, function (_a) {
5782
- switch (_a.label) {
5938
+ return __generator(this, function (_b) {
5939
+ switch (_b.label) {
5783
5940
  case 0:
5784
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5941
+ publicKey = this.provider.wallet.publicKey;
5785
5942
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5786
5943
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5787
5944
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5791,33 +5948,43 @@ var PerpetualsClient = (function () {
5791
5948
  instructions = [];
5792
5949
  postInstructions = [];
5793
5950
  additionalSigners = [];
5794
- _a.label = 1;
5951
+ _b.label = 1;
5795
5952
  case 1:
5796
- _a.trys.push([1, 3, , 4]);
5797
- if (reserveSymbol == 'SOL') {
5798
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
5799
- if (!ephemeralSignerPubkey) {
5800
- wrappedSolAccount = new web3_js_1.Keypair();
5801
- additionalSigners.push(wrappedSolAccount);
5802
- }
5803
- preInstructions = [
5804
- web3_js_1.SystemProgram.createAccount({
5805
- fromPubkey: publicKey,
5806
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5807
- lamports: lamports,
5808
- space: 165,
5809
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5810
- }),
5811
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5812
- ];
5813
- postInstructions = [
5814
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5815
- ];
5953
+ _b.trys.push([1, 7, , 8]);
5954
+ if (!(reserveSymbol == 'SOL')) return [3, 2];
5955
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
5956
+ if (!ephemeralSignerPubkey) {
5957
+ wrappedSolAccount = new web3_js_1.Keypair();
5958
+ additionalSigners.push(wrappedSolAccount);
5816
5959
  }
5817
- else {
5818
- 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);
5819
- 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));
5960
+ preInstructions = [
5961
+ web3_js_1.SystemProgram.createAccount({
5962
+ fromPubkey: publicKey,
5963
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5964
+ lamports: lamports,
5965
+ space: 165,
5966
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
5967
+ }),
5968
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5969
+ ];
5970
+ postInstructions = [
5971
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5972
+ ];
5973
+ return [3, 5];
5974
+ case 2:
5975
+ 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);
5976
+ _a = createUserATA;
5977
+ if (!_a) return [3, 4];
5978
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
5979
+ case 3:
5980
+ _a = !(_b.sent());
5981
+ _b.label = 4;
5982
+ case 4:
5983
+ if (_a) {
5984
+ 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));
5820
5985
  }
5986
+ _b.label = 5;
5987
+ case 5:
5821
5988
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
5822
5989
  orderAccount = poolConfig.getOrderFromMarketPk(publicKey, marketAccount);
5823
5990
  return [4, this.program.methods
@@ -5851,15 +6018,15 @@ var PerpetualsClient = (function () {
5851
6018
  receivingMint: poolConfig.getTokenFromSymbol(reserveSymbol).mintKey
5852
6019
  })
5853
6020
  .instruction()];
5854
- case 2:
5855
- editLimitOrder = _a.sent();
6021
+ case 6:
6022
+ editLimitOrder = _b.sent();
5856
6023
  instructions.push(editLimitOrder);
5857
- return [3, 4];
5858
- case 3:
5859
- err_31 = _a.sent();
6024
+ return [3, 8];
6025
+ case 7:
6026
+ err_31 = _b.sent();
5860
6027
  console.log("perpClient editLimitOrder error:: ", err_31);
5861
6028
  throw err_31;
5862
- case 4: return [2, {
6029
+ case 8: return [2, {
5863
6030
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5864
6031
  additionalSigners: additionalSigners
5865
6032
  }];
@@ -6008,12 +6175,12 @@ var PerpetualsClient = (function () {
6008
6175
  });
6009
6176
  });
6010
6177
  };
6011
- this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6178
+ this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6012
6179
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_34;
6013
6180
  return __generator(this, function (_a) {
6014
6181
  switch (_a.label) {
6015
6182
  case 0:
6016
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6183
+ publicKey = this.provider.wallet.publicKey;
6017
6184
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6018
6185
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6019
6186
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6067,12 +6234,12 @@ var PerpetualsClient = (function () {
6067
6234
  }
6068
6235
  });
6069
6236
  }); };
6070
- this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6237
+ this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6071
6238
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_35;
6072
6239
  return __generator(this, function (_a) {
6073
6240
  switch (_a.label) {
6074
6241
  case 0:
6075
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6242
+ publicKey = this.provider.wallet.publicKey;
6076
6243
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6077
6244
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6078
6245
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6125,12 +6292,12 @@ var PerpetualsClient = (function () {
6125
6292
  }
6126
6293
  });
6127
6294
  }); };
6128
- this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6295
+ this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6129
6296
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_36;
6130
6297
  return __generator(this, function (_a) {
6131
6298
  switch (_a.label) {
6132
6299
  case 0:
6133
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6300
+ publicKey = this.provider.wallet.publicKey;
6134
6301
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6135
6302
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6136
6303
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6169,12 +6336,12 @@ var PerpetualsClient = (function () {
6169
6336
  }
6170
6337
  });
6171
6338
  }); };
6172
- this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6339
+ this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6173
6340
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_37;
6174
6341
  return __generator(this, function (_a) {
6175
6342
  switch (_a.label) {
6176
6343
  case 0:
6177
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6344
+ publicKey = this.provider.wallet.publicKey;
6178
6345
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6179
6346
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6180
6347
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6217,13 +6384,13 @@ var PerpetualsClient = (function () {
6217
6384
  args_1[_i - 9] = arguments[_i];
6218
6385
  }
6219
6386
  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) {
6220
- 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;
6387
+ 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;
6221
6388
  if (createUserATA === void 0) { createUserATA = true; }
6222
6389
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6223
6390
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6224
6391
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6225
- return __generator(this, function (_c) {
6226
- switch (_c.label) {
6392
+ return __generator(this, function (_e) {
6393
+ switch (_e.label) {
6227
6394
  case 0:
6228
6395
  payerPubkey = this.provider.wallet.publicKey;
6229
6396
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6236,23 +6403,42 @@ var PerpetualsClient = (function () {
6236
6403
  additionalSigners = [];
6237
6404
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
6238
6405
  receivingToken = poolConfig.getTokenFromSymbol(receivingSymbol);
6239
- _c.label = 1;
6406
+ _e.label = 1;
6240
6407
  case 1:
6241
- _c.trys.push([1, 3, , 4]);
6242
- if (false) {
6408
+ _e.trys.push([1, 9, , 10]);
6409
+ if (!false) return [3, 2];
6410
+ return [3, 7];
6411
+ case 2:
6412
+ 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);
6413
+ _a = createUserATA;
6414
+ if (!_a) return [3, 4];
6415
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6416
+ case 3:
6417
+ _a = !(_e.sent());
6418
+ _e.label = 4;
6419
+ case 4:
6420
+ if (_a) {
6421
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey));
6243
6422
  }
6244
- else {
6245
- 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);
6246
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, spl_token_1.TOKEN_PROGRAM_ID));
6247
- 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);
6248
- 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));
6423
+ 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);
6424
+ _b = createUserATA;
6425
+ if (!_b) return [3, 6];
6426
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccountCollateral, this.provider.connection)];
6427
+ case 5:
6428
+ _b = !(_e.sent());
6429
+ _e.label = 6;
6430
+ case 6:
6431
+ if (_b) {
6432
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
6249
6433
  }
6434
+ _e.label = 7;
6435
+ case 7:
6250
6436
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6251
6437
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6252
6438
  custodyAccountMetas = [];
6253
6439
  custodyOracleAccountMetas = [];
6254
- for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
6255
- custody = _b[_a];
6440
+ for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
6441
+ custody = _d[_c];
6256
6442
  custodyAccountMetas.push({
6257
6443
  pubkey: custody.custodyAccount,
6258
6444
  isSigner: false,
@@ -6299,15 +6485,15 @@ var PerpetualsClient = (function () {
6299
6485
  })
6300
6486
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6301
6487
  .instruction()];
6302
- case 2:
6303
- executeTriggerWithSwap = _c.sent();
6488
+ case 8:
6489
+ executeTriggerWithSwap = _e.sent();
6304
6490
  instructions.push(executeTriggerWithSwap);
6305
- return [3, 4];
6306
- case 3:
6307
- err_38 = _c.sent();
6491
+ return [3, 10];
6492
+ case 9:
6493
+ err_38 = _e.sent();
6308
6494
  console.log("perpClient executeTriggerWithSwap error:: ", err_38);
6309
6495
  throw err_38;
6310
- case 4: return [2, {
6496
+ case 10: return [2, {
6311
6497
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6312
6498
  additionalSigners: additionalSigners
6313
6499
  }];
@@ -6321,13 +6507,13 @@ var PerpetualsClient = (function () {
6321
6507
  args_1[_i - 8] = arguments[_i];
6322
6508
  }
6323
6509
  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) {
6324
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeTriggerOrder, err_39;
6510
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_39;
6325
6511
  if (createUserATA === void 0) { createUserATA = true; }
6326
6512
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6327
6513
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6328
6514
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6329
- return __generator(this, function (_a) {
6330
- switch (_a.label) {
6515
+ return __generator(this, function (_b) {
6516
+ switch (_b.label) {
6331
6517
  case 0:
6332
6518
  payerPubkey = this.provider.wallet.publicKey;
6333
6519
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6337,15 +6523,25 @@ var PerpetualsClient = (function () {
6337
6523
  instructions = [];
6338
6524
  postInstructions = [];
6339
6525
  additionalSigners = [];
6340
- _a.label = 1;
6526
+ _b.label = 1;
6341
6527
  case 1:
6342
- _a.trys.push([1, 3, , 4]);
6343
- if (false) {
6344
- }
6345
- else {
6346
- 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);
6347
- 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));
6528
+ _b.trys.push([1, 7, , 8]);
6529
+ if (!false) return [3, 2];
6530
+ return [3, 5];
6531
+ case 2:
6532
+ 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);
6533
+ _a = createUserATA;
6534
+ if (!_a) return [3, 4];
6535
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6536
+ case 3:
6537
+ _a = !(_b.sent());
6538
+ _b.label = 4;
6539
+ case 4:
6540
+ if (_a) {
6541
+ 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));
6348
6542
  }
6543
+ _b.label = 5;
6544
+ case 5:
6349
6545
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6350
6546
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6351
6547
  return [4, this.program.methods
@@ -6377,15 +6573,15 @@ var PerpetualsClient = (function () {
6377
6573
  })
6378
6574
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6379
6575
  .instruction()];
6380
- case 2:
6381
- executeTriggerOrder = _a.sent();
6576
+ case 6:
6577
+ executeTriggerOrder = _b.sent();
6382
6578
  instructions.push(executeTriggerOrder);
6383
- return [3, 4];
6384
- case 3:
6385
- err_39 = _a.sent();
6579
+ return [3, 8];
6580
+ case 7:
6581
+ err_39 = _b.sent();
6386
6582
  console.log("perpClient executeTriggerOrder error:: ", err_39);
6387
6583
  throw err_39;
6388
- case 4: return [2, {
6584
+ case 8: return [2, {
6389
6585
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6390
6586
  additionalSigners: additionalSigners
6391
6587
  }];
@@ -6398,16 +6594,16 @@ var PerpetualsClient = (function () {
6398
6594
  for (var _i = 5; _i < arguments.length; _i++) {
6399
6595
  args_1[_i - 5] = arguments[_i];
6400
6596
  }
6401
- 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) {
6402
- 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;
6597
+ 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) {
6598
+ 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;
6403
6599
  if (useFeesPool === void 0) { useFeesPool = false; }
6404
6600
  if (createUserATA === void 0) { createUserATA = true; }
6405
6601
  if (unWrapSol === void 0) { unWrapSol = false; }
6406
6602
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
6407
6603
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6408
6604
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
6409
- return __generator(this, function (_f) {
6410
- switch (_f.label) {
6605
+ return __generator(this, function (_g) {
6606
+ switch (_g.label) {
6411
6607
  case 0:
6412
6608
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
6413
6609
  if (!userInputCustodyConfig) {
@@ -6417,26 +6613,31 @@ var PerpetualsClient = (function () {
6417
6613
  if (!userOutputCustodyConfig) {
6418
6614
  throw "userOutputCustodyConfig not found";
6419
6615
  }
6420
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6616
+ publicKey = this.provider.wallet.publicKey;
6421
6617
  preInstructions = [];
6422
6618
  instructions = [];
6423
6619
  postInstructions = [];
6424
6620
  additionalSigners = [];
6425
- if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
6621
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 5];
6426
6622
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
6427
6623
  case 1:
6428
- wsolAssociatedTokenAccount = _f.sent();
6429
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID));
6430
- if (!!skipBalanceChecks) return [3, 3];
6624
+ wsolAssociatedTokenAccount = _g.sent();
6625
+ return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
6626
+ case 2:
6627
+ wsolATAExist = _g.sent();
6628
+ if (!wsolATAExist) {
6629
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
6630
+ }
6631
+ if (!!skipBalanceChecks) return [3, 4];
6431
6632
  _a = anchor_1.BN.bind;
6432
6633
  return [4, this.provider.connection.getBalance(publicKey)];
6433
- case 2:
6434
- unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
6634
+ case 3:
6635
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _g.sent()]))();
6435
6636
  if (unWrappedSolBalance.lt(amountIn)) {
6436
6637
  throw "Insufficient SOL Funds";
6437
6638
  }
6438
- _f.label = 3;
6439
- case 3:
6639
+ _g.label = 4;
6640
+ case 4:
6440
6641
  instructions.push(web3_js_1.SystemProgram.transfer({
6441
6642
  fromPubkey: publicKey,
6442
6643
  toPubkey: wsolAssociatedTokenAccount,
@@ -6446,7 +6647,7 @@ var PerpetualsClient = (function () {
6446
6647
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6447
6648
  additionalSigners: additionalSigners
6448
6649
  }];
6449
- case 4:
6650
+ case 5:
6450
6651
  if (userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL') {
6451
6652
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
6452
6653
  wsolAssociatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, publicKey, true);
@@ -6457,20 +6658,20 @@ var PerpetualsClient = (function () {
6457
6658
  additionalSigners: additionalSigners
6458
6659
  }];
6459
6660
  }
6460
- _f.label = 5;
6461
- case 5:
6462
- _f.trys.push([5, 16, , 17]);
6463
- if (!(userInputTokenSymbol == 'SOL')) return [3, 8];
6661
+ _g.label = 6;
6662
+ case 6:
6663
+ _g.trys.push([6, 19, , 20]);
6664
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
6464
6665
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
6465
6666
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
6466
- case 6:
6467
- accCreationLamports = (_f.sent());
6667
+ case 7:
6668
+ accCreationLamports = (_g.sent());
6468
6669
  console.log("accCreationLamports:", accCreationLamports);
6469
6670
  lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
6470
6671
  _b = anchor_1.BN.bind;
6471
6672
  return [4, this.provider.connection.getBalance(publicKey)];
6472
- case 7:
6473
- unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
6673
+ case 8:
6674
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _g.sent()]))();
6474
6675
  if (unWrappedSolBalance.lt(amountIn)) {
6475
6676
  throw "Insufficient SOL Funds";
6476
6677
  }
@@ -6492,25 +6693,25 @@ var PerpetualsClient = (function () {
6492
6693
  postInstructions = [
6493
6694
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6494
6695
  ];
6495
- return [3, 11];
6496
- case 8:
6696
+ return [3, 12];
6697
+ case 9:
6497
6698
  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);
6498
6699
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
6499
- case 9:
6500
- if (!(_f.sent())) {
6700
+ case 10:
6701
+ if (!(_g.sent())) {
6501
6702
  throw "Insufficient Funds , Token Account doesn't exist";
6502
6703
  }
6503
- if (!!skipBalanceChecks) return [3, 11];
6704
+ if (!!skipBalanceChecks) return [3, 12];
6504
6705
  _c = anchor_1.BN.bind;
6505
6706
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
6506
- case 10:
6507
- tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
6707
+ case 11:
6708
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
6508
6709
  if (tokenAccountBalance.lt(amountIn)) {
6509
6710
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
6510
6711
  }
6511
- _f.label = 11;
6512
- case 11:
6513
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 12];
6712
+ _g.label = 12;
6713
+ case 12:
6714
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 13];
6514
6715
  lamports = (this.minimumBalanceForRentExemptAccountLamports);
6515
6716
  if (!ephemeralSignerPubkey) {
6516
6717
  wrappedSolAccount = new web3_js_1.Keypair();
@@ -6530,17 +6731,26 @@ var PerpetualsClient = (function () {
6530
6731
  postInstructions = [
6531
6732
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6532
6733
  ];
6533
- return [3, 14];
6534
- 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)];
6535
- case 13:
6536
- userOutputTokenAccount = _f.sent();
6537
- 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));
6538
- _f.label = 14;
6734
+ return [3, 17];
6735
+ 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)];
6539
6736
  case 14:
6737
+ userOutputTokenAccount = _g.sent();
6738
+ _d = createUserATA;
6739
+ if (!_d) return [3, 16];
6740
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
6741
+ case 15:
6742
+ _d = !(_g.sent());
6743
+ _g.label = 16;
6744
+ case 16:
6745
+ if (_d) {
6746
+ 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));
6747
+ }
6748
+ _g.label = 17;
6749
+ case 17:
6540
6750
  custodyAccountMetas = [];
6541
6751
  custodyOracleAccountMetas = [];
6542
- for (_d = 0, _e = poolConfig.custodies; _d < _e.length; _d++) {
6543
- custody = _e[_d];
6752
+ for (_e = 0, _f = poolConfig.custodies; _e < _f.length; _e++) {
6753
+ custody = _f[_e];
6544
6754
  custodyAccountMetas.push({
6545
6755
  pubkey: custody.custodyAccount,
6546
6756
  isSigner: false,
@@ -6588,19 +6798,19 @@ var PerpetualsClient = (function () {
6588
6798
  })
6589
6799
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
6590
6800
  .instruction()];
6591
- case 15:
6592
- inx = _f.sent();
6801
+ case 18:
6802
+ inx = _g.sent();
6593
6803
  instructions.push(inx);
6594
6804
  if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
6595
6805
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
6596
6806
  instructions.push(closeWsolATAIns);
6597
6807
  }
6598
- return [3, 17];
6599
- case 16:
6600
- err_40 = _f.sent();
6808
+ return [3, 20];
6809
+ case 19:
6810
+ err_40 = _g.sent();
6601
6811
  console.error("perpClient Swap error:: ", err_40);
6602
6812
  throw err_40;
6603
- case 17: return [2, {
6813
+ case 20: return [2, {
6604
6814
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6605
6815
  additionalSigners: additionalSigners
6606
6816
  }];
@@ -7642,11 +7852,15 @@ var PerpetualsClient = (function () {
7642
7852
  additionalSigners = [];
7643
7853
  _a.label = 1;
7644
7854
  case 1:
7645
- _a.trys.push([1, 4, , 5]);
7855
+ _a.trys.push([1, 5, , 6]);
7646
7856
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7647
7857
  case 2:
7648
7858
  receivingTokenAccount = _a.sent();
7649
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7859
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7860
+ case 3:
7861
+ if (!(_a.sent())) {
7862
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7863
+ }
7650
7864
  return [4, this.program.methods
7651
7865
  .withdrawInstantFees({})
7652
7866
  .accounts({
@@ -7661,15 +7875,15 @@ var PerpetualsClient = (function () {
7661
7875
  receivingTokenMint: poolConfig.tokenMint,
7662
7876
  })
7663
7877
  .instruction()];
7664
- case 3:
7878
+ case 4:
7665
7879
  withdrawInstantFeeInstruction = _a.sent();
7666
7880
  instructions.push(withdrawInstantFeeInstruction);
7667
- return [3, 5];
7668
- case 4:
7881
+ return [3, 6];
7882
+ case 5:
7669
7883
  err_57 = _a.sent();
7670
7884
  console.log("perpClient withdrawInstantFeeInstruction error:: ", err_57);
7671
7885
  throw err_57;
7672
- case 5: return [2, {
7886
+ case 6: return [2, {
7673
7887
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7674
7888
  additionalSigners: additionalSigners
7675
7889
  }];
@@ -7688,11 +7902,15 @@ var PerpetualsClient = (function () {
7688
7902
  additionalSigners = [];
7689
7903
  _a.label = 1;
7690
7904
  case 1:
7691
- _a.trys.push([1, 4, , 5]);
7905
+ _a.trys.push([1, 5, , 6]);
7692
7906
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7693
7907
  case 2:
7694
7908
  receivingTokenAccount = _a.sent();
7695
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7909
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7910
+ case 3:
7911
+ if (!(_a.sent())) {
7912
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7913
+ }
7696
7914
  return [4, this.program.methods
7697
7915
  .withdrawUnclaimedTokens({})
7698
7916
  .accounts({
@@ -7707,15 +7925,15 @@ var PerpetualsClient = (function () {
7707
7925
  receivingTokenMint: poolConfig.tokenMint,
7708
7926
  })
7709
7927
  .instruction()];
7710
- case 3:
7928
+ case 4:
7711
7929
  withdrawUnclaimedTokensInstruction = _a.sent();
7712
7930
  instructions.push(withdrawUnclaimedTokensInstruction);
7713
- return [3, 5];
7714
- case 4:
7931
+ return [3, 6];
7932
+ case 5:
7715
7933
  err_58 = _a.sent();
7716
7934
  console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_58);
7717
7935
  throw err_58;
7718
- case 5: return [2, {
7936
+ case 6: return [2, {
7719
7937
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7720
7938
  additionalSigners: additionalSigners
7721
7939
  }];