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