flash-sdk 11.6.3-alpha.0 → 11.8.0-alpha.0

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,20 +1631,11 @@ 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
- 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
- console.log("priceDiffProfit: ", priceDiffProfit);
1637
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1638
- }
1639
- else {
1640
- console.log("0 - 0");
1641
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1642
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1643
- }
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 });
1644
1636
  }
1645
1637
  }
1646
1638
  else {
1647
- console.log("0-0");
1648
1639
  priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1649
1640
  priceDiffLoss = new OraclePrice_1.OraclePrice({ price: positionEntryPrice.price.sub(exitOraclePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1650
1641
  }
@@ -1656,14 +1647,8 @@ var PerpetualsClient = (function () {
1656
1647
  priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1657
1648
  }
1658
1649
  else {
1659
- if (positionEntryPrice.price.gt(positionAccount.referencePrice.price)) {
1660
- 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 });
1661
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1662
- }
1663
- else {
1664
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1665
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1666
- }
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 });
1667
1652
  }
1668
1653
  }
1669
1654
  else {
@@ -2719,7 +2704,7 @@ var PerpetualsClient = (function () {
2719
2704
  for (var _i = 8; _i < arguments.length; _i++) {
2720
2705
  args_1[_i - 8] = arguments[_i];
2721
2706
  }
2722
- 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) {
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) {
2723
2708
  var publicKey, targetCustodyConfig, collateralCustodyConfig, collateralToken, marketAccount, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
2724
2709
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2725
2710
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2728,7 +2713,7 @@ var PerpetualsClient = (function () {
2728
2713
  return __generator(this, function (_c) {
2729
2714
  switch (_c.label) {
2730
2715
  case 0:
2731
- publicKey = this.provider.wallet.publicKey;
2716
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2732
2717
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
2733
2718
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2734
2719
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
@@ -2833,59 +2818,48 @@ var PerpetualsClient = (function () {
2833
2818
  for (var _i = 6; _i < arguments.length; _i++) {
2834
2819
  args_1[_i - 6] = arguments[_i];
2835
2820
  }
2836
- 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) {
2837
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
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;
2838
2823
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2839
2824
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
2840
2825
  if (createUserATA === void 0) { createUserATA = true; }
2841
2826
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
2842
2827
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2843
- return __generator(this, function (_b) {
2844
- switch (_b.label) {
2828
+ return __generator(this, function (_a) {
2829
+ switch (_a.label) {
2845
2830
  case 0:
2846
- console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
2847
- publicKey = this.provider.wallet.publicKey;
2831
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2848
2832
  preInstructions = [];
2849
2833
  instructions = [];
2850
2834
  postInstructions = [];
2851
2835
  additionalSigners = [];
2852
- _b.label = 1;
2836
+ _a.label = 1;
2853
2837
  case 1:
2854
- _b.trys.push([1, 7, , 8]);
2855
- if (!(collateralSymbol == 'SOL')) return [3, 2];
2856
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
2857
- if (!ephemeralSignerPubkey) {
2858
- wrappedSolAccount = new web3_js_1.Keypair();
2859
- additionalSigners.push(wrappedSolAccount);
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
+ ];
2860
2858
  }
2861
- preInstructions = [
2862
- web3_js_1.SystemProgram.createAccount({
2863
- fromPubkey: publicKey,
2864
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2865
- lamports: lamports,
2866
- space: 165,
2867
- programId: spl_token_1.TOKEN_PROGRAM_ID,
2868
- }),
2869
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2870
- ];
2871
- postInstructions = [
2872
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2873
- ];
2874
- return [3, 5];
2875
- case 2:
2876
- 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);
2877
- _a = createUserATA;
2878
- if (!_a) return [3, 4];
2879
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
2880
- case 3:
2881
- _a = !(_b.sent());
2882
- _b.label = 4;
2883
- case 4:
2884
- if (_a) {
2885
- 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));
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));
2886
2862
  }
2887
- _b.label = 5;
2888
- case 5:
2889
2863
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2890
2864
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
2891
2865
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -2917,19 +2891,19 @@ var PerpetualsClient = (function () {
2917
2891
  })
2918
2892
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
2919
2893
  .instruction()];
2920
- case 6:
2921
- instruction = _b.sent();
2894
+ case 2:
2895
+ instruction = _a.sent();
2922
2896
  instructions.push(instruction);
2923
2897
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
2924
2898
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
2925
2899
  postInstructions.push(closeWsolATAIns);
2926
2900
  }
2927
- return [3, 8];
2928
- case 7:
2929
- error_1 = _b.sent();
2901
+ return [3, 4];
2902
+ case 3:
2903
+ error_1 = _a.sent();
2930
2904
  console.error("perpclient closePosition error:", error_1);
2931
2905
  throw error_1;
2932
- case 8: return [2, {
2906
+ case 4: return [2, {
2933
2907
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
2934
2908
  additionalSigners: additionalSigners
2935
2909
  }];
@@ -2942,7 +2916,7 @@ var PerpetualsClient = (function () {
2942
2916
  for (var _i = 9; _i < arguments.length; _i++) {
2943
2917
  args_1[_i - 9] = arguments[_i];
2944
2918
  }
2945
- 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) {
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) {
2946
2920
  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;
2947
2921
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2948
2922
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2951,7 +2925,7 @@ var PerpetualsClient = (function () {
2951
2925
  return __generator(this, function (_c) {
2952
2926
  switch (_c.label) {
2953
2927
  case 0:
2954
- publicKey = this.provider.wallet.publicKey;
2928
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2955
2929
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
2956
2930
  if (!userInputCustodyConfig) {
2957
2931
  throw "userInputCustodyConfig not found";
@@ -2975,7 +2949,7 @@ var PerpetualsClient = (function () {
2975
2949
  additionalSigners = [];
2976
2950
  targetToken = poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol);
2977
2951
  userInputToken = poolConfig.getTokenFromSymbol(userInputTokenSymbol);
2978
- if (!(userInputTokenSymbol == 'SOL')) return [3, 5];
2952
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 3];
2979
2953
  console.log("inputSymbol === SOL", userInputTokenSymbol);
2980
2954
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
2981
2955
  if (!!skipBalanceChecks) return [3, 2];
@@ -3005,41 +2979,33 @@ var PerpetualsClient = (function () {
3005
2979
  postInstructions = [
3006
2980
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3007
2981
  ];
3008
- if (!!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) return [3, 4];
3009
- userOutputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3010
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3011
- case 3:
3012
- if (!(_c.sent())) {
3013
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey));
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));
3014
2985
  }
3015
- _c.label = 4;
3016
- case 4: return [3, 10];
3017
- case 5:
2986
+ return [3, 7];
2987
+ case 3:
3018
2988
  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);
3019
- if (!!skipBalanceChecks) return [3, 8];
2989
+ if (!!skipBalanceChecks) return [3, 6];
3020
2990
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3021
- case 6:
2991
+ case 4:
3022
2992
  if (!(_c.sent())) {
3023
2993
  throw "Insufficient Funds , Token Account doesn't exist";
3024
2994
  }
3025
2995
  _b = anchor_1.BN.bind;
3026
2996
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3027
- case 7:
2997
+ case 5:
3028
2998
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
3029
2999
  if (tokenAccountBalance.lt(amountIn)) {
3030
3000
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3031
3001
  }
3032
- _c.label = 8;
3033
- case 8:
3002
+ _c.label = 6;
3003
+ case 6:
3034
3004
  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);
3035
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3036
- case 9:
3037
- if (!(_c.sent())) {
3038
- 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));
3039
- }
3040
- _c.label = 10;
3041
- case 10:
3042
- _c.trys.push([10, 12, , 13]);
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]);
3043
3009
  return [4, this.program.methods
3044
3010
  .swapAndOpen({
3045
3011
  amountIn: amountIn,
@@ -3075,15 +3041,15 @@ var PerpetualsClient = (function () {
3075
3041
  })
3076
3042
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3077
3043
  .instruction()];
3078
- case 11:
3044
+ case 8:
3079
3045
  inx = _c.sent();
3080
3046
  instructions.push(inx);
3081
- return [3, 13];
3082
- case 12:
3047
+ return [3, 10];
3048
+ case 9:
3083
3049
  err_3 = _c.sent();
3084
3050
  console.error("perpClient SwapAndOpen error:: ", err_3);
3085
3051
  throw err_3;
3086
- case 13: return [2, {
3052
+ case 10: return [2, {
3087
3053
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3088
3054
  additionalSigners: additionalSigners
3089
3055
  }];
@@ -3096,7 +3062,7 @@ var PerpetualsClient = (function () {
3096
3062
  for (var _i = 7; _i < arguments.length; _i++) {
3097
3063
  args_1[_i - 7] = arguments[_i];
3098
3064
  }
3099
- 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) {
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) {
3100
3066
  var publicKey, userOutputCustodyConfig, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, collateralToken, userOutputToken, lamports, userCollateralTokenAccount, inx, err_4;
3101
3067
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3102
3068
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -3104,7 +3070,7 @@ var PerpetualsClient = (function () {
3104
3070
  return __generator(this, function (_a) {
3105
3071
  switch (_a.label) {
3106
3072
  case 0:
3107
- publicKey = this.provider.wallet.publicKey;
3073
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3108
3074
  userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey); });
3109
3075
  if (!userOutputCustodyConfig) {
3110
3076
  throw "userOutputCustodyConfig not found";
@@ -3128,46 +3094,37 @@ var PerpetualsClient = (function () {
3128
3094
  additionalSigners = [];
3129
3095
  collateralToken = poolConfig.getTokenFromSymbol(collateralTokenSymbol);
3130
3096
  userOutputToken = poolConfig.getTokenFromSymbol(userOutputTokenSymbol);
3131
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 1];
3132
- console.log("outputSymbol === SOL", userOutputTokenSymbol);
3133
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
3134
- if (!ephemeralSignerPubkey) {
3135
- wrappedSolAccount = new web3_js_1.Keypair();
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
+ ];
3136
3117
  additionalSigners.push(wrappedSolAccount);
3137
3118
  }
3138
- preInstructions = [
3139
- web3_js_1.SystemProgram.createAccount({
3140
- fromPubkey: publicKey,
3141
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3142
- lamports: lamports,
3143
- space: 165,
3144
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3145
- }),
3146
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3147
- ];
3148
- postInstructions = [
3149
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3150
- ];
3151
- additionalSigners.push(wrappedSolAccount);
3152
- return [3, 3];
3153
- case 1:
3154
- 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);
3155
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3156
- case 2:
3157
- if (!(_a.sent())) {
3158
- 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));
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));
3159
3122
  }
3160
- _a.label = 3;
3161
- case 3:
3162
3123
  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);
3163
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3164
- case 4:
3165
- if (!(_a.sent())) {
3166
- 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));
3167
- }
3168
- _a.label = 5;
3169
- case 5:
3170
- _a.trys.push([5, 7, , 8]);
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]);
3171
3128
  return [4, this.program.methods
3172
3129
  .closeAndSwap({
3173
3130
  priceWithSlippage: priceWithSlippage,
@@ -3201,15 +3158,15 @@ var PerpetualsClient = (function () {
3201
3158
  })
3202
3159
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3203
3160
  .instruction()];
3204
- case 6:
3161
+ case 2:
3205
3162
  inx = _a.sent();
3206
3163
  instructions.push(inx);
3207
- return [3, 8];
3208
- case 7:
3164
+ return [3, 4];
3165
+ case 3:
3209
3166
  err_4 = _a.sent();
3210
3167
  console.error("perpClient CloseAndSwap error:: ", err_4);
3211
3168
  throw err_4;
3212
- case 8: return [2, {
3169
+ case 4: return [2, {
3213
3170
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3214
3171
  additionalSigners: additionalSigners
3215
3172
  }];
@@ -3222,14 +3179,14 @@ var PerpetualsClient = (function () {
3222
3179
  for (var _i = 6; _i < arguments.length; _i++) {
3223
3180
  args_1[_i - 6] = arguments[_i];
3224
3181
  }
3225
- 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) {
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) {
3226
3183
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, instruction;
3227
3184
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3228
3185
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3229
3186
  return __generator(this, function (_c) {
3230
3187
  switch (_c.label) {
3231
3188
  case 0:
3232
- publicKey = this.provider.wallet.publicKey;
3189
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3233
3190
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3234
3191
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3235
3192
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -3325,14 +3282,14 @@ var PerpetualsClient = (function () {
3325
3282
  for (var _i = 7; _i < arguments.length; _i++) {
3326
3283
  args_1[_i - 7] = arguments[_i];
3327
3284
  }
3328
- 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) {
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) {
3329
3286
  var publicKey, collateralCustodyConfig, targetCustodyConfig, inputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userCollateralTokenAccount, marketAccount, instruction;
3330
3287
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3331
3288
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3332
3289
  return __generator(this, function (_c) {
3333
3290
  switch (_c.label) {
3334
3291
  case 0:
3335
- publicKey = this.provider.wallet.publicKey;
3292
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3336
3293
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3337
3294
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3338
3295
  inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inputSymbol).mintKey); });
@@ -3396,11 +3353,7 @@ var PerpetualsClient = (function () {
3396
3353
  _c.label = 6;
3397
3354
  case 6:
3398
3355
  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);
3399
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3400
- case 7:
3401
- if (!(_c.sent())) {
3402
- 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));
3403
- }
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));
3404
3357
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3405
3358
  return [4, this.program.methods.swapAndAddCollateral({
3406
3359
  amountIn: amountIn,
@@ -3428,7 +3381,7 @@ var PerpetualsClient = (function () {
3428
3381
  fundingMint: poolConfig.getTokenFromSymbol(inputSymbol).mintKey,
3429
3382
  })
3430
3383
  .instruction()];
3431
- case 8:
3384
+ case 7:
3432
3385
  instruction = _c.sent();
3433
3386
  instructions.push(instruction);
3434
3387
  return [2, {
@@ -3444,15 +3397,15 @@ var PerpetualsClient = (function () {
3444
3397
  for (var _i = 6; _i < arguments.length; _i++) {
3445
3398
  args_1[_i - 6] = arguments[_i];
3446
3399
  }
3447
- 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) {
3448
- var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, marketAccount, instruction, closeWsolATAIns, error_2;
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;
3449
3402
  if (createUserATA === void 0) { createUserATA = true; }
3450
3403
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3451
3404
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3452
- return __generator(this, function (_b) {
3453
- switch (_b.label) {
3405
+ return __generator(this, function (_a) {
3406
+ switch (_a.label) {
3454
3407
  case 0:
3455
- publicKey = this.provider.wallet.publicKey;
3408
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3456
3409
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3457
3410
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3458
3411
  });
@@ -3466,45 +3419,35 @@ var PerpetualsClient = (function () {
3466
3419
  instructions = [];
3467
3420
  postInstructions = [];
3468
3421
  additionalSigners = [];
3469
- _b.label = 1;
3422
+ _a.label = 1;
3470
3423
  case 1:
3471
- _b.trys.push([1, 7, , 8]);
3424
+ _a.trys.push([1, 3, , 4]);
3472
3425
  console.log("removeCollateral -- collateralSymbol:", collateralSymbol);
3473
- if (!(collateralSymbol == 'SOL')) return [3, 2];
3474
- console.log("remove collateral in SOL ...create WSOL temp and close it ");
3475
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3476
- if (!ephemeralSignerPubkey) {
3477
- wrappedSolAccount = new web3_js_1.Keypair();
3478
- additionalSigners.push(wrappedSolAccount);
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
+ ];
3479
3446
  }
3480
- preInstructions = [
3481
- web3_js_1.SystemProgram.createAccount({
3482
- fromPubkey: publicKey,
3483
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3484
- lamports: lamports,
3485
- space: 165,
3486
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3487
- }),
3488
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3489
- ];
3490
- postInstructions = [
3491
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3492
- ];
3493
- return [3, 5];
3494
- case 2:
3495
- 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);
3496
- _a = createUserATA;
3497
- if (!_a) return [3, 4];
3498
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3499
- case 3:
3500
- _a = !(_b.sent());
3501
- _b.label = 4;
3502
- case 4:
3503
- if (_a) {
3504
- 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));
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));
3505
3450
  }
3506
- _b.label = 5;
3507
- case 5:
3508
3451
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3509
3452
  return [4, this.program.methods
3510
3453
  .removeCollateral({
@@ -3530,19 +3473,19 @@ var PerpetualsClient = (function () {
3530
3473
  receivingMint: collateralCustodyConfig.mintKey
3531
3474
  })
3532
3475
  .instruction()];
3533
- case 6:
3534
- instruction = _b.sent();
3476
+ case 2:
3477
+ instruction = _a.sent();
3535
3478
  instructions.push(instruction);
3536
3479
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
3537
3480
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
3538
3481
  postInstructions.push(closeWsolATAIns);
3539
3482
  }
3540
- return [3, 8];
3541
- case 7:
3542
- error_2 = _b.sent();
3483
+ return [3, 4];
3484
+ case 3:
3485
+ error_2 = _a.sent();
3543
3486
  console.error("perpclient removeCollateral error:", error_2);
3544
3487
  throw error_2;
3545
- case 8: return [2, {
3488
+ case 4: return [2, {
3546
3489
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3547
3490
  additionalSigners: additionalSigners
3548
3491
  }];
@@ -3555,13 +3498,13 @@ var PerpetualsClient = (function () {
3555
3498
  for (var _i = 6; _i < arguments.length; _i++) {
3556
3499
  args_1[_i - 6] = arguments[_i];
3557
3500
  }
3558
- 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) {
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) {
3559
3502
  var publicKey, targetCustodyConfig, collateralCustodyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, instruction;
3560
3503
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3561
3504
  return __generator(this, function (_a) {
3562
3505
  switch (_a.label) {
3563
3506
  case 0:
3564
- publicKey = this.provider.wallet.publicKey;
3507
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3565
3508
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3566
3509
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3567
3510
  outputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outputSymbol).mintKey); });
@@ -3572,42 +3515,33 @@ var PerpetualsClient = (function () {
3572
3515
  instructions = [];
3573
3516
  postInstructions = [];
3574
3517
  additionalSigners = [];
3575
- if (!(outputSymbol == 'SOL')) return [3, 1];
3576
- console.log("outputSymbol === SOL", outputSymbol);
3577
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3578
- if (!ephemeralSignerPubkey) {
3579
- wrappedSolAccount = new web3_js_1.Keypair();
3580
- additionalSigners.push(wrappedSolAccount);
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
+ ];
3581
3538
  }
3582
- preInstructions = [
3583
- web3_js_1.SystemProgram.createAccount({
3584
- fromPubkey: publicKey,
3585
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3586
- lamports: lamports,
3587
- space: 165,
3588
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3589
- }),
3590
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3591
- ];
3592
- postInstructions = [
3593
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3594
- ];
3595
- return [3, 3];
3596
- case 1:
3597
- 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);
3598
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3599
- case 2:
3600
- if (!(_a.sent())) {
3601
- 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));
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));
3602
3542
  }
3603
- _a.label = 3;
3604
- case 3:
3605
3543
  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);
3606
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3607
- case 4:
3608
- if (!(_a.sent())) {
3609
- 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));
3610
- }
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));
3611
3545
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3612
3546
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3613
3547
  return [4, this.program.methods
@@ -3641,7 +3575,7 @@ var PerpetualsClient = (function () {
3641
3575
  collateralTokenProgram: poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID
3642
3576
  })
3643
3577
  .instruction()];
3644
- case 5:
3578
+ case 1:
3645
3579
  instruction = _a.sent();
3646
3580
  instructions.push(instruction);
3647
3581
  return [2, {
@@ -3657,14 +3591,14 @@ var PerpetualsClient = (function () {
3657
3591
  for (var _i = 8; _i < arguments.length; _i++) {
3658
3592
  args_1[_i - 8] = arguments[_i];
3659
3593
  }
3660
- 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) {
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) {
3661
3595
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3662
3596
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3663
3597
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3664
3598
  return __generator(this, function (_a) {
3665
3599
  switch (_a.label) {
3666
3600
  case 0:
3667
- publicKey = this.provider.wallet.publicKey;
3601
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3668
3602
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3669
3603
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3670
3604
  });
@@ -3721,14 +3655,14 @@ var PerpetualsClient = (function () {
3721
3655
  for (var _i = 8; _i < arguments.length; _i++) {
3722
3656
  args_1[_i - 8] = arguments[_i];
3723
3657
  }
3724
- 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) {
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) {
3725
3659
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3726
3660
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3727
3661
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3728
3662
  return __generator(this, function (_a) {
3729
3663
  switch (_a.label) {
3730
3664
  case 0:
3731
- publicKey = this.provider.wallet.publicKey;
3665
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3732
3666
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3733
3667
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3734
3668
  });
@@ -3785,7 +3719,7 @@ var PerpetualsClient = (function () {
3785
3719
  for (var _i = 4; _i < arguments.length; _i++) {
3786
3720
  args_1[_i - 4] = arguments[_i];
3787
3721
  }
3788
- 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) {
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) {
3789
3723
  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;
3790
3724
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3791
3725
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
@@ -3793,7 +3727,7 @@ var PerpetualsClient = (function () {
3793
3727
  return __generator(this, function (_g) {
3794
3728
  switch (_g.label) {
3795
3729
  case 0:
3796
- publicKey = this.provider.wallet.publicKey;
3730
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3797
3731
  payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
3798
3732
  if (!payTokenCustodyConfig) {
3799
3733
  throw "payTokenCustodyConfig not found";
@@ -3805,7 +3739,7 @@ var PerpetualsClient = (function () {
3805
3739
  payToken = poolConfig.getTokenFromSymbol(payTokenSymbol);
3806
3740
  _g.label = 1;
3807
3741
  case 1:
3808
- _g.trys.push([1, 10, , 11]);
3742
+ _g.trys.push([1, 9, , 10]);
3809
3743
  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);
3810
3744
  lpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
3811
3745
  custodyAccountMetas = [];
@@ -3832,24 +3766,20 @@ var PerpetualsClient = (function () {
3832
3766
  isWritable: false,
3833
3767
  });
3834
3768
  }
3835
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3836
- case 2:
3837
- if (!(_g.sent())) {
3838
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3839
- }
3840
- if (!(payTokenSymbol == 'SOL')) return [3, 5];
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];
3841
3771
  console.log("payTokenSymbol === SOL", payTokenSymbol);
3842
3772
  lamports = tokenAmountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3843
- if (!!skipBalanceChecks) return [3, 4];
3773
+ if (!!skipBalanceChecks) return [3, 3];
3844
3774
  _e = anchor_1.BN.bind;
3845
3775
  return [4, this.provider.connection.getBalance(publicKey)];
3846
- case 3:
3776
+ case 2:
3847
3777
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _g.sent()]))();
3848
3778
  if (unWrappedSolBalance.lt(lamports)) {
3849
3779
  throw "Insufficient SOL Funds";
3850
3780
  }
3851
- _g.label = 4;
3852
- case 4:
3781
+ _g.label = 3;
3782
+ case 3:
3853
3783
  if (!ephemeralSignerPubkey) {
3854
3784
  wrappedSolAccount = new web3_js_1.Keypair();
3855
3785
  additionalSigners.push(wrappedSolAccount);
@@ -3867,23 +3797,23 @@ var PerpetualsClient = (function () {
3867
3797
  postInstructions = [
3868
3798
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3869
3799
  ];
3870
- return [3, 8];
3871
- case 5:
3872
- if (!!skipBalanceChecks) return [3, 8];
3800
+ return [3, 7];
3801
+ case 4:
3802
+ if (!!skipBalanceChecks) return [3, 7];
3873
3803
  return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3874
- case 6:
3804
+ case 5:
3875
3805
  if (!(_g.sent())) {
3876
3806
  throw "Insufficient Funds , token Account doesn't exist";
3877
3807
  }
3878
3808
  _f = anchor_1.BN.bind;
3879
3809
  return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
3880
- case 7:
3810
+ case 6:
3881
3811
  tokenAccountBalance = new (_f.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3882
3812
  if (tokenAccountBalance.lt(tokenAmountIn)) {
3883
3813
  throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
3884
3814
  }
3885
- _g.label = 8;
3886
- case 8:
3815
+ _g.label = 7;
3816
+ case 7:
3887
3817
  whitelistPda = this.findProgramAddress("whitelist", [publicKey]).publicKey;
3888
3818
  whitelistMeta = {
3889
3819
  pubkey: whitelistPda,
@@ -3915,15 +3845,15 @@ var PerpetualsClient = (function () {
3915
3845
  })
3916
3846
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3917
3847
  .instruction()];
3918
- case 9:
3848
+ case 8:
3919
3849
  instruction = _g.sent();
3920
3850
  instructions.push(instruction);
3921
- return [3, 11];
3922
- case 10:
3851
+ return [3, 10];
3852
+ case 9:
3923
3853
  err_5 = _g.sent();
3924
3854
  console.error("perpClient addLiquidity error:: ", err_5);
3925
3855
  throw err_5;
3926
- case 11: return [2, {
3856
+ case 10: return [2, {
3927
3857
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3928
3858
  additionalSigners: additionalSigners
3929
3859
  }];
@@ -3956,7 +3886,7 @@ var PerpetualsClient = (function () {
3956
3886
  inputToken = poolConfig.getTokenFromSymbol(inputSymbol);
3957
3887
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
3958
3888
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
3959
- if (!(inputSymbol == 'SOL')) return [3, 4];
3889
+ if (!(inputSymbol == 'SOL')) return [3, 3];
3960
3890
  console.log("inputSymbol === SOL", inputSymbol);
3961
3891
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3962
3892
  console.log("lamports:", lamports.toNumber());
@@ -3969,11 +3899,8 @@ var PerpetualsClient = (function () {
3969
3899
  throw "Insufficient SOL Funds";
3970
3900
  }
3971
3901
  _g.label = 2;
3972
- case 2: return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3973
- case 3:
3974
- if (!(_g.sent())) {
3975
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3976
- }
3902
+ case 2:
3903
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3977
3904
  if (!ephemeralSignerPubkey) {
3978
3905
  wrappedSolAccount = new web3_js_1.Keypair();
3979
3906
  additionalSigners.push(wrappedSolAccount);
@@ -3991,24 +3918,24 @@ var PerpetualsClient = (function () {
3991
3918
  postInstructions = [
3992
3919
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3993
3920
  ];
3994
- return [3, 7];
3995
- case 4:
3921
+ return [3, 6];
3922
+ case 3:
3996
3923
  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);
3997
- if (!!skipBalanceChecks) return [3, 7];
3924
+ if (!!skipBalanceChecks) return [3, 6];
3998
3925
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3999
- case 5:
3926
+ case 4:
4000
3927
  if (!(_g.sent())) {
4001
3928
  throw "Insufficient Funds , token Account doesn't exist";
4002
3929
  }
4003
3930
  _b = anchor_1.BN.bind;
4004
3931
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
4005
- case 6:
3932
+ case 5:
4006
3933
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
4007
3934
  if (tokenAccountBalance.lt(amountIn)) {
4008
3935
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
4009
3936
  }
4010
- _g.label = 7;
4011
- case 7:
3937
+ _g.label = 6;
3938
+ case 6:
4012
3939
  custodyAccountMetas = [];
4013
3940
  custodyOracleAccountMetas = [];
4014
3941
  markets = [];
@@ -4065,7 +3992,7 @@ var PerpetualsClient = (function () {
4065
3992
  })
4066
3993
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4067
3994
  .instruction()];
4068
- case 8:
3995
+ case 7:
4069
3996
  instruction = _g.sent();
4070
3997
  instructions.push(instruction);
4071
3998
  return [2, {
@@ -4548,12 +4475,12 @@ var PerpetualsClient = (function () {
4548
4475
  }
4549
4476
  });
4550
4477
  }); };
4551
- this.unstakeRequest = function (unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4478
+ this.unstakeRequest = function (unstakeAmount, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
4552
4479
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_13;
4553
4480
  return __generator(this, function (_a) {
4554
4481
  switch (_a.label) {
4555
4482
  case 0:
4556
- publicKey = this.provider.wallet.publicKey;
4483
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4557
4484
  preInstructions = [];
4558
4485
  instructions = [];
4559
4486
  postInstructions = [];
@@ -4672,10 +4599,10 @@ var PerpetualsClient = (function () {
4672
4599
  args_1[_i - 3] = arguments[_i];
4673
4600
  }
4674
4601
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4675
- var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4602
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4676
4603
  if (createUserATA === void 0) { createUserATA = true; }
4677
- return __generator(this, function (_b) {
4678
- switch (_b.label) {
4604
+ return __generator(this, function (_a) {
4605
+ switch (_a.label) {
4679
4606
  case 0:
4680
4607
  publicKey = this.provider.wallet.publicKey;
4681
4608
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
@@ -4684,22 +4611,13 @@ var PerpetualsClient = (function () {
4684
4611
  instructions = [];
4685
4612
  postInstructions = [];
4686
4613
  additionalSigners = [];
4687
- _b.label = 1;
4614
+ _a.label = 1;
4688
4615
  case 1:
4689
- _b.trys.push([1, 5, , 6]);
4616
+ _a.trys.push([1, 3, , 4]);
4690
4617
  pool = poolConfig.poolAddress;
4691
4618
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4692
4619
  receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
4693
- _a = createUserATA;
4694
- if (!_a) return [3, 3];
4695
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
4696
- case 2:
4697
- _a = !(_b.sent());
4698
- _b.label = 3;
4699
- case 3:
4700
- if (_a) {
4701
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint));
4702
- }
4620
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
4703
4621
  tokenStakeAccounts = [];
4704
4622
  if (tokenStakeAccount) {
4705
4623
  tokenStakeAccounts.push({
@@ -4728,15 +4646,15 @@ var PerpetualsClient = (function () {
4728
4646
  })
4729
4647
  .remainingAccounts(__spreadArray([], tokenStakeAccounts, true))
4730
4648
  .instruction()];
4731
- case 4:
4732
- withdrawStakeInstruction = _b.sent();
4649
+ case 2:
4650
+ withdrawStakeInstruction = _a.sent();
4733
4651
  instructions.push(withdrawStakeInstruction);
4734
- return [3, 6];
4735
- case 5:
4736
- err_15 = _b.sent();
4652
+ return [3, 4];
4653
+ case 3:
4654
+ err_15 = _a.sent();
4737
4655
  console.log("perpClient withdrawStake error:: ", err_15);
4738
4656
  throw err_15;
4739
- case 6: return [2, {
4657
+ case 4: return [2, {
4740
4658
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4741
4659
  additionalSigners: additionalSigners
4742
4660
  }];
@@ -4795,29 +4713,21 @@ var PerpetualsClient = (function () {
4795
4713
  isWritable: false,
4796
4714
  });
4797
4715
  }
4798
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
4799
- case 1:
4800
- if (!(_f.sent())) {
4801
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4802
- }
4803
- return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
4804
- case 2:
4805
- if (!(_f.sent())) {
4806
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
4807
- }
4808
- if (!(inTokenSymbol == 'SOL')) return [3, 5];
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];
4809
4719
  console.log("inTokenSymbol === SOL", inTokenSymbol);
4810
4720
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
4811
- if (!!skipBalanceChecks) return [3, 4];
4721
+ if (!!skipBalanceChecks) return [3, 2];
4812
4722
  _e = anchor_1.BN.bind;
4813
4723
  return [4, this.provider.connection.getBalance(publicKey)];
4814
- case 3:
4724
+ case 1:
4815
4725
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _f.sent()]))();
4816
4726
  if (unWrappedSolBalance.lt(lamports)) {
4817
4727
  throw "Insufficient SOL Funds";
4818
4728
  }
4819
- _f.label = 4;
4820
- case 4:
4729
+ _f.label = 2;
4730
+ case 2:
4821
4731
  if (!ephemeralSignerPubkey) {
4822
4732
  wrappedSolAccount = new web3_js_1.Keypair();
4823
4733
  additionalSigners.push(wrappedSolAccount);
@@ -4835,17 +4745,17 @@ var PerpetualsClient = (function () {
4835
4745
  postInstructions = [
4836
4746
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4837
4747
  ];
4838
- return [3, 7];
4839
- case 5:
4840
- if (!!skipBalanceChecks) return [3, 7];
4748
+ return [3, 5];
4749
+ case 3:
4750
+ if (!!skipBalanceChecks) return [3, 5];
4841
4751
  return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
4842
- case 6:
4752
+ case 4:
4843
4753
  if (!(_f.sent())) {
4844
4754
  throw "Insufficient Funds , token Account doesn't exist";
4845
4755
  }
4846
- _f.label = 7;
4847
- case 7:
4848
- _f.trys.push([7, 9, , 10]);
4756
+ _f.label = 5;
4757
+ case 5:
4758
+ _f.trys.push([5, 7, , 8]);
4849
4759
  if (enableHeapSizeIx) {
4850
4760
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4851
4761
  bytes: 64 * 1024,
@@ -4887,15 +4797,15 @@ var PerpetualsClient = (function () {
4887
4797
  })
4888
4798
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4889
4799
  .instruction()];
4890
- case 8:
4800
+ case 6:
4891
4801
  addCompoundingLiquidity = _f.sent();
4892
4802
  instructions.push(addCompoundingLiquidity);
4893
- return [3, 10];
4894
- case 9:
4803
+ return [3, 8];
4804
+ case 7:
4895
4805
  err_16 = _f.sent();
4896
4806
  console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4897
- return [3, 10];
4898
- case 10: return [2, {
4807
+ return [3, 8];
4808
+ case 8: return [2, {
4899
4809
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4900
4810
  additionalSigners: additionalSigners
4901
4811
  }];
@@ -4909,14 +4819,14 @@ var PerpetualsClient = (function () {
4909
4819
  args_1[_i - 5] = arguments[_i];
4910
4820
  }
4911
4821
  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) {
4912
- 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;
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;
4913
4823
  if (createUserATA === void 0) { createUserATA = true; }
4914
4824
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4915
4825
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4916
4826
  if (enableHeapSizeIx === void 0) { enableHeapSizeIx = true; }
4917
4827
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
4918
- return __generator(this, function (_f) {
4919
- switch (_f.label) {
4828
+ return __generator(this, function (_e) {
4829
+ switch (_e.label) {
4920
4830
  case 0:
4921
4831
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4922
4832
  preInstructions = [];
@@ -4927,45 +4837,35 @@ var PerpetualsClient = (function () {
4927
4837
  outCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outTokenSymbol).mintKey); });
4928
4838
  lpTokenMint = poolConfig.stakedLpTokenMint;
4929
4839
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4930
- if (!(outCustodyConfig.symbol == 'SOL')) return [3, 1];
4931
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4932
- if (!ephemeralSignerPubkey) {
4933
- wrappedSolAccount = new web3_js_1.Keypair();
4934
- additionalSigners.push(wrappedSolAccount);
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
+ ];
4935
4859
  }
4936
- preInstructions = [
4937
- web3_js_1.SystemProgram.createAccount({
4938
- fromPubkey: publicKey,
4939
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4940
- lamports: lamports,
4941
- space: 165,
4942
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4943
- }),
4944
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4945
- ];
4946
- postInstructions = [
4947
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4948
- ];
4949
- return [3, 4];
4950
- case 1:
4951
- 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);
4952
- _a = createUserATA;
4953
- if (!_a) return [3, 3];
4954
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
4955
- case 2:
4956
- _a = !(_f.sent());
4957
- _f.label = 3;
4958
- case 3:
4959
- if (_a) {
4960
- 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));
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));
4961
4863
  }
4962
- _f.label = 4;
4963
- case 4:
4964
4864
  custodyAccountMetas = [];
4965
4865
  custodyOracleAccountMetas = [];
4966
4866
  markets = [];
4967
- for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4968
- custody = _c[_b];
4867
+ for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
4868
+ custody = _b[_a];
4969
4869
  custodyAccountMetas.push({
4970
4870
  pubkey: custody.custodyAccount,
4971
4871
  isSigner: false,
@@ -4977,8 +4877,8 @@ var PerpetualsClient = (function () {
4977
4877
  isWritable: false,
4978
4878
  });
4979
4879
  }
4980
- for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4981
- market = _e[_d];
4880
+ for (_c = 0, _d = poolConfig.markets; _c < _d.length; _c++) {
4881
+ market = _d[_c];
4982
4882
  markets.push({
4983
4883
  pubkey: market.marketAccount,
4984
4884
  isSigner: false,
@@ -4986,9 +4886,9 @@ var PerpetualsClient = (function () {
4986
4886
  });
4987
4887
  }
4988
4888
  compoundingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4989
- _f.label = 5;
4990
- case 5:
4991
- _f.trys.push([5, 7, , 8]);
4889
+ _e.label = 1;
4890
+ case 1:
4891
+ _e.trys.push([1, 3, , 4]);
4992
4892
  if (enableHeapSizeIx) {
4993
4893
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4994
4894
  bytes: 64 * 1024,
@@ -5030,15 +4930,15 @@ var PerpetualsClient = (function () {
5030
4930
  })
5031
4931
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
5032
4932
  .instruction()];
5033
- case 6:
5034
- removeCompoundingLiquidity = _f.sent();
4933
+ case 2:
4934
+ removeCompoundingLiquidity = _e.sent();
5035
4935
  instructions.push(removeCompoundingLiquidity);
5036
- return [3, 8];
5037
- case 7:
5038
- err_17 = _f.sent();
4936
+ return [3, 4];
4937
+ case 3:
4938
+ err_17 = _e.sent();
5039
4939
  console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
5040
- return [3, 8];
5041
- case 8: return [2, {
4940
+ return [3, 4];
4941
+ case 4: return [2, {
5042
4942
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5043
4943
  additionalSigners: additionalSigners
5044
4944
  }];
@@ -5051,13 +4951,13 @@ var PerpetualsClient = (function () {
5051
4951
  for (var _i = 3; _i < arguments.length; _i++) {
5052
4952
  args_1[_i - 3] = arguments[_i];
5053
4953
  }
5054
- return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA) {
5055
- 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;
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;
5056
4956
  if (createUserATA === void 0) { createUserATA = true; }
5057
- return __generator(this, function (_g) {
5058
- switch (_g.label) {
4957
+ return __generator(this, function (_f) {
4958
+ switch (_f.label) {
5059
4959
  case 0:
5060
- publicKey = this.provider.wallet.publicKey;
4960
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5061
4961
  preInstructions = [];
5062
4962
  instructions = [];
5063
4963
  postInstructions = [];
@@ -5066,27 +4966,18 @@ var PerpetualsClient = (function () {
5066
4966
  lpTokenMint = poolConfig.stakedLpTokenMint;
5067
4967
  compoundingTokenMint = poolConfig.compoundingTokenMint;
5068
4968
  compoudingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
5069
- _a = createUserATA;
5070
- if (!_a) return [3, 2];
5071
- return [4, (0, utils_1.checkIfAccountExists)(compoudingTokenAccount, this.provider.connection)];
5072
- case 1:
5073
- _a = !(_g.sent());
5074
- _g.label = 2;
5075
- case 2:
5076
- if (_a) {
5077
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint));
5078
- }
4969
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5079
4970
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
5080
4971
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
5081
4972
  tokenStakeAccounts = [];
5082
- _b = tokenStakeAccount;
5083
- if (!_b) return [3, 4];
4973
+ _a = tokenStakeAccount;
4974
+ if (!_a) return [3, 2];
5084
4975
  return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5085
- case 3:
5086
- _b = (_g.sent());
5087
- _g.label = 4;
5088
- case 4:
5089
- if (_b) {
4976
+ case 1:
4977
+ _a = (_f.sent());
4978
+ _f.label = 2;
4979
+ case 2:
4980
+ if (_a) {
5090
4981
  tokenStakeAccounts.push({
5091
4982
  pubkey: tokenStakeAccount,
5092
4983
  isSigner: false,
@@ -5097,8 +4988,8 @@ var PerpetualsClient = (function () {
5097
4988
  custodyAccountMetas = [];
5098
4989
  custodyOracleAccountMetas = [];
5099
4990
  markets = [];
5100
- for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
5101
- custody = _d[_c];
4991
+ for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4992
+ custody = _c[_b];
5102
4993
  custodyAccountMetas.push({
5103
4994
  pubkey: custody.custodyAccount,
5104
4995
  isSigner: false,
@@ -5110,17 +5001,17 @@ var PerpetualsClient = (function () {
5110
5001
  isWritable: false,
5111
5002
  });
5112
5003
  }
5113
- for (_e = 0, _f = poolConfig.markets; _e < _f.length; _e++) {
5114
- market = _f[_e];
5004
+ for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
5005
+ market = _e[_d];
5115
5006
  markets.push({
5116
5007
  pubkey: market.marketAccount,
5117
5008
  isSigner: false,
5118
5009
  isWritable: false,
5119
5010
  });
5120
5011
  }
5121
- _g.label = 5;
5122
- case 5:
5123
- _g.trys.push([5, 7, , 8]);
5012
+ _f.label = 3;
5013
+ case 3:
5014
+ _f.trys.push([3, 5, , 6]);
5124
5015
  return [4, this.program.methods
5125
5016
  .migrateStake({
5126
5017
  amount: amount
@@ -5145,15 +5036,15 @@ var PerpetualsClient = (function () {
5145
5036
  })
5146
5037
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), tokenStakeAccounts, true))
5147
5038
  .instruction()];
5148
- case 6:
5149
- migrateStake = _g.sent();
5039
+ case 4:
5040
+ migrateStake = _f.sent();
5150
5041
  instructions.push(migrateStake);
5151
- return [3, 8];
5152
- case 7:
5153
- err_18 = _g.sent();
5042
+ return [3, 6];
5043
+ case 5:
5044
+ err_18 = _f.sent();
5154
5045
  console.log("perpClient migrateStake error:: ", err_18);
5155
- return [3, 8];
5156
- case 8: return [2, {
5046
+ return [3, 6];
5047
+ case 6: return [2, {
5157
5048
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5158
5049
  additionalSigners: additionalSigners
5159
5050
  }];
@@ -5161,12 +5052,12 @@ var PerpetualsClient = (function () {
5161
5052
  });
5162
5053
  });
5163
5054
  };
5164
- this.migrateFlp = function (amount, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5055
+ this.migrateFlp = function (amount, rewardTokenMint, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
5165
5056
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_19;
5166
5057
  return __generator(this, function (_d) {
5167
5058
  switch (_d.label) {
5168
5059
  case 0:
5169
- publicKey = this.provider.wallet.publicKey;
5060
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5170
5061
  preInstructions = [];
5171
5062
  instructions = [];
5172
5063
  postInstructions = [];
@@ -5330,14 +5221,10 @@ var PerpetualsClient = (function () {
5330
5221
  additionalSigners = [];
5331
5222
  _a.label = 1;
5332
5223
  case 1:
5333
- _a.trys.push([1, 4, , 5]);
5224
+ _a.trys.push([1, 3, , 4]);
5334
5225
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5335
5226
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5336
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5337
- case 2:
5338
- if (!(_a.sent())) {
5339
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5340
- }
5227
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5341
5228
  return [4, this.program.methods
5342
5229
  .depositTokenStake({
5343
5230
  depositAmount: depositAmount
@@ -5357,15 +5244,15 @@ var PerpetualsClient = (function () {
5357
5244
  tokenMint: poolConfig.tokenMint,
5358
5245
  })
5359
5246
  .instruction()];
5360
- case 3:
5247
+ case 2:
5361
5248
  depositTokenStakeInstruction = _a.sent();
5362
5249
  instructions.push(depositTokenStakeInstruction);
5363
- return [3, 5];
5364
- case 4:
5250
+ return [3, 4];
5251
+ case 3:
5365
5252
  err_21 = _a.sent();
5366
5253
  console.log("perpClient depositStakingInstruction error:: ", err_21);
5367
5254
  throw err_21;
5368
- case 5: return [2, {
5255
+ case 4: return [2, {
5369
5256
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5370
5257
  additionalSigners: additionalSigners
5371
5258
  }];
@@ -5423,14 +5310,10 @@ var PerpetualsClient = (function () {
5423
5310
  additionalSigners = [];
5424
5311
  _a.label = 1;
5425
5312
  case 1:
5426
- _a.trys.push([1, 4, , 5]);
5313
+ _a.trys.push([1, 3, , 4]);
5427
5314
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5428
5315
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5429
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5430
- case 2:
5431
- if (!(_a.sent())) {
5432
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5433
- }
5316
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5434
5317
  return [4, this.program.methods
5435
5318
  .unstakeTokenInstant({
5436
5319
  unstakeAmount: unstakeAmount
@@ -5449,15 +5332,15 @@ var PerpetualsClient = (function () {
5449
5332
  tokenMint: poolConfig.tokenMint,
5450
5333
  })
5451
5334
  .instruction()];
5452
- case 3:
5335
+ case 2:
5453
5336
  unstakeTokenInstantInstruction = _a.sent();
5454
5337
  instructions.push(unstakeTokenInstantInstruction);
5455
- return [3, 5];
5456
- case 4:
5338
+ return [3, 4];
5339
+ case 3:
5457
5340
  err_23 = _a.sent();
5458
5341
  console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5459
5342
  throw err_23;
5460
- case 5: return [2, {
5343
+ case 4: return [2, {
5461
5344
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5462
5345
  additionalSigners: additionalSigners
5463
5346
  }];
@@ -5475,14 +5358,10 @@ var PerpetualsClient = (function () {
5475
5358
  additionalSigners = [];
5476
5359
  _a.label = 1;
5477
5360
  case 1:
5478
- _a.trys.push([1, 4, , 5]);
5361
+ _a.trys.push([1, 3, , 4]);
5479
5362
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5480
5363
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5481
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5482
- case 2:
5483
- if (!(_a.sent())) {
5484
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5485
- }
5364
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5486
5365
  return [4, this.program.methods
5487
5366
  .withdrawToken({
5488
5367
  withdrawRequestId: withdrawRequestId
@@ -5501,15 +5380,15 @@ var PerpetualsClient = (function () {
5501
5380
  tokenMint: poolConfig.tokenMint,
5502
5381
  })
5503
5382
  .instruction()];
5504
- case 3:
5383
+ case 2:
5505
5384
  withdrawTokenInstruction = _a.sent();
5506
5385
  instructions.push(withdrawTokenInstruction);
5507
- return [3, 5];
5508
- case 4:
5386
+ return [3, 4];
5387
+ case 3:
5509
5388
  err_24 = _a.sent();
5510
5389
  console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5511
5390
  throw err_24;
5512
- case 5: return [2, {
5391
+ case 4: return [2, {
5513
5392
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5514
5393
  additionalSigners: additionalSigners
5515
5394
  }];
@@ -5561,32 +5440,23 @@ var PerpetualsClient = (function () {
5561
5440
  for (var _i = 2; _i < arguments.length; _i++) {
5562
5441
  args_1[_i - 2] = arguments[_i];
5563
5442
  }
5564
- return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA) {
5565
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_26;
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;
5566
5445
  if (createUserATA === void 0) { createUserATA = true; }
5567
- return __generator(this, function (_b) {
5568
- switch (_b.label) {
5446
+ return __generator(this, function (_a) {
5447
+ switch (_a.label) {
5569
5448
  case 0:
5570
- publicKey = this.provider.wallet.publicKey;
5449
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5571
5450
  preInstructions = [];
5572
5451
  instructions = [];
5573
5452
  postInstructions = [];
5574
5453
  additionalSigners = [];
5575
- _b.label = 1;
5454
+ _a.label = 1;
5576
5455
  case 1:
5577
- _b.trys.push([1, 5, , 6]);
5456
+ _a.trys.push([1, 3, , 4]);
5578
5457
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5579
5458
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5580
- _a = createUserATA;
5581
- if (!_a) return [3, 3];
5582
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5583
- case 2:
5584
- _a = !(_b.sent());
5585
- _b.label = 3;
5586
- case 3:
5587
- if (_a) {
5588
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint));
5589
- }
5459
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5590
5460
  return [4, this.program.methods
5591
5461
  .collectTokenReward({})
5592
5462
  .accounts({
@@ -5603,15 +5473,15 @@ var PerpetualsClient = (function () {
5603
5473
  tokenMint: poolConfig.tokenMint,
5604
5474
  })
5605
5475
  .instruction()];
5606
- case 4:
5607
- collectTokenRewardInstruction = _b.sent();
5476
+ case 2:
5477
+ collectTokenRewardInstruction = _a.sent();
5608
5478
  instructions.push(collectTokenRewardInstruction);
5609
- return [3, 6];
5610
- case 5:
5611
- err_26 = _b.sent();
5479
+ return [3, 4];
5480
+ case 3:
5481
+ err_26 = _a.sent();
5612
5482
  console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5613
5483
  throw err_26;
5614
- case 6: return [2, {
5484
+ case 4: return [2, {
5615
5485
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5616
5486
  additionalSigners: additionalSigners
5617
5487
  }];
@@ -5625,32 +5495,23 @@ var PerpetualsClient = (function () {
5625
5495
  args_1[_i - 3] = arguments[_i];
5626
5496
  }
5627
5497
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5628
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_27;
5498
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, collectRevenueInstruction, err_27;
5629
5499
  if (createUserATA === void 0) { createUserATA = true; }
5630
- return __generator(this, function (_b) {
5631
- switch (_b.label) {
5500
+ return __generator(this, function (_a) {
5501
+ switch (_a.label) {
5632
5502
  case 0:
5633
5503
  publicKey = this.provider.wallet.publicKey;
5634
5504
  preInstructions = [];
5635
5505
  instructions = [];
5636
5506
  postInstructions = [];
5637
5507
  additionalSigners = [];
5638
- _b.label = 1;
5508
+ _a.label = 1;
5639
5509
  case 1:
5640
- _b.trys.push([1, 5, , 6]);
5510
+ _a.trys.push([1, 3, , 4]);
5641
5511
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5642
5512
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5643
5513
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, owner, true);
5644
- _a = createUserATA;
5645
- if (!_a) return [3, 3];
5646
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5647
- case 2:
5648
- _a = !(_b.sent());
5649
- _b.label = 3;
5650
- case 3:
5651
- if (_a) {
5652
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint));
5653
- }
5514
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
5654
5515
  return [4, this.program.methods
5655
5516
  .collectRevenue({})
5656
5517
  .accounts({
@@ -5667,15 +5528,15 @@ var PerpetualsClient = (function () {
5667
5528
  receivingTokenMint: rewardCustodyMint,
5668
5529
  })
5669
5530
  .instruction()];
5670
- case 4:
5671
- collectRevenueInstruction = _b.sent();
5531
+ case 2:
5532
+ collectRevenueInstruction = _a.sent();
5672
5533
  instructions.push(collectRevenueInstruction);
5673
- return [3, 6];
5674
- case 5:
5675
- err_27 = _b.sent();
5534
+ return [3, 4];
5535
+ case 3:
5536
+ err_27 = _a.sent();
5676
5537
  console.log("perpClient collectRevenueInstruction error:: ", err_27);
5677
5538
  throw err_27;
5678
- case 6: return [2, {
5539
+ case 4: return [2, {
5679
5540
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5680
5541
  additionalSigners: additionalSigners
5681
5542
  }];
@@ -5689,32 +5550,23 @@ var PerpetualsClient = (function () {
5689
5550
  args_1[_i - 3] = arguments[_i];
5690
5551
  }
5691
5552
  return __awaiter(_this, __spreadArray([owner_1, rebateSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rebateSymbol, poolConfig, createUserATA) {
5692
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, _a, collectRebateInstruction, err_28;
5553
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, collectRebateInstruction, err_28;
5693
5554
  if (createUserATA === void 0) { createUserATA = true; }
5694
- return __generator(this, function (_b) {
5695
- switch (_b.label) {
5555
+ return __generator(this, function (_a) {
5556
+ switch (_a.label) {
5696
5557
  case 0:
5697
5558
  publicKey = this.provider.wallet.publicKey;
5698
5559
  preInstructions = [];
5699
5560
  instructions = [];
5700
5561
  postInstructions = [];
5701
5562
  additionalSigners = [];
5702
- _b.label = 1;
5563
+ _a.label = 1;
5703
5564
  case 1:
5704
- _b.trys.push([1, 5, , 6]);
5565
+ _a.trys.push([1, 3, , 4]);
5705
5566
  rebateMint = poolConfig.getTokenFromSymbol(rebateSymbol).mintKey;
5706
5567
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5707
5568
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rebateMint, owner, true);
5708
- _a = createUserATA;
5709
- if (!_a) return [3, 3];
5710
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5711
- case 2:
5712
- _a = !(_b.sent());
5713
- _b.label = 3;
5714
- case 3:
5715
- if (_a) {
5716
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rebateMint));
5717
- }
5569
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rebateMint, spl_token_1.TOKEN_PROGRAM_ID));
5718
5570
  return [4, this.program.methods
5719
5571
  .collectRebate()
5720
5572
  .accounts({
@@ -5731,15 +5583,15 @@ var PerpetualsClient = (function () {
5731
5583
  receivingTokenMint: rebateMint,
5732
5584
  })
5733
5585
  .instruction()];
5734
- case 4:
5735
- collectRebateInstruction = _b.sent();
5586
+ case 2:
5587
+ collectRebateInstruction = _a.sent();
5736
5588
  instructions.push(collectRebateInstruction);
5737
- return [3, 6];
5738
- case 5:
5739
- err_28 = _b.sent();
5589
+ return [3, 4];
5590
+ case 3:
5591
+ err_28 = _a.sent();
5740
5592
  console.log("perpClient collectRebateInstruction error:: ", err_28);
5741
5593
  throw err_28;
5742
- case 6: return [2, {
5594
+ case 4: return [2, {
5743
5595
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5744
5596
  additionalSigners: additionalSigners
5745
5597
  }];
@@ -5799,14 +5651,14 @@ var PerpetualsClient = (function () {
5799
5651
  for (var _i = 11; _i < arguments.length; _i++) {
5800
5652
  args_1[_i - 11] = arguments[_i];
5801
5653
  }
5802
- 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) {
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) {
5803
5655
  var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_30;
5804
5656
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5805
5657
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5806
5658
  return __generator(this, function (_c) {
5807
5659
  switch (_c.label) {
5808
5660
  case 0:
5809
- publicKey = this.provider.wallet.publicKey;
5661
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5810
5662
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5811
5663
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5812
5664
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5922,14 +5774,14 @@ var PerpetualsClient = (function () {
5922
5774
  for (var _i = 11; _i < arguments.length; _i++) {
5923
5775
  args_1[_i - 11] = arguments[_i];
5924
5776
  }
5925
- 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) {
5926
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_31;
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;
5927
5779
  if (createUserATA === void 0) { createUserATA = true; }
5928
5780
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5929
- return __generator(this, function (_b) {
5930
- switch (_b.label) {
5781
+ return __generator(this, function (_a) {
5782
+ switch (_a.label) {
5931
5783
  case 0:
5932
- publicKey = this.provider.wallet.publicKey;
5784
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5933
5785
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5934
5786
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5935
5787
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5939,43 +5791,33 @@ var PerpetualsClient = (function () {
5939
5791
  instructions = [];
5940
5792
  postInstructions = [];
5941
5793
  additionalSigners = [];
5942
- _b.label = 1;
5794
+ _a.label = 1;
5943
5795
  case 1:
5944
- _b.trys.push([1, 7, , 8]);
5945
- if (!(reserveSymbol == 'SOL')) return [3, 2];
5946
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
5947
- if (!ephemeralSignerPubkey) {
5948
- wrappedSolAccount = new web3_js_1.Keypair();
5949
- additionalSigners.push(wrappedSolAccount);
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
+ ];
5950
5816
  }
5951
- preInstructions = [
5952
- web3_js_1.SystemProgram.createAccount({
5953
- fromPubkey: publicKey,
5954
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5955
- lamports: lamports,
5956
- space: 165,
5957
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5958
- }),
5959
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5960
- ];
5961
- postInstructions = [
5962
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5963
- ];
5964
- return [3, 5];
5965
- case 2:
5966
- 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);
5967
- _a = createUserATA;
5968
- if (!_a) return [3, 4];
5969
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
5970
- case 3:
5971
- _a = !(_b.sent());
5972
- _b.label = 4;
5973
- case 4:
5974
- if (_a) {
5975
- 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));
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));
5976
5820
  }
5977
- _b.label = 5;
5978
- case 5:
5979
5821
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
5980
5822
  orderAccount = poolConfig.getOrderFromMarketPk(publicKey, marketAccount);
5981
5823
  return [4, this.program.methods
@@ -6009,15 +5851,15 @@ var PerpetualsClient = (function () {
6009
5851
  receivingMint: poolConfig.getTokenFromSymbol(reserveSymbol).mintKey
6010
5852
  })
6011
5853
  .instruction()];
6012
- case 6:
6013
- editLimitOrder = _b.sent();
5854
+ case 2:
5855
+ editLimitOrder = _a.sent();
6014
5856
  instructions.push(editLimitOrder);
6015
- return [3, 8];
6016
- case 7:
6017
- err_31 = _b.sent();
5857
+ return [3, 4];
5858
+ case 3:
5859
+ err_31 = _a.sent();
6018
5860
  console.log("perpClient editLimitOrder error:: ", err_31);
6019
5861
  throw err_31;
6020
- case 8: return [2, {
5862
+ case 4: return [2, {
6021
5863
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6022
5864
  additionalSigners: additionalSigners
6023
5865
  }];
@@ -6166,12 +6008,12 @@ var PerpetualsClient = (function () {
6166
6008
  });
6167
6009
  });
6168
6010
  };
6169
- this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6011
+ this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6170
6012
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_34;
6171
6013
  return __generator(this, function (_a) {
6172
6014
  switch (_a.label) {
6173
6015
  case 0:
6174
- publicKey = this.provider.wallet.publicKey;
6016
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6175
6017
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6176
6018
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6177
6019
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6225,12 +6067,12 @@ var PerpetualsClient = (function () {
6225
6067
  }
6226
6068
  });
6227
6069
  }); };
6228
- this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6070
+ this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6229
6071
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_35;
6230
6072
  return __generator(this, function (_a) {
6231
6073
  switch (_a.label) {
6232
6074
  case 0:
6233
- publicKey = this.provider.wallet.publicKey;
6075
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6234
6076
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6235
6077
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6236
6078
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6283,12 +6125,12 @@ var PerpetualsClient = (function () {
6283
6125
  }
6284
6126
  });
6285
6127
  }); };
6286
- this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6128
+ this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6287
6129
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_36;
6288
6130
  return __generator(this, function (_a) {
6289
6131
  switch (_a.label) {
6290
6132
  case 0:
6291
- publicKey = this.provider.wallet.publicKey;
6133
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6292
6134
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6293
6135
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6294
6136
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6327,12 +6169,12 @@ var PerpetualsClient = (function () {
6327
6169
  }
6328
6170
  });
6329
6171
  }); };
6330
- this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6172
+ this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6331
6173
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_37;
6332
6174
  return __generator(this, function (_a) {
6333
6175
  switch (_a.label) {
6334
6176
  case 0:
6335
- publicKey = this.provider.wallet.publicKey;
6177
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6336
6178
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6337
6179
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6338
6180
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6375,13 +6217,13 @@ var PerpetualsClient = (function () {
6375
6217
  args_1[_i - 9] = arguments[_i];
6376
6218
  }
6377
6219
  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) {
6378
- 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;
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;
6379
6221
  if (createUserATA === void 0) { createUserATA = true; }
6380
6222
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6381
6223
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6382
6224
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6383
- return __generator(this, function (_e) {
6384
- switch (_e.label) {
6225
+ return __generator(this, function (_c) {
6226
+ switch (_c.label) {
6385
6227
  case 0:
6386
6228
  payerPubkey = this.provider.wallet.publicKey;
6387
6229
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6394,42 +6236,23 @@ var PerpetualsClient = (function () {
6394
6236
  additionalSigners = [];
6395
6237
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
6396
6238
  receivingToken = poolConfig.getTokenFromSymbol(receivingSymbol);
6397
- _e.label = 1;
6239
+ _c.label = 1;
6398
6240
  case 1:
6399
- _e.trys.push([1, 9, , 10]);
6400
- if (!false) return [3, 2];
6401
- return [3, 7];
6402
- case 2:
6403
- 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);
6404
- _a = createUserATA;
6405
- if (!_a) return [3, 4];
6406
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6407
- case 3:
6408
- _a = !(_e.sent());
6409
- _e.label = 4;
6410
- case 4:
6411
- if (_a) {
6412
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey));
6241
+ _c.trys.push([1, 3, , 4]);
6242
+ if (false) {
6413
6243
  }
6414
- 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);
6415
- _b = createUserATA;
6416
- if (!_b) return [3, 6];
6417
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccountCollateral, this.provider.connection)];
6418
- case 5:
6419
- _b = !(_e.sent());
6420
- _e.label = 6;
6421
- case 6:
6422
- if (_b) {
6423
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
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));
6424
6249
  }
6425
- _e.label = 7;
6426
- case 7:
6427
6250
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6428
6251
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6429
6252
  custodyAccountMetas = [];
6430
6253
  custodyOracleAccountMetas = [];
6431
- for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
6432
- custody = _d[_c];
6254
+ for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
6255
+ custody = _b[_a];
6433
6256
  custodyAccountMetas.push({
6434
6257
  pubkey: custody.custodyAccount,
6435
6258
  isSigner: false,
@@ -6476,15 +6299,15 @@ var PerpetualsClient = (function () {
6476
6299
  })
6477
6300
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6478
6301
  .instruction()];
6479
- case 8:
6480
- executeTriggerWithSwap = _e.sent();
6302
+ case 2:
6303
+ executeTriggerWithSwap = _c.sent();
6481
6304
  instructions.push(executeTriggerWithSwap);
6482
- return [3, 10];
6483
- case 9:
6484
- err_38 = _e.sent();
6305
+ return [3, 4];
6306
+ case 3:
6307
+ err_38 = _c.sent();
6485
6308
  console.log("perpClient executeTriggerWithSwap error:: ", err_38);
6486
6309
  throw err_38;
6487
- case 10: return [2, {
6310
+ case 4: return [2, {
6488
6311
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6489
6312
  additionalSigners: additionalSigners
6490
6313
  }];
@@ -6498,13 +6321,13 @@ var PerpetualsClient = (function () {
6498
6321
  args_1[_i - 8] = arguments[_i];
6499
6322
  }
6500
6323
  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) {
6501
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_39;
6324
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeTriggerOrder, err_39;
6502
6325
  if (createUserATA === void 0) { createUserATA = true; }
6503
6326
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6504
6327
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6505
6328
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6506
- return __generator(this, function (_b) {
6507
- switch (_b.label) {
6329
+ return __generator(this, function (_a) {
6330
+ switch (_a.label) {
6508
6331
  case 0:
6509
6332
  payerPubkey = this.provider.wallet.publicKey;
6510
6333
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6514,25 +6337,15 @@ var PerpetualsClient = (function () {
6514
6337
  instructions = [];
6515
6338
  postInstructions = [];
6516
6339
  additionalSigners = [];
6517
- _b.label = 1;
6340
+ _a.label = 1;
6518
6341
  case 1:
6519
- _b.trys.push([1, 7, , 8]);
6520
- if (!false) return [3, 2];
6521
- return [3, 5];
6522
- case 2:
6523
- 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);
6524
- _a = createUserATA;
6525
- if (!_a) return [3, 4];
6526
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6527
- case 3:
6528
- _a = !(_b.sent());
6529
- _b.label = 4;
6530
- case 4:
6531
- if (_a) {
6532
- 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));
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));
6533
6348
  }
6534
- _b.label = 5;
6535
- case 5:
6536
6349
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6537
6350
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6538
6351
  return [4, this.program.methods
@@ -6564,15 +6377,15 @@ var PerpetualsClient = (function () {
6564
6377
  })
6565
6378
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6566
6379
  .instruction()];
6567
- case 6:
6568
- executeTriggerOrder = _b.sent();
6380
+ case 2:
6381
+ executeTriggerOrder = _a.sent();
6569
6382
  instructions.push(executeTriggerOrder);
6570
- return [3, 8];
6571
- case 7:
6572
- err_39 = _b.sent();
6383
+ return [3, 4];
6384
+ case 3:
6385
+ err_39 = _a.sent();
6573
6386
  console.log("perpClient executeTriggerOrder error:: ", err_39);
6574
6387
  throw err_39;
6575
- case 8: return [2, {
6388
+ case 4: return [2, {
6576
6389
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6577
6390
  additionalSigners: additionalSigners
6578
6391
  }];
@@ -6585,16 +6398,16 @@ var PerpetualsClient = (function () {
6585
6398
  for (var _i = 5; _i < arguments.length; _i++) {
6586
6399
  args_1[_i - 5] = arguments[_i];
6587
6400
  }
6588
- 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) {
6589
- 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;
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;
6590
6403
  if (useFeesPool === void 0) { useFeesPool = false; }
6591
6404
  if (createUserATA === void 0) { createUserATA = true; }
6592
6405
  if (unWrapSol === void 0) { unWrapSol = false; }
6593
6406
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
6594
6407
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6595
6408
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
6596
- return __generator(this, function (_g) {
6597
- switch (_g.label) {
6409
+ return __generator(this, function (_f) {
6410
+ switch (_f.label) {
6598
6411
  case 0:
6599
6412
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
6600
6413
  if (!userInputCustodyConfig) {
@@ -6604,31 +6417,26 @@ var PerpetualsClient = (function () {
6604
6417
  if (!userOutputCustodyConfig) {
6605
6418
  throw "userOutputCustodyConfig not found";
6606
6419
  }
6607
- publicKey = this.provider.wallet.publicKey;
6420
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6608
6421
  preInstructions = [];
6609
6422
  instructions = [];
6610
6423
  postInstructions = [];
6611
6424
  additionalSigners = [];
6612
- if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 5];
6425
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
6613
6426
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
6614
6427
  case 1:
6615
- wsolAssociatedTokenAccount = _g.sent();
6616
- return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
6617
- case 2:
6618
- wsolATAExist = _g.sent();
6619
- if (!wsolATAExist) {
6620
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
6621
- }
6622
- if (!!skipBalanceChecks) return [3, 4];
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];
6623
6431
  _a = anchor_1.BN.bind;
6624
6432
  return [4, this.provider.connection.getBalance(publicKey)];
6625
- case 3:
6626
- unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _g.sent()]))();
6433
+ case 2:
6434
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
6627
6435
  if (unWrappedSolBalance.lt(amountIn)) {
6628
6436
  throw "Insufficient SOL Funds";
6629
6437
  }
6630
- _g.label = 4;
6631
- case 4:
6438
+ _f.label = 3;
6439
+ case 3:
6632
6440
  instructions.push(web3_js_1.SystemProgram.transfer({
6633
6441
  fromPubkey: publicKey,
6634
6442
  toPubkey: wsolAssociatedTokenAccount,
@@ -6638,7 +6446,7 @@ var PerpetualsClient = (function () {
6638
6446
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6639
6447
  additionalSigners: additionalSigners
6640
6448
  }];
6641
- case 5:
6449
+ case 4:
6642
6450
  if (userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL') {
6643
6451
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
6644
6452
  wsolAssociatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, publicKey, true);
@@ -6649,20 +6457,20 @@ var PerpetualsClient = (function () {
6649
6457
  additionalSigners: additionalSigners
6650
6458
  }];
6651
6459
  }
6652
- _g.label = 6;
6653
- case 6:
6654
- _g.trys.push([6, 19, , 20]);
6655
- if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
6460
+ _f.label = 5;
6461
+ case 5:
6462
+ _f.trys.push([5, 16, , 17]);
6463
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 8];
6656
6464
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
6657
6465
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
6658
- case 7:
6659
- accCreationLamports = (_g.sent());
6466
+ case 6:
6467
+ accCreationLamports = (_f.sent());
6660
6468
  console.log("accCreationLamports:", accCreationLamports);
6661
6469
  lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
6662
6470
  _b = anchor_1.BN.bind;
6663
6471
  return [4, this.provider.connection.getBalance(publicKey)];
6664
- case 8:
6665
- unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _g.sent()]))();
6472
+ case 7:
6473
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
6666
6474
  if (unWrappedSolBalance.lt(amountIn)) {
6667
6475
  throw "Insufficient SOL Funds";
6668
6476
  }
@@ -6684,25 +6492,25 @@ var PerpetualsClient = (function () {
6684
6492
  postInstructions = [
6685
6493
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6686
6494
  ];
6687
- return [3, 12];
6688
- case 9:
6495
+ return [3, 11];
6496
+ case 8:
6689
6497
  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);
6690
6498
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
6691
- case 10:
6692
- if (!(_g.sent())) {
6499
+ case 9:
6500
+ if (!(_f.sent())) {
6693
6501
  throw "Insufficient Funds , Token Account doesn't exist";
6694
6502
  }
6695
- if (!!skipBalanceChecks) return [3, 12];
6503
+ if (!!skipBalanceChecks) return [3, 11];
6696
6504
  _c = anchor_1.BN.bind;
6697
6505
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
6698
- case 11:
6699
- tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
6506
+ case 10:
6507
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
6700
6508
  if (tokenAccountBalance.lt(amountIn)) {
6701
6509
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
6702
6510
  }
6703
- _g.label = 12;
6704
- case 12:
6705
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 13];
6511
+ _f.label = 11;
6512
+ case 11:
6513
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 12];
6706
6514
  lamports = (this.minimumBalanceForRentExemptAccountLamports);
6707
6515
  if (!ephemeralSignerPubkey) {
6708
6516
  wrappedSolAccount = new web3_js_1.Keypair();
@@ -6722,26 +6530,17 @@ var PerpetualsClient = (function () {
6722
6530
  postInstructions = [
6723
6531
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6724
6532
  ];
6725
- return [3, 17];
6726
- 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)];
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;
6727
6539
  case 14:
6728
- userOutputTokenAccount = _g.sent();
6729
- _d = createUserATA;
6730
- if (!_d) return [3, 16];
6731
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
6732
- case 15:
6733
- _d = !(_g.sent());
6734
- _g.label = 16;
6735
- case 16:
6736
- if (_d) {
6737
- 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));
6738
- }
6739
- _g.label = 17;
6740
- case 17:
6741
6540
  custodyAccountMetas = [];
6742
6541
  custodyOracleAccountMetas = [];
6743
- for (_e = 0, _f = poolConfig.custodies; _e < _f.length; _e++) {
6744
- custody = _f[_e];
6542
+ for (_d = 0, _e = poolConfig.custodies; _d < _e.length; _d++) {
6543
+ custody = _e[_d];
6745
6544
  custodyAccountMetas.push({
6746
6545
  pubkey: custody.custodyAccount,
6747
6546
  isSigner: false,
@@ -6789,19 +6588,19 @@ var PerpetualsClient = (function () {
6789
6588
  })
6790
6589
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
6791
6590
  .instruction()];
6792
- case 18:
6793
- inx = _g.sent();
6591
+ case 15:
6592
+ inx = _f.sent();
6794
6593
  instructions.push(inx);
6795
6594
  if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
6796
6595
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
6797
6596
  instructions.push(closeWsolATAIns);
6798
6597
  }
6799
- return [3, 20];
6800
- case 19:
6801
- err_40 = _g.sent();
6598
+ return [3, 17];
6599
+ case 16:
6600
+ err_40 = _f.sent();
6802
6601
  console.error("perpClient Swap error:: ", err_40);
6803
6602
  throw err_40;
6804
- case 20: return [2, {
6603
+ case 17: return [2, {
6805
6604
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6806
6605
  additionalSigners: additionalSigners
6807
6606
  }];
@@ -7843,15 +7642,11 @@ var PerpetualsClient = (function () {
7843
7642
  additionalSigners = [];
7844
7643
  _a.label = 1;
7845
7644
  case 1:
7846
- _a.trys.push([1, 5, , 6]);
7645
+ _a.trys.push([1, 4, , 5]);
7847
7646
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7848
7647
  case 2:
7849
7648
  receivingTokenAccount = _a.sent();
7850
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7851
- case 3:
7852
- if (!(_a.sent())) {
7853
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7854
- }
7649
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7855
7650
  return [4, this.program.methods
7856
7651
  .withdrawInstantFees({})
7857
7652
  .accounts({
@@ -7866,15 +7661,15 @@ var PerpetualsClient = (function () {
7866
7661
  receivingTokenMint: poolConfig.tokenMint,
7867
7662
  })
7868
7663
  .instruction()];
7869
- case 4:
7664
+ case 3:
7870
7665
  withdrawInstantFeeInstruction = _a.sent();
7871
7666
  instructions.push(withdrawInstantFeeInstruction);
7872
- return [3, 6];
7873
- case 5:
7667
+ return [3, 5];
7668
+ case 4:
7874
7669
  err_57 = _a.sent();
7875
7670
  console.log("perpClient withdrawInstantFeeInstruction error:: ", err_57);
7876
7671
  throw err_57;
7877
- case 6: return [2, {
7672
+ case 5: return [2, {
7878
7673
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7879
7674
  additionalSigners: additionalSigners
7880
7675
  }];
@@ -7893,15 +7688,11 @@ var PerpetualsClient = (function () {
7893
7688
  additionalSigners = [];
7894
7689
  _a.label = 1;
7895
7690
  case 1:
7896
- _a.trys.push([1, 5, , 6]);
7691
+ _a.trys.push([1, 4, , 5]);
7897
7692
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7898
7693
  case 2:
7899
7694
  receivingTokenAccount = _a.sent();
7900
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7901
- case 3:
7902
- if (!(_a.sent())) {
7903
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7904
- }
7695
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7905
7696
  return [4, this.program.methods
7906
7697
  .withdrawUnclaimedTokens({})
7907
7698
  .accounts({
@@ -7916,15 +7707,15 @@ var PerpetualsClient = (function () {
7916
7707
  receivingTokenMint: poolConfig.tokenMint,
7917
7708
  })
7918
7709
  .instruction()];
7919
- case 4:
7710
+ case 3:
7920
7711
  withdrawUnclaimedTokensInstruction = _a.sent();
7921
7712
  instructions.push(withdrawUnclaimedTokensInstruction);
7922
- return [3, 6];
7923
- case 5:
7713
+ return [3, 5];
7714
+ case 4:
7924
7715
  err_58 = _a.sent();
7925
7716
  console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_58);
7926
7717
  throw err_58;
7927
- case 6: return [2, {
7718
+ case 5: return [2, {
7928
7719
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7929
7720
  additionalSigners: additionalSigners
7930
7721
  }];