flash-sdk 11.11.2 → 11.11.4-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2700,7 +2700,7 @@ var PerpetualsClient = (function () {
2700
2700
  for (var _i = 8; _i < arguments.length; _i++) {
2701
2701
  args_1[_i - 8] = arguments[_i];
2702
2702
  }
2703
- 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) {
2703
+ 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) {
2704
2704
  var publicKey, targetCustodyConfig, collateralCustodyConfig, collateralToken, marketAccount, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
2705
2705
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2706
2706
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2709,7 +2709,7 @@ var PerpetualsClient = (function () {
2709
2709
  return __generator(this, function (_c) {
2710
2710
  switch (_c.label) {
2711
2711
  case 0:
2712
- publicKey = this.provider.wallet.publicKey;
2712
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2713
2713
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
2714
2714
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2715
2715
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
@@ -2814,59 +2814,48 @@ var PerpetualsClient = (function () {
2814
2814
  for (var _i = 6; _i < arguments.length; _i++) {
2815
2815
  args_1[_i - 6] = arguments[_i];
2816
2816
  }
2817
- 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) {
2818
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2817
+ 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) {
2818
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2819
2819
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2820
2820
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
2821
2821
  if (createUserATA === void 0) { createUserATA = true; }
2822
2822
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
2823
2823
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2824
- return __generator(this, function (_b) {
2825
- switch (_b.label) {
2824
+ return __generator(this, function (_a) {
2825
+ switch (_a.label) {
2826
2826
  case 0:
2827
- console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
2828
- publicKey = this.provider.wallet.publicKey;
2827
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2829
2828
  preInstructions = [];
2830
2829
  instructions = [];
2831
2830
  postInstructions = [];
2832
2831
  additionalSigners = [];
2833
- _b.label = 1;
2832
+ _a.label = 1;
2834
2833
  case 1:
2835
- _b.trys.push([1, 7, , 8]);
2836
- if (!(collateralSymbol == 'SOL')) return [3, 2];
2837
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
2838
- if (!ephemeralSignerPubkey) {
2839
- wrappedSolAccount = new web3_js_1.Keypair();
2840
- additionalSigners.push(wrappedSolAccount);
2834
+ _a.trys.push([1, 3, , 4]);
2835
+ if (collateralSymbol == 'SOL') {
2836
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
2837
+ if (!ephemeralSignerPubkey) {
2838
+ wrappedSolAccount = new web3_js_1.Keypair();
2839
+ additionalSigners.push(wrappedSolAccount);
2840
+ }
2841
+ preInstructions = [
2842
+ web3_js_1.SystemProgram.createAccount({
2843
+ fromPubkey: publicKey,
2844
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2845
+ lamports: lamports,
2846
+ space: 165,
2847
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
2848
+ }),
2849
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2850
+ ];
2851
+ postInstructions = [
2852
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2853
+ ];
2841
2854
  }
2842
- preInstructions = [
2843
- web3_js_1.SystemProgram.createAccount({
2844
- fromPubkey: publicKey,
2845
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2846
- lamports: lamports,
2847
- space: 165,
2848
- programId: spl_token_1.TOKEN_PROGRAM_ID,
2849
- }),
2850
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2851
- ];
2852
- postInstructions = [
2853
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2854
- ];
2855
- return [3, 5];
2856
- case 2:
2857
- 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);
2858
- _a = createUserATA;
2859
- if (!_a) return [3, 4];
2860
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
2861
- case 3:
2862
- _a = !(_b.sent());
2863
- _b.label = 4;
2864
- case 4:
2865
- if (_a) {
2866
- 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));
2855
+ else {
2856
+ 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);
2857
+ 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));
2867
2858
  }
2868
- _b.label = 5;
2869
- case 5:
2870
2859
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2871
2860
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
2872
2861
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -2898,19 +2887,19 @@ var PerpetualsClient = (function () {
2898
2887
  })
2899
2888
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
2900
2889
  .instruction()];
2901
- case 6:
2902
- instruction = _b.sent();
2890
+ case 2:
2891
+ instruction = _a.sent();
2903
2892
  instructions.push(instruction);
2904
2893
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
2905
2894
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
2906
2895
  postInstructions.push(closeWsolATAIns);
2907
2896
  }
2908
- return [3, 8];
2909
- case 7:
2910
- error_1 = _b.sent();
2897
+ return [3, 4];
2898
+ case 3:
2899
+ error_1 = _a.sent();
2911
2900
  console.error("perpclient closePosition error:", error_1);
2912
2901
  throw error_1;
2913
- case 8: return [2, {
2902
+ case 4: return [2, {
2914
2903
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
2915
2904
  additionalSigners: additionalSigners
2916
2905
  }];
@@ -2923,7 +2912,7 @@ var PerpetualsClient = (function () {
2923
2912
  for (var _i = 9; _i < arguments.length; _i++) {
2924
2913
  args_1[_i - 9] = arguments[_i];
2925
2914
  }
2926
- 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) {
2915
+ 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) {
2927
2916
  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;
2928
2917
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2929
2918
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2932,7 +2921,7 @@ var PerpetualsClient = (function () {
2932
2921
  return __generator(this, function (_c) {
2933
2922
  switch (_c.label) {
2934
2923
  case 0:
2935
- publicKey = this.provider.wallet.publicKey;
2924
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2936
2925
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
2937
2926
  if (!userInputCustodyConfig) {
2938
2927
  throw "userInputCustodyConfig not found";
@@ -2956,7 +2945,7 @@ var PerpetualsClient = (function () {
2956
2945
  additionalSigners = [];
2957
2946
  targetToken = poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol);
2958
2947
  userInputToken = poolConfig.getTokenFromSymbol(userInputTokenSymbol);
2959
- if (!(userInputTokenSymbol == 'SOL')) return [3, 5];
2948
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 3];
2960
2949
  console.log("inputSymbol === SOL", userInputTokenSymbol);
2961
2950
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
2962
2951
  if (!!skipBalanceChecks) return [3, 2];
@@ -2986,41 +2975,33 @@ var PerpetualsClient = (function () {
2986
2975
  postInstructions = [
2987
2976
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2988
2977
  ];
2989
- if (!!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) return [3, 4];
2990
- 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);
2991
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
2992
- case 3:
2993
- if (!(_c.sent())) {
2994
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey));
2978
+ if (!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) {
2979
+ 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);
2980
+ 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));
2995
2981
  }
2996
- _c.label = 4;
2997
- case 4: return [3, 10];
2998
- case 5:
2982
+ return [3, 7];
2983
+ case 3:
2999
2984
  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);
3000
- if (!!skipBalanceChecks) return [3, 8];
2985
+ if (!!skipBalanceChecks) return [3, 6];
3001
2986
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3002
- case 6:
2987
+ case 4:
3003
2988
  if (!(_c.sent())) {
3004
2989
  throw "Insufficient Funds , Token Account doesn't exist";
3005
2990
  }
3006
2991
  _b = anchor_1.BN.bind;
3007
2992
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3008
- case 7:
2993
+ case 5:
3009
2994
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
3010
2995
  if (tokenAccountBalance.lt(amountIn)) {
3011
2996
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3012
2997
  }
3013
- _c.label = 8;
3014
- case 8:
2998
+ _c.label = 6;
2999
+ case 6:
3015
3000
  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);
3016
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3017
- case 9:
3018
- if (!(_c.sent())) {
3019
- 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));
3020
- }
3021
- _c.label = 10;
3022
- case 10:
3023
- _c.trys.push([10, 12, , 13]);
3001
+ 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));
3002
+ _c.label = 7;
3003
+ case 7:
3004
+ _c.trys.push([7, 9, , 10]);
3024
3005
  return [4, this.program.methods
3025
3006
  .swapAndOpen({
3026
3007
  amountIn: amountIn,
@@ -3056,15 +3037,15 @@ var PerpetualsClient = (function () {
3056
3037
  })
3057
3038
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3058
3039
  .instruction()];
3059
- case 11:
3040
+ case 8:
3060
3041
  inx = _c.sent();
3061
3042
  instructions.push(inx);
3062
- return [3, 13];
3063
- case 12:
3043
+ return [3, 10];
3044
+ case 9:
3064
3045
  err_3 = _c.sent();
3065
3046
  console.error("perpClient SwapAndOpen error:: ", err_3);
3066
3047
  throw err_3;
3067
- case 13: return [2, {
3048
+ case 10: return [2, {
3068
3049
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3069
3050
  additionalSigners: additionalSigners
3070
3051
  }];
@@ -3077,7 +3058,7 @@ var PerpetualsClient = (function () {
3077
3058
  for (var _i = 7; _i < arguments.length; _i++) {
3078
3059
  args_1[_i - 7] = arguments[_i];
3079
3060
  }
3080
- 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) {
3061
+ 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) {
3081
3062
  var publicKey, userOutputCustodyConfig, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, collateralToken, userOutputToken, lamports, userCollateralTokenAccount, inx, err_4;
3082
3063
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3083
3064
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -3085,7 +3066,7 @@ var PerpetualsClient = (function () {
3085
3066
  return __generator(this, function (_a) {
3086
3067
  switch (_a.label) {
3087
3068
  case 0:
3088
- publicKey = this.provider.wallet.publicKey;
3069
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3089
3070
  userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey); });
3090
3071
  if (!userOutputCustodyConfig) {
3091
3072
  throw "userOutputCustodyConfig not found";
@@ -3109,46 +3090,37 @@ var PerpetualsClient = (function () {
3109
3090
  additionalSigners = [];
3110
3091
  collateralToken = poolConfig.getTokenFromSymbol(collateralTokenSymbol);
3111
3092
  userOutputToken = poolConfig.getTokenFromSymbol(userOutputTokenSymbol);
3112
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 1];
3113
- console.log("outputSymbol === SOL", userOutputTokenSymbol);
3114
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
3115
- if (!ephemeralSignerPubkey) {
3116
- wrappedSolAccount = new web3_js_1.Keypair();
3093
+ if (userOutputTokenSymbol == 'SOL') {
3094
+ console.log("outputSymbol === SOL", userOutputTokenSymbol);
3095
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
3096
+ if (!ephemeralSignerPubkey) {
3097
+ wrappedSolAccount = new web3_js_1.Keypair();
3098
+ additionalSigners.push(wrappedSolAccount);
3099
+ }
3100
+ preInstructions = [
3101
+ web3_js_1.SystemProgram.createAccount({
3102
+ fromPubkey: publicKey,
3103
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3104
+ lamports: lamports,
3105
+ space: 165,
3106
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3107
+ }),
3108
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3109
+ ];
3110
+ postInstructions = [
3111
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3112
+ ];
3117
3113
  additionalSigners.push(wrappedSolAccount);
3118
3114
  }
3119
- preInstructions = [
3120
- web3_js_1.SystemProgram.createAccount({
3121
- fromPubkey: publicKey,
3122
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3123
- lamports: lamports,
3124
- space: 165,
3125
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3126
- }),
3127
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3128
- ];
3129
- postInstructions = [
3130
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3131
- ];
3132
- additionalSigners.push(wrappedSolAccount);
3133
- return [3, 3];
3134
- case 1:
3135
- 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);
3136
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3137
- case 2:
3138
- if (!(_a.sent())) {
3139
- 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));
3115
+ else {
3116
+ 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);
3117
+ 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));
3140
3118
  }
3141
- _a.label = 3;
3142
- case 3:
3143
3119
  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);
3144
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3145
- case 4:
3146
- if (!(_a.sent())) {
3147
- 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));
3148
- }
3149
- _a.label = 5;
3150
- case 5:
3151
- _a.trys.push([5, 7, , 8]);
3120
+ 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));
3121
+ _a.label = 1;
3122
+ case 1:
3123
+ _a.trys.push([1, 3, , 4]);
3152
3124
  return [4, this.program.methods
3153
3125
  .closeAndSwap({
3154
3126
  priceWithSlippage: priceWithSlippage,
@@ -3182,15 +3154,15 @@ var PerpetualsClient = (function () {
3182
3154
  })
3183
3155
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3184
3156
  .instruction()];
3185
- case 6:
3157
+ case 2:
3186
3158
  inx = _a.sent();
3187
3159
  instructions.push(inx);
3188
- return [3, 8];
3189
- case 7:
3160
+ return [3, 4];
3161
+ case 3:
3190
3162
  err_4 = _a.sent();
3191
3163
  console.error("perpClient CloseAndSwap error:: ", err_4);
3192
3164
  throw err_4;
3193
- case 8: return [2, {
3165
+ case 4: return [2, {
3194
3166
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3195
3167
  additionalSigners: additionalSigners
3196
3168
  }];
@@ -3203,14 +3175,14 @@ var PerpetualsClient = (function () {
3203
3175
  for (var _i = 6; _i < arguments.length; _i++) {
3204
3176
  args_1[_i - 6] = arguments[_i];
3205
3177
  }
3206
- 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) {
3178
+ 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) {
3207
3179
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, instruction;
3208
3180
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3209
3181
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3210
3182
  return __generator(this, function (_c) {
3211
3183
  switch (_c.label) {
3212
3184
  case 0:
3213
- publicKey = this.provider.wallet.publicKey;
3185
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3214
3186
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3215
3187
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3216
3188
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -3306,14 +3278,14 @@ var PerpetualsClient = (function () {
3306
3278
  for (var _i = 7; _i < arguments.length; _i++) {
3307
3279
  args_1[_i - 7] = arguments[_i];
3308
3280
  }
3309
- 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) {
3281
+ 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) {
3310
3282
  var publicKey, collateralCustodyConfig, targetCustodyConfig, inputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userCollateralTokenAccount, marketAccount, instruction;
3311
3283
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3312
3284
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3313
3285
  return __generator(this, function (_c) {
3314
3286
  switch (_c.label) {
3315
3287
  case 0:
3316
- publicKey = this.provider.wallet.publicKey;
3288
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3317
3289
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3318
3290
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3319
3291
  inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inputSymbol).mintKey); });
@@ -3377,11 +3349,7 @@ var PerpetualsClient = (function () {
3377
3349
  _c.label = 6;
3378
3350
  case 6:
3379
3351
  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);
3380
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3381
- case 7:
3382
- if (!(_c.sent())) {
3383
- 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));
3384
- }
3352
+ 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));
3385
3353
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3386
3354
  return [4, this.program.methods.swapAndAddCollateral({
3387
3355
  amountIn: amountIn,
@@ -3409,7 +3377,7 @@ var PerpetualsClient = (function () {
3409
3377
  fundingMint: poolConfig.getTokenFromSymbol(inputSymbol).mintKey,
3410
3378
  })
3411
3379
  .instruction()];
3412
- case 8:
3380
+ case 7:
3413
3381
  instruction = _c.sent();
3414
3382
  instructions.push(instruction);
3415
3383
  return [2, {
@@ -3425,15 +3393,15 @@ var PerpetualsClient = (function () {
3425
3393
  for (var _i = 6; _i < arguments.length; _i++) {
3426
3394
  args_1[_i - 6] = arguments[_i];
3427
3395
  }
3428
- 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) {
3429
- var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, marketAccount, instruction, closeWsolATAIns, error_2;
3396
+ 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) {
3397
+ var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, marketAccount, instruction, closeWsolATAIns, error_2;
3430
3398
  if (createUserATA === void 0) { createUserATA = true; }
3431
3399
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3432
3400
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3433
- return __generator(this, function (_b) {
3434
- switch (_b.label) {
3401
+ return __generator(this, function (_a) {
3402
+ switch (_a.label) {
3435
3403
  case 0:
3436
- publicKey = this.provider.wallet.publicKey;
3404
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3437
3405
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3438
3406
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3439
3407
  });
@@ -3447,45 +3415,35 @@ var PerpetualsClient = (function () {
3447
3415
  instructions = [];
3448
3416
  postInstructions = [];
3449
3417
  additionalSigners = [];
3450
- _b.label = 1;
3418
+ _a.label = 1;
3451
3419
  case 1:
3452
- _b.trys.push([1, 7, , 8]);
3420
+ _a.trys.push([1, 3, , 4]);
3453
3421
  console.log("removeCollateral -- collateralSymbol:", collateralSymbol);
3454
- if (!(collateralSymbol == 'SOL')) return [3, 2];
3455
- console.log("remove collateral in SOL ...create WSOL temp and close it ");
3456
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3457
- if (!ephemeralSignerPubkey) {
3458
- wrappedSolAccount = new web3_js_1.Keypair();
3459
- additionalSigners.push(wrappedSolAccount);
3422
+ if (collateralSymbol == 'SOL') {
3423
+ console.log("remove collateral in SOL ...create WSOL temp and close it ");
3424
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3425
+ if (!ephemeralSignerPubkey) {
3426
+ wrappedSolAccount = new web3_js_1.Keypair();
3427
+ additionalSigners.push(wrappedSolAccount);
3428
+ }
3429
+ preInstructions = [
3430
+ web3_js_1.SystemProgram.createAccount({
3431
+ fromPubkey: publicKey,
3432
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3433
+ lamports: lamports,
3434
+ space: 165,
3435
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3436
+ }),
3437
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3438
+ ];
3439
+ postInstructions = [
3440
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3441
+ ];
3460
3442
  }
3461
- preInstructions = [
3462
- web3_js_1.SystemProgram.createAccount({
3463
- fromPubkey: publicKey,
3464
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3465
- lamports: lamports,
3466
- space: 165,
3467
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3468
- }),
3469
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3470
- ];
3471
- postInstructions = [
3472
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3473
- ];
3474
- return [3, 5];
3475
- case 2:
3476
- 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);
3477
- _a = createUserATA;
3478
- if (!_a) return [3, 4];
3479
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3480
- case 3:
3481
- _a = !(_b.sent());
3482
- _b.label = 4;
3483
- case 4:
3484
- if (_a) {
3485
- 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));
3443
+ else {
3444
+ 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);
3445
+ 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));
3486
3446
  }
3487
- _b.label = 5;
3488
- case 5:
3489
3447
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3490
3448
  return [4, this.program.methods
3491
3449
  .removeCollateral({
@@ -3511,19 +3469,19 @@ var PerpetualsClient = (function () {
3511
3469
  receivingMint: collateralCustodyConfig.mintKey
3512
3470
  })
3513
3471
  .instruction()];
3514
- case 6:
3515
- instruction = _b.sent();
3472
+ case 2:
3473
+ instruction = _a.sent();
3516
3474
  instructions.push(instruction);
3517
3475
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
3518
3476
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
3519
3477
  postInstructions.push(closeWsolATAIns);
3520
3478
  }
3521
- return [3, 8];
3522
- case 7:
3523
- error_2 = _b.sent();
3479
+ return [3, 4];
3480
+ case 3:
3481
+ error_2 = _a.sent();
3524
3482
  console.error("perpclient removeCollateral error:", error_2);
3525
3483
  throw error_2;
3526
- case 8: return [2, {
3484
+ case 4: return [2, {
3527
3485
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3528
3486
  additionalSigners: additionalSigners
3529
3487
  }];
@@ -3536,13 +3494,13 @@ var PerpetualsClient = (function () {
3536
3494
  for (var _i = 6; _i < arguments.length; _i++) {
3537
3495
  args_1[_i - 6] = arguments[_i];
3538
3496
  }
3539
- 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) {
3497
+ 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) {
3540
3498
  var publicKey, targetCustodyConfig, collateralCustodyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, instruction;
3541
3499
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3542
3500
  return __generator(this, function (_a) {
3543
3501
  switch (_a.label) {
3544
3502
  case 0:
3545
- publicKey = this.provider.wallet.publicKey;
3503
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3546
3504
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3547
3505
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3548
3506
  outputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outputSymbol).mintKey); });
@@ -3553,42 +3511,33 @@ var PerpetualsClient = (function () {
3553
3511
  instructions = [];
3554
3512
  postInstructions = [];
3555
3513
  additionalSigners = [];
3556
- if (!(outputSymbol == 'SOL')) return [3, 1];
3557
- console.log("outputSymbol === SOL", outputSymbol);
3558
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3559
- if (!ephemeralSignerPubkey) {
3560
- wrappedSolAccount = new web3_js_1.Keypair();
3561
- additionalSigners.push(wrappedSolAccount);
3514
+ if (outputSymbol == 'SOL') {
3515
+ console.log("outputSymbol === SOL", outputSymbol);
3516
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3517
+ if (!ephemeralSignerPubkey) {
3518
+ wrappedSolAccount = new web3_js_1.Keypair();
3519
+ additionalSigners.push(wrappedSolAccount);
3520
+ }
3521
+ preInstructions = [
3522
+ web3_js_1.SystemProgram.createAccount({
3523
+ fromPubkey: publicKey,
3524
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3525
+ lamports: lamports,
3526
+ space: 165,
3527
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3528
+ }),
3529
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3530
+ ];
3531
+ postInstructions = [
3532
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3533
+ ];
3562
3534
  }
3563
- preInstructions = [
3564
- web3_js_1.SystemProgram.createAccount({
3565
- fromPubkey: publicKey,
3566
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3567
- lamports: lamports,
3568
- space: 165,
3569
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3570
- }),
3571
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3572
- ];
3573
- postInstructions = [
3574
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3575
- ];
3576
- return [3, 3];
3577
- case 1:
3578
- 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);
3579
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3580
- case 2:
3581
- if (!(_a.sent())) {
3582
- 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));
3535
+ else {
3536
+ 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);
3537
+ 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));
3583
3538
  }
3584
- _a.label = 3;
3585
- case 3:
3586
3539
  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);
3587
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3588
- case 4:
3589
- if (!(_a.sent())) {
3590
- 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));
3591
- }
3540
+ 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));
3592
3541
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3593
3542
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3594
3543
  return [4, this.program.methods
@@ -3622,7 +3571,7 @@ var PerpetualsClient = (function () {
3622
3571
  collateralTokenProgram: poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID
3623
3572
  })
3624
3573
  .instruction()];
3625
- case 5:
3574
+ case 1:
3626
3575
  instruction = _a.sent();
3627
3576
  instructions.push(instruction);
3628
3577
  return [2, {
@@ -3638,14 +3587,14 @@ var PerpetualsClient = (function () {
3638
3587
  for (var _i = 8; _i < arguments.length; _i++) {
3639
3588
  args_1[_i - 8] = arguments[_i];
3640
3589
  }
3641
- 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) {
3590
+ 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) {
3642
3591
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3643
3592
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3644
3593
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3645
3594
  return __generator(this, function (_a) {
3646
3595
  switch (_a.label) {
3647
3596
  case 0:
3648
- publicKey = this.provider.wallet.publicKey;
3597
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3649
3598
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3650
3599
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3651
3600
  });
@@ -3702,14 +3651,14 @@ var PerpetualsClient = (function () {
3702
3651
  for (var _i = 8; _i < arguments.length; _i++) {
3703
3652
  args_1[_i - 8] = arguments[_i];
3704
3653
  }
3705
- 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) {
3654
+ 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) {
3706
3655
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3707
3656
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3708
3657
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3709
3658
  return __generator(this, function (_a) {
3710
3659
  switch (_a.label) {
3711
3660
  case 0:
3712
- publicKey = this.provider.wallet.publicKey;
3661
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3713
3662
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3714
3663
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3715
3664
  });
@@ -3766,7 +3715,7 @@ var PerpetualsClient = (function () {
3766
3715
  for (var _i = 4; _i < arguments.length; _i++) {
3767
3716
  args_1[_i - 4] = arguments[_i];
3768
3717
  }
3769
- 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) {
3718
+ 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) {
3770
3719
  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;
3771
3720
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3772
3721
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
@@ -3774,7 +3723,7 @@ var PerpetualsClient = (function () {
3774
3723
  return __generator(this, function (_g) {
3775
3724
  switch (_g.label) {
3776
3725
  case 0:
3777
- publicKey = this.provider.wallet.publicKey;
3726
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3778
3727
  payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
3779
3728
  if (!payTokenCustodyConfig) {
3780
3729
  throw "payTokenCustodyConfig not found";
@@ -3786,7 +3735,7 @@ var PerpetualsClient = (function () {
3786
3735
  payToken = poolConfig.getTokenFromSymbol(payTokenSymbol);
3787
3736
  _g.label = 1;
3788
3737
  case 1:
3789
- _g.trys.push([1, 10, , 11]);
3738
+ _g.trys.push([1, 9, , 10]);
3790
3739
  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);
3791
3740
  lpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
3792
3741
  custodyAccountMetas = [];
@@ -3813,24 +3762,20 @@ var PerpetualsClient = (function () {
3813
3762
  isWritable: false,
3814
3763
  });
3815
3764
  }
3816
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3817
- case 2:
3818
- if (!(_g.sent())) {
3819
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3820
- }
3821
- if (!(payTokenSymbol == 'SOL')) return [3, 5];
3765
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3766
+ if (!(payTokenSymbol == 'SOL')) return [3, 4];
3822
3767
  console.log("payTokenSymbol === SOL", payTokenSymbol);
3823
3768
  lamports = tokenAmountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3824
- if (!!skipBalanceChecks) return [3, 4];
3769
+ if (!!skipBalanceChecks) return [3, 3];
3825
3770
  _e = anchor_1.BN.bind;
3826
3771
  return [4, this.provider.connection.getBalance(publicKey)];
3827
- case 3:
3772
+ case 2:
3828
3773
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _g.sent()]))();
3829
3774
  if (unWrappedSolBalance.lt(lamports)) {
3830
3775
  throw "Insufficient SOL Funds";
3831
3776
  }
3832
- _g.label = 4;
3833
- case 4:
3777
+ _g.label = 3;
3778
+ case 3:
3834
3779
  if (!ephemeralSignerPubkey) {
3835
3780
  wrappedSolAccount = new web3_js_1.Keypair();
3836
3781
  additionalSigners.push(wrappedSolAccount);
@@ -3848,23 +3793,23 @@ var PerpetualsClient = (function () {
3848
3793
  postInstructions = [
3849
3794
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3850
3795
  ];
3851
- return [3, 8];
3852
- case 5:
3853
- if (!!skipBalanceChecks) return [3, 8];
3796
+ return [3, 7];
3797
+ case 4:
3798
+ if (!!skipBalanceChecks) return [3, 7];
3854
3799
  return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3855
- case 6:
3800
+ case 5:
3856
3801
  if (!(_g.sent())) {
3857
3802
  throw "Insufficient Funds , token Account doesn't exist";
3858
3803
  }
3859
3804
  _f = anchor_1.BN.bind;
3860
3805
  return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
3861
- case 7:
3806
+ case 6:
3862
3807
  tokenAccountBalance = new (_f.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3863
3808
  if (tokenAccountBalance.lt(tokenAmountIn)) {
3864
3809
  throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
3865
3810
  }
3866
- _g.label = 8;
3867
- case 8:
3811
+ _g.label = 7;
3812
+ case 7:
3868
3813
  whitelistPda = this.findProgramAddress("whitelist", [publicKey]).publicKey;
3869
3814
  whitelistMeta = {
3870
3815
  pubkey: whitelistPda,
@@ -3896,15 +3841,15 @@ var PerpetualsClient = (function () {
3896
3841
  })
3897
3842
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3898
3843
  .instruction()];
3899
- case 9:
3844
+ case 8:
3900
3845
  instruction = _g.sent();
3901
3846
  instructions.push(instruction);
3902
- return [3, 11];
3903
- case 10:
3847
+ return [3, 10];
3848
+ case 9:
3904
3849
  err_5 = _g.sent();
3905
3850
  console.error("perpClient addLiquidity error:: ", err_5);
3906
3851
  throw err_5;
3907
- case 11: return [2, {
3852
+ case 10: return [2, {
3908
3853
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3909
3854
  additionalSigners: additionalSigners
3910
3855
  }];
@@ -3937,7 +3882,7 @@ var PerpetualsClient = (function () {
3937
3882
  inputToken = poolConfig.getTokenFromSymbol(inputSymbol);
3938
3883
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
3939
3884
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
3940
- if (!(inputSymbol == 'SOL')) return [3, 4];
3885
+ if (!(inputSymbol == 'SOL')) return [3, 3];
3941
3886
  console.log("inputSymbol === SOL", inputSymbol);
3942
3887
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3943
3888
  console.log("lamports:", lamports.toNumber());
@@ -3950,11 +3895,8 @@ var PerpetualsClient = (function () {
3950
3895
  throw "Insufficient SOL Funds";
3951
3896
  }
3952
3897
  _g.label = 2;
3953
- case 2: return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3954
- case 3:
3955
- if (!(_g.sent())) {
3956
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3957
- }
3898
+ case 2:
3899
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3958
3900
  if (!ephemeralSignerPubkey) {
3959
3901
  wrappedSolAccount = new web3_js_1.Keypair();
3960
3902
  additionalSigners.push(wrappedSolAccount);
@@ -3972,24 +3914,24 @@ var PerpetualsClient = (function () {
3972
3914
  postInstructions = [
3973
3915
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3974
3916
  ];
3975
- return [3, 7];
3976
- case 4:
3917
+ return [3, 6];
3918
+ case 3:
3977
3919
  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);
3978
- if (!!skipBalanceChecks) return [3, 7];
3920
+ if (!!skipBalanceChecks) return [3, 6];
3979
3921
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3980
- case 5:
3922
+ case 4:
3981
3923
  if (!(_g.sent())) {
3982
3924
  throw "Insufficient Funds , token Account doesn't exist";
3983
3925
  }
3984
3926
  _b = anchor_1.BN.bind;
3985
3927
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3986
- case 6:
3928
+ case 5:
3987
3929
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3988
3930
  if (tokenAccountBalance.lt(amountIn)) {
3989
3931
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3990
3932
  }
3991
- _g.label = 7;
3992
- case 7:
3933
+ _g.label = 6;
3934
+ case 6:
3993
3935
  custodyAccountMetas = [];
3994
3936
  custodyOracleAccountMetas = [];
3995
3937
  markets = [];
@@ -4046,7 +3988,7 @@ var PerpetualsClient = (function () {
4046
3988
  })
4047
3989
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4048
3990
  .instruction()];
4049
- case 8:
3991
+ case 7:
4050
3992
  instruction = _g.sent();
4051
3993
  instructions.push(instruction);
4052
3994
  return [2, {
@@ -4529,12 +4471,12 @@ var PerpetualsClient = (function () {
4529
4471
  }
4530
4472
  });
4531
4473
  }); };
4532
- this.unstakeRequest = function (unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4474
+ this.unstakeRequest = function (unstakeAmount, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
4533
4475
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_13;
4534
4476
  return __generator(this, function (_a) {
4535
4477
  switch (_a.label) {
4536
4478
  case 0:
4537
- publicKey = this.provider.wallet.publicKey;
4479
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4538
4480
  preInstructions = [];
4539
4481
  instructions = [];
4540
4482
  postInstructions = [];
@@ -4653,10 +4595,10 @@ var PerpetualsClient = (function () {
4653
4595
  args_1[_i - 3] = arguments[_i];
4654
4596
  }
4655
4597
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4656
- var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4598
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4657
4599
  if (createUserATA === void 0) { createUserATA = true; }
4658
- return __generator(this, function (_b) {
4659
- switch (_b.label) {
4600
+ return __generator(this, function (_a) {
4601
+ switch (_a.label) {
4660
4602
  case 0:
4661
4603
  publicKey = this.provider.wallet.publicKey;
4662
4604
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
@@ -4665,22 +4607,13 @@ var PerpetualsClient = (function () {
4665
4607
  instructions = [];
4666
4608
  postInstructions = [];
4667
4609
  additionalSigners = [];
4668
- _b.label = 1;
4610
+ _a.label = 1;
4669
4611
  case 1:
4670
- _b.trys.push([1, 5, , 6]);
4612
+ _a.trys.push([1, 3, , 4]);
4671
4613
  pool = poolConfig.poolAddress;
4672
4614
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4673
4615
  receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
4674
- _a = createUserATA;
4675
- if (!_a) return [3, 3];
4676
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
4677
- case 2:
4678
- _a = !(_b.sent());
4679
- _b.label = 3;
4680
- case 3:
4681
- if (_a) {
4682
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint));
4683
- }
4616
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
4684
4617
  tokenStakeAccounts = [];
4685
4618
  if (tokenStakeAccount) {
4686
4619
  tokenStakeAccounts.push({
@@ -4709,15 +4642,15 @@ var PerpetualsClient = (function () {
4709
4642
  })
4710
4643
  .remainingAccounts(__spreadArray([], tokenStakeAccounts, true))
4711
4644
  .instruction()];
4712
- case 4:
4713
- withdrawStakeInstruction = _b.sent();
4645
+ case 2:
4646
+ withdrawStakeInstruction = _a.sent();
4714
4647
  instructions.push(withdrawStakeInstruction);
4715
- return [3, 6];
4716
- case 5:
4717
- err_15 = _b.sent();
4648
+ return [3, 4];
4649
+ case 3:
4650
+ err_15 = _a.sent();
4718
4651
  console.log("perpClient withdrawStake error:: ", err_15);
4719
4652
  throw err_15;
4720
- case 6: return [2, {
4653
+ case 4: return [2, {
4721
4654
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4722
4655
  additionalSigners: additionalSigners
4723
4656
  }];
@@ -4776,29 +4709,21 @@ var PerpetualsClient = (function () {
4776
4709
  isWritable: false,
4777
4710
  });
4778
4711
  }
4779
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
4780
- case 1:
4781
- if (!(_f.sent())) {
4782
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4783
- }
4784
- return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
4785
- case 2:
4786
- if (!(_f.sent())) {
4787
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
4788
- }
4789
- if (!(inTokenSymbol == 'SOL')) return [3, 5];
4712
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4713
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4714
+ if (!(inTokenSymbol == 'SOL')) return [3, 3];
4790
4715
  console.log("inTokenSymbol === SOL", inTokenSymbol);
4791
4716
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
4792
- if (!!skipBalanceChecks) return [3, 4];
4717
+ if (!!skipBalanceChecks) return [3, 2];
4793
4718
  _e = anchor_1.BN.bind;
4794
4719
  return [4, this.provider.connection.getBalance(publicKey)];
4795
- case 3:
4720
+ case 1:
4796
4721
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _f.sent()]))();
4797
4722
  if (unWrappedSolBalance.lt(lamports)) {
4798
4723
  throw "Insufficient SOL Funds";
4799
4724
  }
4800
- _f.label = 4;
4801
- case 4:
4725
+ _f.label = 2;
4726
+ case 2:
4802
4727
  if (!ephemeralSignerPubkey) {
4803
4728
  wrappedSolAccount = new web3_js_1.Keypair();
4804
4729
  additionalSigners.push(wrappedSolAccount);
@@ -4816,17 +4741,17 @@ var PerpetualsClient = (function () {
4816
4741
  postInstructions = [
4817
4742
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4818
4743
  ];
4819
- return [3, 7];
4820
- case 5:
4821
- if (!!skipBalanceChecks) return [3, 7];
4744
+ return [3, 5];
4745
+ case 3:
4746
+ if (!!skipBalanceChecks) return [3, 5];
4822
4747
  return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
4823
- case 6:
4748
+ case 4:
4824
4749
  if (!(_f.sent())) {
4825
4750
  throw "Insufficient Funds , token Account doesn't exist";
4826
4751
  }
4827
- _f.label = 7;
4828
- case 7:
4829
- _f.trys.push([7, 9, , 10]);
4752
+ _f.label = 5;
4753
+ case 5:
4754
+ _f.trys.push([5, 7, , 8]);
4830
4755
  if (enableHeapSizeIx) {
4831
4756
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4832
4757
  bytes: 64 * 1024,
@@ -4868,15 +4793,15 @@ var PerpetualsClient = (function () {
4868
4793
  })
4869
4794
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4870
4795
  .instruction()];
4871
- case 8:
4796
+ case 6:
4872
4797
  addCompoundingLiquidity = _f.sent();
4873
4798
  instructions.push(addCompoundingLiquidity);
4874
- return [3, 10];
4875
- case 9:
4799
+ return [3, 8];
4800
+ case 7:
4876
4801
  err_16 = _f.sent();
4877
4802
  console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4878
- return [3, 10];
4879
- case 10: return [2, {
4803
+ return [3, 8];
4804
+ case 8: return [2, {
4880
4805
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4881
4806
  additionalSigners: additionalSigners
4882
4807
  }];
@@ -4890,14 +4815,14 @@ var PerpetualsClient = (function () {
4890
4815
  args_1[_i - 5] = arguments[_i];
4891
4816
  }
4892
4817
  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) {
4893
- 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;
4818
+ 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;
4894
4819
  if (createUserATA === void 0) { createUserATA = true; }
4895
4820
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4896
4821
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4897
4822
  if (enableHeapSizeIx === void 0) { enableHeapSizeIx = true; }
4898
4823
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
4899
- return __generator(this, function (_f) {
4900
- switch (_f.label) {
4824
+ return __generator(this, function (_e) {
4825
+ switch (_e.label) {
4901
4826
  case 0:
4902
4827
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4903
4828
  preInstructions = [];
@@ -4908,45 +4833,35 @@ var PerpetualsClient = (function () {
4908
4833
  outCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outTokenSymbol).mintKey); });
4909
4834
  lpTokenMint = poolConfig.stakedLpTokenMint;
4910
4835
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4911
- if (!(outCustodyConfig.symbol == 'SOL')) return [3, 1];
4912
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4913
- if (!ephemeralSignerPubkey) {
4914
- wrappedSolAccount = new web3_js_1.Keypair();
4915
- additionalSigners.push(wrappedSolAccount);
4836
+ if (outCustodyConfig.symbol == 'SOL') {
4837
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
4838
+ if (!ephemeralSignerPubkey) {
4839
+ wrappedSolAccount = new web3_js_1.Keypair();
4840
+ additionalSigners.push(wrappedSolAccount);
4841
+ }
4842
+ preInstructions = [
4843
+ web3_js_1.SystemProgram.createAccount({
4844
+ fromPubkey: publicKey,
4845
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4846
+ lamports: lamports,
4847
+ space: 165,
4848
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
4849
+ }),
4850
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4851
+ ];
4852
+ postInstructions = [
4853
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4854
+ ];
4916
4855
  }
4917
- preInstructions = [
4918
- web3_js_1.SystemProgram.createAccount({
4919
- fromPubkey: publicKey,
4920
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4921
- lamports: lamports,
4922
- space: 165,
4923
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4924
- }),
4925
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4926
- ];
4927
- postInstructions = [
4928
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4929
- ];
4930
- return [3, 4];
4931
- case 1:
4932
- 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);
4933
- _a = createUserATA;
4934
- if (!_a) return [3, 3];
4935
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
4936
- case 2:
4937
- _a = !(_f.sent());
4938
- _f.label = 3;
4939
- case 3:
4940
- if (_a) {
4941
- 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));
4856
+ else {
4857
+ 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);
4858
+ 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));
4942
4859
  }
4943
- _f.label = 4;
4944
- case 4:
4945
4860
  custodyAccountMetas = [];
4946
4861
  custodyOracleAccountMetas = [];
4947
4862
  markets = [];
4948
- for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4949
- custody = _c[_b];
4863
+ for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
4864
+ custody = _b[_a];
4950
4865
  custodyAccountMetas.push({
4951
4866
  pubkey: custody.custodyAccount,
4952
4867
  isSigner: false,
@@ -4958,8 +4873,8 @@ var PerpetualsClient = (function () {
4958
4873
  isWritable: false,
4959
4874
  });
4960
4875
  }
4961
- for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4962
- market = _e[_d];
4876
+ for (_c = 0, _d = poolConfig.markets; _c < _d.length; _c++) {
4877
+ market = _d[_c];
4963
4878
  markets.push({
4964
4879
  pubkey: market.marketAccount,
4965
4880
  isSigner: false,
@@ -4967,9 +4882,9 @@ var PerpetualsClient = (function () {
4967
4882
  });
4968
4883
  }
4969
4884
  compoundingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4970
- _f.label = 5;
4971
- case 5:
4972
- _f.trys.push([5, 7, , 8]);
4885
+ _e.label = 1;
4886
+ case 1:
4887
+ _e.trys.push([1, 3, , 4]);
4973
4888
  if (enableHeapSizeIx) {
4974
4889
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4975
4890
  bytes: 64 * 1024,
@@ -5011,15 +4926,15 @@ var PerpetualsClient = (function () {
5011
4926
  })
5012
4927
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
5013
4928
  .instruction()];
5014
- case 6:
5015
- removeCompoundingLiquidity = _f.sent();
4929
+ case 2:
4930
+ removeCompoundingLiquidity = _e.sent();
5016
4931
  instructions.push(removeCompoundingLiquidity);
5017
- return [3, 8];
5018
- case 7:
5019
- err_17 = _f.sent();
4932
+ return [3, 4];
4933
+ case 3:
4934
+ err_17 = _e.sent();
5020
4935
  console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
5021
- return [3, 8];
5022
- case 8: return [2, {
4936
+ return [3, 4];
4937
+ case 4: return [2, {
5023
4938
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5024
4939
  additionalSigners: additionalSigners
5025
4940
  }];
@@ -5032,13 +4947,13 @@ var PerpetualsClient = (function () {
5032
4947
  for (var _i = 3; _i < arguments.length; _i++) {
5033
4948
  args_1[_i - 3] = arguments[_i];
5034
4949
  }
5035
- return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA) {
5036
- 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;
4950
+ return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA, userPublicKey) {
4951
+ 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;
5037
4952
  if (createUserATA === void 0) { createUserATA = true; }
5038
- return __generator(this, function (_g) {
5039
- switch (_g.label) {
4953
+ return __generator(this, function (_f) {
4954
+ switch (_f.label) {
5040
4955
  case 0:
5041
- publicKey = this.provider.wallet.publicKey;
4956
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5042
4957
  preInstructions = [];
5043
4958
  instructions = [];
5044
4959
  postInstructions = [];
@@ -5047,27 +4962,18 @@ var PerpetualsClient = (function () {
5047
4962
  lpTokenMint = poolConfig.stakedLpTokenMint;
5048
4963
  compoundingTokenMint = poolConfig.compoundingTokenMint;
5049
4964
  compoudingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
5050
- _a = createUserATA;
5051
- if (!_a) return [3, 2];
5052
- return [4, (0, utils_1.checkIfAccountExists)(compoudingTokenAccount, this.provider.connection)];
5053
- case 1:
5054
- _a = !(_g.sent());
5055
- _g.label = 2;
5056
- case 2:
5057
- if (_a) {
5058
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint));
5059
- }
4965
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5060
4966
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
5061
4967
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
5062
4968
  tokenStakeAccounts = [];
5063
- _b = tokenStakeAccount;
5064
- if (!_b) return [3, 4];
4969
+ _a = tokenStakeAccount;
4970
+ if (!_a) return [3, 2];
5065
4971
  return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5066
- case 3:
5067
- _b = (_g.sent());
5068
- _g.label = 4;
5069
- case 4:
5070
- if (_b) {
4972
+ case 1:
4973
+ _a = (_f.sent());
4974
+ _f.label = 2;
4975
+ case 2:
4976
+ if (_a) {
5071
4977
  tokenStakeAccounts.push({
5072
4978
  pubkey: tokenStakeAccount,
5073
4979
  isSigner: false,
@@ -5078,8 +4984,8 @@ var PerpetualsClient = (function () {
5078
4984
  custodyAccountMetas = [];
5079
4985
  custodyOracleAccountMetas = [];
5080
4986
  markets = [];
5081
- for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
5082
- custody = _d[_c];
4987
+ for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4988
+ custody = _c[_b];
5083
4989
  custodyAccountMetas.push({
5084
4990
  pubkey: custody.custodyAccount,
5085
4991
  isSigner: false,
@@ -5091,17 +4997,17 @@ var PerpetualsClient = (function () {
5091
4997
  isWritable: false,
5092
4998
  });
5093
4999
  }
5094
- for (_e = 0, _f = poolConfig.markets; _e < _f.length; _e++) {
5095
- market = _f[_e];
5000
+ for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
5001
+ market = _e[_d];
5096
5002
  markets.push({
5097
5003
  pubkey: market.marketAccount,
5098
5004
  isSigner: false,
5099
5005
  isWritable: false,
5100
5006
  });
5101
5007
  }
5102
- _g.label = 5;
5103
- case 5:
5104
- _g.trys.push([5, 7, , 8]);
5008
+ _f.label = 3;
5009
+ case 3:
5010
+ _f.trys.push([3, 5, , 6]);
5105
5011
  return [4, this.program.methods
5106
5012
  .migrateStake({
5107
5013
  amount: amount
@@ -5126,15 +5032,15 @@ var PerpetualsClient = (function () {
5126
5032
  })
5127
5033
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), tokenStakeAccounts, true))
5128
5034
  .instruction()];
5129
- case 6:
5130
- migrateStake = _g.sent();
5035
+ case 4:
5036
+ migrateStake = _f.sent();
5131
5037
  instructions.push(migrateStake);
5132
- return [3, 8];
5133
- case 7:
5134
- err_18 = _g.sent();
5038
+ return [3, 6];
5039
+ case 5:
5040
+ err_18 = _f.sent();
5135
5041
  console.log("perpClient migrateStake error:: ", err_18);
5136
- return [3, 8];
5137
- case 8: return [2, {
5042
+ return [3, 6];
5043
+ case 6: return [2, {
5138
5044
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5139
5045
  additionalSigners: additionalSigners
5140
5046
  }];
@@ -5142,12 +5048,12 @@ var PerpetualsClient = (function () {
5142
5048
  });
5143
5049
  });
5144
5050
  };
5145
- this.migrateFlp = function (amount, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5051
+ this.migrateFlp = function (amount, rewardTokenMint, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
5146
5052
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_19;
5147
5053
  return __generator(this, function (_d) {
5148
5054
  switch (_d.label) {
5149
5055
  case 0:
5150
- publicKey = this.provider.wallet.publicKey;
5056
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5151
5057
  preInstructions = [];
5152
5058
  instructions = [];
5153
5059
  postInstructions = [];
@@ -5311,14 +5217,10 @@ var PerpetualsClient = (function () {
5311
5217
  additionalSigners = [];
5312
5218
  _a.label = 1;
5313
5219
  case 1:
5314
- _a.trys.push([1, 4, , 5]);
5220
+ _a.trys.push([1, 3, , 4]);
5315
5221
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5316
5222
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5317
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5318
- case 2:
5319
- if (!(_a.sent())) {
5320
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5321
- }
5223
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5322
5224
  return [4, this.program.methods
5323
5225
  .depositTokenStake({
5324
5226
  depositAmount: depositAmount
@@ -5338,15 +5240,15 @@ var PerpetualsClient = (function () {
5338
5240
  tokenMint: poolConfig.tokenMint,
5339
5241
  })
5340
5242
  .instruction()];
5341
- case 3:
5243
+ case 2:
5342
5244
  depositTokenStakeInstruction = _a.sent();
5343
5245
  instructions.push(depositTokenStakeInstruction);
5344
- return [3, 5];
5345
- case 4:
5246
+ return [3, 4];
5247
+ case 3:
5346
5248
  err_21 = _a.sent();
5347
5249
  console.log("perpClient depositStakingInstruction error:: ", err_21);
5348
5250
  throw err_21;
5349
- case 5: return [2, {
5251
+ case 4: return [2, {
5350
5252
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5351
5253
  additionalSigners: additionalSigners
5352
5254
  }];
@@ -5404,14 +5306,10 @@ var PerpetualsClient = (function () {
5404
5306
  additionalSigners = [];
5405
5307
  _a.label = 1;
5406
5308
  case 1:
5407
- _a.trys.push([1, 4, , 5]);
5309
+ _a.trys.push([1, 3, , 4]);
5408
5310
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5409
5311
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5410
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5411
- case 2:
5412
- if (!(_a.sent())) {
5413
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5414
- }
5312
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5415
5313
  return [4, this.program.methods
5416
5314
  .unstakeTokenInstant({
5417
5315
  unstakeAmount: unstakeAmount
@@ -5430,15 +5328,15 @@ var PerpetualsClient = (function () {
5430
5328
  tokenMint: poolConfig.tokenMint,
5431
5329
  })
5432
5330
  .instruction()];
5433
- case 3:
5331
+ case 2:
5434
5332
  unstakeTokenInstantInstruction = _a.sent();
5435
5333
  instructions.push(unstakeTokenInstantInstruction);
5436
- return [3, 5];
5437
- case 4:
5334
+ return [3, 4];
5335
+ case 3:
5438
5336
  err_23 = _a.sent();
5439
5337
  console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5440
5338
  throw err_23;
5441
- case 5: return [2, {
5339
+ case 4: return [2, {
5442
5340
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5443
5341
  additionalSigners: additionalSigners
5444
5342
  }];
@@ -5456,14 +5354,10 @@ var PerpetualsClient = (function () {
5456
5354
  additionalSigners = [];
5457
5355
  _a.label = 1;
5458
5356
  case 1:
5459
- _a.trys.push([1, 4, , 5]);
5357
+ _a.trys.push([1, 3, , 4]);
5460
5358
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5461
5359
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5462
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5463
- case 2:
5464
- if (!(_a.sent())) {
5465
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5466
- }
5360
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5467
5361
  return [4, this.program.methods
5468
5362
  .withdrawToken({
5469
5363
  withdrawRequestId: withdrawRequestId
@@ -5482,15 +5376,15 @@ var PerpetualsClient = (function () {
5482
5376
  tokenMint: poolConfig.tokenMint,
5483
5377
  })
5484
5378
  .instruction()];
5485
- case 3:
5379
+ case 2:
5486
5380
  withdrawTokenInstruction = _a.sent();
5487
5381
  instructions.push(withdrawTokenInstruction);
5488
- return [3, 5];
5489
- case 4:
5382
+ return [3, 4];
5383
+ case 3:
5490
5384
  err_24 = _a.sent();
5491
5385
  console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5492
5386
  throw err_24;
5493
- case 5: return [2, {
5387
+ case 4: return [2, {
5494
5388
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5495
5389
  additionalSigners: additionalSigners
5496
5390
  }];
@@ -5542,32 +5436,23 @@ var PerpetualsClient = (function () {
5542
5436
  for (var _i = 2; _i < arguments.length; _i++) {
5543
5437
  args_1[_i - 2] = arguments[_i];
5544
5438
  }
5545
- return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA) {
5546
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_26;
5439
+ return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA, userPublicKey) {
5440
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, collectTokenRewardInstruction, err_26;
5547
5441
  if (createUserATA === void 0) { createUserATA = true; }
5548
- return __generator(this, function (_b) {
5549
- switch (_b.label) {
5442
+ return __generator(this, function (_a) {
5443
+ switch (_a.label) {
5550
5444
  case 0:
5551
- publicKey = this.provider.wallet.publicKey;
5445
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5552
5446
  preInstructions = [];
5553
5447
  instructions = [];
5554
5448
  postInstructions = [];
5555
5449
  additionalSigners = [];
5556
- _b.label = 1;
5450
+ _a.label = 1;
5557
5451
  case 1:
5558
- _b.trys.push([1, 5, , 6]);
5452
+ _a.trys.push([1, 3, , 4]);
5559
5453
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5560
5454
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5561
- _a = createUserATA;
5562
- if (!_a) return [3, 3];
5563
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5564
- case 2:
5565
- _a = !(_b.sent());
5566
- _b.label = 3;
5567
- case 3:
5568
- if (_a) {
5569
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint));
5570
- }
5455
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5571
5456
  return [4, this.program.methods
5572
5457
  .collectTokenReward({})
5573
5458
  .accounts({
@@ -5584,15 +5469,15 @@ var PerpetualsClient = (function () {
5584
5469
  tokenMint: poolConfig.tokenMint,
5585
5470
  })
5586
5471
  .instruction()];
5587
- case 4:
5588
- collectTokenRewardInstruction = _b.sent();
5472
+ case 2:
5473
+ collectTokenRewardInstruction = _a.sent();
5589
5474
  instructions.push(collectTokenRewardInstruction);
5590
- return [3, 6];
5591
- case 5:
5592
- err_26 = _b.sent();
5475
+ return [3, 4];
5476
+ case 3:
5477
+ err_26 = _a.sent();
5593
5478
  console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5594
5479
  throw err_26;
5595
- case 6: return [2, {
5480
+ case 4: return [2, {
5596
5481
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5597
5482
  additionalSigners: additionalSigners
5598
5483
  }];
@@ -5606,32 +5491,23 @@ var PerpetualsClient = (function () {
5606
5491
  args_1[_i - 3] = arguments[_i];
5607
5492
  }
5608
5493
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5609
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_27;
5494
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, collectRevenueInstruction, err_27;
5610
5495
  if (createUserATA === void 0) { createUserATA = true; }
5611
- return __generator(this, function (_b) {
5612
- switch (_b.label) {
5496
+ return __generator(this, function (_a) {
5497
+ switch (_a.label) {
5613
5498
  case 0:
5614
5499
  publicKey = this.provider.wallet.publicKey;
5615
5500
  preInstructions = [];
5616
5501
  instructions = [];
5617
5502
  postInstructions = [];
5618
5503
  additionalSigners = [];
5619
- _b.label = 1;
5504
+ _a.label = 1;
5620
5505
  case 1:
5621
- _b.trys.push([1, 5, , 6]);
5506
+ _a.trys.push([1, 3, , 4]);
5622
5507
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5623
5508
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5624
5509
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, owner, true);
5625
- _a = createUserATA;
5626
- if (!_a) return [3, 3];
5627
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5628
- case 2:
5629
- _a = !(_b.sent());
5630
- _b.label = 3;
5631
- case 3:
5632
- if (_a) {
5633
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint));
5634
- }
5510
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
5635
5511
  return [4, this.program.methods
5636
5512
  .collectRevenue({})
5637
5513
  .accounts({
@@ -5648,15 +5524,15 @@ var PerpetualsClient = (function () {
5648
5524
  receivingTokenMint: rewardCustodyMint,
5649
5525
  })
5650
5526
  .instruction()];
5651
- case 4:
5652
- collectRevenueInstruction = _b.sent();
5527
+ case 2:
5528
+ collectRevenueInstruction = _a.sent();
5653
5529
  instructions.push(collectRevenueInstruction);
5654
- return [3, 6];
5655
- case 5:
5656
- err_27 = _b.sent();
5530
+ return [3, 4];
5531
+ case 3:
5532
+ err_27 = _a.sent();
5657
5533
  console.log("perpClient collectRevenueInstruction error:: ", err_27);
5658
5534
  throw err_27;
5659
- case 6: return [2, {
5535
+ case 4: return [2, {
5660
5536
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5661
5537
  additionalSigners: additionalSigners
5662
5538
  }];
@@ -5670,32 +5546,23 @@ var PerpetualsClient = (function () {
5670
5546
  args_1[_i - 3] = arguments[_i];
5671
5547
  }
5672
5548
  return __awaiter(_this, __spreadArray([owner_1, rebateSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rebateSymbol, poolConfig, createUserATA) {
5673
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, _a, collectRebateInstruction, err_28;
5549
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, collectRebateInstruction, err_28;
5674
5550
  if (createUserATA === void 0) { createUserATA = true; }
5675
- return __generator(this, function (_b) {
5676
- switch (_b.label) {
5551
+ return __generator(this, function (_a) {
5552
+ switch (_a.label) {
5677
5553
  case 0:
5678
5554
  publicKey = this.provider.wallet.publicKey;
5679
5555
  preInstructions = [];
5680
5556
  instructions = [];
5681
5557
  postInstructions = [];
5682
5558
  additionalSigners = [];
5683
- _b.label = 1;
5559
+ _a.label = 1;
5684
5560
  case 1:
5685
- _b.trys.push([1, 5, , 6]);
5561
+ _a.trys.push([1, 3, , 4]);
5686
5562
  rebateMint = poolConfig.getTokenFromSymbol(rebateSymbol).mintKey;
5687
5563
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5688
5564
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rebateMint, owner, true);
5689
- _a = createUserATA;
5690
- if (!_a) return [3, 3];
5691
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5692
- case 2:
5693
- _a = !(_b.sent());
5694
- _b.label = 3;
5695
- case 3:
5696
- if (_a) {
5697
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rebateMint));
5698
- }
5565
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rebateMint, spl_token_1.TOKEN_PROGRAM_ID));
5699
5566
  return [4, this.program.methods
5700
5567
  .collectRebate()
5701
5568
  .accounts({
@@ -5712,15 +5579,15 @@ var PerpetualsClient = (function () {
5712
5579
  receivingTokenMint: rebateMint,
5713
5580
  })
5714
5581
  .instruction()];
5715
- case 4:
5716
- collectRebateInstruction = _b.sent();
5582
+ case 2:
5583
+ collectRebateInstruction = _a.sent();
5717
5584
  instructions.push(collectRebateInstruction);
5718
- return [3, 6];
5719
- case 5:
5720
- err_28 = _b.sent();
5585
+ return [3, 4];
5586
+ case 3:
5587
+ err_28 = _a.sent();
5721
5588
  console.log("perpClient collectRebateInstruction error:: ", err_28);
5722
5589
  throw err_28;
5723
- case 6: return [2, {
5590
+ case 4: return [2, {
5724
5591
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5725
5592
  additionalSigners: additionalSigners
5726
5593
  }];
@@ -5780,14 +5647,14 @@ var PerpetualsClient = (function () {
5780
5647
  for (var _i = 11; _i < arguments.length; _i++) {
5781
5648
  args_1[_i - 11] = arguments[_i];
5782
5649
  }
5783
- 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) {
5650
+ 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) {
5784
5651
  var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_30;
5785
5652
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5786
5653
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5787
5654
  return __generator(this, function (_c) {
5788
5655
  switch (_c.label) {
5789
5656
  case 0:
5790
- publicKey = this.provider.wallet.publicKey;
5657
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5791
5658
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5792
5659
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5793
5660
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5903,14 +5770,14 @@ var PerpetualsClient = (function () {
5903
5770
  for (var _i = 11; _i < arguments.length; _i++) {
5904
5771
  args_1[_i - 11] = arguments[_i];
5905
5772
  }
5906
- 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) {
5907
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_31;
5773
+ 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) {
5774
+ var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, positionAccount, orderAccount, editLimitOrder, err_31;
5908
5775
  if (createUserATA === void 0) { createUserATA = true; }
5909
5776
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5910
- return __generator(this, function (_b) {
5911
- switch (_b.label) {
5777
+ return __generator(this, function (_a) {
5778
+ switch (_a.label) {
5912
5779
  case 0:
5913
- publicKey = this.provider.wallet.publicKey;
5780
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5914
5781
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5915
5782
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5916
5783
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5920,43 +5787,33 @@ var PerpetualsClient = (function () {
5920
5787
  instructions = [];
5921
5788
  postInstructions = [];
5922
5789
  additionalSigners = [];
5923
- _b.label = 1;
5790
+ _a.label = 1;
5924
5791
  case 1:
5925
- _b.trys.push([1, 7, , 8]);
5926
- if (!(reserveSymbol == 'SOL')) return [3, 2];
5927
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
5928
- if (!ephemeralSignerPubkey) {
5929
- wrappedSolAccount = new web3_js_1.Keypair();
5930
- additionalSigners.push(wrappedSolAccount);
5792
+ _a.trys.push([1, 3, , 4]);
5793
+ if (reserveSymbol == 'SOL') {
5794
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
5795
+ if (!ephemeralSignerPubkey) {
5796
+ wrappedSolAccount = new web3_js_1.Keypair();
5797
+ additionalSigners.push(wrappedSolAccount);
5798
+ }
5799
+ preInstructions = [
5800
+ web3_js_1.SystemProgram.createAccount({
5801
+ fromPubkey: publicKey,
5802
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5803
+ lamports: lamports,
5804
+ space: 165,
5805
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
5806
+ }),
5807
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5808
+ ];
5809
+ postInstructions = [
5810
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5811
+ ];
5931
5812
  }
5932
- preInstructions = [
5933
- web3_js_1.SystemProgram.createAccount({
5934
- fromPubkey: publicKey,
5935
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5936
- lamports: lamports,
5937
- space: 165,
5938
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5939
- }),
5940
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5941
- ];
5942
- postInstructions = [
5943
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5944
- ];
5945
- return [3, 5];
5946
- case 2:
5947
- 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);
5948
- _a = createUserATA;
5949
- if (!_a) return [3, 4];
5950
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
5951
- case 3:
5952
- _a = !(_b.sent());
5953
- _b.label = 4;
5954
- case 4:
5955
- if (_a) {
5956
- 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));
5813
+ else {
5814
+ 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);
5815
+ 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));
5957
5816
  }
5958
- _b.label = 5;
5959
- case 5:
5960
5817
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
5961
5818
  orderAccount = poolConfig.getOrderFromMarketPk(publicKey, marketAccount);
5962
5819
  return [4, this.program.methods
@@ -5990,15 +5847,15 @@ var PerpetualsClient = (function () {
5990
5847
  receivingMint: poolConfig.getTokenFromSymbol(reserveSymbol).mintKey
5991
5848
  })
5992
5849
  .instruction()];
5993
- case 6:
5994
- editLimitOrder = _b.sent();
5850
+ case 2:
5851
+ editLimitOrder = _a.sent();
5995
5852
  instructions.push(editLimitOrder);
5996
- return [3, 8];
5997
- case 7:
5998
- err_31 = _b.sent();
5853
+ return [3, 4];
5854
+ case 3:
5855
+ err_31 = _a.sent();
5999
5856
  console.log("perpClient editLimitOrder error:: ", err_31);
6000
5857
  throw err_31;
6001
- case 8: return [2, {
5858
+ case 4: return [2, {
6002
5859
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6003
5860
  additionalSigners: additionalSigners
6004
5861
  }];
@@ -6147,12 +6004,12 @@ var PerpetualsClient = (function () {
6147
6004
  });
6148
6005
  });
6149
6006
  };
6150
- this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6007
+ this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6151
6008
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_34;
6152
6009
  return __generator(this, function (_a) {
6153
6010
  switch (_a.label) {
6154
6011
  case 0:
6155
- publicKey = this.provider.wallet.publicKey;
6012
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6156
6013
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6157
6014
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6158
6015
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6206,12 +6063,12 @@ var PerpetualsClient = (function () {
6206
6063
  }
6207
6064
  });
6208
6065
  }); };
6209
- this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6066
+ this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6210
6067
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_35;
6211
6068
  return __generator(this, function (_a) {
6212
6069
  switch (_a.label) {
6213
6070
  case 0:
6214
- publicKey = this.provider.wallet.publicKey;
6071
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6215
6072
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6216
6073
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6217
6074
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6264,12 +6121,12 @@ var PerpetualsClient = (function () {
6264
6121
  }
6265
6122
  });
6266
6123
  }); };
6267
- this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6124
+ this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6268
6125
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_36;
6269
6126
  return __generator(this, function (_a) {
6270
6127
  switch (_a.label) {
6271
6128
  case 0:
6272
- publicKey = this.provider.wallet.publicKey;
6129
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6273
6130
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6274
6131
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6275
6132
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6308,12 +6165,12 @@ var PerpetualsClient = (function () {
6308
6165
  }
6309
6166
  });
6310
6167
  }); };
6311
- this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6168
+ this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6312
6169
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_37;
6313
6170
  return __generator(this, function (_a) {
6314
6171
  switch (_a.label) {
6315
6172
  case 0:
6316
- publicKey = this.provider.wallet.publicKey;
6173
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6317
6174
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6318
6175
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6319
6176
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6356,13 +6213,13 @@ var PerpetualsClient = (function () {
6356
6213
  args_1[_i - 9] = arguments[_i];
6357
6214
  }
6358
6215
  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) {
6359
- 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;
6216
+ 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;
6360
6217
  if (createUserATA === void 0) { createUserATA = true; }
6361
6218
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6362
6219
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6363
6220
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6364
- return __generator(this, function (_e) {
6365
- switch (_e.label) {
6221
+ return __generator(this, function (_c) {
6222
+ switch (_c.label) {
6366
6223
  case 0:
6367
6224
  payerPubkey = this.provider.wallet.publicKey;
6368
6225
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6375,42 +6232,23 @@ var PerpetualsClient = (function () {
6375
6232
  additionalSigners = [];
6376
6233
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
6377
6234
  receivingToken = poolConfig.getTokenFromSymbol(receivingSymbol);
6378
- _e.label = 1;
6235
+ _c.label = 1;
6379
6236
  case 1:
6380
- _e.trys.push([1, 9, , 10]);
6381
- if (!false) return [3, 2];
6382
- return [3, 7];
6383
- case 2:
6384
- 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);
6385
- _a = createUserATA;
6386
- if (!_a) return [3, 4];
6387
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6388
- case 3:
6389
- _a = !(_e.sent());
6390
- _e.label = 4;
6391
- case 4:
6392
- if (_a) {
6393
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey));
6237
+ _c.trys.push([1, 3, , 4]);
6238
+ if (false) {
6394
6239
  }
6395
- 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);
6396
- _b = createUserATA;
6397
- if (!_b) return [3, 6];
6398
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccountCollateral, this.provider.connection)];
6399
- case 5:
6400
- _b = !(_e.sent());
6401
- _e.label = 6;
6402
- case 6:
6403
- if (_b) {
6404
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
6240
+ else {
6241
+ 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);
6242
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, spl_token_1.TOKEN_PROGRAM_ID));
6243
+ 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);
6244
+ 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));
6405
6245
  }
6406
- _e.label = 7;
6407
- case 7:
6408
6246
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6409
6247
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6410
6248
  custodyAccountMetas = [];
6411
6249
  custodyOracleAccountMetas = [];
6412
- for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
6413
- custody = _d[_c];
6250
+ for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
6251
+ custody = _b[_a];
6414
6252
  custodyAccountMetas.push({
6415
6253
  pubkey: custody.custodyAccount,
6416
6254
  isSigner: false,
@@ -6457,15 +6295,15 @@ var PerpetualsClient = (function () {
6457
6295
  })
6458
6296
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6459
6297
  .instruction()];
6460
- case 8:
6461
- executeTriggerWithSwap = _e.sent();
6298
+ case 2:
6299
+ executeTriggerWithSwap = _c.sent();
6462
6300
  instructions.push(executeTriggerWithSwap);
6463
- return [3, 10];
6464
- case 9:
6465
- err_38 = _e.sent();
6301
+ return [3, 4];
6302
+ case 3:
6303
+ err_38 = _c.sent();
6466
6304
  console.log("perpClient executeTriggerWithSwap error:: ", err_38);
6467
6305
  throw err_38;
6468
- case 10: return [2, {
6306
+ case 4: return [2, {
6469
6307
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6470
6308
  additionalSigners: additionalSigners
6471
6309
  }];
@@ -6479,13 +6317,13 @@ var PerpetualsClient = (function () {
6479
6317
  args_1[_i - 8] = arguments[_i];
6480
6318
  }
6481
6319
  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) {
6482
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_39;
6320
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeTriggerOrder, err_39;
6483
6321
  if (createUserATA === void 0) { createUserATA = true; }
6484
6322
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6485
6323
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6486
6324
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6487
- return __generator(this, function (_b) {
6488
- switch (_b.label) {
6325
+ return __generator(this, function (_a) {
6326
+ switch (_a.label) {
6489
6327
  case 0:
6490
6328
  payerPubkey = this.provider.wallet.publicKey;
6491
6329
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6495,25 +6333,15 @@ var PerpetualsClient = (function () {
6495
6333
  instructions = [];
6496
6334
  postInstructions = [];
6497
6335
  additionalSigners = [];
6498
- _b.label = 1;
6336
+ _a.label = 1;
6499
6337
  case 1:
6500
- _b.trys.push([1, 7, , 8]);
6501
- if (!false) return [3, 2];
6502
- return [3, 5];
6503
- case 2:
6504
- 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);
6505
- _a = createUserATA;
6506
- if (!_a) return [3, 4];
6507
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6508
- case 3:
6509
- _a = !(_b.sent());
6510
- _b.label = 4;
6511
- case 4:
6512
- if (_a) {
6513
- 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));
6338
+ _a.trys.push([1, 3, , 4]);
6339
+ if (false) {
6340
+ }
6341
+ else {
6342
+ 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);
6343
+ 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));
6514
6344
  }
6515
- _b.label = 5;
6516
- case 5:
6517
6345
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6518
6346
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6519
6347
  return [4, this.program.methods
@@ -6545,15 +6373,15 @@ var PerpetualsClient = (function () {
6545
6373
  })
6546
6374
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6547
6375
  .instruction()];
6548
- case 6:
6549
- executeTriggerOrder = _b.sent();
6376
+ case 2:
6377
+ executeTriggerOrder = _a.sent();
6550
6378
  instructions.push(executeTriggerOrder);
6551
- return [3, 8];
6552
- case 7:
6553
- err_39 = _b.sent();
6379
+ return [3, 4];
6380
+ case 3:
6381
+ err_39 = _a.sent();
6554
6382
  console.log("perpClient executeTriggerOrder error:: ", err_39);
6555
6383
  throw err_39;
6556
- case 8: return [2, {
6384
+ case 4: return [2, {
6557
6385
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6558
6386
  additionalSigners: additionalSigners
6559
6387
  }];
@@ -6566,16 +6394,16 @@ var PerpetualsClient = (function () {
6566
6394
  for (var _i = 5; _i < arguments.length; _i++) {
6567
6395
  args_1[_i - 5] = arguments[_i];
6568
6396
  }
6569
- 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) {
6570
- 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;
6397
+ 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) {
6398
+ 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;
6571
6399
  if (useFeesPool === void 0) { useFeesPool = false; }
6572
6400
  if (createUserATA === void 0) { createUserATA = true; }
6573
6401
  if (unWrapSol === void 0) { unWrapSol = false; }
6574
6402
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
6575
6403
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6576
6404
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
6577
- return __generator(this, function (_g) {
6578
- switch (_g.label) {
6405
+ return __generator(this, function (_f) {
6406
+ switch (_f.label) {
6579
6407
  case 0:
6580
6408
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
6581
6409
  if (!userInputCustodyConfig) {
@@ -6585,31 +6413,26 @@ var PerpetualsClient = (function () {
6585
6413
  if (!userOutputCustodyConfig) {
6586
6414
  throw "userOutputCustodyConfig not found";
6587
6415
  }
6588
- publicKey = this.provider.wallet.publicKey;
6416
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6589
6417
  preInstructions = [];
6590
6418
  instructions = [];
6591
6419
  postInstructions = [];
6592
6420
  additionalSigners = [];
6593
- if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 5];
6421
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
6594
6422
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
6595
6423
  case 1:
6596
- wsolAssociatedTokenAccount = _g.sent();
6597
- return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
6598
- case 2:
6599
- wsolATAExist = _g.sent();
6600
- if (!wsolATAExist) {
6601
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
6602
- }
6603
- if (!!skipBalanceChecks) return [3, 4];
6424
+ wsolAssociatedTokenAccount = _f.sent();
6425
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID));
6426
+ if (!!skipBalanceChecks) return [3, 3];
6604
6427
  _a = anchor_1.BN.bind;
6605
6428
  return [4, this.provider.connection.getBalance(publicKey)];
6606
- case 3:
6607
- unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _g.sent()]))();
6429
+ case 2:
6430
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
6608
6431
  if (unWrappedSolBalance.lt(amountIn)) {
6609
6432
  throw "Insufficient SOL Funds";
6610
6433
  }
6611
- _g.label = 4;
6612
- case 4:
6434
+ _f.label = 3;
6435
+ case 3:
6613
6436
  instructions.push(web3_js_1.SystemProgram.transfer({
6614
6437
  fromPubkey: publicKey,
6615
6438
  toPubkey: wsolAssociatedTokenAccount,
@@ -6619,7 +6442,7 @@ var PerpetualsClient = (function () {
6619
6442
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6620
6443
  additionalSigners: additionalSigners
6621
6444
  }];
6622
- case 5:
6445
+ case 4:
6623
6446
  if (userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL') {
6624
6447
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
6625
6448
  wsolAssociatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, publicKey, true);
@@ -6630,20 +6453,20 @@ var PerpetualsClient = (function () {
6630
6453
  additionalSigners: additionalSigners
6631
6454
  }];
6632
6455
  }
6633
- _g.label = 6;
6634
- case 6:
6635
- _g.trys.push([6, 19, , 20]);
6636
- if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
6456
+ _f.label = 5;
6457
+ case 5:
6458
+ _f.trys.push([5, 16, , 17]);
6459
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 8];
6637
6460
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
6638
6461
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
6639
- case 7:
6640
- accCreationLamports = (_g.sent());
6462
+ case 6:
6463
+ accCreationLamports = (_f.sent());
6641
6464
  console.log("accCreationLamports:", accCreationLamports);
6642
6465
  lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
6643
6466
  _b = anchor_1.BN.bind;
6644
6467
  return [4, this.provider.connection.getBalance(publicKey)];
6645
- case 8:
6646
- unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _g.sent()]))();
6468
+ case 7:
6469
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
6647
6470
  if (unWrappedSolBalance.lt(amountIn)) {
6648
6471
  throw "Insufficient SOL Funds";
6649
6472
  }
@@ -6665,25 +6488,25 @@ var PerpetualsClient = (function () {
6665
6488
  postInstructions = [
6666
6489
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6667
6490
  ];
6668
- return [3, 12];
6669
- case 9:
6491
+ return [3, 11];
6492
+ case 8:
6670
6493
  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);
6671
6494
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
6672
- case 10:
6673
- if (!(_g.sent())) {
6495
+ case 9:
6496
+ if (!(_f.sent())) {
6674
6497
  throw "Insufficient Funds , Token Account doesn't exist";
6675
6498
  }
6676
- if (!!skipBalanceChecks) return [3, 12];
6499
+ if (!!skipBalanceChecks) return [3, 11];
6677
6500
  _c = anchor_1.BN.bind;
6678
6501
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
6679
- case 11:
6680
- tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
6502
+ case 10:
6503
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
6681
6504
  if (tokenAccountBalance.lt(amountIn)) {
6682
6505
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
6683
6506
  }
6684
- _g.label = 12;
6685
- case 12:
6686
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 13];
6507
+ _f.label = 11;
6508
+ case 11:
6509
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 12];
6687
6510
  lamports = (this.minimumBalanceForRentExemptAccountLamports);
6688
6511
  if (!ephemeralSignerPubkey) {
6689
6512
  wrappedSolAccount = new web3_js_1.Keypair();
@@ -6703,26 +6526,17 @@ var PerpetualsClient = (function () {
6703
6526
  postInstructions = [
6704
6527
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6705
6528
  ];
6706
- return [3, 17];
6707
- 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)];
6529
+ return [3, 14];
6530
+ 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)];
6531
+ case 13:
6532
+ userOutputTokenAccount = _f.sent();
6533
+ 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));
6534
+ _f.label = 14;
6708
6535
  case 14:
6709
- userOutputTokenAccount = _g.sent();
6710
- _d = createUserATA;
6711
- if (!_d) return [3, 16];
6712
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
6713
- case 15:
6714
- _d = !(_g.sent());
6715
- _g.label = 16;
6716
- case 16:
6717
- if (_d) {
6718
- 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));
6719
- }
6720
- _g.label = 17;
6721
- case 17:
6722
6536
  custodyAccountMetas = [];
6723
6537
  custodyOracleAccountMetas = [];
6724
- for (_e = 0, _f = poolConfig.custodies; _e < _f.length; _e++) {
6725
- custody = _f[_e];
6538
+ for (_d = 0, _e = poolConfig.custodies; _d < _e.length; _d++) {
6539
+ custody = _e[_d];
6726
6540
  custodyAccountMetas.push({
6727
6541
  pubkey: custody.custodyAccount,
6728
6542
  isSigner: false,
@@ -6770,19 +6584,19 @@ var PerpetualsClient = (function () {
6770
6584
  })
6771
6585
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
6772
6586
  .instruction()];
6773
- case 18:
6774
- inx = _g.sent();
6587
+ case 15:
6588
+ inx = _f.sent();
6775
6589
  instructions.push(inx);
6776
6590
  if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
6777
6591
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
6778
6592
  instructions.push(closeWsolATAIns);
6779
6593
  }
6780
- return [3, 20];
6781
- case 19:
6782
- err_40 = _g.sent();
6594
+ return [3, 17];
6595
+ case 16:
6596
+ err_40 = _f.sent();
6783
6597
  console.error("perpClient Swap error:: ", err_40);
6784
6598
  throw err_40;
6785
- case 20: return [2, {
6599
+ case 17: return [2, {
6786
6600
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6787
6601
  additionalSigners: additionalSigners
6788
6602
  }];
@@ -7860,15 +7674,11 @@ var PerpetualsClient = (function () {
7860
7674
  additionalSigners = [];
7861
7675
  _a.label = 1;
7862
7676
  case 1:
7863
- _a.trys.push([1, 5, , 6]);
7677
+ _a.trys.push([1, 4, , 5]);
7864
7678
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7865
7679
  case 2:
7866
7680
  receivingTokenAccount = _a.sent();
7867
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7868
- case 3:
7869
- if (!(_a.sent())) {
7870
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7871
- }
7681
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7872
7682
  return [4, this.program.methods
7873
7683
  .withdrawInstantFees({})
7874
7684
  .accounts({
@@ -7883,15 +7693,15 @@ var PerpetualsClient = (function () {
7883
7693
  receivingTokenMint: poolConfig.tokenMint,
7884
7694
  })
7885
7695
  .instruction()];
7886
- case 4:
7696
+ case 3:
7887
7697
  withdrawInstantFeeInstruction = _a.sent();
7888
7698
  instructions.push(withdrawInstantFeeInstruction);
7889
- return [3, 6];
7890
- case 5:
7699
+ return [3, 5];
7700
+ case 4:
7891
7701
  err_58 = _a.sent();
7892
7702
  console.log("perpClient withdrawInstantFeeInstruction error:: ", err_58);
7893
7703
  throw err_58;
7894
- case 6: return [2, {
7704
+ case 5: return [2, {
7895
7705
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7896
7706
  additionalSigners: additionalSigners
7897
7707
  }];
@@ -7910,15 +7720,11 @@ var PerpetualsClient = (function () {
7910
7720
  additionalSigners = [];
7911
7721
  _a.label = 1;
7912
7722
  case 1:
7913
- _a.trys.push([1, 5, , 6]);
7723
+ _a.trys.push([1, 4, , 5]);
7914
7724
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7915
7725
  case 2:
7916
7726
  receivingTokenAccount = _a.sent();
7917
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7918
- case 3:
7919
- if (!(_a.sent())) {
7920
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7921
- }
7727
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7922
7728
  return [4, this.program.methods
7923
7729
  .withdrawUnclaimedTokens({})
7924
7730
  .accounts({
@@ -7933,15 +7739,15 @@ var PerpetualsClient = (function () {
7933
7739
  receivingTokenMint: poolConfig.tokenMint,
7934
7740
  })
7935
7741
  .instruction()];
7936
- case 4:
7742
+ case 3:
7937
7743
  withdrawUnclaimedTokensInstruction = _a.sent();
7938
7744
  instructions.push(withdrawUnclaimedTokensInstruction);
7939
- return [3, 6];
7940
- case 5:
7745
+ return [3, 5];
7746
+ case 4:
7941
7747
  err_59 = _a.sent();
7942
7748
  console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_59);
7943
7749
  throw err_59;
7944
- case 6: return [2, {
7750
+ case 5: return [2, {
7945
7751
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7946
7752
  additionalSigners: additionalSigners
7947
7753
  }];