flash-sdk 11.5.1 → 11.6.0-alpha.0

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