flash-sdk 11.4.8 → 11.4.10-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.
@@ -2592,7 +2592,7 @@ var PerpetualsClient = (function () {
2592
2592
  for (var _i = 8; _i < arguments.length; _i++) {
2593
2593
  args_1[_i - 8] = arguments[_i];
2594
2594
  }
2595
- 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) {
2595
+ 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) {
2596
2596
  var publicKey, targetCustodyConfig, collateralCustodyConfig, collateralToken, marketAccount, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
2597
2597
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2598
2598
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2601,7 +2601,7 @@ var PerpetualsClient = (function () {
2601
2601
  return __generator(this, function (_c) {
2602
2602
  switch (_c.label) {
2603
2603
  case 0:
2604
- publicKey = this.provider.wallet.publicKey;
2604
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2605
2605
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
2606
2606
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2607
2607
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
@@ -2706,59 +2706,49 @@ var PerpetualsClient = (function () {
2706
2706
  for (var _i = 6; _i < arguments.length; _i++) {
2707
2707
  args_1[_i - 6] = arguments[_i];
2708
2708
  }
2709
- 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) {
2710
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2709
+ 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) {
2710
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2711
2711
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2712
2712
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
2713
2713
  if (createUserATA === void 0) { createUserATA = true; }
2714
2714
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
2715
2715
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2716
- return __generator(this, function (_b) {
2717
- switch (_b.label) {
2716
+ return __generator(this, function (_a) {
2717
+ switch (_a.label) {
2718
2718
  case 0:
2719
2719
  console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
2720
- publicKey = this.provider.wallet.publicKey;
2720
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2721
2721
  preInstructions = [];
2722
2722
  instructions = [];
2723
2723
  postInstructions = [];
2724
2724
  additionalSigners = [];
2725
- _b.label = 1;
2725
+ _a.label = 1;
2726
2726
  case 1:
2727
- _b.trys.push([1, 7, , 8]);
2728
- if (!(collateralSymbol == 'SOL')) return [3, 2];
2729
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
2730
- if (!ephemeralSignerPubkey) {
2731
- wrappedSolAccount = new web3_js_1.Keypair();
2732
- additionalSigners.push(wrappedSolAccount);
2727
+ _a.trys.push([1, 3, , 4]);
2728
+ if (collateralSymbol == 'SOL') {
2729
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
2730
+ if (!ephemeralSignerPubkey) {
2731
+ wrappedSolAccount = new web3_js_1.Keypair();
2732
+ additionalSigners.push(wrappedSolAccount);
2733
+ }
2734
+ preInstructions = [
2735
+ web3_js_1.SystemProgram.createAccount({
2736
+ fromPubkey: publicKey,
2737
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2738
+ lamports: lamports,
2739
+ space: 165,
2740
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
2741
+ }),
2742
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2743
+ ];
2744
+ postInstructions = [
2745
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2746
+ ];
2733
2747
  }
2734
- preInstructions = [
2735
- web3_js_1.SystemProgram.createAccount({
2736
- fromPubkey: publicKey,
2737
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2738
- lamports: lamports,
2739
- space: 165,
2740
- programId: spl_token_1.TOKEN_PROGRAM_ID,
2741
- }),
2742
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2743
- ];
2744
- postInstructions = [
2745
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2746
- ];
2747
- return [3, 5];
2748
- case 2:
2749
- 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);
2750
- _a = createUserATA;
2751
- if (!_a) return [3, 4];
2752
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
2753
- case 3:
2754
- _a = !(_b.sent());
2755
- _b.label = 4;
2756
- case 4:
2757
- if (_a) {
2758
- 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));
2748
+ else {
2749
+ 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);
2750
+ 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));
2759
2751
  }
2760
- _b.label = 5;
2761
- case 5:
2762
2752
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2763
2753
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
2764
2754
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -2790,19 +2780,19 @@ var PerpetualsClient = (function () {
2790
2780
  })
2791
2781
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
2792
2782
  .instruction()];
2793
- case 6:
2794
- instruction = _b.sent();
2783
+ case 2:
2784
+ instruction = _a.sent();
2795
2785
  instructions.push(instruction);
2796
2786
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
2797
2787
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
2798
2788
  postInstructions.push(closeWsolATAIns);
2799
2789
  }
2800
- return [3, 8];
2801
- case 7:
2802
- error_1 = _b.sent();
2790
+ return [3, 4];
2791
+ case 3:
2792
+ error_1 = _a.sent();
2803
2793
  console.error("perpclient closePosition error:", error_1);
2804
2794
  throw error_1;
2805
- case 8: return [2, {
2795
+ case 4: return [2, {
2806
2796
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
2807
2797
  additionalSigners: additionalSigners
2808
2798
  }];
@@ -2815,7 +2805,7 @@ var PerpetualsClient = (function () {
2815
2805
  for (var _i = 9; _i < arguments.length; _i++) {
2816
2806
  args_1[_i - 9] = arguments[_i];
2817
2807
  }
2818
- 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) {
2808
+ 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) {
2819
2809
  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;
2820
2810
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2821
2811
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2824,7 +2814,7 @@ var PerpetualsClient = (function () {
2824
2814
  return __generator(this, function (_c) {
2825
2815
  switch (_c.label) {
2826
2816
  case 0:
2827
- publicKey = this.provider.wallet.publicKey;
2817
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2828
2818
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
2829
2819
  if (!userInputCustodyConfig) {
2830
2820
  throw "userInputCustodyConfig not found";
@@ -2848,7 +2838,7 @@ var PerpetualsClient = (function () {
2848
2838
  additionalSigners = [];
2849
2839
  targetToken = poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol);
2850
2840
  userInputToken = poolConfig.getTokenFromSymbol(userInputTokenSymbol);
2851
- if (!(userInputTokenSymbol == 'SOL')) return [3, 5];
2841
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 3];
2852
2842
  console.log("inputSymbol === SOL", userInputTokenSymbol);
2853
2843
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
2854
2844
  if (!!skipBalanceChecks) return [3, 2];
@@ -2878,41 +2868,33 @@ var PerpetualsClient = (function () {
2878
2868
  postInstructions = [
2879
2869
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2880
2870
  ];
2881
- if (!!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) return [3, 4];
2882
- 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);
2883
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
2884
- case 3:
2885
- if (!(_c.sent())) {
2886
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey));
2871
+ if (!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) {
2872
+ 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);
2873
+ 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));
2887
2874
  }
2888
- _c.label = 4;
2889
- case 4: return [3, 10];
2890
- case 5:
2875
+ return [3, 7];
2876
+ case 3:
2891
2877
  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);
2892
- if (!!skipBalanceChecks) return [3, 8];
2878
+ if (!!skipBalanceChecks) return [3, 6];
2893
2879
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
2894
- case 6:
2880
+ case 4:
2895
2881
  if (!(_c.sent())) {
2896
2882
  throw "Insufficient Funds , Token Account doesn't exist";
2897
2883
  }
2898
2884
  _b = anchor_1.BN.bind;
2899
2885
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
2900
- case 7:
2886
+ case 5:
2901
2887
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
2902
2888
  if (tokenAccountBalance.lt(amountIn)) {
2903
2889
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
2904
2890
  }
2905
- _c.label = 8;
2906
- case 8:
2891
+ _c.label = 6;
2892
+ case 6:
2907
2893
  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);
2908
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
2909
- case 9:
2910
- if (!(_c.sent())) {
2911
- 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));
2912
- }
2913
- _c.label = 10;
2914
- case 10:
2915
- _c.trys.push([10, 12, , 13]);
2894
+ 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));
2895
+ _c.label = 7;
2896
+ case 7:
2897
+ _c.trys.push([7, 9, , 10]);
2916
2898
  return [4, this.program.methods
2917
2899
  .swapAndOpen({
2918
2900
  amountIn: amountIn,
@@ -2948,15 +2930,15 @@ var PerpetualsClient = (function () {
2948
2930
  })
2949
2931
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
2950
2932
  .instruction()];
2951
- case 11:
2933
+ case 8:
2952
2934
  inx = _c.sent();
2953
2935
  instructions.push(inx);
2954
- return [3, 13];
2955
- case 12:
2936
+ return [3, 10];
2937
+ case 9:
2956
2938
  err_3 = _c.sent();
2957
2939
  console.error("perpClient SwapAndOpen error:: ", err_3);
2958
2940
  throw err_3;
2959
- case 13: return [2, {
2941
+ case 10: return [2, {
2960
2942
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
2961
2943
  additionalSigners: additionalSigners
2962
2944
  }];
@@ -2969,7 +2951,7 @@ var PerpetualsClient = (function () {
2969
2951
  for (var _i = 7; _i < arguments.length; _i++) {
2970
2952
  args_1[_i - 7] = arguments[_i];
2971
2953
  }
2972
- 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) {
2954
+ 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) {
2973
2955
  var publicKey, userOutputCustodyConfig, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, collateralToken, userOutputToken, lamports, userCollateralTokenAccount, inx, err_4;
2974
2956
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2975
2957
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2977,7 +2959,7 @@ var PerpetualsClient = (function () {
2977
2959
  return __generator(this, function (_a) {
2978
2960
  switch (_a.label) {
2979
2961
  case 0:
2980
- publicKey = this.provider.wallet.publicKey;
2962
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2981
2963
  userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey); });
2982
2964
  if (!userOutputCustodyConfig) {
2983
2965
  throw "userOutputCustodyConfig not found";
@@ -3001,46 +2983,37 @@ var PerpetualsClient = (function () {
3001
2983
  additionalSigners = [];
3002
2984
  collateralToken = poolConfig.getTokenFromSymbol(collateralTokenSymbol);
3003
2985
  userOutputToken = poolConfig.getTokenFromSymbol(userOutputTokenSymbol);
3004
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 1];
3005
- console.log("outputSymbol === SOL", userOutputTokenSymbol);
3006
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
3007
- if (!ephemeralSignerPubkey) {
3008
- wrappedSolAccount = new web3_js_1.Keypair();
2986
+ if (userOutputTokenSymbol == 'SOL') {
2987
+ console.log("outputSymbol === SOL", userOutputTokenSymbol);
2988
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
2989
+ if (!ephemeralSignerPubkey) {
2990
+ wrappedSolAccount = new web3_js_1.Keypair();
2991
+ additionalSigners.push(wrappedSolAccount);
2992
+ }
2993
+ preInstructions = [
2994
+ web3_js_1.SystemProgram.createAccount({
2995
+ fromPubkey: publicKey,
2996
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2997
+ lamports: lamports,
2998
+ space: 165,
2999
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3000
+ }),
3001
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3002
+ ];
3003
+ postInstructions = [
3004
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3005
+ ];
3009
3006
  additionalSigners.push(wrappedSolAccount);
3010
3007
  }
3011
- preInstructions = [
3012
- web3_js_1.SystemProgram.createAccount({
3013
- fromPubkey: publicKey,
3014
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3015
- lamports: lamports,
3016
- space: 165,
3017
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3018
- }),
3019
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3020
- ];
3021
- postInstructions = [
3022
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3023
- ];
3024
- additionalSigners.push(wrappedSolAccount);
3025
- return [3, 3];
3026
- case 1:
3027
- 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);
3028
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3029
- case 2:
3030
- if (!(_a.sent())) {
3031
- 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));
3008
+ else {
3009
+ 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);
3010
+ 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));
3032
3011
  }
3033
- _a.label = 3;
3034
- case 3:
3035
3012
  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);
3036
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3037
- case 4:
3038
- if (!(_a.sent())) {
3039
- 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));
3040
- }
3041
- _a.label = 5;
3042
- case 5:
3043
- _a.trys.push([5, 7, , 8]);
3013
+ 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));
3014
+ _a.label = 1;
3015
+ case 1:
3016
+ _a.trys.push([1, 3, , 4]);
3044
3017
  return [4, this.program.methods
3045
3018
  .closeAndSwap({
3046
3019
  priceWithSlippage: priceWithSlippage,
@@ -3074,15 +3047,15 @@ var PerpetualsClient = (function () {
3074
3047
  })
3075
3048
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3076
3049
  .instruction()];
3077
- case 6:
3050
+ case 2:
3078
3051
  inx = _a.sent();
3079
3052
  instructions.push(inx);
3080
- return [3, 8];
3081
- case 7:
3053
+ return [3, 4];
3054
+ case 3:
3082
3055
  err_4 = _a.sent();
3083
3056
  console.error("perpClient CloseAndSwap error:: ", err_4);
3084
3057
  throw err_4;
3085
- case 8: return [2, {
3058
+ case 4: return [2, {
3086
3059
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3087
3060
  additionalSigners: additionalSigners
3088
3061
  }];
@@ -3095,14 +3068,14 @@ var PerpetualsClient = (function () {
3095
3068
  for (var _i = 6; _i < arguments.length; _i++) {
3096
3069
  args_1[_i - 6] = arguments[_i];
3097
3070
  }
3098
- 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) {
3071
+ 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) {
3099
3072
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, instruction;
3100
3073
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3101
3074
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3102
3075
  return __generator(this, function (_c) {
3103
3076
  switch (_c.label) {
3104
3077
  case 0:
3105
- publicKey = this.provider.wallet.publicKey;
3078
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3106
3079
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3107
3080
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3108
3081
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -3198,14 +3171,14 @@ var PerpetualsClient = (function () {
3198
3171
  for (var _i = 7; _i < arguments.length; _i++) {
3199
3172
  args_1[_i - 7] = arguments[_i];
3200
3173
  }
3201
- 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) {
3174
+ 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) {
3202
3175
  var publicKey, collateralCustodyConfig, targetCustodyConfig, inputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userCollateralTokenAccount, marketAccount, instruction;
3203
3176
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3204
3177
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3205
3178
  return __generator(this, function (_c) {
3206
3179
  switch (_c.label) {
3207
3180
  case 0:
3208
- publicKey = this.provider.wallet.publicKey;
3181
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3209
3182
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3210
3183
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3211
3184
  inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inputSymbol).mintKey); });
@@ -3269,11 +3242,7 @@ var PerpetualsClient = (function () {
3269
3242
  _c.label = 6;
3270
3243
  case 6:
3271
3244
  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);
3272
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3273
- case 7:
3274
- if (!(_c.sent())) {
3275
- 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));
3276
- }
3245
+ 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));
3277
3246
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3278
3247
  return [4, this.program.methods.swapAndAddCollateral({
3279
3248
  amountIn: amountIn,
@@ -3301,7 +3270,7 @@ var PerpetualsClient = (function () {
3301
3270
  fundingMint: poolConfig.getTokenFromSymbol(inputSymbol).mintKey,
3302
3271
  })
3303
3272
  .instruction()];
3304
- case 8:
3273
+ case 7:
3305
3274
  instruction = _c.sent();
3306
3275
  instructions.push(instruction);
3307
3276
  return [2, {
@@ -3317,15 +3286,15 @@ var PerpetualsClient = (function () {
3317
3286
  for (var _i = 6; _i < arguments.length; _i++) {
3318
3287
  args_1[_i - 6] = arguments[_i];
3319
3288
  }
3320
- 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) {
3321
- var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, marketAccount, instruction, closeWsolATAIns, error_2;
3289
+ 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) {
3290
+ var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, marketAccount, instruction, closeWsolATAIns, error_2;
3322
3291
  if (createUserATA === void 0) { createUserATA = true; }
3323
3292
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3324
3293
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3325
- return __generator(this, function (_b) {
3326
- switch (_b.label) {
3294
+ return __generator(this, function (_a) {
3295
+ switch (_a.label) {
3327
3296
  case 0:
3328
- publicKey = this.provider.wallet.publicKey;
3297
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3329
3298
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3330
3299
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3331
3300
  });
@@ -3339,45 +3308,35 @@ var PerpetualsClient = (function () {
3339
3308
  instructions = [];
3340
3309
  postInstructions = [];
3341
3310
  additionalSigners = [];
3342
- _b.label = 1;
3311
+ _a.label = 1;
3343
3312
  case 1:
3344
- _b.trys.push([1, 7, , 8]);
3313
+ _a.trys.push([1, 3, , 4]);
3345
3314
  console.log("removeCollateral -- collateralSymbol:", collateralSymbol);
3346
- if (!(collateralSymbol == 'SOL')) return [3, 2];
3347
- console.log("remove collateral in SOL ...create WSOL temp and close it ");
3348
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3349
- if (!ephemeralSignerPubkey) {
3350
- wrappedSolAccount = new web3_js_1.Keypair();
3351
- additionalSigners.push(wrappedSolAccount);
3315
+ if (collateralSymbol == 'SOL') {
3316
+ console.log("remove collateral in SOL ...create WSOL temp and close it ");
3317
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3318
+ if (!ephemeralSignerPubkey) {
3319
+ wrappedSolAccount = new web3_js_1.Keypair();
3320
+ additionalSigners.push(wrappedSolAccount);
3321
+ }
3322
+ preInstructions = [
3323
+ web3_js_1.SystemProgram.createAccount({
3324
+ fromPubkey: publicKey,
3325
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3326
+ lamports: lamports,
3327
+ space: 165,
3328
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3329
+ }),
3330
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3331
+ ];
3332
+ postInstructions = [
3333
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3334
+ ];
3352
3335
  }
3353
- preInstructions = [
3354
- web3_js_1.SystemProgram.createAccount({
3355
- fromPubkey: publicKey,
3356
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3357
- lamports: lamports,
3358
- space: 165,
3359
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3360
- }),
3361
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3362
- ];
3363
- postInstructions = [
3364
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3365
- ];
3366
- return [3, 5];
3367
- case 2:
3368
- 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);
3369
- _a = createUserATA;
3370
- if (!_a) return [3, 4];
3371
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3372
- case 3:
3373
- _a = !(_b.sent());
3374
- _b.label = 4;
3375
- case 4:
3376
- if (_a) {
3377
- 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));
3336
+ else {
3337
+ 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);
3338
+ 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));
3378
3339
  }
3379
- _b.label = 5;
3380
- case 5:
3381
3340
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3382
3341
  return [4, this.program.methods
3383
3342
  .removeCollateral({
@@ -3403,19 +3362,19 @@ var PerpetualsClient = (function () {
3403
3362
  receivingMint: collateralCustodyConfig.mintKey
3404
3363
  })
3405
3364
  .instruction()];
3406
- case 6:
3407
- instruction = _b.sent();
3365
+ case 2:
3366
+ instruction = _a.sent();
3408
3367
  instructions.push(instruction);
3409
3368
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
3410
3369
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
3411
3370
  postInstructions.push(closeWsolATAIns);
3412
3371
  }
3413
- return [3, 8];
3414
- case 7:
3415
- error_2 = _b.sent();
3372
+ return [3, 4];
3373
+ case 3:
3374
+ error_2 = _a.sent();
3416
3375
  console.error("perpclient removeCollateral error:", error_2);
3417
3376
  throw error_2;
3418
- case 8: return [2, {
3377
+ case 4: return [2, {
3419
3378
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3420
3379
  additionalSigners: additionalSigners
3421
3380
  }];
@@ -3428,13 +3387,13 @@ var PerpetualsClient = (function () {
3428
3387
  for (var _i = 6; _i < arguments.length; _i++) {
3429
3388
  args_1[_i - 6] = arguments[_i];
3430
3389
  }
3431
- 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) {
3390
+ 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) {
3432
3391
  var publicKey, targetCustodyConfig, collateralCustodyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, instruction;
3433
3392
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3434
3393
  return __generator(this, function (_a) {
3435
3394
  switch (_a.label) {
3436
3395
  case 0:
3437
- publicKey = this.provider.wallet.publicKey;
3396
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3438
3397
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3439
3398
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3440
3399
  outputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outputSymbol).mintKey); });
@@ -3445,42 +3404,33 @@ var PerpetualsClient = (function () {
3445
3404
  instructions = [];
3446
3405
  postInstructions = [];
3447
3406
  additionalSigners = [];
3448
- if (!(outputSymbol == 'SOL')) return [3, 1];
3449
- console.log("outputSymbol === SOL", outputSymbol);
3450
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3451
- if (!ephemeralSignerPubkey) {
3452
- wrappedSolAccount = new web3_js_1.Keypair();
3453
- additionalSigners.push(wrappedSolAccount);
3407
+ if (outputSymbol == 'SOL') {
3408
+ console.log("outputSymbol === SOL", outputSymbol);
3409
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3410
+ if (!ephemeralSignerPubkey) {
3411
+ wrappedSolAccount = new web3_js_1.Keypair();
3412
+ additionalSigners.push(wrappedSolAccount);
3413
+ }
3414
+ preInstructions = [
3415
+ web3_js_1.SystemProgram.createAccount({
3416
+ fromPubkey: publicKey,
3417
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3418
+ lamports: lamports,
3419
+ space: 165,
3420
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3421
+ }),
3422
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3423
+ ];
3424
+ postInstructions = [
3425
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3426
+ ];
3454
3427
  }
3455
- preInstructions = [
3456
- web3_js_1.SystemProgram.createAccount({
3457
- fromPubkey: publicKey,
3458
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3459
- lamports: lamports,
3460
- space: 165,
3461
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3462
- }),
3463
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3464
- ];
3465
- postInstructions = [
3466
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3467
- ];
3468
- return [3, 3];
3469
- case 1:
3470
- 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);
3471
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3472
- case 2:
3473
- if (!(_a.sent())) {
3474
- 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));
3428
+ else {
3429
+ 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);
3430
+ 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));
3475
3431
  }
3476
- _a.label = 3;
3477
- case 3:
3478
3432
  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);
3479
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3480
- case 4:
3481
- if (!(_a.sent())) {
3482
- 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));
3483
- }
3433
+ 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));
3484
3434
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3485
3435
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3486
3436
  return [4, this.program.methods
@@ -3514,7 +3464,7 @@ var PerpetualsClient = (function () {
3514
3464
  collateralTokenProgram: poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID
3515
3465
  })
3516
3466
  .instruction()];
3517
- case 5:
3467
+ case 1:
3518
3468
  instruction = _a.sent();
3519
3469
  instructions.push(instruction);
3520
3470
  return [2, {
@@ -3530,14 +3480,14 @@ var PerpetualsClient = (function () {
3530
3480
  for (var _i = 8; _i < arguments.length; _i++) {
3531
3481
  args_1[_i - 8] = arguments[_i];
3532
3482
  }
3533
- 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) {
3483
+ 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) {
3534
3484
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3535
3485
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3536
3486
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3537
3487
  return __generator(this, function (_a) {
3538
3488
  switch (_a.label) {
3539
3489
  case 0:
3540
- publicKey = this.provider.wallet.publicKey;
3490
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3541
3491
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3542
3492
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3543
3493
  });
@@ -3594,14 +3544,14 @@ var PerpetualsClient = (function () {
3594
3544
  for (var _i = 8; _i < arguments.length; _i++) {
3595
3545
  args_1[_i - 8] = arguments[_i];
3596
3546
  }
3597
- 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) {
3547
+ 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) {
3598
3548
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3599
3549
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3600
3550
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3601
3551
  return __generator(this, function (_a) {
3602
3552
  switch (_a.label) {
3603
3553
  case 0:
3604
- publicKey = this.provider.wallet.publicKey;
3554
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3605
3555
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3606
3556
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3607
3557
  });
@@ -3658,7 +3608,7 @@ var PerpetualsClient = (function () {
3658
3608
  for (var _i = 4; _i < arguments.length; _i++) {
3659
3609
  args_1[_i - 4] = arguments[_i];
3660
3610
  }
3661
- 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) {
3611
+ 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) {
3662
3612
  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;
3663
3613
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3664
3614
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
@@ -3666,7 +3616,7 @@ var PerpetualsClient = (function () {
3666
3616
  return __generator(this, function (_g) {
3667
3617
  switch (_g.label) {
3668
3618
  case 0:
3669
- publicKey = this.provider.wallet.publicKey;
3619
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3670
3620
  payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
3671
3621
  if (!payTokenCustodyConfig) {
3672
3622
  throw "payTokenCustodyConfig not found";
@@ -3678,7 +3628,7 @@ var PerpetualsClient = (function () {
3678
3628
  payToken = poolConfig.getTokenFromSymbol(payTokenSymbol);
3679
3629
  _g.label = 1;
3680
3630
  case 1:
3681
- _g.trys.push([1, 10, , 11]);
3631
+ _g.trys.push([1, 9, , 10]);
3682
3632
  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);
3683
3633
  lpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
3684
3634
  custodyAccountMetas = [];
@@ -3705,24 +3655,20 @@ var PerpetualsClient = (function () {
3705
3655
  isWritable: false,
3706
3656
  });
3707
3657
  }
3708
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3709
- case 2:
3710
- if (!(_g.sent())) {
3711
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3712
- }
3713
- if (!(payTokenSymbol == 'SOL')) return [3, 5];
3658
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3659
+ if (!(payTokenSymbol == 'SOL')) return [3, 4];
3714
3660
  console.log("payTokenSymbol === SOL", payTokenSymbol);
3715
3661
  lamports = tokenAmountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3716
- if (!!skipBalanceChecks) return [3, 4];
3662
+ if (!!skipBalanceChecks) return [3, 3];
3717
3663
  _e = anchor_1.BN.bind;
3718
3664
  return [4, this.provider.connection.getBalance(publicKey)];
3719
- case 3:
3665
+ case 2:
3720
3666
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _g.sent()]))();
3721
3667
  if (unWrappedSolBalance.lt(lamports)) {
3722
3668
  throw "Insufficient SOL Funds";
3723
3669
  }
3724
- _g.label = 4;
3725
- case 4:
3670
+ _g.label = 3;
3671
+ case 3:
3726
3672
  if (!ephemeralSignerPubkey) {
3727
3673
  wrappedSolAccount = new web3_js_1.Keypair();
3728
3674
  additionalSigners.push(wrappedSolAccount);
@@ -3740,23 +3686,23 @@ var PerpetualsClient = (function () {
3740
3686
  postInstructions = [
3741
3687
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3742
3688
  ];
3743
- return [3, 8];
3744
- case 5:
3745
- if (!!skipBalanceChecks) return [3, 8];
3689
+ return [3, 7];
3690
+ case 4:
3691
+ if (!!skipBalanceChecks) return [3, 7];
3746
3692
  return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3747
- case 6:
3693
+ case 5:
3748
3694
  if (!(_g.sent())) {
3749
3695
  throw "Insufficient Funds , token Account doesn't exist";
3750
3696
  }
3751
3697
  _f = anchor_1.BN.bind;
3752
3698
  return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
3753
- case 7:
3699
+ case 6:
3754
3700
  tokenAccountBalance = new (_f.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3755
3701
  if (tokenAccountBalance.lt(tokenAmountIn)) {
3756
3702
  throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
3757
3703
  }
3758
- _g.label = 8;
3759
- case 8:
3704
+ _g.label = 7;
3705
+ case 7:
3760
3706
  whitelistPda = this.findProgramAddress("whitelist", [publicKey]).publicKey;
3761
3707
  whitelistMeta = {
3762
3708
  pubkey: whitelistPda,
@@ -3788,15 +3734,15 @@ var PerpetualsClient = (function () {
3788
3734
  })
3789
3735
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3790
3736
  .instruction()];
3791
- case 9:
3737
+ case 8:
3792
3738
  instruction = _g.sent();
3793
3739
  instructions.push(instruction);
3794
- return [3, 11];
3795
- case 10:
3740
+ return [3, 10];
3741
+ case 9:
3796
3742
  err_5 = _g.sent();
3797
3743
  console.error("perpClient addLiquidity error:: ", err_5);
3798
3744
  throw err_5;
3799
- case 11: return [2, {
3745
+ case 10: return [2, {
3800
3746
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3801
3747
  additionalSigners: additionalSigners
3802
3748
  }];
@@ -3829,7 +3775,7 @@ var PerpetualsClient = (function () {
3829
3775
  inputToken = poolConfig.getTokenFromSymbol(inputSymbol);
3830
3776
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
3831
3777
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
3832
- if (!(inputSymbol == 'SOL')) return [3, 4];
3778
+ if (!(inputSymbol == 'SOL')) return [3, 3];
3833
3779
  console.log("inputSymbol === SOL", inputSymbol);
3834
3780
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3835
3781
  console.log("lamports:", lamports.toNumber());
@@ -3842,11 +3788,8 @@ var PerpetualsClient = (function () {
3842
3788
  throw "Insufficient SOL Funds";
3843
3789
  }
3844
3790
  _g.label = 2;
3845
- case 2: return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3846
- case 3:
3847
- if (!(_g.sent())) {
3848
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3849
- }
3791
+ case 2:
3792
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3850
3793
  if (!ephemeralSignerPubkey) {
3851
3794
  wrappedSolAccount = new web3_js_1.Keypair();
3852
3795
  additionalSigners.push(wrappedSolAccount);
@@ -3864,24 +3807,24 @@ var PerpetualsClient = (function () {
3864
3807
  postInstructions = [
3865
3808
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3866
3809
  ];
3867
- return [3, 7];
3868
- case 4:
3810
+ return [3, 6];
3811
+ case 3:
3869
3812
  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);
3870
- if (!!skipBalanceChecks) return [3, 7];
3813
+ if (!!skipBalanceChecks) return [3, 6];
3871
3814
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3872
- case 5:
3815
+ case 4:
3873
3816
  if (!(_g.sent())) {
3874
3817
  throw "Insufficient Funds , token Account doesn't exist";
3875
3818
  }
3876
3819
  _b = anchor_1.BN.bind;
3877
3820
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3878
- case 6:
3821
+ case 5:
3879
3822
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3880
3823
  if (tokenAccountBalance.lt(amountIn)) {
3881
3824
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3882
3825
  }
3883
- _g.label = 7;
3884
- case 7:
3826
+ _g.label = 6;
3827
+ case 6:
3885
3828
  custodyAccountMetas = [];
3886
3829
  custodyOracleAccountMetas = [];
3887
3830
  markets = [];
@@ -3938,7 +3881,7 @@ var PerpetualsClient = (function () {
3938
3881
  })
3939
3882
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3940
3883
  .instruction()];
3941
- case 8:
3884
+ case 7:
3942
3885
  instruction = _g.sent();
3943
3886
  instructions.push(instruction);
3944
3887
  return [2, {
@@ -3955,15 +3898,15 @@ var PerpetualsClient = (function () {
3955
3898
  args_1[_i - 4] = arguments[_i];
3956
3899
  }
3957
3900
  return __awaiter(_this, __spreadArray([recieveTokenSymbol_1, liquidityAmountIn_1, minTokenAmountOut_1, poolConfig_1], args_1, true), void 0, function (recieveTokenSymbol, liquidityAmountIn, minTokenAmountOut, poolConfig, closeLpATA, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey, userPublicKey, isWhitelistedUser) {
3958
- var recieveTokenCustodyConfig, publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, recieveToken, stakedLpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, lamports, _e, whitelistPda, whitelistMeta, removeLiquidityTx, closeInx, closeWsolATAIns, err_6;
3901
+ var recieveTokenCustodyConfig, publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, recieveToken, stakedLpTokenAccount, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, lamports, whitelistPda, whitelistMeta, removeLiquidityTx, closeInx, closeWsolATAIns, err_6;
3959
3902
  if (closeLpATA === void 0) { closeLpATA = false; }
3960
3903
  if (createUserATA === void 0) { createUserATA = true; }
3961
3904
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3962
3905
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3963
3906
  if (userPublicKey === void 0) { userPublicKey = undefined; }
3964
3907
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
3965
- return __generator(this, function (_f) {
3966
- switch (_f.label) {
3908
+ return __generator(this, function (_e) {
3909
+ switch (_e.label) {
3967
3910
  case 0:
3968
3911
  recieveTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(recieveTokenSymbol).mintKey); });
3969
3912
  if (!recieveTokenCustodyConfig) {
@@ -3975,9 +3918,9 @@ var PerpetualsClient = (function () {
3975
3918
  postInstructions = [];
3976
3919
  additionalSigners = [];
3977
3920
  recieveToken = poolConfig.getTokenFromSymbol(recieveTokenSymbol);
3978
- _f.label = 1;
3921
+ _e.label = 1;
3979
3922
  case 1:
3980
- _f.trys.push([1, 7, , 8]);
3923
+ _e.trys.push([1, 3, , 4]);
3981
3924
  stakedLpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
3982
3925
  custodyAccountMetas = [];
3983
3926
  custodyOracleAccountMetas = [];
@@ -4003,40 +3946,30 @@ var PerpetualsClient = (function () {
4003
3946
  isWritable: false,
4004
3947
  });
4005
3948
  }
4006
- if (!(recieveTokenSymbol == 'SOL')) return [3, 2];
4007
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4008
- if (!ephemeralSignerPubkey) {
4009
- wrappedSolAccount = new web3_js_1.Keypair();
4010
- additionalSigners.push(wrappedSolAccount);
3949
+ if (recieveTokenSymbol == 'SOL') {
3950
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3951
+ if (!ephemeralSignerPubkey) {
3952
+ wrappedSolAccount = new web3_js_1.Keypair();
3953
+ additionalSigners.push(wrappedSolAccount);
3954
+ }
3955
+ preInstructions = [
3956
+ web3_js_1.SystemProgram.createAccount({
3957
+ fromPubkey: publicKey,
3958
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3959
+ lamports: lamports,
3960
+ space: 165,
3961
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3962
+ }),
3963
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3964
+ ];
3965
+ postInstructions = [
3966
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3967
+ ];
4011
3968
  }
4012
- preInstructions = [
4013
- web3_js_1.SystemProgram.createAccount({
4014
- fromPubkey: publicKey,
4015
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4016
- lamports: lamports,
4017
- space: 165,
4018
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4019
- }),
4020
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4021
- ];
4022
- postInstructions = [
4023
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4024
- ];
4025
- return [3, 5];
4026
- case 2:
4027
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(recieveToken.mintKey, publicKey, true, recieveToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
4028
- _e = createUserATA;
4029
- if (!_e) return [3, 4];
4030
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
4031
- case 3:
4032
- _e = !(_f.sent());
4033
- _f.label = 4;
4034
- case 4:
4035
- if (_e) {
4036
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, recieveToken.mintKey, recieveToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3969
+ else {
3970
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(recieveToken.mintKey, publicKey, true, recieveToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3971
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, recieveToken.mintKey, recieveToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
4037
3972
  }
4038
- _f.label = 5;
4039
- case 5:
4040
3973
  whitelistPda = this.findProgramAddress("whitelist", [publicKey]).publicKey;
4041
3974
  whitelistMeta = {
4042
3975
  pubkey: whitelistPda,
@@ -4068,8 +4001,8 @@ var PerpetualsClient = (function () {
4068
4001
  })
4069
4002
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4070
4003
  .instruction()];
4071
- case 6:
4072
- removeLiquidityTx = _f.sent();
4004
+ case 2:
4005
+ removeLiquidityTx = _e.sent();
4073
4006
  instructions.push(removeLiquidityTx);
4074
4007
  if (closeLpATA) {
4075
4008
  closeInx = (0, spl_token_1.createCloseAccountInstruction)(stakedLpTokenAccount, publicKey, publicKey);
@@ -4079,12 +4012,12 @@ var PerpetualsClient = (function () {
4079
4012
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
4080
4013
  postInstructions.push(closeWsolATAIns);
4081
4014
  }
4082
- return [3, 8];
4083
- case 7:
4084
- err_6 = _f.sent();
4015
+ return [3, 4];
4016
+ case 3:
4017
+ err_6 = _e.sent();
4085
4018
  console.log("perpClient removeLiquidity error:: ", err_6);
4086
4019
  throw err_6;
4087
- case 8: return [2, {
4020
+ case 4: return [2, {
4088
4021
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4089
4022
  additionalSigners: additionalSigners
4090
4023
  }];
@@ -4421,12 +4354,12 @@ var PerpetualsClient = (function () {
4421
4354
  }
4422
4355
  });
4423
4356
  }); };
4424
- this.unstakeRequest = function (unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4357
+ this.unstakeRequest = function (unstakeAmount, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
4425
4358
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_13;
4426
4359
  return __generator(this, function (_a) {
4427
4360
  switch (_a.label) {
4428
4361
  case 0:
4429
- publicKey = this.provider.wallet.publicKey;
4362
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4430
4363
  preInstructions = [];
4431
4364
  instructions = [];
4432
4365
  postInstructions = [];
@@ -4472,37 +4405,28 @@ var PerpetualsClient = (function () {
4472
4405
  args_1[_i - 1] = arguments[_i];
4473
4406
  }
4474
4407
  return __awaiter(_this, __spreadArray([poolConfig_1], args_1, true), void 0, function (poolConfig, pendingActivation, deactivated, createUserLPTA, userPublicKey) {
4475
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, pool, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, _a, withdrawStakeInstruction, err_14;
4408
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, pool, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, withdrawStakeInstruction, err_14;
4476
4409
  if (pendingActivation === void 0) { pendingActivation = true; }
4477
4410
  if (deactivated === void 0) { deactivated = true; }
4478
4411
  if (createUserLPTA === void 0) { createUserLPTA = true; }
4479
4412
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4480
- return __generator(this, function (_b) {
4481
- switch (_b.label) {
4413
+ return __generator(this, function (_a) {
4414
+ switch (_a.label) {
4482
4415
  case 0:
4483
4416
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4484
4417
  preInstructions = [];
4485
4418
  instructions = [];
4486
4419
  postInstructions = [];
4487
4420
  additionalSigners = [];
4488
- _b.label = 1;
4421
+ _a.label = 1;
4489
4422
  case 1:
4490
- _b.trys.push([1, 5, , 6]);
4423
+ _a.trys.push([1, 3, , 4]);
4491
4424
  lpTokenMint = poolConfig.stakedLpTokenMint;
4492
4425
  pool = poolConfig.poolAddress;
4493
4426
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), pool.toBuffer(), lpTokenMint.toBuffer()], this.program.programId)[0];
4494
4427
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4495
4428
  userLpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
4496
- _a = createUserLPTA;
4497
- if (!_a) return [3, 3];
4498
- return [4, (0, utils_1.checkIfAccountExists)(userLpTokenAccount, this.provider.connection)];
4499
- case 2:
4500
- _a = !(_b.sent());
4501
- _b.label = 3;
4502
- case 3:
4503
- if (_a) {
4504
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userLpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4505
- }
4429
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userLpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4506
4430
  return [4, this.program.methods
4507
4431
  .withdrawStake({
4508
4432
  pendingActivation: pendingActivation,
@@ -4523,15 +4447,15 @@ var PerpetualsClient = (function () {
4523
4447
  lpMint: poolConfig.stakedLpTokenMint,
4524
4448
  })
4525
4449
  .instruction()];
4526
- case 4:
4527
- withdrawStakeInstruction = _b.sent();
4450
+ case 2:
4451
+ withdrawStakeInstruction = _a.sent();
4528
4452
  instructions.push(withdrawStakeInstruction);
4529
- return [3, 6];
4530
- case 5:
4531
- err_14 = _b.sent();
4453
+ return [3, 4];
4454
+ case 3:
4455
+ err_14 = _a.sent();
4532
4456
  console.log("perpClient withdrawStake error:: ", err_14);
4533
4457
  throw err_14;
4534
- case 6: return [2, {
4458
+ case 4: return [2, {
4535
4459
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4536
4460
  additionalSigners: additionalSigners
4537
4461
  }];
@@ -4545,10 +4469,10 @@ var PerpetualsClient = (function () {
4545
4469
  args_1[_i - 3] = arguments[_i];
4546
4470
  }
4547
4471
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4548
- var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4472
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4549
4473
  if (createUserATA === void 0) { createUserATA = true; }
4550
- return __generator(this, function (_b) {
4551
- switch (_b.label) {
4474
+ return __generator(this, function (_a) {
4475
+ switch (_a.label) {
4552
4476
  case 0:
4553
4477
  publicKey = this.provider.wallet.publicKey;
4554
4478
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
@@ -4557,22 +4481,13 @@ var PerpetualsClient = (function () {
4557
4481
  instructions = [];
4558
4482
  postInstructions = [];
4559
4483
  additionalSigners = [];
4560
- _b.label = 1;
4484
+ _a.label = 1;
4561
4485
  case 1:
4562
- _b.trys.push([1, 5, , 6]);
4486
+ _a.trys.push([1, 3, , 4]);
4563
4487
  pool = poolConfig.poolAddress;
4564
4488
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4565
4489
  receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
4566
- _a = createUserATA;
4567
- if (!_a) return [3, 3];
4568
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
4569
- case 2:
4570
- _a = !(_b.sent());
4571
- _b.label = 3;
4572
- case 3:
4573
- if (_a) {
4574
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint));
4575
- }
4490
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
4576
4491
  tokenStakeAccounts = [];
4577
4492
  if (tokenStakeAccount) {
4578
4493
  tokenStakeAccounts.push({
@@ -4601,15 +4516,15 @@ var PerpetualsClient = (function () {
4601
4516
  })
4602
4517
  .remainingAccounts(__spreadArray([], tokenStakeAccounts, true))
4603
4518
  .instruction()];
4604
- case 4:
4605
- withdrawStakeInstruction = _b.sent();
4519
+ case 2:
4520
+ withdrawStakeInstruction = _a.sent();
4606
4521
  instructions.push(withdrawStakeInstruction);
4607
- return [3, 6];
4608
- case 5:
4609
- err_15 = _b.sent();
4522
+ return [3, 4];
4523
+ case 3:
4524
+ err_15 = _a.sent();
4610
4525
  console.log("perpClient withdrawStake error:: ", err_15);
4611
4526
  throw err_15;
4612
- case 6: return [2, {
4527
+ case 4: return [2, {
4613
4528
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4614
4529
  additionalSigners: additionalSigners
4615
4530
  }];
@@ -4668,29 +4583,21 @@ var PerpetualsClient = (function () {
4668
4583
  isWritable: false,
4669
4584
  });
4670
4585
  }
4671
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
4672
- case 1:
4673
- if (!(_f.sent())) {
4674
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4675
- }
4676
- return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
4677
- case 2:
4678
- if (!(_f.sent())) {
4679
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
4680
- }
4681
- if (!(inTokenSymbol == 'SOL')) return [3, 5];
4586
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4587
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4588
+ if (!(inTokenSymbol == 'SOL')) return [3, 3];
4682
4589
  console.log("inTokenSymbol === SOL", inTokenSymbol);
4683
4590
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
4684
- if (!!skipBalanceChecks) return [3, 4];
4591
+ if (!!skipBalanceChecks) return [3, 2];
4685
4592
  _e = anchor_1.BN.bind;
4686
4593
  return [4, this.provider.connection.getBalance(publicKey)];
4687
- case 3:
4594
+ case 1:
4688
4595
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _f.sent()]))();
4689
4596
  if (unWrappedSolBalance.lt(lamports)) {
4690
4597
  throw "Insufficient SOL Funds";
4691
4598
  }
4692
- _f.label = 4;
4693
- case 4:
4599
+ _f.label = 2;
4600
+ case 2:
4694
4601
  if (!ephemeralSignerPubkey) {
4695
4602
  wrappedSolAccount = new web3_js_1.Keypair();
4696
4603
  additionalSigners.push(wrappedSolAccount);
@@ -4708,17 +4615,17 @@ var PerpetualsClient = (function () {
4708
4615
  postInstructions = [
4709
4616
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4710
4617
  ];
4711
- return [3, 7];
4712
- case 5:
4713
- if (!!skipBalanceChecks) return [3, 7];
4618
+ return [3, 5];
4619
+ case 3:
4620
+ if (!!skipBalanceChecks) return [3, 5];
4714
4621
  return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
4715
- case 6:
4622
+ case 4:
4716
4623
  if (!(_f.sent())) {
4717
4624
  throw "Insufficient Funds , token Account doesn't exist";
4718
4625
  }
4719
- _f.label = 7;
4720
- case 7:
4721
- _f.trys.push([7, 9, , 10]);
4626
+ _f.label = 5;
4627
+ case 5:
4628
+ _f.trys.push([5, 7, , 8]);
4722
4629
  if (enableHeapSizeIx) {
4723
4630
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4724
4631
  bytes: 64 * 1024,
@@ -4760,15 +4667,15 @@ var PerpetualsClient = (function () {
4760
4667
  })
4761
4668
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4762
4669
  .instruction()];
4763
- case 8:
4670
+ case 6:
4764
4671
  addCompoundingLiquidity = _f.sent();
4765
4672
  instructions.push(addCompoundingLiquidity);
4766
- return [3, 10];
4767
- case 9:
4673
+ return [3, 8];
4674
+ case 7:
4768
4675
  err_16 = _f.sent();
4769
4676
  console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4770
- return [3, 10];
4771
- case 10: return [2, {
4677
+ return [3, 8];
4678
+ case 8: return [2, {
4772
4679
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4773
4680
  additionalSigners: additionalSigners
4774
4681
  }];
@@ -4782,14 +4689,14 @@ var PerpetualsClient = (function () {
4782
4689
  args_1[_i - 5] = arguments[_i];
4783
4690
  }
4784
4691
  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) {
4785
- 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;
4692
+ 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;
4786
4693
  if (createUserATA === void 0) { createUserATA = true; }
4787
4694
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4788
4695
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4789
4696
  if (enableHeapSizeIx === void 0) { enableHeapSizeIx = true; }
4790
4697
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
4791
- return __generator(this, function (_f) {
4792
- switch (_f.label) {
4698
+ return __generator(this, function (_e) {
4699
+ switch (_e.label) {
4793
4700
  case 0:
4794
4701
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4795
4702
  preInstructions = [];
@@ -4800,45 +4707,35 @@ var PerpetualsClient = (function () {
4800
4707
  outCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outTokenSymbol).mintKey); });
4801
4708
  lpTokenMint = poolConfig.stakedLpTokenMint;
4802
4709
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4803
- if (!(outCustodyConfig.symbol == 'SOL')) return [3, 1];
4804
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4805
- if (!ephemeralSignerPubkey) {
4806
- wrappedSolAccount = new web3_js_1.Keypair();
4807
- additionalSigners.push(wrappedSolAccount);
4710
+ if (outCustodyConfig.symbol == 'SOL') {
4711
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
4712
+ if (!ephemeralSignerPubkey) {
4713
+ wrappedSolAccount = new web3_js_1.Keypair();
4714
+ additionalSigners.push(wrappedSolAccount);
4715
+ }
4716
+ preInstructions = [
4717
+ web3_js_1.SystemProgram.createAccount({
4718
+ fromPubkey: publicKey,
4719
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4720
+ lamports: lamports,
4721
+ space: 165,
4722
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
4723
+ }),
4724
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4725
+ ];
4726
+ postInstructions = [
4727
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4728
+ ];
4808
4729
  }
4809
- preInstructions = [
4810
- web3_js_1.SystemProgram.createAccount({
4811
- fromPubkey: publicKey,
4812
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4813
- lamports: lamports,
4814
- space: 165,
4815
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4816
- }),
4817
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4818
- ];
4819
- postInstructions = [
4820
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4821
- ];
4822
- return [3, 4];
4823
- case 1:
4824
- 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);
4825
- _a = createUserATA;
4826
- if (!_a) return [3, 3];
4827
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
4828
- case 2:
4829
- _a = !(_f.sent());
4830
- _f.label = 3;
4831
- case 3:
4832
- if (_a) {
4833
- 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));
4730
+ else {
4731
+ 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);
4732
+ 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));
4834
4733
  }
4835
- _f.label = 4;
4836
- case 4:
4837
4734
  custodyAccountMetas = [];
4838
4735
  custodyOracleAccountMetas = [];
4839
4736
  markets = [];
4840
- for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4841
- custody = _c[_b];
4737
+ for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
4738
+ custody = _b[_a];
4842
4739
  custodyAccountMetas.push({
4843
4740
  pubkey: custody.custodyAccount,
4844
4741
  isSigner: false,
@@ -4850,8 +4747,8 @@ var PerpetualsClient = (function () {
4850
4747
  isWritable: false,
4851
4748
  });
4852
4749
  }
4853
- for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4854
- market = _e[_d];
4750
+ for (_c = 0, _d = poolConfig.markets; _c < _d.length; _c++) {
4751
+ market = _d[_c];
4855
4752
  markets.push({
4856
4753
  pubkey: market.marketAccount,
4857
4754
  isSigner: false,
@@ -4859,9 +4756,9 @@ var PerpetualsClient = (function () {
4859
4756
  });
4860
4757
  }
4861
4758
  compoundingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4862
- _f.label = 5;
4863
- case 5:
4864
- _f.trys.push([5, 7, , 8]);
4759
+ _e.label = 1;
4760
+ case 1:
4761
+ _e.trys.push([1, 3, , 4]);
4865
4762
  if (enableHeapSizeIx) {
4866
4763
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4867
4764
  bytes: 64 * 1024,
@@ -4903,15 +4800,15 @@ var PerpetualsClient = (function () {
4903
4800
  })
4904
4801
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4905
4802
  .instruction()];
4906
- case 6:
4907
- removeCompoundingLiquidity = _f.sent();
4803
+ case 2:
4804
+ removeCompoundingLiquidity = _e.sent();
4908
4805
  instructions.push(removeCompoundingLiquidity);
4909
- return [3, 8];
4910
- case 7:
4911
- err_17 = _f.sent();
4806
+ return [3, 4];
4807
+ case 3:
4808
+ err_17 = _e.sent();
4912
4809
  console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
4913
- return [3, 8];
4914
- case 8: return [2, {
4810
+ return [3, 4];
4811
+ case 4: return [2, {
4915
4812
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4916
4813
  additionalSigners: additionalSigners
4917
4814
  }];
@@ -4924,13 +4821,13 @@ var PerpetualsClient = (function () {
4924
4821
  for (var _i = 3; _i < arguments.length; _i++) {
4925
4822
  args_1[_i - 3] = arguments[_i];
4926
4823
  }
4927
- return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA) {
4928
- 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;
4824
+ return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA, userPublicKey) {
4825
+ 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;
4929
4826
  if (createUserATA === void 0) { createUserATA = true; }
4930
- return __generator(this, function (_g) {
4931
- switch (_g.label) {
4827
+ return __generator(this, function (_f) {
4828
+ switch (_f.label) {
4932
4829
  case 0:
4933
- publicKey = this.provider.wallet.publicKey;
4830
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4934
4831
  preInstructions = [];
4935
4832
  instructions = [];
4936
4833
  postInstructions = [];
@@ -4939,27 +4836,18 @@ var PerpetualsClient = (function () {
4939
4836
  lpTokenMint = poolConfig.stakedLpTokenMint;
4940
4837
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4941
4838
  compoudingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4942
- _a = createUserATA;
4943
- if (!_a) return [3, 2];
4944
- return [4, (0, utils_1.checkIfAccountExists)(compoudingTokenAccount, this.provider.connection)];
4945
- case 1:
4946
- _a = !(_g.sent());
4947
- _g.label = 2;
4948
- case 2:
4949
- if (_a) {
4950
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint));
4951
- }
4839
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4952
4840
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
4953
4841
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
4954
4842
  tokenStakeAccounts = [];
4955
- _b = tokenStakeAccount;
4956
- if (!_b) return [3, 4];
4843
+ _a = tokenStakeAccount;
4844
+ if (!_a) return [3, 2];
4957
4845
  return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
4958
- case 3:
4959
- _b = (_g.sent());
4960
- _g.label = 4;
4961
- case 4:
4962
- if (_b) {
4846
+ case 1:
4847
+ _a = (_f.sent());
4848
+ _f.label = 2;
4849
+ case 2:
4850
+ if (_a) {
4963
4851
  tokenStakeAccounts.push({
4964
4852
  pubkey: tokenStakeAccount,
4965
4853
  isSigner: false,
@@ -4970,8 +4858,8 @@ var PerpetualsClient = (function () {
4970
4858
  custodyAccountMetas = [];
4971
4859
  custodyOracleAccountMetas = [];
4972
4860
  markets = [];
4973
- for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
4974
- custody = _d[_c];
4861
+ for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4862
+ custody = _c[_b];
4975
4863
  custodyAccountMetas.push({
4976
4864
  pubkey: custody.custodyAccount,
4977
4865
  isSigner: false,
@@ -4983,17 +4871,17 @@ var PerpetualsClient = (function () {
4983
4871
  isWritable: false,
4984
4872
  });
4985
4873
  }
4986
- for (_e = 0, _f = poolConfig.markets; _e < _f.length; _e++) {
4987
- market = _f[_e];
4874
+ for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4875
+ market = _e[_d];
4988
4876
  markets.push({
4989
4877
  pubkey: market.marketAccount,
4990
4878
  isSigner: false,
4991
4879
  isWritable: false,
4992
4880
  });
4993
4881
  }
4994
- _g.label = 5;
4995
- case 5:
4996
- _g.trys.push([5, 7, , 8]);
4882
+ _f.label = 3;
4883
+ case 3:
4884
+ _f.trys.push([3, 5, , 6]);
4997
4885
  return [4, this.program.methods
4998
4886
  .migrateStake({
4999
4887
  amount: amount
@@ -5018,15 +4906,15 @@ var PerpetualsClient = (function () {
5018
4906
  })
5019
4907
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), tokenStakeAccounts, true))
5020
4908
  .instruction()];
5021
- case 6:
5022
- migrateStake = _g.sent();
4909
+ case 4:
4910
+ migrateStake = _f.sent();
5023
4911
  instructions.push(migrateStake);
5024
- return [3, 8];
5025
- case 7:
5026
- err_18 = _g.sent();
4912
+ return [3, 6];
4913
+ case 5:
4914
+ err_18 = _f.sent();
5027
4915
  console.log("perpClient migrateStake error:: ", err_18);
5028
- return [3, 8];
5029
- case 8: return [2, {
4916
+ return [3, 6];
4917
+ case 6: return [2, {
5030
4918
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5031
4919
  additionalSigners: additionalSigners
5032
4920
  }];
@@ -5034,12 +4922,12 @@ var PerpetualsClient = (function () {
5034
4922
  });
5035
4923
  });
5036
4924
  };
5037
- this.migrateFlp = function (amount, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4925
+ this.migrateFlp = function (amount, rewardTokenMint, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
5038
4926
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_19;
5039
4927
  return __generator(this, function (_d) {
5040
4928
  switch (_d.label) {
5041
4929
  case 0:
5042
- publicKey = this.provider.wallet.publicKey;
4930
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5043
4931
  preInstructions = [];
5044
4932
  instructions = [];
5045
4933
  postInstructions = [];
@@ -5203,14 +5091,10 @@ var PerpetualsClient = (function () {
5203
5091
  additionalSigners = [];
5204
5092
  _a.label = 1;
5205
5093
  case 1:
5206
- _a.trys.push([1, 4, , 5]);
5094
+ _a.trys.push([1, 3, , 4]);
5207
5095
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5208
5096
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5209
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5210
- case 2:
5211
- if (!(_a.sent())) {
5212
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5213
- }
5097
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5214
5098
  return [4, this.program.methods
5215
5099
  .depositTokenStake({
5216
5100
  depositAmount: depositAmount
@@ -5230,15 +5114,15 @@ var PerpetualsClient = (function () {
5230
5114
  tokenMint: poolConfig.tokenMint,
5231
5115
  })
5232
5116
  .instruction()];
5233
- case 3:
5117
+ case 2:
5234
5118
  depositTokenStakeInstruction = _a.sent();
5235
5119
  instructions.push(depositTokenStakeInstruction);
5236
- return [3, 5];
5237
- case 4:
5120
+ return [3, 4];
5121
+ case 3:
5238
5122
  err_21 = _a.sent();
5239
5123
  console.log("perpClient depositStakingInstruction error:: ", err_21);
5240
5124
  throw err_21;
5241
- case 5: return [2, {
5125
+ case 4: return [2, {
5242
5126
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5243
5127
  additionalSigners: additionalSigners
5244
5128
  }];
@@ -5296,14 +5180,10 @@ var PerpetualsClient = (function () {
5296
5180
  additionalSigners = [];
5297
5181
  _a.label = 1;
5298
5182
  case 1:
5299
- _a.trys.push([1, 4, , 5]);
5183
+ _a.trys.push([1, 3, , 4]);
5300
5184
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5301
5185
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5302
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5303
- case 2:
5304
- if (!(_a.sent())) {
5305
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5306
- }
5186
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5307
5187
  return [4, this.program.methods
5308
5188
  .unstakeTokenInstant({
5309
5189
  unstakeAmount: unstakeAmount
@@ -5322,15 +5202,15 @@ var PerpetualsClient = (function () {
5322
5202
  tokenMint: poolConfig.tokenMint,
5323
5203
  })
5324
5204
  .instruction()];
5325
- case 3:
5205
+ case 2:
5326
5206
  unstakeTokenInstantInstruction = _a.sent();
5327
5207
  instructions.push(unstakeTokenInstantInstruction);
5328
- return [3, 5];
5329
- case 4:
5208
+ return [3, 4];
5209
+ case 3:
5330
5210
  err_23 = _a.sent();
5331
5211
  console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5332
5212
  throw err_23;
5333
- case 5: return [2, {
5213
+ case 4: return [2, {
5334
5214
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5335
5215
  additionalSigners: additionalSigners
5336
5216
  }];
@@ -5348,14 +5228,10 @@ var PerpetualsClient = (function () {
5348
5228
  additionalSigners = [];
5349
5229
  _a.label = 1;
5350
5230
  case 1:
5351
- _a.trys.push([1, 4, , 5]);
5231
+ _a.trys.push([1, 3, , 4]);
5352
5232
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5353
5233
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5354
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5355
- case 2:
5356
- if (!(_a.sent())) {
5357
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5358
- }
5234
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5359
5235
  return [4, this.program.methods
5360
5236
  .withdrawToken({
5361
5237
  withdrawRequestId: withdrawRequestId
@@ -5374,15 +5250,15 @@ var PerpetualsClient = (function () {
5374
5250
  tokenMint: poolConfig.tokenMint,
5375
5251
  })
5376
5252
  .instruction()];
5377
- case 3:
5253
+ case 2:
5378
5254
  withdrawTokenInstruction = _a.sent();
5379
5255
  instructions.push(withdrawTokenInstruction);
5380
- return [3, 5];
5381
- case 4:
5256
+ return [3, 4];
5257
+ case 3:
5382
5258
  err_24 = _a.sent();
5383
5259
  console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5384
5260
  throw err_24;
5385
- case 5: return [2, {
5261
+ case 4: return [2, {
5386
5262
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5387
5263
  additionalSigners: additionalSigners
5388
5264
  }];
@@ -5434,32 +5310,23 @@ var PerpetualsClient = (function () {
5434
5310
  for (var _i = 2; _i < arguments.length; _i++) {
5435
5311
  args_1[_i - 2] = arguments[_i];
5436
5312
  }
5437
- return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA) {
5438
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_26;
5313
+ return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA, userPublicKey) {
5314
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, collectTokenRewardInstruction, err_26;
5439
5315
  if (createUserATA === void 0) { createUserATA = true; }
5440
- return __generator(this, function (_b) {
5441
- switch (_b.label) {
5316
+ return __generator(this, function (_a) {
5317
+ switch (_a.label) {
5442
5318
  case 0:
5443
- publicKey = this.provider.wallet.publicKey;
5319
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5444
5320
  preInstructions = [];
5445
5321
  instructions = [];
5446
5322
  postInstructions = [];
5447
5323
  additionalSigners = [];
5448
- _b.label = 1;
5324
+ _a.label = 1;
5449
5325
  case 1:
5450
- _b.trys.push([1, 5, , 6]);
5326
+ _a.trys.push([1, 3, , 4]);
5451
5327
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5452
5328
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5453
- _a = createUserATA;
5454
- if (!_a) return [3, 3];
5455
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5456
- case 2:
5457
- _a = !(_b.sent());
5458
- _b.label = 3;
5459
- case 3:
5460
- if (_a) {
5461
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint));
5462
- }
5329
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5463
5330
  return [4, this.program.methods
5464
5331
  .collectTokenReward({})
5465
5332
  .accounts({
@@ -5476,15 +5343,15 @@ var PerpetualsClient = (function () {
5476
5343
  tokenMint: poolConfig.tokenMint,
5477
5344
  })
5478
5345
  .instruction()];
5479
- case 4:
5480
- collectTokenRewardInstruction = _b.sent();
5346
+ case 2:
5347
+ collectTokenRewardInstruction = _a.sent();
5481
5348
  instructions.push(collectTokenRewardInstruction);
5482
- return [3, 6];
5483
- case 5:
5484
- err_26 = _b.sent();
5349
+ return [3, 4];
5350
+ case 3:
5351
+ err_26 = _a.sent();
5485
5352
  console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5486
5353
  throw err_26;
5487
- case 6: return [2, {
5354
+ case 4: return [2, {
5488
5355
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5489
5356
  additionalSigners: additionalSigners
5490
5357
  }];
@@ -5498,32 +5365,23 @@ var PerpetualsClient = (function () {
5498
5365
  args_1[_i - 3] = arguments[_i];
5499
5366
  }
5500
5367
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5501
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_27;
5368
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, collectRevenueInstruction, err_27;
5502
5369
  if (createUserATA === void 0) { createUserATA = true; }
5503
- return __generator(this, function (_b) {
5504
- switch (_b.label) {
5370
+ return __generator(this, function (_a) {
5371
+ switch (_a.label) {
5505
5372
  case 0:
5506
5373
  publicKey = this.provider.wallet.publicKey;
5507
5374
  preInstructions = [];
5508
5375
  instructions = [];
5509
5376
  postInstructions = [];
5510
5377
  additionalSigners = [];
5511
- _b.label = 1;
5378
+ _a.label = 1;
5512
5379
  case 1:
5513
- _b.trys.push([1, 5, , 6]);
5380
+ _a.trys.push([1, 3, , 4]);
5514
5381
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5515
5382
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5516
5383
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, owner, true);
5517
- _a = createUserATA;
5518
- if (!_a) return [3, 3];
5519
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5520
- case 2:
5521
- _a = !(_b.sent());
5522
- _b.label = 3;
5523
- case 3:
5524
- if (_a) {
5525
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint));
5526
- }
5384
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
5527
5385
  return [4, this.program.methods
5528
5386
  .collectRevenue({})
5529
5387
  .accounts({
@@ -5540,15 +5398,15 @@ var PerpetualsClient = (function () {
5540
5398
  receivingTokenMint: rewardCustodyMint,
5541
5399
  })
5542
5400
  .instruction()];
5543
- case 4:
5544
- collectRevenueInstruction = _b.sent();
5401
+ case 2:
5402
+ collectRevenueInstruction = _a.sent();
5545
5403
  instructions.push(collectRevenueInstruction);
5546
- return [3, 6];
5547
- case 5:
5548
- err_27 = _b.sent();
5404
+ return [3, 4];
5405
+ case 3:
5406
+ err_27 = _a.sent();
5549
5407
  console.log("perpClient collectRevenueInstruction error:: ", err_27);
5550
5408
  throw err_27;
5551
- case 6: return [2, {
5409
+ case 4: return [2, {
5552
5410
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5553
5411
  additionalSigners: additionalSigners
5554
5412
  }];
@@ -5562,32 +5420,23 @@ var PerpetualsClient = (function () {
5562
5420
  args_1[_i - 3] = arguments[_i];
5563
5421
  }
5564
5422
  return __awaiter(_this, __spreadArray([owner_1, rebateSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rebateSymbol, poolConfig, createUserATA) {
5565
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, _a, collectRebateInstruction, err_28;
5423
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, collectRebateInstruction, err_28;
5566
5424
  if (createUserATA === void 0) { createUserATA = true; }
5567
- return __generator(this, function (_b) {
5568
- switch (_b.label) {
5425
+ return __generator(this, function (_a) {
5426
+ switch (_a.label) {
5569
5427
  case 0:
5570
5428
  publicKey = this.provider.wallet.publicKey;
5571
5429
  preInstructions = [];
5572
5430
  instructions = [];
5573
5431
  postInstructions = [];
5574
5432
  additionalSigners = [];
5575
- _b.label = 1;
5433
+ _a.label = 1;
5576
5434
  case 1:
5577
- _b.trys.push([1, 5, , 6]);
5435
+ _a.trys.push([1, 3, , 4]);
5578
5436
  rebateMint = poolConfig.getTokenFromSymbol(rebateSymbol).mintKey;
5579
5437
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5580
5438
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rebateMint, owner, true);
5581
- _a = createUserATA;
5582
- if (!_a) return [3, 3];
5583
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5584
- case 2:
5585
- _a = !(_b.sent());
5586
- _b.label = 3;
5587
- case 3:
5588
- if (_a) {
5589
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rebateMint));
5590
- }
5439
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rebateMint, spl_token_1.TOKEN_PROGRAM_ID));
5591
5440
  return [4, this.program.methods
5592
5441
  .collectRebate()
5593
5442
  .accounts({
@@ -5604,15 +5453,15 @@ var PerpetualsClient = (function () {
5604
5453
  receivingTokenMint: rebateMint,
5605
5454
  })
5606
5455
  .instruction()];
5607
- case 4:
5608
- collectRebateInstruction = _b.sent();
5456
+ case 2:
5457
+ collectRebateInstruction = _a.sent();
5609
5458
  instructions.push(collectRebateInstruction);
5610
- return [3, 6];
5611
- case 5:
5612
- err_28 = _b.sent();
5459
+ return [3, 4];
5460
+ case 3:
5461
+ err_28 = _a.sent();
5613
5462
  console.log("perpClient collectRebateInstruction error:: ", err_28);
5614
5463
  throw err_28;
5615
- case 6: return [2, {
5464
+ case 4: return [2, {
5616
5465
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5617
5466
  additionalSigners: additionalSigners
5618
5467
  }];
@@ -5672,14 +5521,14 @@ var PerpetualsClient = (function () {
5672
5521
  for (var _i = 11; _i < arguments.length; _i++) {
5673
5522
  args_1[_i - 11] = arguments[_i];
5674
5523
  }
5675
- 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) {
5524
+ 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) {
5676
5525
  var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_30;
5677
5526
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5678
5527
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5679
5528
  return __generator(this, function (_c) {
5680
5529
  switch (_c.label) {
5681
5530
  case 0:
5682
- publicKey = this.provider.wallet.publicKey;
5531
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5683
5532
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5684
5533
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5685
5534
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5795,14 +5644,14 @@ var PerpetualsClient = (function () {
5795
5644
  for (var _i = 11; _i < arguments.length; _i++) {
5796
5645
  args_1[_i - 11] = arguments[_i];
5797
5646
  }
5798
- 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) {
5799
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_31;
5647
+ 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) {
5648
+ var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, positionAccount, orderAccount, editLimitOrder, err_31;
5800
5649
  if (createUserATA === void 0) { createUserATA = true; }
5801
5650
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5802
- return __generator(this, function (_b) {
5803
- switch (_b.label) {
5651
+ return __generator(this, function (_a) {
5652
+ switch (_a.label) {
5804
5653
  case 0:
5805
- publicKey = this.provider.wallet.publicKey;
5654
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5806
5655
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5807
5656
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5808
5657
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5812,43 +5661,33 @@ var PerpetualsClient = (function () {
5812
5661
  instructions = [];
5813
5662
  postInstructions = [];
5814
5663
  additionalSigners = [];
5815
- _b.label = 1;
5664
+ _a.label = 1;
5816
5665
  case 1:
5817
- _b.trys.push([1, 7, , 8]);
5818
- if (!(reserveSymbol == 'SOL')) return [3, 2];
5819
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
5820
- if (!ephemeralSignerPubkey) {
5821
- wrappedSolAccount = new web3_js_1.Keypair();
5822
- additionalSigners.push(wrappedSolAccount);
5666
+ _a.trys.push([1, 3, , 4]);
5667
+ if (reserveSymbol == 'SOL') {
5668
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
5669
+ if (!ephemeralSignerPubkey) {
5670
+ wrappedSolAccount = new web3_js_1.Keypair();
5671
+ additionalSigners.push(wrappedSolAccount);
5672
+ }
5673
+ preInstructions = [
5674
+ web3_js_1.SystemProgram.createAccount({
5675
+ fromPubkey: publicKey,
5676
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5677
+ lamports: lamports,
5678
+ space: 165,
5679
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
5680
+ }),
5681
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5682
+ ];
5683
+ postInstructions = [
5684
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5685
+ ];
5823
5686
  }
5824
- preInstructions = [
5825
- web3_js_1.SystemProgram.createAccount({
5826
- fromPubkey: publicKey,
5827
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5828
- lamports: lamports,
5829
- space: 165,
5830
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5831
- }),
5832
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5833
- ];
5834
- postInstructions = [
5835
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5836
- ];
5837
- return [3, 5];
5838
- case 2:
5839
- 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);
5840
- _a = createUserATA;
5841
- if (!_a) return [3, 4];
5842
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
5843
- case 3:
5844
- _a = !(_b.sent());
5845
- _b.label = 4;
5846
- case 4:
5847
- if (_a) {
5848
- 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));
5687
+ else {
5688
+ 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);
5689
+ 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));
5849
5690
  }
5850
- _b.label = 5;
5851
- case 5:
5852
5691
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
5853
5692
  orderAccount = poolConfig.getOrderFromMarketPk(publicKey, marketAccount);
5854
5693
  return [4, this.program.methods
@@ -5882,15 +5721,15 @@ var PerpetualsClient = (function () {
5882
5721
  receivingMint: poolConfig.getTokenFromSymbol(reserveSymbol).mintKey
5883
5722
  })
5884
5723
  .instruction()];
5885
- case 6:
5886
- editLimitOrder = _b.sent();
5724
+ case 2:
5725
+ editLimitOrder = _a.sent();
5887
5726
  instructions.push(editLimitOrder);
5888
- return [3, 8];
5889
- case 7:
5890
- err_31 = _b.sent();
5727
+ return [3, 4];
5728
+ case 3:
5729
+ err_31 = _a.sent();
5891
5730
  console.log("perpClient editLimitOrder error:: ", err_31);
5892
5731
  throw err_31;
5893
- case 8: return [2, {
5732
+ case 4: return [2, {
5894
5733
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5895
5734
  additionalSigners: additionalSigners
5896
5735
  }];
@@ -6039,12 +5878,12 @@ var PerpetualsClient = (function () {
6039
5878
  });
6040
5879
  });
6041
5880
  };
6042
- this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5881
+ this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6043
5882
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_34;
6044
5883
  return __generator(this, function (_a) {
6045
5884
  switch (_a.label) {
6046
5885
  case 0:
6047
- publicKey = this.provider.wallet.publicKey;
5886
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6048
5887
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6049
5888
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6050
5889
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6098,12 +5937,12 @@ var PerpetualsClient = (function () {
6098
5937
  }
6099
5938
  });
6100
5939
  }); };
6101
- this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5940
+ this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6102
5941
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_35;
6103
5942
  return __generator(this, function (_a) {
6104
5943
  switch (_a.label) {
6105
5944
  case 0:
6106
- publicKey = this.provider.wallet.publicKey;
5945
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6107
5946
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6108
5947
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6109
5948
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6156,12 +5995,12 @@ var PerpetualsClient = (function () {
6156
5995
  }
6157
5996
  });
6158
5997
  }); };
6159
- this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5998
+ this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6160
5999
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_36;
6161
6000
  return __generator(this, function (_a) {
6162
6001
  switch (_a.label) {
6163
6002
  case 0:
6164
- publicKey = this.provider.wallet.publicKey;
6003
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6165
6004
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6166
6005
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6167
6006
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6200,12 +6039,12 @@ var PerpetualsClient = (function () {
6200
6039
  }
6201
6040
  });
6202
6041
  }); };
6203
- this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6042
+ this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6204
6043
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_37;
6205
6044
  return __generator(this, function (_a) {
6206
6045
  switch (_a.label) {
6207
6046
  case 0:
6208
- publicKey = this.provider.wallet.publicKey;
6047
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6209
6048
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6210
6049
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6211
6050
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6248,13 +6087,13 @@ var PerpetualsClient = (function () {
6248
6087
  args_1[_i - 9] = arguments[_i];
6249
6088
  }
6250
6089
  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) {
6251
- 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;
6090
+ 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;
6252
6091
  if (createUserATA === void 0) { createUserATA = true; }
6253
6092
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6254
6093
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6255
6094
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6256
- return __generator(this, function (_e) {
6257
- switch (_e.label) {
6095
+ return __generator(this, function (_c) {
6096
+ switch (_c.label) {
6258
6097
  case 0:
6259
6098
  payerPubkey = this.provider.wallet.publicKey;
6260
6099
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6267,42 +6106,23 @@ var PerpetualsClient = (function () {
6267
6106
  additionalSigners = [];
6268
6107
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
6269
6108
  receivingToken = poolConfig.getTokenFromSymbol(receivingSymbol);
6270
- _e.label = 1;
6109
+ _c.label = 1;
6271
6110
  case 1:
6272
- _e.trys.push([1, 9, , 10]);
6273
- if (!false) return [3, 2];
6274
- return [3, 7];
6275
- case 2:
6276
- 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);
6277
- _a = createUserATA;
6278
- if (!_a) return [3, 4];
6279
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6280
- case 3:
6281
- _a = !(_e.sent());
6282
- _e.label = 4;
6283
- case 4:
6284
- if (_a) {
6285
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey));
6111
+ _c.trys.push([1, 3, , 4]);
6112
+ if (false) {
6286
6113
  }
6287
- 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);
6288
- _b = createUserATA;
6289
- if (!_b) return [3, 6];
6290
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccountCollateral, this.provider.connection)];
6291
- case 5:
6292
- _b = !(_e.sent());
6293
- _e.label = 6;
6294
- case 6:
6295
- if (_b) {
6296
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
6114
+ else {
6115
+ 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);
6116
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, spl_token_1.TOKEN_PROGRAM_ID));
6117
+ 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);
6118
+ 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));
6297
6119
  }
6298
- _e.label = 7;
6299
- case 7:
6300
6120
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6301
6121
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6302
6122
  custodyAccountMetas = [];
6303
6123
  custodyOracleAccountMetas = [];
6304
- for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
6305
- custody = _d[_c];
6124
+ for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
6125
+ custody = _b[_a];
6306
6126
  custodyAccountMetas.push({
6307
6127
  pubkey: custody.custodyAccount,
6308
6128
  isSigner: false,
@@ -6349,15 +6169,15 @@ var PerpetualsClient = (function () {
6349
6169
  })
6350
6170
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6351
6171
  .instruction()];
6352
- case 8:
6353
- executeTriggerWithSwap = _e.sent();
6172
+ case 2:
6173
+ executeTriggerWithSwap = _c.sent();
6354
6174
  instructions.push(executeTriggerWithSwap);
6355
- return [3, 10];
6356
- case 9:
6357
- err_38 = _e.sent();
6175
+ return [3, 4];
6176
+ case 3:
6177
+ err_38 = _c.sent();
6358
6178
  console.log("perpClient executeTriggerWithSwap error:: ", err_38);
6359
6179
  throw err_38;
6360
- case 10: return [2, {
6180
+ case 4: return [2, {
6361
6181
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6362
6182
  additionalSigners: additionalSigners
6363
6183
  }];
@@ -6371,13 +6191,13 @@ var PerpetualsClient = (function () {
6371
6191
  args_1[_i - 8] = arguments[_i];
6372
6192
  }
6373
6193
  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) {
6374
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_39;
6194
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeTriggerOrder, err_39;
6375
6195
  if (createUserATA === void 0) { createUserATA = true; }
6376
6196
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6377
6197
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6378
6198
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6379
- return __generator(this, function (_b) {
6380
- switch (_b.label) {
6199
+ return __generator(this, function (_a) {
6200
+ switch (_a.label) {
6381
6201
  case 0:
6382
6202
  payerPubkey = this.provider.wallet.publicKey;
6383
6203
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6387,25 +6207,15 @@ var PerpetualsClient = (function () {
6387
6207
  instructions = [];
6388
6208
  postInstructions = [];
6389
6209
  additionalSigners = [];
6390
- _b.label = 1;
6210
+ _a.label = 1;
6391
6211
  case 1:
6392
- _b.trys.push([1, 7, , 8]);
6393
- if (!false) return [3, 2];
6394
- return [3, 5];
6395
- case 2:
6396
- 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);
6397
- _a = createUserATA;
6398
- if (!_a) return [3, 4];
6399
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6400
- case 3:
6401
- _a = !(_b.sent());
6402
- _b.label = 4;
6403
- case 4:
6404
- if (_a) {
6405
- 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));
6212
+ _a.trys.push([1, 3, , 4]);
6213
+ if (false) {
6214
+ }
6215
+ else {
6216
+ 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);
6217
+ 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));
6406
6218
  }
6407
- _b.label = 5;
6408
- case 5:
6409
6219
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6410
6220
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6411
6221
  return [4, this.program.methods
@@ -6437,15 +6247,15 @@ var PerpetualsClient = (function () {
6437
6247
  })
6438
6248
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6439
6249
  .instruction()];
6440
- case 6:
6441
- executeTriggerOrder = _b.sent();
6250
+ case 2:
6251
+ executeTriggerOrder = _a.sent();
6442
6252
  instructions.push(executeTriggerOrder);
6443
- return [3, 8];
6444
- case 7:
6445
- err_39 = _b.sent();
6253
+ return [3, 4];
6254
+ case 3:
6255
+ err_39 = _a.sent();
6446
6256
  console.log("perpClient executeTriggerOrder error:: ", err_39);
6447
6257
  throw err_39;
6448
- case 8: return [2, {
6258
+ case 4: return [2, {
6449
6259
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6450
6260
  additionalSigners: additionalSigners
6451
6261
  }];
@@ -6458,16 +6268,16 @@ var PerpetualsClient = (function () {
6458
6268
  for (var _i = 5; _i < arguments.length; _i++) {
6459
6269
  args_1[_i - 5] = arguments[_i];
6460
6270
  }
6461
- 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) {
6462
- 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;
6271
+ 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) {
6272
+ 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;
6463
6273
  if (useFeesPool === void 0) { useFeesPool = false; }
6464
6274
  if (createUserATA === void 0) { createUserATA = true; }
6465
6275
  if (unWrapSol === void 0) { unWrapSol = false; }
6466
6276
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
6467
6277
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6468
6278
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
6469
- return __generator(this, function (_g) {
6470
- switch (_g.label) {
6279
+ return __generator(this, function (_f) {
6280
+ switch (_f.label) {
6471
6281
  case 0:
6472
6282
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
6473
6283
  if (!userInputCustodyConfig) {
@@ -6477,31 +6287,26 @@ var PerpetualsClient = (function () {
6477
6287
  if (!userOutputCustodyConfig) {
6478
6288
  throw "userOutputCustodyConfig not found";
6479
6289
  }
6480
- publicKey = this.provider.wallet.publicKey;
6290
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6481
6291
  preInstructions = [];
6482
6292
  instructions = [];
6483
6293
  postInstructions = [];
6484
6294
  additionalSigners = [];
6485
- if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 5];
6295
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
6486
6296
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
6487
6297
  case 1:
6488
- wsolAssociatedTokenAccount = _g.sent();
6489
- return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
6490
- case 2:
6491
- wsolATAExist = _g.sent();
6492
- if (!wsolATAExist) {
6493
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
6494
- }
6495
- if (!!skipBalanceChecks) return [3, 4];
6298
+ wsolAssociatedTokenAccount = _f.sent();
6299
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID));
6300
+ if (!!skipBalanceChecks) return [3, 3];
6496
6301
  _a = anchor_1.BN.bind;
6497
6302
  return [4, this.provider.connection.getBalance(publicKey)];
6498
- case 3:
6499
- unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _g.sent()]))();
6303
+ case 2:
6304
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
6500
6305
  if (unWrappedSolBalance.lt(amountIn)) {
6501
6306
  throw "Insufficient SOL Funds";
6502
6307
  }
6503
- _g.label = 4;
6504
- case 4:
6308
+ _f.label = 3;
6309
+ case 3:
6505
6310
  instructions.push(web3_js_1.SystemProgram.transfer({
6506
6311
  fromPubkey: publicKey,
6507
6312
  toPubkey: wsolAssociatedTokenAccount,
@@ -6511,7 +6316,7 @@ var PerpetualsClient = (function () {
6511
6316
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6512
6317
  additionalSigners: additionalSigners
6513
6318
  }];
6514
- case 5:
6319
+ case 4:
6515
6320
  if (userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL') {
6516
6321
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
6517
6322
  wsolAssociatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, publicKey, true);
@@ -6522,20 +6327,20 @@ var PerpetualsClient = (function () {
6522
6327
  additionalSigners: additionalSigners
6523
6328
  }];
6524
6329
  }
6525
- _g.label = 6;
6526
- case 6:
6527
- _g.trys.push([6, 19, , 20]);
6528
- if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
6330
+ _f.label = 5;
6331
+ case 5:
6332
+ _f.trys.push([5, 16, , 17]);
6333
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 8];
6529
6334
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
6530
6335
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
6531
- case 7:
6532
- accCreationLamports = (_g.sent());
6336
+ case 6:
6337
+ accCreationLamports = (_f.sent());
6533
6338
  console.log("accCreationLamports:", accCreationLamports);
6534
6339
  lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
6535
6340
  _b = anchor_1.BN.bind;
6536
6341
  return [4, this.provider.connection.getBalance(publicKey)];
6537
- case 8:
6538
- unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _g.sent()]))();
6342
+ case 7:
6343
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
6539
6344
  if (unWrappedSolBalance.lt(amountIn)) {
6540
6345
  throw "Insufficient SOL Funds";
6541
6346
  }
@@ -6557,25 +6362,25 @@ var PerpetualsClient = (function () {
6557
6362
  postInstructions = [
6558
6363
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6559
6364
  ];
6560
- return [3, 12];
6561
- case 9:
6365
+ return [3, 11];
6366
+ case 8:
6562
6367
  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);
6563
6368
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
6564
- case 10:
6565
- if (!(_g.sent())) {
6369
+ case 9:
6370
+ if (!(_f.sent())) {
6566
6371
  throw "Insufficient Funds , Token Account doesn't exist";
6567
6372
  }
6568
- if (!!skipBalanceChecks) return [3, 12];
6373
+ if (!!skipBalanceChecks) return [3, 11];
6569
6374
  _c = anchor_1.BN.bind;
6570
6375
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
6571
- case 11:
6572
- tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
6376
+ case 10:
6377
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
6573
6378
  if (tokenAccountBalance.lt(amountIn)) {
6574
6379
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
6575
6380
  }
6576
- _g.label = 12;
6577
- case 12:
6578
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 13];
6381
+ _f.label = 11;
6382
+ case 11:
6383
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 12];
6579
6384
  lamports = (this.minimumBalanceForRentExemptAccountLamports);
6580
6385
  if (!ephemeralSignerPubkey) {
6581
6386
  wrappedSolAccount = new web3_js_1.Keypair();
@@ -6595,26 +6400,17 @@ var PerpetualsClient = (function () {
6595
6400
  postInstructions = [
6596
6401
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6597
6402
  ];
6598
- return [3, 17];
6599
- 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)];
6403
+ return [3, 14];
6404
+ 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)];
6405
+ case 13:
6406
+ userOutputTokenAccount = _f.sent();
6407
+ 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));
6408
+ _f.label = 14;
6600
6409
  case 14:
6601
- userOutputTokenAccount = _g.sent();
6602
- _d = createUserATA;
6603
- if (!_d) return [3, 16];
6604
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
6605
- case 15:
6606
- _d = !(_g.sent());
6607
- _g.label = 16;
6608
- case 16:
6609
- if (_d) {
6610
- 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));
6611
- }
6612
- _g.label = 17;
6613
- case 17:
6614
6410
  custodyAccountMetas = [];
6615
6411
  custodyOracleAccountMetas = [];
6616
- for (_e = 0, _f = poolConfig.custodies; _e < _f.length; _e++) {
6617
- custody = _f[_e];
6412
+ for (_d = 0, _e = poolConfig.custodies; _d < _e.length; _d++) {
6413
+ custody = _e[_d];
6618
6414
  custodyAccountMetas.push({
6619
6415
  pubkey: custody.custodyAccount,
6620
6416
  isSigner: false,
@@ -6662,19 +6458,19 @@ var PerpetualsClient = (function () {
6662
6458
  })
6663
6459
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
6664
6460
  .instruction()];
6665
- case 18:
6666
- inx = _g.sent();
6461
+ case 15:
6462
+ inx = _f.sent();
6667
6463
  instructions.push(inx);
6668
6464
  if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
6669
6465
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
6670
6466
  instructions.push(closeWsolATAIns);
6671
6467
  }
6672
- return [3, 20];
6673
- case 19:
6674
- err_40 = _g.sent();
6468
+ return [3, 17];
6469
+ case 16:
6470
+ err_40 = _f.sent();
6675
6471
  console.error("perpClient Swap error:: ", err_40);
6676
6472
  throw err_40;
6677
- case 20: return [2, {
6473
+ case 17: return [2, {
6678
6474
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6679
6475
  additionalSigners: additionalSigners
6680
6476
  }];
@@ -7716,15 +7512,11 @@ var PerpetualsClient = (function () {
7716
7512
  additionalSigners = [];
7717
7513
  _a.label = 1;
7718
7514
  case 1:
7719
- _a.trys.push([1, 5, , 6]);
7515
+ _a.trys.push([1, 4, , 5]);
7720
7516
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7721
7517
  case 2:
7722
7518
  receivingTokenAccount = _a.sent();
7723
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7724
- case 3:
7725
- if (!(_a.sent())) {
7726
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7727
- }
7519
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7728
7520
  return [4, this.program.methods
7729
7521
  .withdrawInstantFees({})
7730
7522
  .accounts({
@@ -7739,15 +7531,15 @@ var PerpetualsClient = (function () {
7739
7531
  receivingTokenMint: poolConfig.tokenMint,
7740
7532
  })
7741
7533
  .instruction()];
7742
- case 4:
7534
+ case 3:
7743
7535
  withdrawInstantFeeInstruction = _a.sent();
7744
7536
  instructions.push(withdrawInstantFeeInstruction);
7745
- return [3, 6];
7746
- case 5:
7537
+ return [3, 5];
7538
+ case 4:
7747
7539
  err_57 = _a.sent();
7748
7540
  console.log("perpClient withdrawInstantFeeInstruction error:: ", err_57);
7749
7541
  throw err_57;
7750
- case 6: return [2, {
7542
+ case 5: return [2, {
7751
7543
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7752
7544
  additionalSigners: additionalSigners
7753
7545
  }];
@@ -7766,15 +7558,11 @@ var PerpetualsClient = (function () {
7766
7558
  additionalSigners = [];
7767
7559
  _a.label = 1;
7768
7560
  case 1:
7769
- _a.trys.push([1, 5, , 6]);
7561
+ _a.trys.push([1, 4, , 5]);
7770
7562
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7771
7563
  case 2:
7772
7564
  receivingTokenAccount = _a.sent();
7773
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7774
- case 3:
7775
- if (!(_a.sent())) {
7776
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7777
- }
7565
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7778
7566
  return [4, this.program.methods
7779
7567
  .withdrawUnclaimedTokens({})
7780
7568
  .accounts({
@@ -7789,15 +7577,15 @@ var PerpetualsClient = (function () {
7789
7577
  receivingTokenMint: poolConfig.tokenMint,
7790
7578
  })
7791
7579
  .instruction()];
7792
- case 4:
7580
+ case 3:
7793
7581
  withdrawUnclaimedTokensInstruction = _a.sent();
7794
7582
  instructions.push(withdrawUnclaimedTokensInstruction);
7795
- return [3, 6];
7796
- case 5:
7583
+ return [3, 5];
7584
+ case 4:
7797
7585
  err_58 = _a.sent();
7798
7586
  console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_58);
7799
7587
  throw err_58;
7800
- case 6: return [2, {
7588
+ case 5: return [2, {
7801
7589
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7802
7590
  additionalSigners: additionalSigners
7803
7591
  }];