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