flash-sdk 11.8.0-alpha.0 → 11.8.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.
@@ -2704,7 +2704,7 @@ var PerpetualsClient = (function () {
2704
2704
  for (var _i = 8; _i < arguments.length; _i++) {
2705
2705
  args_1[_i - 8] = arguments[_i];
2706
2706
  }
2707
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, priceWithSlippage_1, collateralWithfee_1, size_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, priceWithSlippage, collateralWithfee, size, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
2707
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, priceWithSlippage_1, collateralWithfee_1, size_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, priceWithSlippage, collateralWithfee, size, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey) {
2708
2708
  var publicKey, targetCustodyConfig, collateralCustodyConfig, collateralToken, marketAccount, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
2709
2709
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2710
2710
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2713,7 +2713,7 @@ var PerpetualsClient = (function () {
2713
2713
  return __generator(this, function (_c) {
2714
2714
  switch (_c.label) {
2715
2715
  case 0:
2716
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2716
+ publicKey = this.provider.wallet.publicKey;
2717
2717
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
2718
2718
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2719
2719
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
@@ -2818,48 +2818,59 @@ var PerpetualsClient = (function () {
2818
2818
  for (var _i = 6; _i < arguments.length; _i++) {
2819
2819
  args_1[_i - 6] = arguments[_i];
2820
2820
  }
2821
- return __awaiter(_this, __spreadArray([marketSymbol_1, collateralSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (marketSymbol, collateralSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey, userPublicKey) {
2822
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2821
+ return __awaiter(_this, __spreadArray([marketSymbol_1, collateralSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (marketSymbol, collateralSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey) {
2822
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2823
2823
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2824
2824
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
2825
2825
  if (createUserATA === void 0) { createUserATA = true; }
2826
2826
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
2827
2827
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2828
- return __generator(this, function (_a) {
2829
- switch (_a.label) {
2828
+ return __generator(this, function (_b) {
2829
+ switch (_b.label) {
2830
2830
  case 0:
2831
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2831
+ console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
2832
+ publicKey = this.provider.wallet.publicKey;
2832
2833
  preInstructions = [];
2833
2834
  instructions = [];
2834
2835
  postInstructions = [];
2835
2836
  additionalSigners = [];
2836
- _a.label = 1;
2837
+ _b.label = 1;
2837
2838
  case 1:
2838
- _a.trys.push([1, 3, , 4]);
2839
- if (collateralSymbol == 'SOL') {
2840
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
2841
- if (!ephemeralSignerPubkey) {
2842
- wrappedSolAccount = new web3_js_1.Keypair();
2843
- additionalSigners.push(wrappedSolAccount);
2844
- }
2845
- preInstructions = [
2846
- web3_js_1.SystemProgram.createAccount({
2847
- fromPubkey: publicKey,
2848
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2849
- lamports: lamports,
2850
- space: 165,
2851
- programId: spl_token_1.TOKEN_PROGRAM_ID,
2852
- }),
2853
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2854
- ];
2855
- postInstructions = [
2856
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2857
- ];
2839
+ _b.trys.push([1, 7, , 8]);
2840
+ if (!(collateralSymbol == 'SOL')) return [3, 2];
2841
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
2842
+ if (!ephemeralSignerPubkey) {
2843
+ wrappedSolAccount = new web3_js_1.Keypair();
2844
+ additionalSigners.push(wrappedSolAccount);
2858
2845
  }
2859
- else {
2860
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
2861
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
2846
+ preInstructions = [
2847
+ web3_js_1.SystemProgram.createAccount({
2848
+ fromPubkey: publicKey,
2849
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2850
+ lamports: lamports,
2851
+ space: 165,
2852
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
2853
+ }),
2854
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2855
+ ];
2856
+ postInstructions = [
2857
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2858
+ ];
2859
+ return [3, 5];
2860
+ case 2:
2861
+ 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);
2862
+ _a = createUserATA;
2863
+ if (!_a) return [3, 4];
2864
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
2865
+ case 3:
2866
+ _a = !(_b.sent());
2867
+ _b.label = 4;
2868
+ case 4:
2869
+ if (_a) {
2870
+ 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));
2862
2871
  }
2872
+ _b.label = 5;
2873
+ case 5:
2863
2874
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2864
2875
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
2865
2876
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -2891,19 +2902,19 @@ var PerpetualsClient = (function () {
2891
2902
  })
2892
2903
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
2893
2904
  .instruction()];
2894
- case 2:
2895
- instruction = _a.sent();
2905
+ case 6:
2906
+ instruction = _b.sent();
2896
2907
  instructions.push(instruction);
2897
2908
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
2898
2909
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
2899
2910
  postInstructions.push(closeWsolATAIns);
2900
2911
  }
2901
- return [3, 4];
2902
- case 3:
2903
- error_1 = _a.sent();
2912
+ return [3, 8];
2913
+ case 7:
2914
+ error_1 = _b.sent();
2904
2915
  console.error("perpclient closePosition error:", error_1);
2905
2916
  throw error_1;
2906
- case 4: return [2, {
2917
+ case 8: return [2, {
2907
2918
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
2908
2919
  additionalSigners: additionalSigners
2909
2920
  }];
@@ -2916,7 +2927,7 @@ var PerpetualsClient = (function () {
2916
2927
  for (var _i = 9; _i < arguments.length; _i++) {
2917
2928
  args_1[_i - 9] = arguments[_i];
2918
2929
  }
2919
- return __awaiter(_this, __spreadArray([targetTokenSymbol_1, collateralTokenSymbol_1, userInputTokenSymbol_1, amountIn_1, priceWithSlippage_1, sizeAmount_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetTokenSymbol, collateralTokenSymbol, userInputTokenSymbol, amountIn, priceWithSlippage, sizeAmount, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
2930
+ 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) {
2920
2931
  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;
2921
2932
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2922
2933
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2925,7 +2936,7 @@ var PerpetualsClient = (function () {
2925
2936
  return __generator(this, function (_c) {
2926
2937
  switch (_c.label) {
2927
2938
  case 0:
2928
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2939
+ publicKey = this.provider.wallet.publicKey;
2929
2940
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
2930
2941
  if (!userInputCustodyConfig) {
2931
2942
  throw "userInputCustodyConfig not found";
@@ -2949,7 +2960,7 @@ var PerpetualsClient = (function () {
2949
2960
  additionalSigners = [];
2950
2961
  targetToken = poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol);
2951
2962
  userInputToken = poolConfig.getTokenFromSymbol(userInputTokenSymbol);
2952
- if (!(userInputTokenSymbol == 'SOL')) return [3, 3];
2963
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 5];
2953
2964
  console.log("inputSymbol === SOL", userInputTokenSymbol);
2954
2965
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
2955
2966
  if (!!skipBalanceChecks) return [3, 2];
@@ -2979,33 +2990,41 @@ var PerpetualsClient = (function () {
2979
2990
  postInstructions = [
2980
2991
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2981
2992
  ];
2982
- if (!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) {
2983
- userOutputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
2984
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
2985
- }
2986
- return [3, 7];
2993
+ if (!!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) return [3, 4];
2994
+ 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);
2995
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
2987
2996
  case 3:
2997
+ if (!(_c.sent())) {
2998
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey));
2999
+ }
3000
+ _c.label = 4;
3001
+ case 4: return [3, 10];
3002
+ case 5:
2988
3003
  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);
2989
- if (!!skipBalanceChecks) return [3, 6];
3004
+ if (!!skipBalanceChecks) return [3, 8];
2990
3005
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
2991
- case 4:
3006
+ case 6:
2992
3007
  if (!(_c.sent())) {
2993
3008
  throw "Insufficient Funds , Token Account doesn't exist";
2994
3009
  }
2995
3010
  _b = anchor_1.BN.bind;
2996
3011
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
2997
- case 5:
3012
+ case 7:
2998
3013
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
2999
3014
  if (tokenAccountBalance.lt(amountIn)) {
3000
3015
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3001
3016
  }
3002
- _c.label = 6;
3003
- case 6:
3017
+ _c.label = 8;
3018
+ case 8:
3004
3019
  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);
3005
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3006
- _c.label = 7;
3007
- case 7:
3008
- _c.trys.push([7, 9, , 10]);
3020
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3021
+ case 9:
3022
+ if (!(_c.sent())) {
3023
+ 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));
3024
+ }
3025
+ _c.label = 10;
3026
+ case 10:
3027
+ _c.trys.push([10, 12, , 13]);
3009
3028
  return [4, this.program.methods
3010
3029
  .swapAndOpen({
3011
3030
  amountIn: amountIn,
@@ -3041,15 +3060,15 @@ var PerpetualsClient = (function () {
3041
3060
  })
3042
3061
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3043
3062
  .instruction()];
3044
- case 8:
3063
+ case 11:
3045
3064
  inx = _c.sent();
3046
3065
  instructions.push(inx);
3047
- return [3, 10];
3048
- case 9:
3066
+ return [3, 13];
3067
+ case 12:
3049
3068
  err_3 = _c.sent();
3050
3069
  console.error("perpClient SwapAndOpen error:: ", err_3);
3051
3070
  throw err_3;
3052
- case 10: return [2, {
3071
+ case 13: return [2, {
3053
3072
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3054
3073
  additionalSigners: additionalSigners
3055
3074
  }];
@@ -3062,7 +3081,7 @@ var PerpetualsClient = (function () {
3062
3081
  for (var _i = 7; _i < arguments.length; _i++) {
3063
3082
  args_1[_i - 7] = arguments[_i];
3064
3083
  }
3065
- return __awaiter(_this, __spreadArray([targetTokenSymbol_1, userOutputTokenSymbol_1, collateralTokenSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetTokenSymbol, userOutputTokenSymbol, collateralTokenSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, ephemeralSignerPubkey, userPublicKey) {
3084
+ 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) {
3066
3085
  var publicKey, userOutputCustodyConfig, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, collateralToken, userOutputToken, lamports, userCollateralTokenAccount, inx, err_4;
3067
3086
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3068
3087
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -3070,7 +3089,7 @@ var PerpetualsClient = (function () {
3070
3089
  return __generator(this, function (_a) {
3071
3090
  switch (_a.label) {
3072
3091
  case 0:
3073
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3092
+ publicKey = this.provider.wallet.publicKey;
3074
3093
  userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey); });
3075
3094
  if (!userOutputCustodyConfig) {
3076
3095
  throw "userOutputCustodyConfig not found";
@@ -3094,37 +3113,46 @@ var PerpetualsClient = (function () {
3094
3113
  additionalSigners = [];
3095
3114
  collateralToken = poolConfig.getTokenFromSymbol(collateralTokenSymbol);
3096
3115
  userOutputToken = poolConfig.getTokenFromSymbol(userOutputTokenSymbol);
3097
- if (userOutputTokenSymbol == 'SOL') {
3098
- console.log("outputSymbol === SOL", userOutputTokenSymbol);
3099
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
3100
- if (!ephemeralSignerPubkey) {
3101
- wrappedSolAccount = new web3_js_1.Keypair();
3102
- additionalSigners.push(wrappedSolAccount);
3103
- }
3104
- preInstructions = [
3105
- web3_js_1.SystemProgram.createAccount({
3106
- fromPubkey: publicKey,
3107
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3108
- lamports: lamports,
3109
- space: 165,
3110
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3111
- }),
3112
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3113
- ];
3114
- postInstructions = [
3115
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3116
- ];
3116
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 1];
3117
+ console.log("outputSymbol === SOL", userOutputTokenSymbol);
3118
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
3119
+ if (!ephemeralSignerPubkey) {
3120
+ wrappedSolAccount = new web3_js_1.Keypair();
3117
3121
  additionalSigners.push(wrappedSolAccount);
3118
3122
  }
3119
- else {
3120
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(userOutputToken.mintKey, publicKey, true, userOutputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3121
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, userOutputToken.mintKey, userOutputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3123
+ preInstructions = [
3124
+ web3_js_1.SystemProgram.createAccount({
3125
+ fromPubkey: publicKey,
3126
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3127
+ lamports: lamports,
3128
+ space: 165,
3129
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3130
+ }),
3131
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3132
+ ];
3133
+ postInstructions = [
3134
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3135
+ ];
3136
+ additionalSigners.push(wrappedSolAccount);
3137
+ return [3, 3];
3138
+ case 1:
3139
+ 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);
3140
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3141
+ case 2:
3142
+ if (!(_a.sent())) {
3143
+ 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));
3122
3144
  }
3145
+ _a.label = 3;
3146
+ case 3:
3123
3147
  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);
3124
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, collateralToken.mintKey, collateralToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3125
- _a.label = 1;
3126
- case 1:
3127
- _a.trys.push([1, 3, , 4]);
3148
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3149
+ case 4:
3150
+ if (!(_a.sent())) {
3151
+ 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));
3152
+ }
3153
+ _a.label = 5;
3154
+ case 5:
3155
+ _a.trys.push([5, 7, , 8]);
3128
3156
  return [4, this.program.methods
3129
3157
  .closeAndSwap({
3130
3158
  priceWithSlippage: priceWithSlippage,
@@ -3158,15 +3186,15 @@ var PerpetualsClient = (function () {
3158
3186
  })
3159
3187
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3160
3188
  .instruction()];
3161
- case 2:
3189
+ case 6:
3162
3190
  inx = _a.sent();
3163
3191
  instructions.push(inx);
3164
- return [3, 4];
3165
- case 3:
3192
+ return [3, 8];
3193
+ case 7:
3166
3194
  err_4 = _a.sent();
3167
3195
  console.error("perpClient CloseAndSwap error:: ", err_4);
3168
3196
  throw err_4;
3169
- case 4: return [2, {
3197
+ case 8: return [2, {
3170
3198
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3171
3199
  additionalSigners: additionalSigners
3172
3200
  }];
@@ -3179,14 +3207,14 @@ var PerpetualsClient = (function () {
3179
3207
  for (var _i = 6; _i < arguments.length; _i++) {
3180
3208
  args_1[_i - 6] = arguments[_i];
3181
3209
  }
3182
- return __awaiter(_this, __spreadArray([collateralWithFee_1, targetSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (collateralWithFee, targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
3210
+ 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) {
3183
3211
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, instruction;
3184
3212
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3185
3213
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3186
3214
  return __generator(this, function (_c) {
3187
3215
  switch (_c.label) {
3188
3216
  case 0:
3189
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3217
+ publicKey = this.provider.wallet.publicKey;
3190
3218
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3191
3219
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3192
3220
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -3282,14 +3310,14 @@ var PerpetualsClient = (function () {
3282
3310
  for (var _i = 7; _i < arguments.length; _i++) {
3283
3311
  args_1[_i - 7] = arguments[_i];
3284
3312
  }
3285
- return __awaiter(_this, __spreadArray([targetSymbol_1, inputSymbol_1, collateralSymbol_1, amountIn_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, inputSymbol, collateralSymbol, amountIn, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
3313
+ 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) {
3286
3314
  var publicKey, collateralCustodyConfig, targetCustodyConfig, inputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userCollateralTokenAccount, marketAccount, instruction;
3287
3315
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3288
3316
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3289
3317
  return __generator(this, function (_c) {
3290
3318
  switch (_c.label) {
3291
3319
  case 0:
3292
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3320
+ publicKey = this.provider.wallet.publicKey;
3293
3321
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3294
3322
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3295
3323
  inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inputSymbol).mintKey); });
@@ -3353,7 +3381,11 @@ var PerpetualsClient = (function () {
3353
3381
  _c.label = 6;
3354
3382
  case 6:
3355
3383
  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);
3356
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, collateralCustodyConfig.mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3384
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3385
+ case 7:
3386
+ if (!(_c.sent())) {
3387
+ 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));
3388
+ }
3357
3389
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3358
3390
  return [4, this.program.methods.swapAndAddCollateral({
3359
3391
  amountIn: amountIn,
@@ -3381,7 +3413,7 @@ var PerpetualsClient = (function () {
3381
3413
  fundingMint: poolConfig.getTokenFromSymbol(inputSymbol).mintKey,
3382
3414
  })
3383
3415
  .instruction()];
3384
- case 7:
3416
+ case 8:
3385
3417
  instruction = _c.sent();
3386
3418
  instructions.push(instruction);
3387
3419
  return [2, {
@@ -3397,15 +3429,15 @@ var PerpetualsClient = (function () {
3397
3429
  for (var _i = 6; _i < arguments.length; _i++) {
3398
3430
  args_1[_i - 6] = arguments[_i];
3399
3431
  }
3400
- return __awaiter(_this, __spreadArray([collateralDeltaUsd_1, marketSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (collateralDeltaUsd, marketSymbol, collateralSymbol, side, positionPubKey, poolConfig, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey, userPublicKey) {
3401
- var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, marketAccount, instruction, closeWsolATAIns, error_2;
3432
+ 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) {
3433
+ var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, marketAccount, instruction, closeWsolATAIns, error_2;
3402
3434
  if (createUserATA === void 0) { createUserATA = true; }
3403
3435
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3404
3436
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3405
- return __generator(this, function (_a) {
3406
- switch (_a.label) {
3437
+ return __generator(this, function (_b) {
3438
+ switch (_b.label) {
3407
3439
  case 0:
3408
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3440
+ publicKey = this.provider.wallet.publicKey;
3409
3441
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3410
3442
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3411
3443
  });
@@ -3419,35 +3451,45 @@ var PerpetualsClient = (function () {
3419
3451
  instructions = [];
3420
3452
  postInstructions = [];
3421
3453
  additionalSigners = [];
3422
- _a.label = 1;
3454
+ _b.label = 1;
3423
3455
  case 1:
3424
- _a.trys.push([1, 3, , 4]);
3456
+ _b.trys.push([1, 7, , 8]);
3425
3457
  console.log("removeCollateral -- collateralSymbol:", collateralSymbol);
3426
- if (collateralSymbol == 'SOL') {
3427
- console.log("remove collateral in SOL ...create WSOL temp and close it ");
3428
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3429
- if (!ephemeralSignerPubkey) {
3430
- wrappedSolAccount = new web3_js_1.Keypair();
3431
- additionalSigners.push(wrappedSolAccount);
3432
- }
3433
- preInstructions = [
3434
- web3_js_1.SystemProgram.createAccount({
3435
- fromPubkey: publicKey,
3436
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3437
- lamports: lamports,
3438
- space: 165,
3439
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3440
- }),
3441
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3442
- ];
3443
- postInstructions = [
3444
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3445
- ];
3458
+ if (!(collateralSymbol == 'SOL')) return [3, 2];
3459
+ console.log("remove collateral in SOL ...create WSOL temp and close it ");
3460
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3461
+ if (!ephemeralSignerPubkey) {
3462
+ wrappedSolAccount = new web3_js_1.Keypair();
3463
+ additionalSigners.push(wrappedSolAccount);
3446
3464
  }
3447
- else {
3448
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3449
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3465
+ preInstructions = [
3466
+ web3_js_1.SystemProgram.createAccount({
3467
+ fromPubkey: publicKey,
3468
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3469
+ lamports: lamports,
3470
+ space: 165,
3471
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3472
+ }),
3473
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3474
+ ];
3475
+ postInstructions = [
3476
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3477
+ ];
3478
+ return [3, 5];
3479
+ case 2:
3480
+ 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);
3481
+ _a = createUserATA;
3482
+ if (!_a) return [3, 4];
3483
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3484
+ case 3:
3485
+ _a = !(_b.sent());
3486
+ _b.label = 4;
3487
+ case 4:
3488
+ if (_a) {
3489
+ 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));
3450
3490
  }
3491
+ _b.label = 5;
3492
+ case 5:
3451
3493
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3452
3494
  return [4, this.program.methods
3453
3495
  .removeCollateral({
@@ -3473,19 +3515,19 @@ var PerpetualsClient = (function () {
3473
3515
  receivingMint: collateralCustodyConfig.mintKey
3474
3516
  })
3475
3517
  .instruction()];
3476
- case 2:
3477
- instruction = _a.sent();
3518
+ case 6:
3519
+ instruction = _b.sent();
3478
3520
  instructions.push(instruction);
3479
3521
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
3480
3522
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
3481
3523
  postInstructions.push(closeWsolATAIns);
3482
3524
  }
3483
- return [3, 4];
3484
- case 3:
3485
- error_2 = _a.sent();
3525
+ return [3, 8];
3526
+ case 7:
3527
+ error_2 = _b.sent();
3486
3528
  console.error("perpclient removeCollateral error:", error_2);
3487
3529
  throw error_2;
3488
- case 4: return [2, {
3530
+ case 8: return [2, {
3489
3531
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3490
3532
  additionalSigners: additionalSigners
3491
3533
  }];
@@ -3498,13 +3540,13 @@ var PerpetualsClient = (function () {
3498
3540
  for (var _i = 6; _i < arguments.length; _i++) {
3499
3541
  args_1[_i - 6] = arguments[_i];
3500
3542
  }
3501
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, outputSymbol_1, collateralDeltaUsd_1, side_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, outputSymbol, collateralDeltaUsd, side, poolConfig, ephemeralSignerPubkey, userPublicKey) {
3543
+ 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) {
3502
3544
  var publicKey, targetCustodyConfig, collateralCustodyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, instruction;
3503
3545
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3504
3546
  return __generator(this, function (_a) {
3505
3547
  switch (_a.label) {
3506
3548
  case 0:
3507
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3549
+ publicKey = this.provider.wallet.publicKey;
3508
3550
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3509
3551
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3510
3552
  outputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outputSymbol).mintKey); });
@@ -3515,33 +3557,42 @@ var PerpetualsClient = (function () {
3515
3557
  instructions = [];
3516
3558
  postInstructions = [];
3517
3559
  additionalSigners = [];
3518
- if (outputSymbol == 'SOL') {
3519
- console.log("outputSymbol === SOL", outputSymbol);
3520
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3521
- if (!ephemeralSignerPubkey) {
3522
- wrappedSolAccount = new web3_js_1.Keypair();
3523
- additionalSigners.push(wrappedSolAccount);
3524
- }
3525
- preInstructions = [
3526
- web3_js_1.SystemProgram.createAccount({
3527
- fromPubkey: publicKey,
3528
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3529
- lamports: lamports,
3530
- space: 165,
3531
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3532
- }),
3533
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3534
- ];
3535
- postInstructions = [
3536
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3537
- ];
3560
+ if (!(outputSymbol == 'SOL')) return [3, 1];
3561
+ console.log("outputSymbol === SOL", outputSymbol);
3562
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3563
+ if (!ephemeralSignerPubkey) {
3564
+ wrappedSolAccount = new web3_js_1.Keypair();
3565
+ additionalSigners.push(wrappedSolAccount);
3538
3566
  }
3539
- else {
3540
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(outputSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(outputSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3541
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(outputSymbol).mintKey, poolConfig.getTokenFromSymbol(outputSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3567
+ preInstructions = [
3568
+ web3_js_1.SystemProgram.createAccount({
3569
+ fromPubkey: publicKey,
3570
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3571
+ lamports: lamports,
3572
+ space: 165,
3573
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3574
+ }),
3575
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3576
+ ];
3577
+ postInstructions = [
3578
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3579
+ ];
3580
+ return [3, 3];
3581
+ case 1:
3582
+ 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);
3583
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3584
+ case 2:
3585
+ if (!(_a.sent())) {
3586
+ 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));
3542
3587
  }
3588
+ _a.label = 3;
3589
+ case 3:
3543
3590
  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);
3544
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3591
+ return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3592
+ case 4:
3593
+ if (!(_a.sent())) {
3594
+ 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));
3595
+ }
3545
3596
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3546
3597
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3547
3598
  return [4, this.program.methods
@@ -3575,7 +3626,7 @@ var PerpetualsClient = (function () {
3575
3626
  collateralTokenProgram: poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID
3576
3627
  })
3577
3628
  .instruction()];
3578
- case 1:
3629
+ case 5:
3579
3630
  instruction = _a.sent();
3580
3631
  instructions.push(instruction);
3581
3632
  return [2, {
@@ -3591,14 +3642,14 @@ var PerpetualsClient = (function () {
3591
3642
  for (var _i = 8; _i < arguments.length; _i++) {
3592
3643
  args_1[_i - 8] = arguments[_i];
3593
3644
  }
3594
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, positionPubKey_1, side_1, poolConfig_1, priceWithSlippage_1, sizeDelta_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, positionPubKey, side, poolConfig, priceWithSlippage, sizeDelta, privilege, tokenStakeAccount, userReferralAccount, userPublicKey) {
3645
+ 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) {
3595
3646
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3596
3647
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3597
3648
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3598
3649
  return __generator(this, function (_a) {
3599
3650
  switch (_a.label) {
3600
3651
  case 0:
3601
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3652
+ publicKey = this.provider.wallet.publicKey;
3602
3653
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3603
3654
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3604
3655
  });
@@ -3655,14 +3706,14 @@ var PerpetualsClient = (function () {
3655
3706
  for (var _i = 8; _i < arguments.length; _i++) {
3656
3707
  args_1[_i - 8] = arguments[_i];
3657
3708
  }
3658
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1, priceWithSlippage_1, sizeDelta_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, priceWithSlippage, sizeDelta, privilege, tokenStakeAccount, userReferralAccount, userPublicKey) {
3709
+ 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) {
3659
3710
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3660
3711
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3661
3712
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3662
3713
  return __generator(this, function (_a) {
3663
3714
  switch (_a.label) {
3664
3715
  case 0:
3665
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3716
+ publicKey = this.provider.wallet.publicKey;
3666
3717
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3667
3718
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3668
3719
  });
@@ -3719,7 +3770,7 @@ var PerpetualsClient = (function () {
3719
3770
  for (var _i = 4; _i < arguments.length; _i++) {
3720
3771
  args_1[_i - 4] = arguments[_i];
3721
3772
  }
3722
- return __awaiter(_this, __spreadArray([payTokenSymbol_1, tokenAmountIn_1, minLpAmountOut_1, poolConfig_1], args_1, true), void 0, function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, isWhitelistedUser, userPublicKey) {
3773
+ 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) {
3723
3774
  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;
3724
3775
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3725
3776
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
@@ -3727,7 +3778,7 @@ var PerpetualsClient = (function () {
3727
3778
  return __generator(this, function (_g) {
3728
3779
  switch (_g.label) {
3729
3780
  case 0:
3730
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3781
+ publicKey = this.provider.wallet.publicKey;
3731
3782
  payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
3732
3783
  if (!payTokenCustodyConfig) {
3733
3784
  throw "payTokenCustodyConfig not found";
@@ -3739,7 +3790,7 @@ var PerpetualsClient = (function () {
3739
3790
  payToken = poolConfig.getTokenFromSymbol(payTokenSymbol);
3740
3791
  _g.label = 1;
3741
3792
  case 1:
3742
- _g.trys.push([1, 9, , 10]);
3793
+ _g.trys.push([1, 10, , 11]);
3743
3794
  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);
3744
3795
  lpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
3745
3796
  custodyAccountMetas = [];
@@ -3766,20 +3817,24 @@ var PerpetualsClient = (function () {
3766
3817
  isWritable: false,
3767
3818
  });
3768
3819
  }
3769
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3770
- if (!(payTokenSymbol == 'SOL')) return [3, 4];
3820
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3821
+ case 2:
3822
+ if (!(_g.sent())) {
3823
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3824
+ }
3825
+ if (!(payTokenSymbol == 'SOL')) return [3, 5];
3771
3826
  console.log("payTokenSymbol === SOL", payTokenSymbol);
3772
3827
  lamports = tokenAmountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3773
- if (!!skipBalanceChecks) return [3, 3];
3828
+ if (!!skipBalanceChecks) return [3, 4];
3774
3829
  _e = anchor_1.BN.bind;
3775
3830
  return [4, this.provider.connection.getBalance(publicKey)];
3776
- case 2:
3831
+ case 3:
3777
3832
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _g.sent()]))();
3778
3833
  if (unWrappedSolBalance.lt(lamports)) {
3779
3834
  throw "Insufficient SOL Funds";
3780
3835
  }
3781
- _g.label = 3;
3782
- case 3:
3836
+ _g.label = 4;
3837
+ case 4:
3783
3838
  if (!ephemeralSignerPubkey) {
3784
3839
  wrappedSolAccount = new web3_js_1.Keypair();
3785
3840
  additionalSigners.push(wrappedSolAccount);
@@ -3797,23 +3852,23 @@ var PerpetualsClient = (function () {
3797
3852
  postInstructions = [
3798
3853
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3799
3854
  ];
3800
- return [3, 7];
3801
- case 4:
3802
- if (!!skipBalanceChecks) return [3, 7];
3803
- return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3855
+ return [3, 8];
3804
3856
  case 5:
3857
+ if (!!skipBalanceChecks) return [3, 8];
3858
+ return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3859
+ case 6:
3805
3860
  if (!(_g.sent())) {
3806
3861
  throw "Insufficient Funds , token Account doesn't exist";
3807
3862
  }
3808
3863
  _f = anchor_1.BN.bind;
3809
3864
  return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
3810
- case 6:
3865
+ case 7:
3811
3866
  tokenAccountBalance = new (_f.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3812
3867
  if (tokenAccountBalance.lt(tokenAmountIn)) {
3813
3868
  throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
3814
3869
  }
3815
- _g.label = 7;
3816
- case 7:
3870
+ _g.label = 8;
3871
+ case 8:
3817
3872
  whitelistPda = this.findProgramAddress("whitelist", [publicKey]).publicKey;
3818
3873
  whitelistMeta = {
3819
3874
  pubkey: whitelistPda,
@@ -3845,15 +3900,15 @@ var PerpetualsClient = (function () {
3845
3900
  })
3846
3901
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3847
3902
  .instruction()];
3848
- case 8:
3903
+ case 9:
3849
3904
  instruction = _g.sent();
3850
3905
  instructions.push(instruction);
3851
- return [3, 10];
3852
- case 9:
3906
+ return [3, 11];
3907
+ case 10:
3853
3908
  err_5 = _g.sent();
3854
3909
  console.error("perpClient addLiquidity error:: ", err_5);
3855
3910
  throw err_5;
3856
- case 10: return [2, {
3911
+ case 11: return [2, {
3857
3912
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3858
3913
  additionalSigners: additionalSigners
3859
3914
  }];
@@ -3886,7 +3941,7 @@ var PerpetualsClient = (function () {
3886
3941
  inputToken = poolConfig.getTokenFromSymbol(inputSymbol);
3887
3942
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
3888
3943
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
3889
- if (!(inputSymbol == 'SOL')) return [3, 3];
3944
+ if (!(inputSymbol == 'SOL')) return [3, 4];
3890
3945
  console.log("inputSymbol === SOL", inputSymbol);
3891
3946
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3892
3947
  console.log("lamports:", lamports.toNumber());
@@ -3899,8 +3954,11 @@ var PerpetualsClient = (function () {
3899
3954
  throw "Insufficient SOL Funds";
3900
3955
  }
3901
3956
  _g.label = 2;
3902
- case 2:
3903
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3957
+ case 2: return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3958
+ case 3:
3959
+ if (!(_g.sent())) {
3960
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3961
+ }
3904
3962
  if (!ephemeralSignerPubkey) {
3905
3963
  wrappedSolAccount = new web3_js_1.Keypair();
3906
3964
  additionalSigners.push(wrappedSolAccount);
@@ -3918,24 +3976,24 @@ var PerpetualsClient = (function () {
3918
3976
  postInstructions = [
3919
3977
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3920
3978
  ];
3921
- return [3, 6];
3922
- case 3:
3979
+ return [3, 7];
3980
+ case 4:
3923
3981
  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);
3924
- if (!!skipBalanceChecks) return [3, 6];
3982
+ if (!!skipBalanceChecks) return [3, 7];
3925
3983
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3926
- case 4:
3984
+ case 5:
3927
3985
  if (!(_g.sent())) {
3928
3986
  throw "Insufficient Funds , token Account doesn't exist";
3929
3987
  }
3930
3988
  _b = anchor_1.BN.bind;
3931
3989
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3932
- case 5:
3990
+ case 6:
3933
3991
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3934
3992
  if (tokenAccountBalance.lt(amountIn)) {
3935
3993
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3936
3994
  }
3937
- _g.label = 6;
3938
- case 6:
3995
+ _g.label = 7;
3996
+ case 7:
3939
3997
  custodyAccountMetas = [];
3940
3998
  custodyOracleAccountMetas = [];
3941
3999
  markets = [];
@@ -3992,7 +4050,7 @@ var PerpetualsClient = (function () {
3992
4050
  })
3993
4051
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3994
4052
  .instruction()];
3995
- case 7:
4053
+ case 8:
3996
4054
  instruction = _g.sent();
3997
4055
  instructions.push(instruction);
3998
4056
  return [2, {
@@ -4475,12 +4533,12 @@ var PerpetualsClient = (function () {
4475
4533
  }
4476
4534
  });
4477
4535
  }); };
4478
- this.unstakeRequest = function (unstakeAmount, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
4536
+ this.unstakeRequest = function (unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4479
4537
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_13;
4480
4538
  return __generator(this, function (_a) {
4481
4539
  switch (_a.label) {
4482
4540
  case 0:
4483
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4541
+ publicKey = this.provider.wallet.publicKey;
4484
4542
  preInstructions = [];
4485
4543
  instructions = [];
4486
4544
  postInstructions = [];
@@ -4599,10 +4657,10 @@ var PerpetualsClient = (function () {
4599
4657
  args_1[_i - 3] = arguments[_i];
4600
4658
  }
4601
4659
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4602
- var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4660
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4603
4661
  if (createUserATA === void 0) { createUserATA = true; }
4604
- return __generator(this, function (_a) {
4605
- switch (_a.label) {
4662
+ return __generator(this, function (_b) {
4663
+ switch (_b.label) {
4606
4664
  case 0:
4607
4665
  publicKey = this.provider.wallet.publicKey;
4608
4666
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
@@ -4611,13 +4669,22 @@ var PerpetualsClient = (function () {
4611
4669
  instructions = [];
4612
4670
  postInstructions = [];
4613
4671
  additionalSigners = [];
4614
- _a.label = 1;
4672
+ _b.label = 1;
4615
4673
  case 1:
4616
- _a.trys.push([1, 3, , 4]);
4674
+ _b.trys.push([1, 5, , 6]);
4617
4675
  pool = poolConfig.poolAddress;
4618
4676
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4619
4677
  receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
4620
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
4678
+ _a = createUserATA;
4679
+ if (!_a) return [3, 3];
4680
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
4681
+ case 2:
4682
+ _a = !(_b.sent());
4683
+ _b.label = 3;
4684
+ case 3:
4685
+ if (_a) {
4686
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint));
4687
+ }
4621
4688
  tokenStakeAccounts = [];
4622
4689
  if (tokenStakeAccount) {
4623
4690
  tokenStakeAccounts.push({
@@ -4646,15 +4713,15 @@ var PerpetualsClient = (function () {
4646
4713
  })
4647
4714
  .remainingAccounts(__spreadArray([], tokenStakeAccounts, true))
4648
4715
  .instruction()];
4649
- case 2:
4650
- withdrawStakeInstruction = _a.sent();
4716
+ case 4:
4717
+ withdrawStakeInstruction = _b.sent();
4651
4718
  instructions.push(withdrawStakeInstruction);
4652
- return [3, 4];
4653
- case 3:
4654
- err_15 = _a.sent();
4719
+ return [3, 6];
4720
+ case 5:
4721
+ err_15 = _b.sent();
4655
4722
  console.log("perpClient withdrawStake error:: ", err_15);
4656
4723
  throw err_15;
4657
- case 4: return [2, {
4724
+ case 6: return [2, {
4658
4725
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4659
4726
  additionalSigners: additionalSigners
4660
4727
  }];
@@ -4713,21 +4780,29 @@ var PerpetualsClient = (function () {
4713
4780
  isWritable: false,
4714
4781
  });
4715
4782
  }
4716
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4717
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4718
- if (!(inTokenSymbol == 'SOL')) return [3, 3];
4783
+ return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
4784
+ case 1:
4785
+ if (!(_f.sent())) {
4786
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4787
+ }
4788
+ return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
4789
+ case 2:
4790
+ if (!(_f.sent())) {
4791
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
4792
+ }
4793
+ if (!(inTokenSymbol == 'SOL')) return [3, 5];
4719
4794
  console.log("inTokenSymbol === SOL", inTokenSymbol);
4720
4795
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
4721
- if (!!skipBalanceChecks) return [3, 2];
4796
+ if (!!skipBalanceChecks) return [3, 4];
4722
4797
  _e = anchor_1.BN.bind;
4723
4798
  return [4, this.provider.connection.getBalance(publicKey)];
4724
- case 1:
4799
+ case 3:
4725
4800
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _f.sent()]))();
4726
4801
  if (unWrappedSolBalance.lt(lamports)) {
4727
4802
  throw "Insufficient SOL Funds";
4728
4803
  }
4729
- _f.label = 2;
4730
- case 2:
4804
+ _f.label = 4;
4805
+ case 4:
4731
4806
  if (!ephemeralSignerPubkey) {
4732
4807
  wrappedSolAccount = new web3_js_1.Keypair();
4733
4808
  additionalSigners.push(wrappedSolAccount);
@@ -4745,17 +4820,17 @@ var PerpetualsClient = (function () {
4745
4820
  postInstructions = [
4746
4821
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4747
4822
  ];
4748
- return [3, 5];
4749
- case 3:
4750
- if (!!skipBalanceChecks) return [3, 5];
4823
+ return [3, 7];
4824
+ case 5:
4825
+ if (!!skipBalanceChecks) return [3, 7];
4751
4826
  return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
4752
- case 4:
4827
+ case 6:
4753
4828
  if (!(_f.sent())) {
4754
4829
  throw "Insufficient Funds , token Account doesn't exist";
4755
4830
  }
4756
- _f.label = 5;
4757
- case 5:
4758
- _f.trys.push([5, 7, , 8]);
4831
+ _f.label = 7;
4832
+ case 7:
4833
+ _f.trys.push([7, 9, , 10]);
4759
4834
  if (enableHeapSizeIx) {
4760
4835
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4761
4836
  bytes: 64 * 1024,
@@ -4797,15 +4872,15 @@ var PerpetualsClient = (function () {
4797
4872
  })
4798
4873
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4799
4874
  .instruction()];
4800
- case 6:
4875
+ case 8:
4801
4876
  addCompoundingLiquidity = _f.sent();
4802
4877
  instructions.push(addCompoundingLiquidity);
4803
- return [3, 8];
4804
- case 7:
4878
+ return [3, 10];
4879
+ case 9:
4805
4880
  err_16 = _f.sent();
4806
4881
  console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4807
- return [3, 8];
4808
- case 8: return [2, {
4882
+ return [3, 10];
4883
+ case 10: return [2, {
4809
4884
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4810
4885
  additionalSigners: additionalSigners
4811
4886
  }];
@@ -4819,14 +4894,14 @@ var PerpetualsClient = (function () {
4819
4894
  args_1[_i - 5] = arguments[_i];
4820
4895
  }
4821
4896
  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) {
4822
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, outCustodyConfig, lpTokenMint, compoundingTokenMint, lamports, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, compoundingTokenAccount, heapSizeIx, whitelistPda, whitelistMeta, removeCompoundingLiquidity, err_17;
4897
+ 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;
4823
4898
  if (createUserATA === void 0) { createUserATA = true; }
4824
4899
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4825
4900
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4826
4901
  if (enableHeapSizeIx === void 0) { enableHeapSizeIx = true; }
4827
4902
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
4828
- return __generator(this, function (_e) {
4829
- switch (_e.label) {
4903
+ return __generator(this, function (_f) {
4904
+ switch (_f.label) {
4830
4905
  case 0:
4831
4906
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4832
4907
  preInstructions = [];
@@ -4837,35 +4912,45 @@ var PerpetualsClient = (function () {
4837
4912
  outCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outTokenSymbol).mintKey); });
4838
4913
  lpTokenMint = poolConfig.stakedLpTokenMint;
4839
4914
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4840
- if (outCustodyConfig.symbol == 'SOL') {
4841
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4842
- if (!ephemeralSignerPubkey) {
4843
- wrappedSolAccount = new web3_js_1.Keypair();
4844
- additionalSigners.push(wrappedSolAccount);
4845
- }
4846
- preInstructions = [
4847
- web3_js_1.SystemProgram.createAccount({
4848
- fromPubkey: publicKey,
4849
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4850
- lamports: lamports,
4851
- space: 165,
4852
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4853
- }),
4854
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4855
- ];
4856
- postInstructions = [
4857
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4858
- ];
4915
+ if (!(outCustodyConfig.symbol == 'SOL')) return [3, 1];
4916
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
4917
+ if (!ephemeralSignerPubkey) {
4918
+ wrappedSolAccount = new web3_js_1.Keypair();
4919
+ additionalSigners.push(wrappedSolAccount);
4859
4920
  }
4860
- else {
4861
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(outCustodyConfig.mintKey, publicKey, true, poolConfig.getTokenFromSymbol(outTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
4862
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, outCustodyConfig.mintKey, poolConfig.getTokenFromSymbol(outTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
4921
+ preInstructions = [
4922
+ web3_js_1.SystemProgram.createAccount({
4923
+ fromPubkey: publicKey,
4924
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4925
+ lamports: lamports,
4926
+ space: 165,
4927
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
4928
+ }),
4929
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4930
+ ];
4931
+ postInstructions = [
4932
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4933
+ ];
4934
+ return [3, 4];
4935
+ case 1:
4936
+ 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);
4937
+ _a = createUserATA;
4938
+ if (!_a) return [3, 3];
4939
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
4940
+ case 2:
4941
+ _a = !(_f.sent());
4942
+ _f.label = 3;
4943
+ case 3:
4944
+ if (_a) {
4945
+ 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));
4863
4946
  }
4947
+ _f.label = 4;
4948
+ case 4:
4864
4949
  custodyAccountMetas = [];
4865
4950
  custodyOracleAccountMetas = [];
4866
4951
  markets = [];
4867
- for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
4868
- custody = _b[_a];
4952
+ for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4953
+ custody = _c[_b];
4869
4954
  custodyAccountMetas.push({
4870
4955
  pubkey: custody.custodyAccount,
4871
4956
  isSigner: false,
@@ -4877,8 +4962,8 @@ var PerpetualsClient = (function () {
4877
4962
  isWritable: false,
4878
4963
  });
4879
4964
  }
4880
- for (_c = 0, _d = poolConfig.markets; _c < _d.length; _c++) {
4881
- market = _d[_c];
4965
+ for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4966
+ market = _e[_d];
4882
4967
  markets.push({
4883
4968
  pubkey: market.marketAccount,
4884
4969
  isSigner: false,
@@ -4886,9 +4971,9 @@ var PerpetualsClient = (function () {
4886
4971
  });
4887
4972
  }
4888
4973
  compoundingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4889
- _e.label = 1;
4890
- case 1:
4891
- _e.trys.push([1, 3, , 4]);
4974
+ _f.label = 5;
4975
+ case 5:
4976
+ _f.trys.push([5, 7, , 8]);
4892
4977
  if (enableHeapSizeIx) {
4893
4978
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4894
4979
  bytes: 64 * 1024,
@@ -4930,15 +5015,15 @@ var PerpetualsClient = (function () {
4930
5015
  })
4931
5016
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4932
5017
  .instruction()];
4933
- case 2:
4934
- removeCompoundingLiquidity = _e.sent();
5018
+ case 6:
5019
+ removeCompoundingLiquidity = _f.sent();
4935
5020
  instructions.push(removeCompoundingLiquidity);
4936
- return [3, 4];
4937
- case 3:
4938
- err_17 = _e.sent();
5021
+ return [3, 8];
5022
+ case 7:
5023
+ err_17 = _f.sent();
4939
5024
  console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
4940
- return [3, 4];
4941
- case 4: return [2, {
5025
+ return [3, 8];
5026
+ case 8: return [2, {
4942
5027
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4943
5028
  additionalSigners: additionalSigners
4944
5029
  }];
@@ -4951,13 +5036,13 @@ var PerpetualsClient = (function () {
4951
5036
  for (var _i = 3; _i < arguments.length; _i++) {
4952
5037
  args_1[_i - 3] = arguments[_i];
4953
5038
  }
4954
- return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA, userPublicKey) {
4955
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, tokenStakeAccount, tokenStakeAccounts, _a, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _b, _c, custody, _d, _e, market, migrateStake, err_18;
5039
+ return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA) {
5040
+ 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;
4956
5041
  if (createUserATA === void 0) { createUserATA = true; }
4957
- return __generator(this, function (_f) {
4958
- switch (_f.label) {
5042
+ return __generator(this, function (_g) {
5043
+ switch (_g.label) {
4959
5044
  case 0:
4960
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5045
+ publicKey = this.provider.wallet.publicKey;
4961
5046
  preInstructions = [];
4962
5047
  instructions = [];
4963
5048
  postInstructions = [];
@@ -4966,18 +5051,27 @@ var PerpetualsClient = (function () {
4966
5051
  lpTokenMint = poolConfig.stakedLpTokenMint;
4967
5052
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4968
5053
  compoudingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4969
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4970
- flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
4971
- tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
4972
- tokenStakeAccounts = [];
4973
- _a = tokenStakeAccount;
5054
+ _a = createUserATA;
4974
5055
  if (!_a) return [3, 2];
4975
- return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5056
+ return [4, (0, utils_1.checkIfAccountExists)(compoudingTokenAccount, this.provider.connection)];
4976
5057
  case 1:
4977
- _a = (_f.sent());
4978
- _f.label = 2;
5058
+ _a = !(_g.sent());
5059
+ _g.label = 2;
4979
5060
  case 2:
4980
5061
  if (_a) {
5062
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint));
5063
+ }
5064
+ flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
5065
+ tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
5066
+ tokenStakeAccounts = [];
5067
+ _b = tokenStakeAccount;
5068
+ if (!_b) return [3, 4];
5069
+ return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5070
+ case 3:
5071
+ _b = (_g.sent());
5072
+ _g.label = 4;
5073
+ case 4:
5074
+ if (_b) {
4981
5075
  tokenStakeAccounts.push({
4982
5076
  pubkey: tokenStakeAccount,
4983
5077
  isSigner: false,
@@ -4988,8 +5082,8 @@ var PerpetualsClient = (function () {
4988
5082
  custodyAccountMetas = [];
4989
5083
  custodyOracleAccountMetas = [];
4990
5084
  markets = [];
4991
- for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4992
- custody = _c[_b];
5085
+ for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
5086
+ custody = _d[_c];
4993
5087
  custodyAccountMetas.push({
4994
5088
  pubkey: custody.custodyAccount,
4995
5089
  isSigner: false,
@@ -5001,17 +5095,17 @@ var PerpetualsClient = (function () {
5001
5095
  isWritable: false,
5002
5096
  });
5003
5097
  }
5004
- for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
5005
- market = _e[_d];
5098
+ for (_e = 0, _f = poolConfig.markets; _e < _f.length; _e++) {
5099
+ market = _f[_e];
5006
5100
  markets.push({
5007
5101
  pubkey: market.marketAccount,
5008
5102
  isSigner: false,
5009
5103
  isWritable: false,
5010
5104
  });
5011
5105
  }
5012
- _f.label = 3;
5013
- case 3:
5014
- _f.trys.push([3, 5, , 6]);
5106
+ _g.label = 5;
5107
+ case 5:
5108
+ _g.trys.push([5, 7, , 8]);
5015
5109
  return [4, this.program.methods
5016
5110
  .migrateStake({
5017
5111
  amount: amount
@@ -5036,15 +5130,15 @@ var PerpetualsClient = (function () {
5036
5130
  })
5037
5131
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), tokenStakeAccounts, true))
5038
5132
  .instruction()];
5039
- case 4:
5040
- migrateStake = _f.sent();
5133
+ case 6:
5134
+ migrateStake = _g.sent();
5041
5135
  instructions.push(migrateStake);
5042
- return [3, 6];
5043
- case 5:
5044
- err_18 = _f.sent();
5136
+ return [3, 8];
5137
+ case 7:
5138
+ err_18 = _g.sent();
5045
5139
  console.log("perpClient migrateStake error:: ", err_18);
5046
- return [3, 6];
5047
- case 6: return [2, {
5140
+ return [3, 8];
5141
+ case 8: return [2, {
5048
5142
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5049
5143
  additionalSigners: additionalSigners
5050
5144
  }];
@@ -5052,12 +5146,12 @@ var PerpetualsClient = (function () {
5052
5146
  });
5053
5147
  });
5054
5148
  };
5055
- this.migrateFlp = function (amount, rewardTokenMint, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
5149
+ this.migrateFlp = function (amount, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5056
5150
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_19;
5057
5151
  return __generator(this, function (_d) {
5058
5152
  switch (_d.label) {
5059
5153
  case 0:
5060
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5154
+ publicKey = this.provider.wallet.publicKey;
5061
5155
  preInstructions = [];
5062
5156
  instructions = [];
5063
5157
  postInstructions = [];
@@ -5221,10 +5315,14 @@ var PerpetualsClient = (function () {
5221
5315
  additionalSigners = [];
5222
5316
  _a.label = 1;
5223
5317
  case 1:
5224
- _a.trys.push([1, 3, , 4]);
5318
+ _a.trys.push([1, 4, , 5]);
5225
5319
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5226
5320
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5227
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5321
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5322
+ case 2:
5323
+ if (!(_a.sent())) {
5324
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5325
+ }
5228
5326
  return [4, this.program.methods
5229
5327
  .depositTokenStake({
5230
5328
  depositAmount: depositAmount
@@ -5244,15 +5342,15 @@ var PerpetualsClient = (function () {
5244
5342
  tokenMint: poolConfig.tokenMint,
5245
5343
  })
5246
5344
  .instruction()];
5247
- case 2:
5345
+ case 3:
5248
5346
  depositTokenStakeInstruction = _a.sent();
5249
5347
  instructions.push(depositTokenStakeInstruction);
5250
- return [3, 4];
5251
- case 3:
5348
+ return [3, 5];
5349
+ case 4:
5252
5350
  err_21 = _a.sent();
5253
5351
  console.log("perpClient depositStakingInstruction error:: ", err_21);
5254
5352
  throw err_21;
5255
- case 4: return [2, {
5353
+ case 5: return [2, {
5256
5354
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5257
5355
  additionalSigners: additionalSigners
5258
5356
  }];
@@ -5310,10 +5408,14 @@ var PerpetualsClient = (function () {
5310
5408
  additionalSigners = [];
5311
5409
  _a.label = 1;
5312
5410
  case 1:
5313
- _a.trys.push([1, 3, , 4]);
5411
+ _a.trys.push([1, 4, , 5]);
5314
5412
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5315
5413
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5316
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5414
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5415
+ case 2:
5416
+ if (!(_a.sent())) {
5417
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5418
+ }
5317
5419
  return [4, this.program.methods
5318
5420
  .unstakeTokenInstant({
5319
5421
  unstakeAmount: unstakeAmount
@@ -5332,15 +5434,15 @@ var PerpetualsClient = (function () {
5332
5434
  tokenMint: poolConfig.tokenMint,
5333
5435
  })
5334
5436
  .instruction()];
5335
- case 2:
5437
+ case 3:
5336
5438
  unstakeTokenInstantInstruction = _a.sent();
5337
5439
  instructions.push(unstakeTokenInstantInstruction);
5338
- return [3, 4];
5339
- case 3:
5440
+ return [3, 5];
5441
+ case 4:
5340
5442
  err_23 = _a.sent();
5341
5443
  console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5342
5444
  throw err_23;
5343
- case 4: return [2, {
5445
+ case 5: return [2, {
5344
5446
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5345
5447
  additionalSigners: additionalSigners
5346
5448
  }];
@@ -5358,10 +5460,14 @@ var PerpetualsClient = (function () {
5358
5460
  additionalSigners = [];
5359
5461
  _a.label = 1;
5360
5462
  case 1:
5361
- _a.trys.push([1, 3, , 4]);
5463
+ _a.trys.push([1, 4, , 5]);
5362
5464
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5363
5465
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5364
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5466
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5467
+ case 2:
5468
+ if (!(_a.sent())) {
5469
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5470
+ }
5365
5471
  return [4, this.program.methods
5366
5472
  .withdrawToken({
5367
5473
  withdrawRequestId: withdrawRequestId
@@ -5380,15 +5486,15 @@ var PerpetualsClient = (function () {
5380
5486
  tokenMint: poolConfig.tokenMint,
5381
5487
  })
5382
5488
  .instruction()];
5383
- case 2:
5489
+ case 3:
5384
5490
  withdrawTokenInstruction = _a.sent();
5385
5491
  instructions.push(withdrawTokenInstruction);
5386
- return [3, 4];
5387
- case 3:
5492
+ return [3, 5];
5493
+ case 4:
5388
5494
  err_24 = _a.sent();
5389
5495
  console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5390
5496
  throw err_24;
5391
- case 4: return [2, {
5497
+ case 5: return [2, {
5392
5498
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5393
5499
  additionalSigners: additionalSigners
5394
5500
  }];
@@ -5440,23 +5546,32 @@ var PerpetualsClient = (function () {
5440
5546
  for (var _i = 2; _i < arguments.length; _i++) {
5441
5547
  args_1[_i - 2] = arguments[_i];
5442
5548
  }
5443
- return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA, userPublicKey) {
5444
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, collectTokenRewardInstruction, err_26;
5549
+ return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA) {
5550
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_26;
5445
5551
  if (createUserATA === void 0) { createUserATA = true; }
5446
- return __generator(this, function (_a) {
5447
- switch (_a.label) {
5552
+ return __generator(this, function (_b) {
5553
+ switch (_b.label) {
5448
5554
  case 0:
5449
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5555
+ publicKey = this.provider.wallet.publicKey;
5450
5556
  preInstructions = [];
5451
5557
  instructions = [];
5452
5558
  postInstructions = [];
5453
5559
  additionalSigners = [];
5454
- _a.label = 1;
5560
+ _b.label = 1;
5455
5561
  case 1:
5456
- _a.trys.push([1, 3, , 4]);
5562
+ _b.trys.push([1, 5, , 6]);
5457
5563
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5458
5564
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5459
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5565
+ _a = createUserATA;
5566
+ if (!_a) return [3, 3];
5567
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5568
+ case 2:
5569
+ _a = !(_b.sent());
5570
+ _b.label = 3;
5571
+ case 3:
5572
+ if (_a) {
5573
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint));
5574
+ }
5460
5575
  return [4, this.program.methods
5461
5576
  .collectTokenReward({})
5462
5577
  .accounts({
@@ -5473,15 +5588,15 @@ var PerpetualsClient = (function () {
5473
5588
  tokenMint: poolConfig.tokenMint,
5474
5589
  })
5475
5590
  .instruction()];
5476
- case 2:
5477
- collectTokenRewardInstruction = _a.sent();
5591
+ case 4:
5592
+ collectTokenRewardInstruction = _b.sent();
5478
5593
  instructions.push(collectTokenRewardInstruction);
5479
- return [3, 4];
5480
- case 3:
5481
- err_26 = _a.sent();
5594
+ return [3, 6];
5595
+ case 5:
5596
+ err_26 = _b.sent();
5482
5597
  console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5483
5598
  throw err_26;
5484
- case 4: return [2, {
5599
+ case 6: return [2, {
5485
5600
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5486
5601
  additionalSigners: additionalSigners
5487
5602
  }];
@@ -5495,23 +5610,32 @@ var PerpetualsClient = (function () {
5495
5610
  args_1[_i - 3] = arguments[_i];
5496
5611
  }
5497
5612
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5498
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, collectRevenueInstruction, err_27;
5613
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_27;
5499
5614
  if (createUserATA === void 0) { createUserATA = true; }
5500
- return __generator(this, function (_a) {
5501
- switch (_a.label) {
5615
+ return __generator(this, function (_b) {
5616
+ switch (_b.label) {
5502
5617
  case 0:
5503
5618
  publicKey = this.provider.wallet.publicKey;
5504
5619
  preInstructions = [];
5505
5620
  instructions = [];
5506
5621
  postInstructions = [];
5507
5622
  additionalSigners = [];
5508
- _a.label = 1;
5623
+ _b.label = 1;
5509
5624
  case 1:
5510
- _a.trys.push([1, 3, , 4]);
5625
+ _b.trys.push([1, 5, , 6]);
5511
5626
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5512
5627
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5513
5628
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, owner, true);
5514
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
5629
+ _a = createUserATA;
5630
+ if (!_a) return [3, 3];
5631
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5632
+ case 2:
5633
+ _a = !(_b.sent());
5634
+ _b.label = 3;
5635
+ case 3:
5636
+ if (_a) {
5637
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint));
5638
+ }
5515
5639
  return [4, this.program.methods
5516
5640
  .collectRevenue({})
5517
5641
  .accounts({
@@ -5528,15 +5652,15 @@ var PerpetualsClient = (function () {
5528
5652
  receivingTokenMint: rewardCustodyMint,
5529
5653
  })
5530
5654
  .instruction()];
5531
- case 2:
5532
- collectRevenueInstruction = _a.sent();
5655
+ case 4:
5656
+ collectRevenueInstruction = _b.sent();
5533
5657
  instructions.push(collectRevenueInstruction);
5534
- return [3, 4];
5535
- case 3:
5536
- err_27 = _a.sent();
5658
+ return [3, 6];
5659
+ case 5:
5660
+ err_27 = _b.sent();
5537
5661
  console.log("perpClient collectRevenueInstruction error:: ", err_27);
5538
5662
  throw err_27;
5539
- case 4: return [2, {
5663
+ case 6: return [2, {
5540
5664
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5541
5665
  additionalSigners: additionalSigners
5542
5666
  }];
@@ -5550,23 +5674,32 @@ var PerpetualsClient = (function () {
5550
5674
  args_1[_i - 3] = arguments[_i];
5551
5675
  }
5552
5676
  return __awaiter(_this, __spreadArray([owner_1, rebateSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rebateSymbol, poolConfig, createUserATA) {
5553
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, collectRebateInstruction, err_28;
5677
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, _a, collectRebateInstruction, err_28;
5554
5678
  if (createUserATA === void 0) { createUserATA = true; }
5555
- return __generator(this, function (_a) {
5556
- switch (_a.label) {
5679
+ return __generator(this, function (_b) {
5680
+ switch (_b.label) {
5557
5681
  case 0:
5558
5682
  publicKey = this.provider.wallet.publicKey;
5559
5683
  preInstructions = [];
5560
5684
  instructions = [];
5561
5685
  postInstructions = [];
5562
5686
  additionalSigners = [];
5563
- _a.label = 1;
5687
+ _b.label = 1;
5564
5688
  case 1:
5565
- _a.trys.push([1, 3, , 4]);
5689
+ _b.trys.push([1, 5, , 6]);
5566
5690
  rebateMint = poolConfig.getTokenFromSymbol(rebateSymbol).mintKey;
5567
5691
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5568
5692
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rebateMint, owner, true);
5569
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rebateMint, spl_token_1.TOKEN_PROGRAM_ID));
5693
+ _a = createUserATA;
5694
+ if (!_a) return [3, 3];
5695
+ return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5696
+ case 2:
5697
+ _a = !(_b.sent());
5698
+ _b.label = 3;
5699
+ case 3:
5700
+ if (_a) {
5701
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rebateMint));
5702
+ }
5570
5703
  return [4, this.program.methods
5571
5704
  .collectRebate()
5572
5705
  .accounts({
@@ -5583,15 +5716,15 @@ var PerpetualsClient = (function () {
5583
5716
  receivingTokenMint: rebateMint,
5584
5717
  })
5585
5718
  .instruction()];
5586
- case 2:
5587
- collectRebateInstruction = _a.sent();
5719
+ case 4:
5720
+ collectRebateInstruction = _b.sent();
5588
5721
  instructions.push(collectRebateInstruction);
5589
- return [3, 4];
5590
- case 3:
5591
- err_28 = _a.sent();
5722
+ return [3, 6];
5723
+ case 5:
5724
+ err_28 = _b.sent();
5592
5725
  console.log("perpClient collectRebateInstruction error:: ", err_28);
5593
5726
  throw err_28;
5594
- case 4: return [2, {
5727
+ case 6: return [2, {
5595
5728
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5596
5729
  additionalSigners: additionalSigners
5597
5730
  }];
@@ -5651,14 +5784,14 @@ var PerpetualsClient = (function () {
5651
5784
  for (var _i = 11; _i < arguments.length; _i++) {
5652
5785
  args_1[_i - 11] = arguments[_i];
5653
5786
  }
5654
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, reserveSymbol_1, receiveSymbol_1, side_1, limitPrice_1, reserveAmount_1, sizeAmount_1, stopLossPrice_1, takeProfitPrice_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, reserveSymbol, receiveSymbol, side, limitPrice, reserveAmount, sizeAmount, stopLossPrice, takeProfitPrice, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
5787
+ 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) {
5655
5788
  var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_30;
5656
5789
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5657
5790
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5658
5791
  return __generator(this, function (_c) {
5659
5792
  switch (_c.label) {
5660
5793
  case 0:
5661
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5794
+ publicKey = this.provider.wallet.publicKey;
5662
5795
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5663
5796
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5664
5797
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5774,14 +5907,14 @@ var PerpetualsClient = (function () {
5774
5907
  for (var _i = 11; _i < arguments.length; _i++) {
5775
5908
  args_1[_i - 11] = arguments[_i];
5776
5909
  }
5777
- return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, reserveSymbol_1, receiveSymbol_1, side_1, orderId_1, limitPrice_1, sizeAmount_1, stopLossPrice_1, takeProfitPrice_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, reserveSymbol, receiveSymbol, side, orderId, limitPrice, sizeAmount, stopLossPrice, takeProfitPrice, poolConfig, createUserATA, ephemeralSignerPubkey, userPublicKey) {
5778
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, positionAccount, orderAccount, editLimitOrder, err_31;
5910
+ 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) {
5911
+ var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_31;
5779
5912
  if (createUserATA === void 0) { createUserATA = true; }
5780
5913
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5781
- return __generator(this, function (_a) {
5782
- switch (_a.label) {
5914
+ return __generator(this, function (_b) {
5915
+ switch (_b.label) {
5783
5916
  case 0:
5784
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5917
+ publicKey = this.provider.wallet.publicKey;
5785
5918
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5786
5919
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5787
5920
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5791,33 +5924,43 @@ var PerpetualsClient = (function () {
5791
5924
  instructions = [];
5792
5925
  postInstructions = [];
5793
5926
  additionalSigners = [];
5794
- _a.label = 1;
5927
+ _b.label = 1;
5795
5928
  case 1:
5796
- _a.trys.push([1, 3, , 4]);
5797
- if (reserveSymbol == 'SOL') {
5798
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
5799
- if (!ephemeralSignerPubkey) {
5800
- wrappedSolAccount = new web3_js_1.Keypair();
5801
- additionalSigners.push(wrappedSolAccount);
5802
- }
5803
- preInstructions = [
5804
- web3_js_1.SystemProgram.createAccount({
5805
- fromPubkey: publicKey,
5806
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5807
- lamports: lamports,
5808
- space: 165,
5809
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5810
- }),
5811
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5812
- ];
5813
- postInstructions = [
5814
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5815
- ];
5929
+ _b.trys.push([1, 7, , 8]);
5930
+ if (!(reserveSymbol == 'SOL')) return [3, 2];
5931
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
5932
+ if (!ephemeralSignerPubkey) {
5933
+ wrappedSolAccount = new web3_js_1.Keypair();
5934
+ additionalSigners.push(wrappedSolAccount);
5816
5935
  }
5817
- else {
5818
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(reserveSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
5819
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(reserveSymbol).mintKey, poolConfig.getTokenFromSymbol(reserveSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
5936
+ preInstructions = [
5937
+ web3_js_1.SystemProgram.createAccount({
5938
+ fromPubkey: publicKey,
5939
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5940
+ lamports: lamports,
5941
+ space: 165,
5942
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
5943
+ }),
5944
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5945
+ ];
5946
+ postInstructions = [
5947
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5948
+ ];
5949
+ return [3, 5];
5950
+ case 2:
5951
+ 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);
5952
+ _a = createUserATA;
5953
+ if (!_a) return [3, 4];
5954
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
5955
+ case 3:
5956
+ _a = !(_b.sent());
5957
+ _b.label = 4;
5958
+ case 4:
5959
+ if (_a) {
5960
+ 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));
5820
5961
  }
5962
+ _b.label = 5;
5963
+ case 5:
5821
5964
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
5822
5965
  orderAccount = poolConfig.getOrderFromMarketPk(publicKey, marketAccount);
5823
5966
  return [4, this.program.methods
@@ -5851,15 +5994,15 @@ var PerpetualsClient = (function () {
5851
5994
  receivingMint: poolConfig.getTokenFromSymbol(reserveSymbol).mintKey
5852
5995
  })
5853
5996
  .instruction()];
5854
- case 2:
5855
- editLimitOrder = _a.sent();
5997
+ case 6:
5998
+ editLimitOrder = _b.sent();
5856
5999
  instructions.push(editLimitOrder);
5857
- return [3, 4];
5858
- case 3:
5859
- err_31 = _a.sent();
6000
+ return [3, 8];
6001
+ case 7:
6002
+ err_31 = _b.sent();
5860
6003
  console.log("perpClient editLimitOrder error:: ", err_31);
5861
6004
  throw err_31;
5862
- case 4: return [2, {
6005
+ case 8: return [2, {
5863
6006
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5864
6007
  additionalSigners: additionalSigners
5865
6008
  }];
@@ -6008,12 +6151,12 @@ var PerpetualsClient = (function () {
6008
6151
  });
6009
6152
  });
6010
6153
  };
6011
- this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6154
+ this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6012
6155
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_34;
6013
6156
  return __generator(this, function (_a) {
6014
6157
  switch (_a.label) {
6015
6158
  case 0:
6016
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6159
+ publicKey = this.provider.wallet.publicKey;
6017
6160
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6018
6161
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6019
6162
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6067,12 +6210,12 @@ var PerpetualsClient = (function () {
6067
6210
  }
6068
6211
  });
6069
6212
  }); };
6070
- this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6213
+ this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6071
6214
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_35;
6072
6215
  return __generator(this, function (_a) {
6073
6216
  switch (_a.label) {
6074
6217
  case 0:
6075
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6218
+ publicKey = this.provider.wallet.publicKey;
6076
6219
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6077
6220
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6078
6221
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6125,12 +6268,12 @@ var PerpetualsClient = (function () {
6125
6268
  }
6126
6269
  });
6127
6270
  }); };
6128
- this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6271
+ this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6129
6272
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_36;
6130
6273
  return __generator(this, function (_a) {
6131
6274
  switch (_a.label) {
6132
6275
  case 0:
6133
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6276
+ publicKey = this.provider.wallet.publicKey;
6134
6277
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6135
6278
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6136
6279
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6169,12 +6312,12 @@ var PerpetualsClient = (function () {
6169
6312
  }
6170
6313
  });
6171
6314
  }); };
6172
- this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6315
+ this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6173
6316
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_37;
6174
6317
  return __generator(this, function (_a) {
6175
6318
  switch (_a.label) {
6176
6319
  case 0:
6177
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6320
+ publicKey = this.provider.wallet.publicKey;
6178
6321
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6179
6322
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6180
6323
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6217,13 +6360,13 @@ var PerpetualsClient = (function () {
6217
6360
  args_1[_i - 9] = arguments[_i];
6218
6361
  }
6219
6362
  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) {
6220
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, userReceivingTokenAccount, userReceivingTokenAccountCollateral, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, collateralToken, receivingToken, positionAccount, orderAccount, custodyAccountMetas, custodyOracleAccountMetas, _a, _b, custody, executeTriggerWithSwap, err_38;
6363
+ 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;
6221
6364
  if (createUserATA === void 0) { createUserATA = true; }
6222
6365
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6223
6366
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6224
6367
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6225
- return __generator(this, function (_c) {
6226
- switch (_c.label) {
6368
+ return __generator(this, function (_e) {
6369
+ switch (_e.label) {
6227
6370
  case 0:
6228
6371
  payerPubkey = this.provider.wallet.publicKey;
6229
6372
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6236,23 +6379,42 @@ var PerpetualsClient = (function () {
6236
6379
  additionalSigners = [];
6237
6380
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
6238
6381
  receivingToken = poolConfig.getTokenFromSymbol(receivingSymbol);
6239
- _c.label = 1;
6382
+ _e.label = 1;
6240
6383
  case 1:
6241
- _c.trys.push([1, 3, , 4]);
6242
- if (false) {
6384
+ _e.trys.push([1, 9, , 10]);
6385
+ if (!false) return [3, 2];
6386
+ return [3, 7];
6387
+ case 2:
6388
+ 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);
6389
+ _a = createUserATA;
6390
+ if (!_a) return [3, 4];
6391
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6392
+ case 3:
6393
+ _a = !(_e.sent());
6394
+ _e.label = 4;
6395
+ case 4:
6396
+ if (_a) {
6397
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey));
6243
6398
  }
6244
- else {
6245
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, owner, true, receivingToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6246
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, spl_token_1.TOKEN_PROGRAM_ID));
6247
- userReceivingTokenAccountCollateral = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, owner, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6248
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6399
+ 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);
6400
+ _b = createUserATA;
6401
+ if (!_b) return [3, 6];
6402
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccountCollateral, this.provider.connection)];
6403
+ case 5:
6404
+ _b = !(_e.sent());
6405
+ _e.label = 6;
6406
+ case 6:
6407
+ if (_b) {
6408
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
6249
6409
  }
6410
+ _e.label = 7;
6411
+ case 7:
6250
6412
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6251
6413
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6252
6414
  custodyAccountMetas = [];
6253
6415
  custodyOracleAccountMetas = [];
6254
- for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
6255
- custody = _b[_a];
6416
+ for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
6417
+ custody = _d[_c];
6256
6418
  custodyAccountMetas.push({
6257
6419
  pubkey: custody.custodyAccount,
6258
6420
  isSigner: false,
@@ -6299,15 +6461,15 @@ var PerpetualsClient = (function () {
6299
6461
  })
6300
6462
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6301
6463
  .instruction()];
6302
- case 2:
6303
- executeTriggerWithSwap = _c.sent();
6464
+ case 8:
6465
+ executeTriggerWithSwap = _e.sent();
6304
6466
  instructions.push(executeTriggerWithSwap);
6305
- return [3, 4];
6306
- case 3:
6307
- err_38 = _c.sent();
6467
+ return [3, 10];
6468
+ case 9:
6469
+ err_38 = _e.sent();
6308
6470
  console.log("perpClient executeTriggerWithSwap error:: ", err_38);
6309
6471
  throw err_38;
6310
- case 4: return [2, {
6472
+ case 10: return [2, {
6311
6473
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6312
6474
  additionalSigners: additionalSigners
6313
6475
  }];
@@ -6321,13 +6483,13 @@ var PerpetualsClient = (function () {
6321
6483
  args_1[_i - 8] = arguments[_i];
6322
6484
  }
6323
6485
  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) {
6324
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeTriggerOrder, err_39;
6486
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_39;
6325
6487
  if (createUserATA === void 0) { createUserATA = true; }
6326
6488
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6327
6489
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6328
6490
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6329
- return __generator(this, function (_a) {
6330
- switch (_a.label) {
6491
+ return __generator(this, function (_b) {
6492
+ switch (_b.label) {
6331
6493
  case 0:
6332
6494
  payerPubkey = this.provider.wallet.publicKey;
6333
6495
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6337,15 +6499,25 @@ var PerpetualsClient = (function () {
6337
6499
  instructions = [];
6338
6500
  postInstructions = [];
6339
6501
  additionalSigners = [];
6340
- _a.label = 1;
6502
+ _b.label = 1;
6341
6503
  case 1:
6342
- _a.trys.push([1, 3, , 4]);
6343
- if (false) {
6344
- }
6345
- else {
6346
- userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, owner, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6347
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6504
+ _b.trys.push([1, 7, , 8]);
6505
+ if (!false) return [3, 2];
6506
+ return [3, 5];
6507
+ case 2:
6508
+ 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);
6509
+ _a = createUserATA;
6510
+ if (!_a) return [3, 4];
6511
+ return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6512
+ case 3:
6513
+ _a = !(_b.sent());
6514
+ _b.label = 4;
6515
+ case 4:
6516
+ if (_a) {
6517
+ 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));
6348
6518
  }
6519
+ _b.label = 5;
6520
+ case 5:
6349
6521
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6350
6522
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6351
6523
  return [4, this.program.methods
@@ -6377,15 +6549,15 @@ var PerpetualsClient = (function () {
6377
6549
  })
6378
6550
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6379
6551
  .instruction()];
6380
- case 2:
6381
- executeTriggerOrder = _a.sent();
6552
+ case 6:
6553
+ executeTriggerOrder = _b.sent();
6382
6554
  instructions.push(executeTriggerOrder);
6383
- return [3, 4];
6384
- case 3:
6385
- err_39 = _a.sent();
6555
+ return [3, 8];
6556
+ case 7:
6557
+ err_39 = _b.sent();
6386
6558
  console.log("perpClient executeTriggerOrder error:: ", err_39);
6387
6559
  throw err_39;
6388
- case 4: return [2, {
6560
+ case 8: return [2, {
6389
6561
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6390
6562
  additionalSigners: additionalSigners
6391
6563
  }];
@@ -6398,16 +6570,16 @@ var PerpetualsClient = (function () {
6398
6570
  for (var _i = 5; _i < arguments.length; _i++) {
6399
6571
  args_1[_i - 5] = arguments[_i];
6400
6572
  }
6401
- return __awaiter(_this, __spreadArray([userInputTokenSymbol_1, userOutputTokenSymbol_1, amountIn_1, minAmountOut_1, poolConfig_1], args_1, true), void 0, function (userInputTokenSymbol, userOutputTokenSymbol, amountIn, minAmountOut, poolConfig, useFeesPool, createUserATA, unWrapSol, skipBalanceChecks, ephemeralSignerPubkey, isWhitelistedUser, userPublicKey) {
6402
- var userInputCustodyConfig, userOutputCustodyConfig, publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userOutputTokenAccount, userInputTokenAccount, wsolAssociatedTokenAccount, unWrappedSolBalance, _a, wsolAssociatedTokenAccount, closeWsolATAIns, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, lamports, custodyAccountMetas, custodyOracleAccountMetas, _d, _e, custody, whitelistPda, whitelistMeta, params, inx, closeWsolATAIns, err_40;
6573
+ 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) {
6574
+ 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;
6403
6575
  if (useFeesPool === void 0) { useFeesPool = false; }
6404
6576
  if (createUserATA === void 0) { createUserATA = true; }
6405
6577
  if (unWrapSol === void 0) { unWrapSol = false; }
6406
6578
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
6407
6579
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6408
6580
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
6409
- return __generator(this, function (_f) {
6410
- switch (_f.label) {
6581
+ return __generator(this, function (_g) {
6582
+ switch (_g.label) {
6411
6583
  case 0:
6412
6584
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
6413
6585
  if (!userInputCustodyConfig) {
@@ -6417,26 +6589,31 @@ var PerpetualsClient = (function () {
6417
6589
  if (!userOutputCustodyConfig) {
6418
6590
  throw "userOutputCustodyConfig not found";
6419
6591
  }
6420
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6592
+ publicKey = this.provider.wallet.publicKey;
6421
6593
  preInstructions = [];
6422
6594
  instructions = [];
6423
6595
  postInstructions = [];
6424
6596
  additionalSigners = [];
6425
- if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
6597
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 5];
6426
6598
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
6427
6599
  case 1:
6428
- wsolAssociatedTokenAccount = _f.sent();
6429
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID));
6430
- if (!!skipBalanceChecks) return [3, 3];
6600
+ wsolAssociatedTokenAccount = _g.sent();
6601
+ return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
6602
+ case 2:
6603
+ wsolATAExist = _g.sent();
6604
+ if (!wsolATAExist) {
6605
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
6606
+ }
6607
+ if (!!skipBalanceChecks) return [3, 4];
6431
6608
  _a = anchor_1.BN.bind;
6432
6609
  return [4, this.provider.connection.getBalance(publicKey)];
6433
- case 2:
6434
- unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
6610
+ case 3:
6611
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _g.sent()]))();
6435
6612
  if (unWrappedSolBalance.lt(amountIn)) {
6436
6613
  throw "Insufficient SOL Funds";
6437
6614
  }
6438
- _f.label = 3;
6439
- case 3:
6615
+ _g.label = 4;
6616
+ case 4:
6440
6617
  instructions.push(web3_js_1.SystemProgram.transfer({
6441
6618
  fromPubkey: publicKey,
6442
6619
  toPubkey: wsolAssociatedTokenAccount,
@@ -6446,7 +6623,7 @@ var PerpetualsClient = (function () {
6446
6623
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6447
6624
  additionalSigners: additionalSigners
6448
6625
  }];
6449
- case 4:
6626
+ case 5:
6450
6627
  if (userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL') {
6451
6628
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
6452
6629
  wsolAssociatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, publicKey, true);
@@ -6457,20 +6634,20 @@ var PerpetualsClient = (function () {
6457
6634
  additionalSigners: additionalSigners
6458
6635
  }];
6459
6636
  }
6460
- _f.label = 5;
6461
- case 5:
6462
- _f.trys.push([5, 16, , 17]);
6463
- if (!(userInputTokenSymbol == 'SOL')) return [3, 8];
6637
+ _g.label = 6;
6638
+ case 6:
6639
+ _g.trys.push([6, 19, , 20]);
6640
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
6464
6641
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
6465
6642
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
6466
- case 6:
6467
- accCreationLamports = (_f.sent());
6643
+ case 7:
6644
+ accCreationLamports = (_g.sent());
6468
6645
  console.log("accCreationLamports:", accCreationLamports);
6469
6646
  lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
6470
6647
  _b = anchor_1.BN.bind;
6471
6648
  return [4, this.provider.connection.getBalance(publicKey)];
6472
- case 7:
6473
- unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
6649
+ case 8:
6650
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _g.sent()]))();
6474
6651
  if (unWrappedSolBalance.lt(amountIn)) {
6475
6652
  throw "Insufficient SOL Funds";
6476
6653
  }
@@ -6492,25 +6669,25 @@ var PerpetualsClient = (function () {
6492
6669
  postInstructions = [
6493
6670
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6494
6671
  ];
6495
- return [3, 11];
6496
- case 8:
6672
+ return [3, 12];
6673
+ case 9:
6497
6674
  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);
6498
6675
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
6499
- case 9:
6500
- if (!(_f.sent())) {
6676
+ case 10:
6677
+ if (!(_g.sent())) {
6501
6678
  throw "Insufficient Funds , Token Account doesn't exist";
6502
6679
  }
6503
- if (!!skipBalanceChecks) return [3, 11];
6680
+ if (!!skipBalanceChecks) return [3, 12];
6504
6681
  _c = anchor_1.BN.bind;
6505
6682
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
6506
- case 10:
6507
- tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
6683
+ case 11:
6684
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
6508
6685
  if (tokenAccountBalance.lt(amountIn)) {
6509
6686
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
6510
6687
  }
6511
- _f.label = 11;
6512
- case 11:
6513
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 12];
6688
+ _g.label = 12;
6689
+ case 12:
6690
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 13];
6514
6691
  lamports = (this.minimumBalanceForRentExemptAccountLamports);
6515
6692
  if (!ephemeralSignerPubkey) {
6516
6693
  wrappedSolAccount = new web3_js_1.Keypair();
@@ -6530,17 +6707,26 @@ var PerpetualsClient = (function () {
6530
6707
  postInstructions = [
6531
6708
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6532
6709
  ];
6533
- return [3, 14];
6534
- case 12: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID)];
6535
- case 13:
6536
- userOutputTokenAccount = _f.sent();
6537
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6538
- _f.label = 14;
6710
+ return [3, 17];
6711
+ 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)];
6539
6712
  case 14:
6713
+ userOutputTokenAccount = _g.sent();
6714
+ _d = createUserATA;
6715
+ if (!_d) return [3, 16];
6716
+ return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
6717
+ case 15:
6718
+ _d = !(_g.sent());
6719
+ _g.label = 16;
6720
+ case 16:
6721
+ if (_d) {
6722
+ 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));
6723
+ }
6724
+ _g.label = 17;
6725
+ case 17:
6540
6726
  custodyAccountMetas = [];
6541
6727
  custodyOracleAccountMetas = [];
6542
- for (_d = 0, _e = poolConfig.custodies; _d < _e.length; _d++) {
6543
- custody = _e[_d];
6728
+ for (_e = 0, _f = poolConfig.custodies; _e < _f.length; _e++) {
6729
+ custody = _f[_e];
6544
6730
  custodyAccountMetas.push({
6545
6731
  pubkey: custody.custodyAccount,
6546
6732
  isSigner: false,
@@ -6588,19 +6774,19 @@ var PerpetualsClient = (function () {
6588
6774
  })
6589
6775
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
6590
6776
  .instruction()];
6591
- case 15:
6592
- inx = _f.sent();
6777
+ case 18:
6778
+ inx = _g.sent();
6593
6779
  instructions.push(inx);
6594
6780
  if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
6595
6781
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
6596
6782
  instructions.push(closeWsolATAIns);
6597
6783
  }
6598
- return [3, 17];
6599
- case 16:
6600
- err_40 = _f.sent();
6784
+ return [3, 20];
6785
+ case 19:
6786
+ err_40 = _g.sent();
6601
6787
  console.error("perpClient Swap error:: ", err_40);
6602
6788
  throw err_40;
6603
- case 17: return [2, {
6789
+ case 20: return [2, {
6604
6790
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6605
6791
  additionalSigners: additionalSigners
6606
6792
  }];
@@ -7642,11 +7828,15 @@ var PerpetualsClient = (function () {
7642
7828
  additionalSigners = [];
7643
7829
  _a.label = 1;
7644
7830
  case 1:
7645
- _a.trys.push([1, 4, , 5]);
7831
+ _a.trys.push([1, 5, , 6]);
7646
7832
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7647
7833
  case 2:
7648
7834
  receivingTokenAccount = _a.sent();
7649
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7835
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7836
+ case 3:
7837
+ if (!(_a.sent())) {
7838
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7839
+ }
7650
7840
  return [4, this.program.methods
7651
7841
  .withdrawInstantFees({})
7652
7842
  .accounts({
@@ -7661,15 +7851,15 @@ var PerpetualsClient = (function () {
7661
7851
  receivingTokenMint: poolConfig.tokenMint,
7662
7852
  })
7663
7853
  .instruction()];
7664
- case 3:
7854
+ case 4:
7665
7855
  withdrawInstantFeeInstruction = _a.sent();
7666
7856
  instructions.push(withdrawInstantFeeInstruction);
7667
- return [3, 5];
7668
- case 4:
7857
+ return [3, 6];
7858
+ case 5:
7669
7859
  err_57 = _a.sent();
7670
7860
  console.log("perpClient withdrawInstantFeeInstruction error:: ", err_57);
7671
7861
  throw err_57;
7672
- case 5: return [2, {
7862
+ case 6: return [2, {
7673
7863
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7674
7864
  additionalSigners: additionalSigners
7675
7865
  }];
@@ -7688,11 +7878,15 @@ var PerpetualsClient = (function () {
7688
7878
  additionalSigners = [];
7689
7879
  _a.label = 1;
7690
7880
  case 1:
7691
- _a.trys.push([1, 4, , 5]);
7881
+ _a.trys.push([1, 5, , 6]);
7692
7882
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7693
7883
  case 2:
7694
7884
  receivingTokenAccount = _a.sent();
7695
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7885
+ return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7886
+ case 3:
7887
+ if (!(_a.sent())) {
7888
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7889
+ }
7696
7890
  return [4, this.program.methods
7697
7891
  .withdrawUnclaimedTokens({})
7698
7892
  .accounts({
@@ -7707,15 +7901,15 @@ var PerpetualsClient = (function () {
7707
7901
  receivingTokenMint: poolConfig.tokenMint,
7708
7902
  })
7709
7903
  .instruction()];
7710
- case 3:
7904
+ case 4:
7711
7905
  withdrawUnclaimedTokensInstruction = _a.sent();
7712
7906
  instructions.push(withdrawUnclaimedTokensInstruction);
7713
- return [3, 5];
7714
- case 4:
7907
+ return [3, 6];
7908
+ case 5:
7715
7909
  err_58 = _a.sent();
7716
7910
  console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_58);
7717
7911
  throw err_58;
7718
- case 5: return [2, {
7912
+ case 6: return [2, {
7719
7913
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7720
7914
  additionalSigners: additionalSigners
7721
7915
  }];