flash-sdk 11.7.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 = [];
@@ -4526,28 +4584,37 @@ var PerpetualsClient = (function () {
4526
4584
  args_1[_i - 1] = arguments[_i];
4527
4585
  }
4528
4586
  return __awaiter(_this, __spreadArray([poolConfig_1], args_1, true), void 0, function (poolConfig, pendingActivation, deactivated, createUserLPTA, userPublicKey) {
4529
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, pool, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, withdrawStakeInstruction, err_14;
4587
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, pool, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, _a, withdrawStakeInstruction, err_14;
4530
4588
  if (pendingActivation === void 0) { pendingActivation = true; }
4531
4589
  if (deactivated === void 0) { deactivated = true; }
4532
4590
  if (createUserLPTA === void 0) { createUserLPTA = true; }
4533
4591
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4534
- return __generator(this, function (_a) {
4535
- switch (_a.label) {
4592
+ return __generator(this, function (_b) {
4593
+ switch (_b.label) {
4536
4594
  case 0:
4537
4595
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4538
4596
  preInstructions = [];
4539
4597
  instructions = [];
4540
4598
  postInstructions = [];
4541
4599
  additionalSigners = [];
4542
- _a.label = 1;
4600
+ _b.label = 1;
4543
4601
  case 1:
4544
- _a.trys.push([1, 3, , 4]);
4602
+ _b.trys.push([1, 5, , 6]);
4545
4603
  lpTokenMint = poolConfig.stakedLpTokenMint;
4546
4604
  pool = poolConfig.poolAddress;
4547
4605
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), pool.toBuffer(), lpTokenMint.toBuffer()], this.program.programId)[0];
4548
4606
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4549
4607
  userLpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
4550
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userLpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4608
+ _a = createUserLPTA;
4609
+ if (!_a) return [3, 3];
4610
+ return [4, (0, utils_1.checkIfAccountExists)(userLpTokenAccount, this.provider.connection)];
4611
+ case 2:
4612
+ _a = !(_b.sent());
4613
+ _b.label = 3;
4614
+ case 3:
4615
+ if (_a) {
4616
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userLpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4617
+ }
4551
4618
  return [4, this.program.methods
4552
4619
  .withdrawStake({
4553
4620
  pendingActivation: pendingActivation,
@@ -4568,15 +4635,15 @@ var PerpetualsClient = (function () {
4568
4635
  lpMint: poolConfig.stakedLpTokenMint,
4569
4636
  })
4570
4637
  .instruction()];
4571
- case 2:
4572
- withdrawStakeInstruction = _a.sent();
4638
+ case 4:
4639
+ withdrawStakeInstruction = _b.sent();
4573
4640
  instructions.push(withdrawStakeInstruction);
4574
- return [3, 4];
4575
- case 3:
4576
- err_14 = _a.sent();
4641
+ return [3, 6];
4642
+ case 5:
4643
+ err_14 = _b.sent();
4577
4644
  console.log("perpClient withdrawStake error:: ", err_14);
4578
4645
  throw err_14;
4579
- case 4: return [2, {
4646
+ case 6: return [2, {
4580
4647
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4581
4648
  additionalSigners: additionalSigners
4582
4649
  }];
@@ -4590,10 +4657,10 @@ var PerpetualsClient = (function () {
4590
4657
  args_1[_i - 3] = arguments[_i];
4591
4658
  }
4592
4659
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4593
- 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;
4594
4661
  if (createUserATA === void 0) { createUserATA = true; }
4595
- return __generator(this, function (_a) {
4596
- switch (_a.label) {
4662
+ return __generator(this, function (_b) {
4663
+ switch (_b.label) {
4597
4664
  case 0:
4598
4665
  publicKey = this.provider.wallet.publicKey;
4599
4666
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
@@ -4602,13 +4669,22 @@ var PerpetualsClient = (function () {
4602
4669
  instructions = [];
4603
4670
  postInstructions = [];
4604
4671
  additionalSigners = [];
4605
- _a.label = 1;
4672
+ _b.label = 1;
4606
4673
  case 1:
4607
- _a.trys.push([1, 3, , 4]);
4674
+ _b.trys.push([1, 5, , 6]);
4608
4675
  pool = poolConfig.poolAddress;
4609
4676
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4610
4677
  receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
4611
- 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
+ }
4612
4688
  tokenStakeAccounts = [];
4613
4689
  if (tokenStakeAccount) {
4614
4690
  tokenStakeAccounts.push({
@@ -4637,15 +4713,15 @@ var PerpetualsClient = (function () {
4637
4713
  })
4638
4714
  .remainingAccounts(__spreadArray([], tokenStakeAccounts, true))
4639
4715
  .instruction()];
4640
- case 2:
4641
- withdrawStakeInstruction = _a.sent();
4716
+ case 4:
4717
+ withdrawStakeInstruction = _b.sent();
4642
4718
  instructions.push(withdrawStakeInstruction);
4643
- return [3, 4];
4644
- case 3:
4645
- err_15 = _a.sent();
4719
+ return [3, 6];
4720
+ case 5:
4721
+ err_15 = _b.sent();
4646
4722
  console.log("perpClient withdrawStake error:: ", err_15);
4647
4723
  throw err_15;
4648
- case 4: return [2, {
4724
+ case 6: return [2, {
4649
4725
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4650
4726
  additionalSigners: additionalSigners
4651
4727
  }];
@@ -4704,21 +4780,29 @@ var PerpetualsClient = (function () {
4704
4780
  isWritable: false,
4705
4781
  });
4706
4782
  }
4707
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4708
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4709
- 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];
4710
4794
  console.log("inTokenSymbol === SOL", inTokenSymbol);
4711
4795
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
4712
- if (!!skipBalanceChecks) return [3, 2];
4796
+ if (!!skipBalanceChecks) return [3, 4];
4713
4797
  _e = anchor_1.BN.bind;
4714
4798
  return [4, this.provider.connection.getBalance(publicKey)];
4715
- case 1:
4799
+ case 3:
4716
4800
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _f.sent()]))();
4717
4801
  if (unWrappedSolBalance.lt(lamports)) {
4718
4802
  throw "Insufficient SOL Funds";
4719
4803
  }
4720
- _f.label = 2;
4721
- case 2:
4804
+ _f.label = 4;
4805
+ case 4:
4722
4806
  if (!ephemeralSignerPubkey) {
4723
4807
  wrappedSolAccount = new web3_js_1.Keypair();
4724
4808
  additionalSigners.push(wrappedSolAccount);
@@ -4736,17 +4820,17 @@ var PerpetualsClient = (function () {
4736
4820
  postInstructions = [
4737
4821
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4738
4822
  ];
4739
- return [3, 5];
4740
- case 3:
4741
- if (!!skipBalanceChecks) return [3, 5];
4823
+ return [3, 7];
4824
+ case 5:
4825
+ if (!!skipBalanceChecks) return [3, 7];
4742
4826
  return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
4743
- case 4:
4827
+ case 6:
4744
4828
  if (!(_f.sent())) {
4745
4829
  throw "Insufficient Funds , token Account doesn't exist";
4746
4830
  }
4747
- _f.label = 5;
4748
- case 5:
4749
- _f.trys.push([5, 7, , 8]);
4831
+ _f.label = 7;
4832
+ case 7:
4833
+ _f.trys.push([7, 9, , 10]);
4750
4834
  if (enableHeapSizeIx) {
4751
4835
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4752
4836
  bytes: 64 * 1024,
@@ -4788,15 +4872,15 @@ var PerpetualsClient = (function () {
4788
4872
  })
4789
4873
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4790
4874
  .instruction()];
4791
- case 6:
4875
+ case 8:
4792
4876
  addCompoundingLiquidity = _f.sent();
4793
4877
  instructions.push(addCompoundingLiquidity);
4794
- return [3, 8];
4795
- case 7:
4878
+ return [3, 10];
4879
+ case 9:
4796
4880
  err_16 = _f.sent();
4797
4881
  console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4798
- return [3, 8];
4799
- case 8: return [2, {
4882
+ return [3, 10];
4883
+ case 10: return [2, {
4800
4884
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4801
4885
  additionalSigners: additionalSigners
4802
4886
  }];
@@ -4810,14 +4894,14 @@ var PerpetualsClient = (function () {
4810
4894
  args_1[_i - 5] = arguments[_i];
4811
4895
  }
4812
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) {
4813
- 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;
4814
4898
  if (createUserATA === void 0) { createUserATA = true; }
4815
4899
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4816
4900
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4817
4901
  if (enableHeapSizeIx === void 0) { enableHeapSizeIx = true; }
4818
4902
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
4819
- return __generator(this, function (_e) {
4820
- switch (_e.label) {
4903
+ return __generator(this, function (_f) {
4904
+ switch (_f.label) {
4821
4905
  case 0:
4822
4906
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4823
4907
  preInstructions = [];
@@ -4828,35 +4912,45 @@ var PerpetualsClient = (function () {
4828
4912
  outCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outTokenSymbol).mintKey); });
4829
4913
  lpTokenMint = poolConfig.stakedLpTokenMint;
4830
4914
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4831
- if (outCustodyConfig.symbol == 'SOL') {
4832
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4833
- if (!ephemeralSignerPubkey) {
4834
- wrappedSolAccount = new web3_js_1.Keypair();
4835
- additionalSigners.push(wrappedSolAccount);
4836
- }
4837
- preInstructions = [
4838
- web3_js_1.SystemProgram.createAccount({
4839
- fromPubkey: publicKey,
4840
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4841
- lamports: lamports,
4842
- space: 165,
4843
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4844
- }),
4845
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4846
- ];
4847
- postInstructions = [
4848
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4849
- ];
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);
4850
4920
  }
4851
- else {
4852
- 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);
4853
- 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));
4854
4946
  }
4947
+ _f.label = 4;
4948
+ case 4:
4855
4949
  custodyAccountMetas = [];
4856
4950
  custodyOracleAccountMetas = [];
4857
4951
  markets = [];
4858
- for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
4859
- custody = _b[_a];
4952
+ for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4953
+ custody = _c[_b];
4860
4954
  custodyAccountMetas.push({
4861
4955
  pubkey: custody.custodyAccount,
4862
4956
  isSigner: false,
@@ -4868,8 +4962,8 @@ var PerpetualsClient = (function () {
4868
4962
  isWritable: false,
4869
4963
  });
4870
4964
  }
4871
- for (_c = 0, _d = poolConfig.markets; _c < _d.length; _c++) {
4872
- market = _d[_c];
4965
+ for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4966
+ market = _e[_d];
4873
4967
  markets.push({
4874
4968
  pubkey: market.marketAccount,
4875
4969
  isSigner: false,
@@ -4877,9 +4971,9 @@ var PerpetualsClient = (function () {
4877
4971
  });
4878
4972
  }
4879
4973
  compoundingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4880
- _e.label = 1;
4881
- case 1:
4882
- _e.trys.push([1, 3, , 4]);
4974
+ _f.label = 5;
4975
+ case 5:
4976
+ _f.trys.push([5, 7, , 8]);
4883
4977
  if (enableHeapSizeIx) {
4884
4978
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4885
4979
  bytes: 64 * 1024,
@@ -4921,15 +5015,15 @@ var PerpetualsClient = (function () {
4921
5015
  })
4922
5016
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4923
5017
  .instruction()];
4924
- case 2:
4925
- removeCompoundingLiquidity = _e.sent();
5018
+ case 6:
5019
+ removeCompoundingLiquidity = _f.sent();
4926
5020
  instructions.push(removeCompoundingLiquidity);
4927
- return [3, 4];
4928
- case 3:
4929
- err_17 = _e.sent();
5021
+ return [3, 8];
5022
+ case 7:
5023
+ err_17 = _f.sent();
4930
5024
  console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
4931
- return [3, 4];
4932
- case 4: return [2, {
5025
+ return [3, 8];
5026
+ case 8: return [2, {
4933
5027
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4934
5028
  additionalSigners: additionalSigners
4935
5029
  }];
@@ -4942,13 +5036,13 @@ var PerpetualsClient = (function () {
4942
5036
  for (var _i = 3; _i < arguments.length; _i++) {
4943
5037
  args_1[_i - 3] = arguments[_i];
4944
5038
  }
4945
- return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA, userPublicKey) {
4946
- 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;
4947
5041
  if (createUserATA === void 0) { createUserATA = true; }
4948
- return __generator(this, function (_f) {
4949
- switch (_f.label) {
5042
+ return __generator(this, function (_g) {
5043
+ switch (_g.label) {
4950
5044
  case 0:
4951
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5045
+ publicKey = this.provider.wallet.publicKey;
4952
5046
  preInstructions = [];
4953
5047
  instructions = [];
4954
5048
  postInstructions = [];
@@ -4957,18 +5051,27 @@ var PerpetualsClient = (function () {
4957
5051
  lpTokenMint = poolConfig.stakedLpTokenMint;
4958
5052
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4959
5053
  compoudingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4960
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4961
- flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
4962
- tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
4963
- tokenStakeAccounts = [];
4964
- _a = tokenStakeAccount;
5054
+ _a = createUserATA;
4965
5055
  if (!_a) return [3, 2];
4966
- return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5056
+ return [4, (0, utils_1.checkIfAccountExists)(compoudingTokenAccount, this.provider.connection)];
4967
5057
  case 1:
4968
- _a = (_f.sent());
4969
- _f.label = 2;
5058
+ _a = !(_g.sent());
5059
+ _g.label = 2;
4970
5060
  case 2:
4971
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) {
4972
5075
  tokenStakeAccounts.push({
4973
5076
  pubkey: tokenStakeAccount,
4974
5077
  isSigner: false,
@@ -4979,8 +5082,8 @@ var PerpetualsClient = (function () {
4979
5082
  custodyAccountMetas = [];
4980
5083
  custodyOracleAccountMetas = [];
4981
5084
  markets = [];
4982
- for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4983
- custody = _c[_b];
5085
+ for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
5086
+ custody = _d[_c];
4984
5087
  custodyAccountMetas.push({
4985
5088
  pubkey: custody.custodyAccount,
4986
5089
  isSigner: false,
@@ -4992,17 +5095,17 @@ var PerpetualsClient = (function () {
4992
5095
  isWritable: false,
4993
5096
  });
4994
5097
  }
4995
- for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4996
- market = _e[_d];
5098
+ for (_e = 0, _f = poolConfig.markets; _e < _f.length; _e++) {
5099
+ market = _f[_e];
4997
5100
  markets.push({
4998
5101
  pubkey: market.marketAccount,
4999
5102
  isSigner: false,
5000
5103
  isWritable: false,
5001
5104
  });
5002
5105
  }
5003
- _f.label = 3;
5004
- case 3:
5005
- _f.trys.push([3, 5, , 6]);
5106
+ _g.label = 5;
5107
+ case 5:
5108
+ _g.trys.push([5, 7, , 8]);
5006
5109
  return [4, this.program.methods
5007
5110
  .migrateStake({
5008
5111
  amount: amount
@@ -5027,15 +5130,15 @@ var PerpetualsClient = (function () {
5027
5130
  })
5028
5131
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), tokenStakeAccounts, true))
5029
5132
  .instruction()];
5030
- case 4:
5031
- migrateStake = _f.sent();
5133
+ case 6:
5134
+ migrateStake = _g.sent();
5032
5135
  instructions.push(migrateStake);
5033
- return [3, 6];
5034
- case 5:
5035
- err_18 = _f.sent();
5136
+ return [3, 8];
5137
+ case 7:
5138
+ err_18 = _g.sent();
5036
5139
  console.log("perpClient migrateStake error:: ", err_18);
5037
- return [3, 6];
5038
- case 6: return [2, {
5140
+ return [3, 8];
5141
+ case 8: return [2, {
5039
5142
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5040
5143
  additionalSigners: additionalSigners
5041
5144
  }];
@@ -5043,12 +5146,12 @@ var PerpetualsClient = (function () {
5043
5146
  });
5044
5147
  });
5045
5148
  };
5046
- 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 () {
5047
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;
5048
5151
  return __generator(this, function (_d) {
5049
5152
  switch (_d.label) {
5050
5153
  case 0:
5051
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5154
+ publicKey = this.provider.wallet.publicKey;
5052
5155
  preInstructions = [];
5053
5156
  instructions = [];
5054
5157
  postInstructions = [];
@@ -5212,10 +5315,14 @@ var PerpetualsClient = (function () {
5212
5315
  additionalSigners = [];
5213
5316
  _a.label = 1;
5214
5317
  case 1:
5215
- _a.trys.push([1, 3, , 4]);
5318
+ _a.trys.push([1, 4, , 5]);
5216
5319
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5217
5320
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5218
- 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
+ }
5219
5326
  return [4, this.program.methods
5220
5327
  .depositTokenStake({
5221
5328
  depositAmount: depositAmount
@@ -5235,15 +5342,15 @@ var PerpetualsClient = (function () {
5235
5342
  tokenMint: poolConfig.tokenMint,
5236
5343
  })
5237
5344
  .instruction()];
5238
- case 2:
5345
+ case 3:
5239
5346
  depositTokenStakeInstruction = _a.sent();
5240
5347
  instructions.push(depositTokenStakeInstruction);
5241
- return [3, 4];
5242
- case 3:
5348
+ return [3, 5];
5349
+ case 4:
5243
5350
  err_21 = _a.sent();
5244
5351
  console.log("perpClient depositStakingInstruction error:: ", err_21);
5245
5352
  throw err_21;
5246
- case 4: return [2, {
5353
+ case 5: return [2, {
5247
5354
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5248
5355
  additionalSigners: additionalSigners
5249
5356
  }];
@@ -5301,10 +5408,14 @@ var PerpetualsClient = (function () {
5301
5408
  additionalSigners = [];
5302
5409
  _a.label = 1;
5303
5410
  case 1:
5304
- _a.trys.push([1, 3, , 4]);
5411
+ _a.trys.push([1, 4, , 5]);
5305
5412
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5306
5413
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5307
- 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
+ }
5308
5419
  return [4, this.program.methods
5309
5420
  .unstakeTokenInstant({
5310
5421
  unstakeAmount: unstakeAmount
@@ -5323,15 +5434,15 @@ var PerpetualsClient = (function () {
5323
5434
  tokenMint: poolConfig.tokenMint,
5324
5435
  })
5325
5436
  .instruction()];
5326
- case 2:
5437
+ case 3:
5327
5438
  unstakeTokenInstantInstruction = _a.sent();
5328
5439
  instructions.push(unstakeTokenInstantInstruction);
5329
- return [3, 4];
5330
- case 3:
5440
+ return [3, 5];
5441
+ case 4:
5331
5442
  err_23 = _a.sent();
5332
5443
  console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5333
5444
  throw err_23;
5334
- case 4: return [2, {
5445
+ case 5: return [2, {
5335
5446
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5336
5447
  additionalSigners: additionalSigners
5337
5448
  }];
@@ -5349,10 +5460,14 @@ var PerpetualsClient = (function () {
5349
5460
  additionalSigners = [];
5350
5461
  _a.label = 1;
5351
5462
  case 1:
5352
- _a.trys.push([1, 3, , 4]);
5463
+ _a.trys.push([1, 4, , 5]);
5353
5464
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5354
5465
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5355
- 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
+ }
5356
5471
  return [4, this.program.methods
5357
5472
  .withdrawToken({
5358
5473
  withdrawRequestId: withdrawRequestId
@@ -5371,15 +5486,15 @@ var PerpetualsClient = (function () {
5371
5486
  tokenMint: poolConfig.tokenMint,
5372
5487
  })
5373
5488
  .instruction()];
5374
- case 2:
5489
+ case 3:
5375
5490
  withdrawTokenInstruction = _a.sent();
5376
5491
  instructions.push(withdrawTokenInstruction);
5377
- return [3, 4];
5378
- case 3:
5492
+ return [3, 5];
5493
+ case 4:
5379
5494
  err_24 = _a.sent();
5380
5495
  console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5381
5496
  throw err_24;
5382
- case 4: return [2, {
5497
+ case 5: return [2, {
5383
5498
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5384
5499
  additionalSigners: additionalSigners
5385
5500
  }];
@@ -5431,23 +5546,32 @@ var PerpetualsClient = (function () {
5431
5546
  for (var _i = 2; _i < arguments.length; _i++) {
5432
5547
  args_1[_i - 2] = arguments[_i];
5433
5548
  }
5434
- return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA, userPublicKey) {
5435
- 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;
5436
5551
  if (createUserATA === void 0) { createUserATA = true; }
5437
- return __generator(this, function (_a) {
5438
- switch (_a.label) {
5552
+ return __generator(this, function (_b) {
5553
+ switch (_b.label) {
5439
5554
  case 0:
5440
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5555
+ publicKey = this.provider.wallet.publicKey;
5441
5556
  preInstructions = [];
5442
5557
  instructions = [];
5443
5558
  postInstructions = [];
5444
5559
  additionalSigners = [];
5445
- _a.label = 1;
5560
+ _b.label = 1;
5446
5561
  case 1:
5447
- _a.trys.push([1, 3, , 4]);
5562
+ _b.trys.push([1, 5, , 6]);
5448
5563
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5449
5564
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5450
- 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
+ }
5451
5575
  return [4, this.program.methods
5452
5576
  .collectTokenReward({})
5453
5577
  .accounts({
@@ -5464,15 +5588,15 @@ var PerpetualsClient = (function () {
5464
5588
  tokenMint: poolConfig.tokenMint,
5465
5589
  })
5466
5590
  .instruction()];
5467
- case 2:
5468
- collectTokenRewardInstruction = _a.sent();
5591
+ case 4:
5592
+ collectTokenRewardInstruction = _b.sent();
5469
5593
  instructions.push(collectTokenRewardInstruction);
5470
- return [3, 4];
5471
- case 3:
5472
- err_26 = _a.sent();
5594
+ return [3, 6];
5595
+ case 5:
5596
+ err_26 = _b.sent();
5473
5597
  console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5474
5598
  throw err_26;
5475
- case 4: return [2, {
5599
+ case 6: return [2, {
5476
5600
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5477
5601
  additionalSigners: additionalSigners
5478
5602
  }];
@@ -5486,23 +5610,32 @@ var PerpetualsClient = (function () {
5486
5610
  args_1[_i - 3] = arguments[_i];
5487
5611
  }
5488
5612
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5489
- 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;
5490
5614
  if (createUserATA === void 0) { createUserATA = true; }
5491
- return __generator(this, function (_a) {
5492
- switch (_a.label) {
5615
+ return __generator(this, function (_b) {
5616
+ switch (_b.label) {
5493
5617
  case 0:
5494
5618
  publicKey = this.provider.wallet.publicKey;
5495
5619
  preInstructions = [];
5496
5620
  instructions = [];
5497
5621
  postInstructions = [];
5498
5622
  additionalSigners = [];
5499
- _a.label = 1;
5623
+ _b.label = 1;
5500
5624
  case 1:
5501
- _a.trys.push([1, 3, , 4]);
5625
+ _b.trys.push([1, 5, , 6]);
5502
5626
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5503
5627
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5504
5628
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, owner, true);
5505
- 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
+ }
5506
5639
  return [4, this.program.methods
5507
5640
  .collectRevenue({})
5508
5641
  .accounts({
@@ -5519,15 +5652,15 @@ var PerpetualsClient = (function () {
5519
5652
  receivingTokenMint: rewardCustodyMint,
5520
5653
  })
5521
5654
  .instruction()];
5522
- case 2:
5523
- collectRevenueInstruction = _a.sent();
5655
+ case 4:
5656
+ collectRevenueInstruction = _b.sent();
5524
5657
  instructions.push(collectRevenueInstruction);
5525
- return [3, 4];
5526
- case 3:
5527
- err_27 = _a.sent();
5658
+ return [3, 6];
5659
+ case 5:
5660
+ err_27 = _b.sent();
5528
5661
  console.log("perpClient collectRevenueInstruction error:: ", err_27);
5529
5662
  throw err_27;
5530
- case 4: return [2, {
5663
+ case 6: return [2, {
5531
5664
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5532
5665
  additionalSigners: additionalSigners
5533
5666
  }];
@@ -5541,23 +5674,32 @@ var PerpetualsClient = (function () {
5541
5674
  args_1[_i - 3] = arguments[_i];
5542
5675
  }
5543
5676
  return __awaiter(_this, __spreadArray([owner_1, rebateSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rebateSymbol, poolConfig, createUserATA) {
5544
- 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;
5545
5678
  if (createUserATA === void 0) { createUserATA = true; }
5546
- return __generator(this, function (_a) {
5547
- switch (_a.label) {
5679
+ return __generator(this, function (_b) {
5680
+ switch (_b.label) {
5548
5681
  case 0:
5549
5682
  publicKey = this.provider.wallet.publicKey;
5550
5683
  preInstructions = [];
5551
5684
  instructions = [];
5552
5685
  postInstructions = [];
5553
5686
  additionalSigners = [];
5554
- _a.label = 1;
5687
+ _b.label = 1;
5555
5688
  case 1:
5556
- _a.trys.push([1, 3, , 4]);
5689
+ _b.trys.push([1, 5, , 6]);
5557
5690
  rebateMint = poolConfig.getTokenFromSymbol(rebateSymbol).mintKey;
5558
5691
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5559
5692
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rebateMint, owner, true);
5560
- 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
+ }
5561
5703
  return [4, this.program.methods
5562
5704
  .collectRebate()
5563
5705
  .accounts({
@@ -5574,15 +5716,15 @@ var PerpetualsClient = (function () {
5574
5716
  receivingTokenMint: rebateMint,
5575
5717
  })
5576
5718
  .instruction()];
5577
- case 2:
5578
- collectRebateInstruction = _a.sent();
5719
+ case 4:
5720
+ collectRebateInstruction = _b.sent();
5579
5721
  instructions.push(collectRebateInstruction);
5580
- return [3, 4];
5581
- case 3:
5582
- err_28 = _a.sent();
5722
+ return [3, 6];
5723
+ case 5:
5724
+ err_28 = _b.sent();
5583
5725
  console.log("perpClient collectRebateInstruction error:: ", err_28);
5584
5726
  throw err_28;
5585
- case 4: return [2, {
5727
+ case 6: return [2, {
5586
5728
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5587
5729
  additionalSigners: additionalSigners
5588
5730
  }];
@@ -5642,14 +5784,14 @@ var PerpetualsClient = (function () {
5642
5784
  for (var _i = 11; _i < arguments.length; _i++) {
5643
5785
  args_1[_i - 11] = arguments[_i];
5644
5786
  }
5645
- 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) {
5646
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;
5647
5789
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5648
5790
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5649
5791
  return __generator(this, function (_c) {
5650
5792
  switch (_c.label) {
5651
5793
  case 0:
5652
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5794
+ publicKey = this.provider.wallet.publicKey;
5653
5795
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5654
5796
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5655
5797
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5765,14 +5907,14 @@ var PerpetualsClient = (function () {
5765
5907
  for (var _i = 11; _i < arguments.length; _i++) {
5766
5908
  args_1[_i - 11] = arguments[_i];
5767
5909
  }
5768
- 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) {
5769
- 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;
5770
5912
  if (createUserATA === void 0) { createUserATA = true; }
5771
5913
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5772
- return __generator(this, function (_a) {
5773
- switch (_a.label) {
5914
+ return __generator(this, function (_b) {
5915
+ switch (_b.label) {
5774
5916
  case 0:
5775
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5917
+ publicKey = this.provider.wallet.publicKey;
5776
5918
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5777
5919
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5778
5920
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5782,33 +5924,43 @@ var PerpetualsClient = (function () {
5782
5924
  instructions = [];
5783
5925
  postInstructions = [];
5784
5926
  additionalSigners = [];
5785
- _a.label = 1;
5927
+ _b.label = 1;
5786
5928
  case 1:
5787
- _a.trys.push([1, 3, , 4]);
5788
- if (reserveSymbol == 'SOL') {
5789
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
5790
- if (!ephemeralSignerPubkey) {
5791
- wrappedSolAccount = new web3_js_1.Keypair();
5792
- additionalSigners.push(wrappedSolAccount);
5793
- }
5794
- preInstructions = [
5795
- web3_js_1.SystemProgram.createAccount({
5796
- fromPubkey: publicKey,
5797
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5798
- lamports: lamports,
5799
- space: 165,
5800
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5801
- }),
5802
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5803
- ];
5804
- postInstructions = [
5805
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5806
- ];
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);
5807
5935
  }
5808
- else {
5809
- 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);
5810
- 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));
5811
5961
  }
5962
+ _b.label = 5;
5963
+ case 5:
5812
5964
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
5813
5965
  orderAccount = poolConfig.getOrderFromMarketPk(publicKey, marketAccount);
5814
5966
  return [4, this.program.methods
@@ -5842,15 +5994,15 @@ var PerpetualsClient = (function () {
5842
5994
  receivingMint: poolConfig.getTokenFromSymbol(reserveSymbol).mintKey
5843
5995
  })
5844
5996
  .instruction()];
5845
- case 2:
5846
- editLimitOrder = _a.sent();
5997
+ case 6:
5998
+ editLimitOrder = _b.sent();
5847
5999
  instructions.push(editLimitOrder);
5848
- return [3, 4];
5849
- case 3:
5850
- err_31 = _a.sent();
6000
+ return [3, 8];
6001
+ case 7:
6002
+ err_31 = _b.sent();
5851
6003
  console.log("perpClient editLimitOrder error:: ", err_31);
5852
6004
  throw err_31;
5853
- case 4: return [2, {
6005
+ case 8: return [2, {
5854
6006
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5855
6007
  additionalSigners: additionalSigners
5856
6008
  }];
@@ -5999,12 +6151,12 @@ var PerpetualsClient = (function () {
5999
6151
  });
6000
6152
  });
6001
6153
  };
6002
- 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 () {
6003
6155
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_34;
6004
6156
  return __generator(this, function (_a) {
6005
6157
  switch (_a.label) {
6006
6158
  case 0:
6007
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6159
+ publicKey = this.provider.wallet.publicKey;
6008
6160
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6009
6161
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6010
6162
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6058,12 +6210,12 @@ var PerpetualsClient = (function () {
6058
6210
  }
6059
6211
  });
6060
6212
  }); };
6061
- 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 () {
6062
6214
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_35;
6063
6215
  return __generator(this, function (_a) {
6064
6216
  switch (_a.label) {
6065
6217
  case 0:
6066
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6218
+ publicKey = this.provider.wallet.publicKey;
6067
6219
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6068
6220
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6069
6221
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6116,12 +6268,12 @@ var PerpetualsClient = (function () {
6116
6268
  }
6117
6269
  });
6118
6270
  }); };
6119
- 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 () {
6120
6272
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_36;
6121
6273
  return __generator(this, function (_a) {
6122
6274
  switch (_a.label) {
6123
6275
  case 0:
6124
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6276
+ publicKey = this.provider.wallet.publicKey;
6125
6277
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6126
6278
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6127
6279
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6160,12 +6312,12 @@ var PerpetualsClient = (function () {
6160
6312
  }
6161
6313
  });
6162
6314
  }); };
6163
- 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 () {
6164
6316
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_37;
6165
6317
  return __generator(this, function (_a) {
6166
6318
  switch (_a.label) {
6167
6319
  case 0:
6168
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6320
+ publicKey = this.provider.wallet.publicKey;
6169
6321
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6170
6322
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6171
6323
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6208,13 +6360,13 @@ var PerpetualsClient = (function () {
6208
6360
  args_1[_i - 9] = arguments[_i];
6209
6361
  }
6210
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) {
6211
- 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;
6212
6364
  if (createUserATA === void 0) { createUserATA = true; }
6213
6365
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6214
6366
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6215
6367
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6216
- return __generator(this, function (_c) {
6217
- switch (_c.label) {
6368
+ return __generator(this, function (_e) {
6369
+ switch (_e.label) {
6218
6370
  case 0:
6219
6371
  payerPubkey = this.provider.wallet.publicKey;
6220
6372
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6227,23 +6379,42 @@ var PerpetualsClient = (function () {
6227
6379
  additionalSigners = [];
6228
6380
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
6229
6381
  receivingToken = poolConfig.getTokenFromSymbol(receivingSymbol);
6230
- _c.label = 1;
6382
+ _e.label = 1;
6231
6383
  case 1:
6232
- _c.trys.push([1, 3, , 4]);
6233
- 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));
6234
6398
  }
6235
- else {
6236
- 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);
6237
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, spl_token_1.TOKEN_PROGRAM_ID));
6238
- 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);
6239
- 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));
6240
6409
  }
6410
+ _e.label = 7;
6411
+ case 7:
6241
6412
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6242
6413
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6243
6414
  custodyAccountMetas = [];
6244
6415
  custodyOracleAccountMetas = [];
6245
- for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
6246
- custody = _b[_a];
6416
+ for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
6417
+ custody = _d[_c];
6247
6418
  custodyAccountMetas.push({
6248
6419
  pubkey: custody.custodyAccount,
6249
6420
  isSigner: false,
@@ -6290,15 +6461,15 @@ var PerpetualsClient = (function () {
6290
6461
  })
6291
6462
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6292
6463
  .instruction()];
6293
- case 2:
6294
- executeTriggerWithSwap = _c.sent();
6464
+ case 8:
6465
+ executeTriggerWithSwap = _e.sent();
6295
6466
  instructions.push(executeTriggerWithSwap);
6296
- return [3, 4];
6297
- case 3:
6298
- err_38 = _c.sent();
6467
+ return [3, 10];
6468
+ case 9:
6469
+ err_38 = _e.sent();
6299
6470
  console.log("perpClient executeTriggerWithSwap error:: ", err_38);
6300
6471
  throw err_38;
6301
- case 4: return [2, {
6472
+ case 10: return [2, {
6302
6473
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6303
6474
  additionalSigners: additionalSigners
6304
6475
  }];
@@ -6312,13 +6483,13 @@ var PerpetualsClient = (function () {
6312
6483
  args_1[_i - 8] = arguments[_i];
6313
6484
  }
6314
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) {
6315
- 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;
6316
6487
  if (createUserATA === void 0) { createUserATA = true; }
6317
6488
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6318
6489
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6319
6490
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6320
- return __generator(this, function (_a) {
6321
- switch (_a.label) {
6491
+ return __generator(this, function (_b) {
6492
+ switch (_b.label) {
6322
6493
  case 0:
6323
6494
  payerPubkey = this.provider.wallet.publicKey;
6324
6495
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6328,15 +6499,25 @@ var PerpetualsClient = (function () {
6328
6499
  instructions = [];
6329
6500
  postInstructions = [];
6330
6501
  additionalSigners = [];
6331
- _a.label = 1;
6502
+ _b.label = 1;
6332
6503
  case 1:
6333
- _a.trys.push([1, 3, , 4]);
6334
- if (false) {
6335
- }
6336
- else {
6337
- 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);
6338
- 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));
6339
6518
  }
6519
+ _b.label = 5;
6520
+ case 5:
6340
6521
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6341
6522
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6342
6523
  return [4, this.program.methods
@@ -6368,15 +6549,15 @@ var PerpetualsClient = (function () {
6368
6549
  })
6369
6550
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6370
6551
  .instruction()];
6371
- case 2:
6372
- executeTriggerOrder = _a.sent();
6552
+ case 6:
6553
+ executeTriggerOrder = _b.sent();
6373
6554
  instructions.push(executeTriggerOrder);
6374
- return [3, 4];
6375
- case 3:
6376
- err_39 = _a.sent();
6555
+ return [3, 8];
6556
+ case 7:
6557
+ err_39 = _b.sent();
6377
6558
  console.log("perpClient executeTriggerOrder error:: ", err_39);
6378
6559
  throw err_39;
6379
- case 4: return [2, {
6560
+ case 8: return [2, {
6380
6561
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6381
6562
  additionalSigners: additionalSigners
6382
6563
  }];
@@ -6389,16 +6570,16 @@ var PerpetualsClient = (function () {
6389
6570
  for (var _i = 5; _i < arguments.length; _i++) {
6390
6571
  args_1[_i - 5] = arguments[_i];
6391
6572
  }
6392
- 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) {
6393
- 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;
6394
6575
  if (useFeesPool === void 0) { useFeesPool = false; }
6395
6576
  if (createUserATA === void 0) { createUserATA = true; }
6396
6577
  if (unWrapSol === void 0) { unWrapSol = false; }
6397
6578
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
6398
6579
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6399
6580
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
6400
- return __generator(this, function (_f) {
6401
- switch (_f.label) {
6581
+ return __generator(this, function (_g) {
6582
+ switch (_g.label) {
6402
6583
  case 0:
6403
6584
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
6404
6585
  if (!userInputCustodyConfig) {
@@ -6408,26 +6589,31 @@ var PerpetualsClient = (function () {
6408
6589
  if (!userOutputCustodyConfig) {
6409
6590
  throw "userOutputCustodyConfig not found";
6410
6591
  }
6411
- publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6592
+ publicKey = this.provider.wallet.publicKey;
6412
6593
  preInstructions = [];
6413
6594
  instructions = [];
6414
6595
  postInstructions = [];
6415
6596
  additionalSigners = [];
6416
- if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
6597
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 5];
6417
6598
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
6418
6599
  case 1:
6419
- wsolAssociatedTokenAccount = _f.sent();
6420
- instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID));
6421
- 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];
6422
6608
  _a = anchor_1.BN.bind;
6423
6609
  return [4, this.provider.connection.getBalance(publicKey)];
6424
- case 2:
6425
- 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()]))();
6426
6612
  if (unWrappedSolBalance.lt(amountIn)) {
6427
6613
  throw "Insufficient SOL Funds";
6428
6614
  }
6429
- _f.label = 3;
6430
- case 3:
6615
+ _g.label = 4;
6616
+ case 4:
6431
6617
  instructions.push(web3_js_1.SystemProgram.transfer({
6432
6618
  fromPubkey: publicKey,
6433
6619
  toPubkey: wsolAssociatedTokenAccount,
@@ -6437,7 +6623,7 @@ var PerpetualsClient = (function () {
6437
6623
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6438
6624
  additionalSigners: additionalSigners
6439
6625
  }];
6440
- case 4:
6626
+ case 5:
6441
6627
  if (userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL') {
6442
6628
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
6443
6629
  wsolAssociatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, publicKey, true);
@@ -6448,20 +6634,20 @@ var PerpetualsClient = (function () {
6448
6634
  additionalSigners: additionalSigners
6449
6635
  }];
6450
6636
  }
6451
- _f.label = 5;
6452
- case 5:
6453
- _f.trys.push([5, 16, , 17]);
6454
- 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];
6455
6641
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
6456
6642
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
6457
- case 6:
6458
- accCreationLamports = (_f.sent());
6643
+ case 7:
6644
+ accCreationLamports = (_g.sent());
6459
6645
  console.log("accCreationLamports:", accCreationLamports);
6460
6646
  lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
6461
6647
  _b = anchor_1.BN.bind;
6462
6648
  return [4, this.provider.connection.getBalance(publicKey)];
6463
- case 7:
6464
- 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()]))();
6465
6651
  if (unWrappedSolBalance.lt(amountIn)) {
6466
6652
  throw "Insufficient SOL Funds";
6467
6653
  }
@@ -6483,25 +6669,25 @@ var PerpetualsClient = (function () {
6483
6669
  postInstructions = [
6484
6670
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6485
6671
  ];
6486
- return [3, 11];
6487
- case 8:
6672
+ return [3, 12];
6673
+ case 9:
6488
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);
6489
6675
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
6490
- case 9:
6491
- if (!(_f.sent())) {
6676
+ case 10:
6677
+ if (!(_g.sent())) {
6492
6678
  throw "Insufficient Funds , Token Account doesn't exist";
6493
6679
  }
6494
- if (!!skipBalanceChecks) return [3, 11];
6680
+ if (!!skipBalanceChecks) return [3, 12];
6495
6681
  _c = anchor_1.BN.bind;
6496
6682
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
6497
- case 10:
6498
- 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]))();
6499
6685
  if (tokenAccountBalance.lt(amountIn)) {
6500
6686
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
6501
6687
  }
6502
- _f.label = 11;
6503
- case 11:
6504
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 12];
6688
+ _g.label = 12;
6689
+ case 12:
6690
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 13];
6505
6691
  lamports = (this.minimumBalanceForRentExemptAccountLamports);
6506
6692
  if (!ephemeralSignerPubkey) {
6507
6693
  wrappedSolAccount = new web3_js_1.Keypair();
@@ -6521,17 +6707,26 @@ var PerpetualsClient = (function () {
6521
6707
  postInstructions = [
6522
6708
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6523
6709
  ];
6524
- return [3, 14];
6525
- 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)];
6526
- case 13:
6527
- userOutputTokenAccount = _f.sent();
6528
- 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));
6529
- _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)];
6530
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:
6531
6726
  custodyAccountMetas = [];
6532
6727
  custodyOracleAccountMetas = [];
6533
- for (_d = 0, _e = poolConfig.custodies; _d < _e.length; _d++) {
6534
- custody = _e[_d];
6728
+ for (_e = 0, _f = poolConfig.custodies; _e < _f.length; _e++) {
6729
+ custody = _f[_e];
6535
6730
  custodyAccountMetas.push({
6536
6731
  pubkey: custody.custodyAccount,
6537
6732
  isSigner: false,
@@ -6579,19 +6774,19 @@ var PerpetualsClient = (function () {
6579
6774
  })
6580
6775
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
6581
6776
  .instruction()];
6582
- case 15:
6583
- inx = _f.sent();
6777
+ case 18:
6778
+ inx = _g.sent();
6584
6779
  instructions.push(inx);
6585
6780
  if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
6586
6781
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
6587
6782
  instructions.push(closeWsolATAIns);
6588
6783
  }
6589
- return [3, 17];
6590
- case 16:
6591
- err_40 = _f.sent();
6784
+ return [3, 20];
6785
+ case 19:
6786
+ err_40 = _g.sent();
6592
6787
  console.error("perpClient Swap error:: ", err_40);
6593
6788
  throw err_40;
6594
- case 17: return [2, {
6789
+ case 20: return [2, {
6595
6790
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6596
6791
  additionalSigners: additionalSigners
6597
6792
  }];
@@ -7633,11 +7828,15 @@ var PerpetualsClient = (function () {
7633
7828
  additionalSigners = [];
7634
7829
  _a.label = 1;
7635
7830
  case 1:
7636
- _a.trys.push([1, 4, , 5]);
7831
+ _a.trys.push([1, 5, , 6]);
7637
7832
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7638
7833
  case 2:
7639
7834
  receivingTokenAccount = _a.sent();
7640
- 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
+ }
7641
7840
  return [4, this.program.methods
7642
7841
  .withdrawInstantFees({})
7643
7842
  .accounts({
@@ -7652,15 +7851,15 @@ var PerpetualsClient = (function () {
7652
7851
  receivingTokenMint: poolConfig.tokenMint,
7653
7852
  })
7654
7853
  .instruction()];
7655
- case 3:
7854
+ case 4:
7656
7855
  withdrawInstantFeeInstruction = _a.sent();
7657
7856
  instructions.push(withdrawInstantFeeInstruction);
7658
- return [3, 5];
7659
- case 4:
7857
+ return [3, 6];
7858
+ case 5:
7660
7859
  err_57 = _a.sent();
7661
7860
  console.log("perpClient withdrawInstantFeeInstruction error:: ", err_57);
7662
7861
  throw err_57;
7663
- case 5: return [2, {
7862
+ case 6: return [2, {
7664
7863
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7665
7864
  additionalSigners: additionalSigners
7666
7865
  }];
@@ -7679,11 +7878,15 @@ var PerpetualsClient = (function () {
7679
7878
  additionalSigners = [];
7680
7879
  _a.label = 1;
7681
7880
  case 1:
7682
- _a.trys.push([1, 4, , 5]);
7881
+ _a.trys.push([1, 5, , 6]);
7683
7882
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7684
7883
  case 2:
7685
7884
  receivingTokenAccount = _a.sent();
7686
- 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
+ }
7687
7890
  return [4, this.program.methods
7688
7891
  .withdrawUnclaimedTokens({})
7689
7892
  .accounts({
@@ -7698,15 +7901,15 @@ var PerpetualsClient = (function () {
7698
7901
  receivingTokenMint: poolConfig.tokenMint,
7699
7902
  })
7700
7903
  .instruction()];
7701
- case 3:
7904
+ case 4:
7702
7905
  withdrawUnclaimedTokensInstruction = _a.sent();
7703
7906
  instructions.push(withdrawUnclaimedTokensInstruction);
7704
- return [3, 5];
7705
- case 4:
7907
+ return [3, 6];
7908
+ case 5:
7706
7909
  err_58 = _a.sent();
7707
7910
  console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_58);
7708
7911
  throw err_58;
7709
- case 5: return [2, {
7912
+ case 6: return [2, {
7710
7913
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7711
7914
  additionalSigners: additionalSigners
7712
7915
  }];