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