flash-sdk 11.6.2-alpha.0 → 11.7.0-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.
@@ -1631,14 +1631,8 @@ var PerpetualsClient = (function () {
1631
1631
  priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1632
1632
  }
1633
1633
  else {
1634
- if (positionAccount.referencePrice.price.gt(positionEntryPrice.price)) {
1635
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: positionAccount.referencePrice.price.sub(positionEntryPrice.price), exponent: positionEntryPrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1636
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1637
- }
1638
- else {
1639
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1640
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1641
- }
1634
+ priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1635
+ priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1642
1636
  }
1643
1637
  }
1644
1638
  else {
@@ -1653,14 +1647,8 @@ var PerpetualsClient = (function () {
1653
1647
  priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1654
1648
  }
1655
1649
  else {
1656
- if (positionEntryPrice.price.gt(positionAccount.referencePrice.price)) {
1657
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: positionEntryPrice.price.sub(positionAccount.referencePrice.price), exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1658
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1659
- }
1660
- else {
1661
- priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1662
- priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1663
- }
1650
+ priceDiffProfit = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1651
+ priceDiffLoss = new OraclePrice_1.OraclePrice({ price: constants_1.BN_ZERO, exponent: exitOraclePrice.exponent, confidence: constants_1.BN_ZERO, timestamp: constants_1.BN_ZERO });
1664
1652
  }
1665
1653
  }
1666
1654
  else {
@@ -2716,7 +2704,7 @@ var PerpetualsClient = (function () {
2716
2704
  for (var _i = 8; _i < arguments.length; _i++) {
2717
2705
  args_1[_i - 8] = arguments[_i];
2718
2706
  }
2719
- 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) {
2707
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, priceWithSlippage_1, collateralWithfee_1, size_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, priceWithSlippage, collateralWithfee, size, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
2720
2708
  var publicKey, targetCustodyConfig, collateralCustodyConfig, collateralToken, marketAccount, userCollateralTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, params, instruction;
2721
2709
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2722
2710
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2725,7 +2713,7 @@ var PerpetualsClient = (function () {
2725
2713
  return __generator(this, function (_c) {
2726
2714
  switch (_c.label) {
2727
2715
  case 0:
2728
- publicKey = this.provider.wallet.publicKey;
2716
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2729
2717
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
2730
2718
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2731
2719
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
@@ -2830,59 +2818,48 @@ var PerpetualsClient = (function () {
2830
2818
  for (var _i = 6; _i < arguments.length; _i++) {
2831
2819
  args_1[_i - 6] = arguments[_i];
2832
2820
  }
2833
- 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) {
2834
- var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2821
+ return __awaiter(_this, __spreadArray([marketSymbol_1, collateralSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (marketSymbol, collateralSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey, userPublicKey) {
2822
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, instruction, closeWsolATAIns, error_1;
2835
2823
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2836
2824
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
2837
2825
  if (createUserATA === void 0) { createUserATA = true; }
2838
2826
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
2839
2827
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
2840
- return __generator(this, function (_b) {
2841
- switch (_b.label) {
2828
+ return __generator(this, function (_a) {
2829
+ switch (_a.label) {
2842
2830
  case 0:
2843
- console.log("close position :::", marketSymbol, poolConfig.getTokenFromSymbol(marketSymbol).mintKey.toBase58());
2844
- publicKey = this.provider.wallet.publicKey;
2831
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2845
2832
  preInstructions = [];
2846
2833
  instructions = [];
2847
2834
  postInstructions = [];
2848
2835
  additionalSigners = [];
2849
- _b.label = 1;
2836
+ _a.label = 1;
2850
2837
  case 1:
2851
- _b.trys.push([1, 7, , 8]);
2852
- if (!(collateralSymbol == 'SOL')) return [3, 2];
2853
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
2854
- if (!ephemeralSignerPubkey) {
2855
- wrappedSolAccount = new web3_js_1.Keypair();
2856
- additionalSigners.push(wrappedSolAccount);
2838
+ _a.trys.push([1, 3, , 4]);
2839
+ if (collateralSymbol == 'SOL') {
2840
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
2841
+ if (!ephemeralSignerPubkey) {
2842
+ wrappedSolAccount = new web3_js_1.Keypair();
2843
+ additionalSigners.push(wrappedSolAccount);
2844
+ }
2845
+ preInstructions = [
2846
+ web3_js_1.SystemProgram.createAccount({
2847
+ fromPubkey: publicKey,
2848
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2849
+ lamports: lamports,
2850
+ space: 165,
2851
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
2852
+ }),
2853
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2854
+ ];
2855
+ postInstructions = [
2856
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2857
+ ];
2857
2858
  }
2858
- preInstructions = [
2859
- web3_js_1.SystemProgram.createAccount({
2860
- fromPubkey: publicKey,
2861
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
2862
- lamports: lamports,
2863
- space: 165,
2864
- programId: spl_token_1.TOKEN_PROGRAM_ID,
2865
- }),
2866
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
2867
- ];
2868
- postInstructions = [
2869
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
2870
- ];
2871
- return [3, 5];
2872
- case 2:
2873
- 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);
2874
- _a = createUserATA;
2875
- if (!_a) return [3, 4];
2876
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
2877
- case 3:
2878
- _a = !(_b.sent());
2879
- _b.label = 4;
2880
- case 4:
2881
- if (_a) {
2882
- 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));
2859
+ else {
2860
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
2861
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
2883
2862
  }
2884
- _b.label = 5;
2885
- case 5:
2886
2863
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
2887
2864
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(marketSymbol).mintKey); });
2888
2865
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -2914,19 +2891,19 @@ var PerpetualsClient = (function () {
2914
2891
  })
2915
2892
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
2916
2893
  .instruction()];
2917
- case 6:
2918
- instruction = _b.sent();
2894
+ case 2:
2895
+ instruction = _a.sent();
2919
2896
  instructions.push(instruction);
2920
2897
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
2921
2898
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
2922
2899
  postInstructions.push(closeWsolATAIns);
2923
2900
  }
2924
- return [3, 8];
2925
- case 7:
2926
- error_1 = _b.sent();
2901
+ return [3, 4];
2902
+ case 3:
2903
+ error_1 = _a.sent();
2927
2904
  console.error("perpclient closePosition error:", error_1);
2928
2905
  throw error_1;
2929
- case 8: return [2, {
2906
+ case 4: return [2, {
2930
2907
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
2931
2908
  additionalSigners: additionalSigners
2932
2909
  }];
@@ -2939,7 +2916,7 @@ var PerpetualsClient = (function () {
2939
2916
  for (var _i = 9; _i < arguments.length; _i++) {
2940
2917
  args_1[_i - 9] = arguments[_i];
2941
2918
  }
2942
- 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) {
2919
+ return __awaiter(_this, __spreadArray([targetTokenSymbol_1, collateralTokenSymbol_1, userInputTokenSymbol_1, amountIn_1, priceWithSlippage_1, sizeAmount_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetTokenSymbol, collateralTokenSymbol, userInputTokenSymbol, amountIn, priceWithSlippage, sizeAmount, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
2943
2920
  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;
2944
2921
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
2945
2922
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -2948,7 +2925,7 @@ var PerpetualsClient = (function () {
2948
2925
  return __generator(this, function (_c) {
2949
2926
  switch (_c.label) {
2950
2927
  case 0:
2951
- publicKey = this.provider.wallet.publicKey;
2928
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
2952
2929
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
2953
2930
  if (!userInputCustodyConfig) {
2954
2931
  throw "userInputCustodyConfig not found";
@@ -2972,7 +2949,7 @@ var PerpetualsClient = (function () {
2972
2949
  additionalSigners = [];
2973
2950
  targetToken = poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol);
2974
2951
  userInputToken = poolConfig.getTokenFromSymbol(userInputTokenSymbol);
2975
- if (!(userInputTokenSymbol == 'SOL')) return [3, 5];
2952
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 3];
2976
2953
  console.log("inputSymbol === SOL", userInputTokenSymbol);
2977
2954
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
2978
2955
  if (!!skipBalanceChecks) return [3, 2];
@@ -3002,41 +2979,33 @@ var PerpetualsClient = (function () {
3002
2979
  postInstructions = [
3003
2980
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3004
2981
  ];
3005
- if (!!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) return [3, 4];
3006
- 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);
3007
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3008
- case 3:
3009
- if (!(_c.sent())) {
3010
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey));
2982
+ if (!poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey.equals(spl_token_1.NATIVE_MINT)) {
2983
+ userOutputTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
2984
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3011
2985
  }
3012
- _c.label = 4;
3013
- case 4: return [3, 10];
3014
- case 5:
2986
+ return [3, 7];
2987
+ case 3:
3015
2988
  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);
3016
- if (!!skipBalanceChecks) return [3, 8];
2989
+ if (!!skipBalanceChecks) return [3, 6];
3017
2990
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3018
- case 6:
2991
+ case 4:
3019
2992
  if (!(_c.sent())) {
3020
2993
  throw "Insufficient Funds , Token Account doesn't exist";
3021
2994
  }
3022
2995
  _b = anchor_1.BN.bind;
3023
2996
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
3024
- case 7:
2997
+ case 5:
3025
2998
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_c.sent()).value.amount]))();
3026
2999
  if (tokenAccountBalance.lt(amountIn)) {
3027
3000
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
3028
3001
  }
3029
- _c.label = 8;
3030
- case 8:
3002
+ _c.label = 6;
3003
+ case 6:
3031
3004
  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);
3032
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
3033
- case 9:
3034
- if (!(_c.sent())) {
3035
- 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));
3036
- }
3037
- _c.label = 10;
3038
- case 10:
3039
- _c.trys.push([10, 12, , 13]);
3005
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).mintKey, poolConfig.getTokenFromSymbol(targetCustodyConfig.symbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3006
+ _c.label = 7;
3007
+ case 7:
3008
+ _c.trys.push([7, 9, , 10]);
3040
3009
  return [4, this.program.methods
3041
3010
  .swapAndOpen({
3042
3011
  amountIn: amountIn,
@@ -3072,15 +3041,15 @@ var PerpetualsClient = (function () {
3072
3041
  })
3073
3042
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3074
3043
  .instruction()];
3075
- case 11:
3044
+ case 8:
3076
3045
  inx = _c.sent();
3077
3046
  instructions.push(inx);
3078
- return [3, 13];
3079
- case 12:
3047
+ return [3, 10];
3048
+ case 9:
3080
3049
  err_3 = _c.sent();
3081
3050
  console.error("perpClient SwapAndOpen error:: ", err_3);
3082
3051
  throw err_3;
3083
- case 13: return [2, {
3052
+ case 10: return [2, {
3084
3053
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3085
3054
  additionalSigners: additionalSigners
3086
3055
  }];
@@ -3093,7 +3062,7 @@ var PerpetualsClient = (function () {
3093
3062
  for (var _i = 7; _i < arguments.length; _i++) {
3094
3063
  args_1[_i - 7] = arguments[_i];
3095
3064
  }
3096
- 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) {
3065
+ return __awaiter(_this, __spreadArray([targetTokenSymbol_1, userOutputTokenSymbol_1, collateralTokenSymbol_1, priceWithSlippage_1, side_1, poolConfig_1, privilege_1], args_1, true), void 0, function (targetTokenSymbol, userOutputTokenSymbol, collateralTokenSymbol, priceWithSlippage, side, poolConfig, privilege, tokenStakeAccount, userReferralAccount, ephemeralSignerPubkey, userPublicKey) {
3097
3066
  var publicKey, userOutputCustodyConfig, collateralCustodyConfig, targetCustodyConfig, marketAccount, positionAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, collateralToken, userOutputToken, lamports, userCollateralTokenAccount, inx, err_4;
3098
3067
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3099
3068
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
@@ -3101,7 +3070,7 @@ var PerpetualsClient = (function () {
3101
3070
  return __generator(this, function (_a) {
3102
3071
  switch (_a.label) {
3103
3072
  case 0:
3104
- publicKey = this.provider.wallet.publicKey;
3073
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3105
3074
  userOutputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey); });
3106
3075
  if (!userOutputCustodyConfig) {
3107
3076
  throw "userOutputCustodyConfig not found";
@@ -3125,46 +3094,37 @@ var PerpetualsClient = (function () {
3125
3094
  additionalSigners = [];
3126
3095
  collateralToken = poolConfig.getTokenFromSymbol(collateralTokenSymbol);
3127
3096
  userOutputToken = poolConfig.getTokenFromSymbol(userOutputTokenSymbol);
3128
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 1];
3129
- console.log("outputSymbol === SOL", userOutputTokenSymbol);
3130
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
3131
- if (!ephemeralSignerPubkey) {
3132
- wrappedSolAccount = new web3_js_1.Keypair();
3097
+ if (userOutputTokenSymbol == 'SOL') {
3098
+ console.log("outputSymbol === SOL", userOutputTokenSymbol);
3099
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
3100
+ if (!ephemeralSignerPubkey) {
3101
+ wrappedSolAccount = new web3_js_1.Keypair();
3102
+ additionalSigners.push(wrappedSolAccount);
3103
+ }
3104
+ preInstructions = [
3105
+ web3_js_1.SystemProgram.createAccount({
3106
+ fromPubkey: publicKey,
3107
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3108
+ lamports: lamports,
3109
+ space: 165,
3110
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3111
+ }),
3112
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3113
+ ];
3114
+ postInstructions = [
3115
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3116
+ ];
3133
3117
  additionalSigners.push(wrappedSolAccount);
3134
3118
  }
3135
- preInstructions = [
3136
- web3_js_1.SystemProgram.createAccount({
3137
- fromPubkey: publicKey,
3138
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3139
- lamports: lamports,
3140
- space: 165,
3141
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3142
- }),
3143
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3144
- ];
3145
- postInstructions = [
3146
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3147
- ];
3148
- additionalSigners.push(wrappedSolAccount);
3149
- return [3, 3];
3150
- case 1:
3151
- 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);
3152
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3153
- case 2:
3154
- if (!(_a.sent())) {
3155
- 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));
3119
+ else {
3120
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(userOutputToken.mintKey, publicKey, true, userOutputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3121
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, userOutputToken.mintKey, userOutputToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3156
3122
  }
3157
- _a.label = 3;
3158
- case 3:
3159
3123
  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);
3160
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3161
- case 4:
3162
- if (!(_a.sent())) {
3163
- 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));
3164
- }
3165
- _a.label = 5;
3166
- case 5:
3167
- _a.trys.push([5, 7, , 8]);
3124
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, collateralToken.mintKey, collateralToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3125
+ _a.label = 1;
3126
+ case 1:
3127
+ _a.trys.push([1, 3, , 4]);
3168
3128
  return [4, this.program.methods
3169
3129
  .closeAndSwap({
3170
3130
  priceWithSlippage: priceWithSlippage,
@@ -3198,15 +3158,15 @@ var PerpetualsClient = (function () {
3198
3158
  })
3199
3159
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
3200
3160
  .instruction()];
3201
- case 6:
3161
+ case 2:
3202
3162
  inx = _a.sent();
3203
3163
  instructions.push(inx);
3204
- return [3, 8];
3205
- case 7:
3164
+ return [3, 4];
3165
+ case 3:
3206
3166
  err_4 = _a.sent();
3207
3167
  console.error("perpClient CloseAndSwap error:: ", err_4);
3208
3168
  throw err_4;
3209
- case 8: return [2, {
3169
+ case 4: return [2, {
3210
3170
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3211
3171
  additionalSigners: additionalSigners
3212
3172
  }];
@@ -3219,14 +3179,14 @@ var PerpetualsClient = (function () {
3219
3179
  for (var _i = 6; _i < arguments.length; _i++) {
3220
3180
  args_1[_i - 6] = arguments[_i];
3221
3181
  }
3222
- 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) {
3182
+ return __awaiter(_this, __spreadArray([collateralWithFee_1, targetSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (collateralWithFee, targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
3223
3183
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, userPayingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, instruction;
3224
3184
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3225
3185
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3226
3186
  return __generator(this, function (_c) {
3227
3187
  switch (_c.label) {
3228
3188
  case 0:
3229
- publicKey = this.provider.wallet.publicKey;
3189
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3230
3190
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3231
3191
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3232
3192
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -3322,14 +3282,14 @@ var PerpetualsClient = (function () {
3322
3282
  for (var _i = 7; _i < arguments.length; _i++) {
3323
3283
  args_1[_i - 7] = arguments[_i];
3324
3284
  }
3325
- 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) {
3285
+ return __awaiter(_this, __spreadArray([targetSymbol_1, inputSymbol_1, collateralSymbol_1, amountIn_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, inputSymbol, collateralSymbol, amountIn, side, positionPubKey, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
3326
3286
  var publicKey, collateralCustodyConfig, targetCustodyConfig, inputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userInputTokenAccount, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, userCollateralTokenAccount, marketAccount, instruction;
3327
3287
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3328
3288
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3329
3289
  return __generator(this, function (_c) {
3330
3290
  switch (_c.label) {
3331
3291
  case 0:
3332
- publicKey = this.provider.wallet.publicKey;
3292
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3333
3293
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3334
3294
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3335
3295
  inputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(inputSymbol).mintKey); });
@@ -3393,11 +3353,7 @@ var PerpetualsClient = (function () {
3393
3353
  _c.label = 6;
3394
3354
  case 6:
3395
3355
  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);
3396
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3397
- case 7:
3398
- if (!(_c.sent())) {
3399
- 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));
3400
- }
3356
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, collateralCustodyConfig.mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3401
3357
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3402
3358
  return [4, this.program.methods.swapAndAddCollateral({
3403
3359
  amountIn: amountIn,
@@ -3425,7 +3381,7 @@ var PerpetualsClient = (function () {
3425
3381
  fundingMint: poolConfig.getTokenFromSymbol(inputSymbol).mintKey,
3426
3382
  })
3427
3383
  .instruction()];
3428
- case 8:
3384
+ case 7:
3429
3385
  instruction = _c.sent();
3430
3386
  instructions.push(instruction);
3431
3387
  return [2, {
@@ -3441,15 +3397,15 @@ var PerpetualsClient = (function () {
3441
3397
  for (var _i = 6; _i < arguments.length; _i++) {
3442
3398
  args_1[_i - 6] = arguments[_i];
3443
3399
  }
3444
- 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) {
3445
- var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, _a, marketAccount, instruction, closeWsolATAIns, error_2;
3400
+ return __awaiter(_this, __spreadArray([collateralDeltaUsd_1, marketSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1], args_1, true), void 0, function (collateralDeltaUsd, marketSymbol, collateralSymbol, side, positionPubKey, poolConfig, createUserATA, closeUsersWSOLATA, ephemeralSignerPubkey, userPublicKey) {
3401
+ var publicKey, collateralCustodyConfig, targetCustodyConfig, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, lamports, marketAccount, instruction, closeWsolATAIns, error_2;
3446
3402
  if (createUserATA === void 0) { createUserATA = true; }
3447
3403
  if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
3448
3404
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3449
- return __generator(this, function (_b) {
3450
- switch (_b.label) {
3405
+ return __generator(this, function (_a) {
3406
+ switch (_a.label) {
3451
3407
  case 0:
3452
- publicKey = this.provider.wallet.publicKey;
3408
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3453
3409
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3454
3410
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3455
3411
  });
@@ -3463,45 +3419,35 @@ var PerpetualsClient = (function () {
3463
3419
  instructions = [];
3464
3420
  postInstructions = [];
3465
3421
  additionalSigners = [];
3466
- _b.label = 1;
3422
+ _a.label = 1;
3467
3423
  case 1:
3468
- _b.trys.push([1, 7, , 8]);
3424
+ _a.trys.push([1, 3, , 4]);
3469
3425
  console.log("removeCollateral -- collateralSymbol:", collateralSymbol);
3470
- if (!(collateralSymbol == 'SOL')) return [3, 2];
3471
- console.log("remove collateral in SOL ...create WSOL temp and close it ");
3472
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3473
- if (!ephemeralSignerPubkey) {
3474
- wrappedSolAccount = new web3_js_1.Keypair();
3475
- additionalSigners.push(wrappedSolAccount);
3426
+ if (collateralSymbol == 'SOL') {
3427
+ console.log("remove collateral in SOL ...create WSOL temp and close it ");
3428
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3429
+ if (!ephemeralSignerPubkey) {
3430
+ wrappedSolAccount = new web3_js_1.Keypair();
3431
+ additionalSigners.push(wrappedSolAccount);
3432
+ }
3433
+ preInstructions = [
3434
+ web3_js_1.SystemProgram.createAccount({
3435
+ fromPubkey: publicKey,
3436
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3437
+ lamports: lamports,
3438
+ space: 165,
3439
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3440
+ }),
3441
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3442
+ ];
3443
+ postInstructions = [
3444
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3445
+ ];
3476
3446
  }
3477
- preInstructions = [
3478
- web3_js_1.SystemProgram.createAccount({
3479
- fromPubkey: publicKey,
3480
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3481
- lamports: lamports,
3482
- space: 165,
3483
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3484
- }),
3485
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3486
- ];
3487
- postInstructions = [
3488
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3489
- ];
3490
- return [3, 5];
3491
- case 2:
3492
- 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);
3493
- _a = createUserATA;
3494
- if (!_a) return [3, 4];
3495
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3496
- case 3:
3497
- _a = !(_b.sent());
3498
- _b.label = 4;
3499
- case 4:
3500
- if (_a) {
3501
- 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));
3447
+ else {
3448
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3449
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3502
3450
  }
3503
- _b.label = 5;
3504
- case 5:
3505
3451
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3506
3452
  return [4, this.program.methods
3507
3453
  .removeCollateral({
@@ -3527,19 +3473,19 @@ var PerpetualsClient = (function () {
3527
3473
  receivingMint: collateralCustodyConfig.mintKey
3528
3474
  })
3529
3475
  .instruction()];
3530
- case 6:
3531
- instruction = _b.sent();
3476
+ case 2:
3477
+ instruction = _a.sent();
3532
3478
  instructions.push(instruction);
3533
3479
  if (collateralSymbol == 'WSOL' && closeUsersWSOLATA) {
3534
3480
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
3535
3481
  postInstructions.push(closeWsolATAIns);
3536
3482
  }
3537
- return [3, 8];
3538
- case 7:
3539
- error_2 = _b.sent();
3483
+ return [3, 4];
3484
+ case 3:
3485
+ error_2 = _a.sent();
3540
3486
  console.error("perpclient removeCollateral error:", error_2);
3541
3487
  throw error_2;
3542
- case 8: return [2, {
3488
+ case 4: return [2, {
3543
3489
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3544
3490
  additionalSigners: additionalSigners
3545
3491
  }];
@@ -3552,13 +3498,13 @@ var PerpetualsClient = (function () {
3552
3498
  for (var _i = 6; _i < arguments.length; _i++) {
3553
3499
  args_1[_i - 6] = arguments[_i];
3554
3500
  }
3555
- 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) {
3501
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, outputSymbol_1, collateralDeltaUsd_1, side_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, outputSymbol, collateralDeltaUsd, side, poolConfig, ephemeralSignerPubkey, userPublicKey) {
3556
3502
  var publicKey, targetCustodyConfig, collateralCustodyConfig, outputCustodyConfig, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userReceivingTokenAccount, lamports, userCollateralTokenAccount, marketAccount, positionAccount, instruction;
3557
3503
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
3558
3504
  return __generator(this, function (_a) {
3559
3505
  switch (_a.label) {
3560
3506
  case 0:
3561
- publicKey = this.provider.wallet.publicKey;
3507
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3562
3508
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
3563
3509
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
3564
3510
  outputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outputSymbol).mintKey); });
@@ -3569,42 +3515,33 @@ var PerpetualsClient = (function () {
3569
3515
  instructions = [];
3570
3516
  postInstructions = [];
3571
3517
  additionalSigners = [];
3572
- if (!(outputSymbol == 'SOL')) return [3, 1];
3573
- console.log("outputSymbol === SOL", outputSymbol);
3574
- lamports = this.minimumBalanceForRentExemptAccountLamports;
3575
- if (!ephemeralSignerPubkey) {
3576
- wrappedSolAccount = new web3_js_1.Keypair();
3577
- additionalSigners.push(wrappedSolAccount);
3518
+ if (outputSymbol == 'SOL') {
3519
+ console.log("outputSymbol === SOL", outputSymbol);
3520
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
3521
+ if (!ephemeralSignerPubkey) {
3522
+ wrappedSolAccount = new web3_js_1.Keypair();
3523
+ additionalSigners.push(wrappedSolAccount);
3524
+ }
3525
+ preInstructions = [
3526
+ web3_js_1.SystemProgram.createAccount({
3527
+ fromPubkey: publicKey,
3528
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3529
+ lamports: lamports,
3530
+ space: 165,
3531
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
3532
+ }),
3533
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3534
+ ];
3535
+ postInstructions = [
3536
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3537
+ ];
3578
3538
  }
3579
- preInstructions = [
3580
- web3_js_1.SystemProgram.createAccount({
3581
- fromPubkey: publicKey,
3582
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
3583
- lamports: lamports,
3584
- space: 165,
3585
- programId: spl_token_1.TOKEN_PROGRAM_ID,
3586
- }),
3587
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
3588
- ];
3589
- postInstructions = [
3590
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3591
- ];
3592
- return [3, 3];
3593
- case 1:
3594
- 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);
3595
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
3596
- case 2:
3597
- if (!(_a.sent())) {
3598
- 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));
3539
+ else {
3540
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(outputSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(outputSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
3541
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(outputSymbol).mintKey, poolConfig.getTokenFromSymbol(outputSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3599
3542
  }
3600
- _a.label = 3;
3601
- case 3:
3602
3543
  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);
3603
- return [4, (0, utils_1.checkIfAccountExists)(userCollateralTokenAccount, this.provider.connection)];
3604
- case 4:
3605
- if (!(_a.sent())) {
3606
- 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));
3607
- }
3544
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userCollateralTokenAccount, publicKey, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
3608
3545
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
3609
3546
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
3610
3547
  return [4, this.program.methods
@@ -3638,7 +3575,7 @@ var PerpetualsClient = (function () {
3638
3575
  collateralTokenProgram: poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID
3639
3576
  })
3640
3577
  .instruction()];
3641
- case 5:
3578
+ case 1:
3642
3579
  instruction = _a.sent();
3643
3580
  instructions.push(instruction);
3644
3581
  return [2, {
@@ -3654,14 +3591,14 @@ var PerpetualsClient = (function () {
3654
3591
  for (var _i = 8; _i < arguments.length; _i++) {
3655
3592
  args_1[_i - 8] = arguments[_i];
3656
3593
  }
3657
- 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) {
3594
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, positionPubKey_1, side_1, poolConfig_1, priceWithSlippage_1, sizeDelta_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, positionPubKey, side, poolConfig, priceWithSlippage, sizeDelta, privilege, tokenStakeAccount, userReferralAccount, userPublicKey) {
3658
3595
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3659
3596
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3660
3597
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3661
3598
  return __generator(this, function (_a) {
3662
3599
  switch (_a.label) {
3663
3600
  case 0:
3664
- publicKey = this.provider.wallet.publicKey;
3601
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3665
3602
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3666
3603
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3667
3604
  });
@@ -3718,14 +3655,14 @@ var PerpetualsClient = (function () {
3718
3655
  for (var _i = 8; _i < arguments.length; _i++) {
3719
3656
  args_1[_i - 8] = arguments[_i];
3720
3657
  }
3721
- 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) {
3658
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, side_1, positionPubKey_1, poolConfig_1, priceWithSlippage_1, sizeDelta_1, privilege_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, side, positionPubKey, poolConfig, priceWithSlippage, sizeDelta, privilege, tokenStakeAccount, userReferralAccount, userPublicKey) {
3722
3659
  var publicKey, collateralCustodyConfig, targetCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, instruction;
3723
3660
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
3724
3661
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
3725
3662
  return __generator(this, function (_a) {
3726
3663
  switch (_a.label) {
3727
3664
  case 0:
3728
- publicKey = this.provider.wallet.publicKey;
3665
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3729
3666
  collateralCustodyConfig = poolConfig.custodies.find(function (i) {
3730
3667
  return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey);
3731
3668
  });
@@ -3782,7 +3719,7 @@ var PerpetualsClient = (function () {
3782
3719
  for (var _i = 4; _i < arguments.length; _i++) {
3783
3720
  args_1[_i - 4] = arguments[_i];
3784
3721
  }
3785
- 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) {
3722
+ return __awaiter(_this, __spreadArray([payTokenSymbol_1, tokenAmountIn_1, minLpAmountOut_1, poolConfig_1], args_1, true), void 0, function (payTokenSymbol, tokenAmountIn, minLpAmountOut, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, isWhitelistedUser, userPublicKey) {
3786
3723
  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;
3787
3724
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
3788
3725
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
@@ -3790,7 +3727,7 @@ var PerpetualsClient = (function () {
3790
3727
  return __generator(this, function (_g) {
3791
3728
  switch (_g.label) {
3792
3729
  case 0:
3793
- publicKey = this.provider.wallet.publicKey;
3730
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
3794
3731
  payTokenCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(payTokenSymbol).mintKey); });
3795
3732
  if (!payTokenCustodyConfig) {
3796
3733
  throw "payTokenCustodyConfig not found";
@@ -3802,7 +3739,7 @@ var PerpetualsClient = (function () {
3802
3739
  payToken = poolConfig.getTokenFromSymbol(payTokenSymbol);
3803
3740
  _g.label = 1;
3804
3741
  case 1:
3805
- _g.trys.push([1, 10, , 11]);
3742
+ _g.trys.push([1, 9, , 10]);
3806
3743
  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);
3807
3744
  lpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
3808
3745
  custodyAccountMetas = [];
@@ -3829,24 +3766,20 @@ var PerpetualsClient = (function () {
3829
3766
  isWritable: false,
3830
3767
  });
3831
3768
  }
3832
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3833
- case 2:
3834
- if (!(_g.sent())) {
3835
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3836
- }
3837
- if (!(payTokenSymbol == 'SOL')) return [3, 5];
3769
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3770
+ if (!(payTokenSymbol == 'SOL')) return [3, 4];
3838
3771
  console.log("payTokenSymbol === SOL", payTokenSymbol);
3839
3772
  lamports = tokenAmountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3840
- if (!!skipBalanceChecks) return [3, 4];
3773
+ if (!!skipBalanceChecks) return [3, 3];
3841
3774
  _e = anchor_1.BN.bind;
3842
3775
  return [4, this.provider.connection.getBalance(publicKey)];
3843
- case 3:
3776
+ case 2:
3844
3777
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _g.sent()]))();
3845
3778
  if (unWrappedSolBalance.lt(lamports)) {
3846
3779
  throw "Insufficient SOL Funds";
3847
3780
  }
3848
- _g.label = 4;
3849
- case 4:
3781
+ _g.label = 3;
3782
+ case 3:
3850
3783
  if (!ephemeralSignerPubkey) {
3851
3784
  wrappedSolAccount = new web3_js_1.Keypair();
3852
3785
  additionalSigners.push(wrappedSolAccount);
@@ -3864,23 +3797,23 @@ var PerpetualsClient = (function () {
3864
3797
  postInstructions = [
3865
3798
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3866
3799
  ];
3867
- return [3, 8];
3868
- case 5:
3869
- if (!!skipBalanceChecks) return [3, 8];
3800
+ return [3, 7];
3801
+ case 4:
3802
+ if (!!skipBalanceChecks) return [3, 7];
3870
3803
  return [4, (0, utils_1.checkIfAccountExists)(userPayingTokenAccount, this.provider.connection)];
3871
- case 6:
3804
+ case 5:
3872
3805
  if (!(_g.sent())) {
3873
3806
  throw "Insufficient Funds , token Account doesn't exist";
3874
3807
  }
3875
3808
  _f = anchor_1.BN.bind;
3876
3809
  return [4, this.provider.connection.getTokenAccountBalance(userPayingTokenAccount)];
3877
- case 7:
3810
+ case 6:
3878
3811
  tokenAccountBalance = new (_f.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
3879
3812
  if (tokenAccountBalance.lt(tokenAmountIn)) {
3880
3813
  throw "Insufficient Funds need more ".concat(tokenAmountIn.sub(tokenAccountBalance), " tokens");
3881
3814
  }
3882
- _g.label = 8;
3883
- case 8:
3815
+ _g.label = 7;
3816
+ case 7:
3884
3817
  whitelistPda = this.findProgramAddress("whitelist", [publicKey]).publicKey;
3885
3818
  whitelistMeta = {
3886
3819
  pubkey: whitelistPda,
@@ -3912,15 +3845,15 @@ var PerpetualsClient = (function () {
3912
3845
  })
3913
3846
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
3914
3847
  .instruction()];
3915
- case 9:
3848
+ case 8:
3916
3849
  instruction = _g.sent();
3917
3850
  instructions.push(instruction);
3918
- return [3, 11];
3919
- case 10:
3851
+ return [3, 10];
3852
+ case 9:
3920
3853
  err_5 = _g.sent();
3921
3854
  console.error("perpClient addLiquidity error:: ", err_5);
3922
3855
  throw err_5;
3923
- case 11: return [2, {
3856
+ case 10: return [2, {
3924
3857
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
3925
3858
  additionalSigners: additionalSigners
3926
3859
  }];
@@ -3953,7 +3886,7 @@ var PerpetualsClient = (function () {
3953
3886
  inputToken = poolConfig.getTokenFromSymbol(inputSymbol);
3954
3887
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
3955
3888
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), poolConfig.poolAddress.toBuffer(), lpTokenMint.toBuffer()], this.programId)[0];
3956
- if (!(inputSymbol == 'SOL')) return [3, 4];
3889
+ if (!(inputSymbol == 'SOL')) return [3, 3];
3957
3890
  console.log("inputSymbol === SOL", inputSymbol);
3958
3891
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
3959
3892
  console.log("lamports:", lamports.toNumber());
@@ -3966,11 +3899,8 @@ var PerpetualsClient = (function () {
3966
3899
  throw "Insufficient SOL Funds";
3967
3900
  }
3968
3901
  _g.label = 2;
3969
- case 2: return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
3970
- case 3:
3971
- if (!(_g.sent())) {
3972
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
3973
- }
3902
+ case 2:
3903
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
3974
3904
  if (!ephemeralSignerPubkey) {
3975
3905
  wrappedSolAccount = new web3_js_1.Keypair();
3976
3906
  additionalSigners.push(wrappedSolAccount);
@@ -3988,24 +3918,24 @@ var PerpetualsClient = (function () {
3988
3918
  postInstructions = [
3989
3919
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
3990
3920
  ];
3991
- return [3, 7];
3992
- case 4:
3921
+ return [3, 6];
3922
+ case 3:
3993
3923
  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);
3994
- if (!!skipBalanceChecks) return [3, 7];
3924
+ if (!!skipBalanceChecks) return [3, 6];
3995
3925
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
3996
- case 5:
3926
+ case 4:
3997
3927
  if (!(_g.sent())) {
3998
3928
  throw "Insufficient Funds , token Account doesn't exist";
3999
3929
  }
4000
3930
  _b = anchor_1.BN.bind;
4001
3931
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
4002
- case 6:
3932
+ case 5:
4003
3933
  tokenAccountBalance = new (_b.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
4004
3934
  if (tokenAccountBalance.lt(amountIn)) {
4005
3935
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
4006
3936
  }
4007
- _g.label = 7;
4008
- case 7:
3937
+ _g.label = 6;
3938
+ case 6:
4009
3939
  custodyAccountMetas = [];
4010
3940
  custodyOracleAccountMetas = [];
4011
3941
  markets = [];
@@ -4062,7 +3992,7 @@ var PerpetualsClient = (function () {
4062
3992
  })
4063
3993
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4064
3994
  .instruction()];
4065
- case 8:
3995
+ case 7:
4066
3996
  instruction = _g.sent();
4067
3997
  instructions.push(instruction);
4068
3998
  return [2, {
@@ -4545,12 +4475,12 @@ var PerpetualsClient = (function () {
4545
4475
  }
4546
4476
  });
4547
4477
  }); };
4548
- this.unstakeRequest = function (unstakeAmount, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
4478
+ this.unstakeRequest = function (unstakeAmount, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
4549
4479
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, unstakeRequestInstruction, err_13;
4550
4480
  return __generator(this, function (_a) {
4551
4481
  switch (_a.label) {
4552
4482
  case 0:
4553
- publicKey = this.provider.wallet.publicKey;
4483
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4554
4484
  preInstructions = [];
4555
4485
  instructions = [];
4556
4486
  postInstructions = [];
@@ -4596,37 +4526,28 @@ var PerpetualsClient = (function () {
4596
4526
  args_1[_i - 1] = arguments[_i];
4597
4527
  }
4598
4528
  return __awaiter(_this, __spreadArray([poolConfig_1], args_1, true), void 0, function (poolConfig, pendingActivation, deactivated, createUserLPTA, userPublicKey) {
4599
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, pool, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, _a, withdrawStakeInstruction, err_14;
4529
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, lpTokenMint, pool, poolStakedLpVault, flpStakeAccount, userLpTokenAccount, withdrawStakeInstruction, err_14;
4600
4530
  if (pendingActivation === void 0) { pendingActivation = true; }
4601
4531
  if (deactivated === void 0) { deactivated = true; }
4602
4532
  if (createUserLPTA === void 0) { createUserLPTA = true; }
4603
4533
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4604
- return __generator(this, function (_b) {
4605
- switch (_b.label) {
4534
+ return __generator(this, function (_a) {
4535
+ switch (_a.label) {
4606
4536
  case 0:
4607
4537
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4608
4538
  preInstructions = [];
4609
4539
  instructions = [];
4610
4540
  postInstructions = [];
4611
4541
  additionalSigners = [];
4612
- _b.label = 1;
4542
+ _a.label = 1;
4613
4543
  case 1:
4614
- _b.trys.push([1, 5, , 6]);
4544
+ _a.trys.push([1, 3, , 4]);
4615
4545
  lpTokenMint = poolConfig.stakedLpTokenMint;
4616
4546
  pool = poolConfig.poolAddress;
4617
4547
  poolStakedLpVault = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("staked_lp_token_account"), pool.toBuffer(), lpTokenMint.toBuffer()], this.program.programId)[0];
4618
4548
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4619
4549
  userLpTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.stakedLpTokenMint, publicKey, true);
4620
- _a = createUserLPTA;
4621
- if (!_a) return [3, 3];
4622
- return [4, (0, utils_1.checkIfAccountExists)(userLpTokenAccount, this.provider.connection)];
4623
- case 2:
4624
- _a = !(_b.sent());
4625
- _b.label = 3;
4626
- case 3:
4627
- if (_a) {
4628
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userLpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4629
- }
4550
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userLpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4630
4551
  return [4, this.program.methods
4631
4552
  .withdrawStake({
4632
4553
  pendingActivation: pendingActivation,
@@ -4647,15 +4568,15 @@ var PerpetualsClient = (function () {
4647
4568
  lpMint: poolConfig.stakedLpTokenMint,
4648
4569
  })
4649
4570
  .instruction()];
4650
- case 4:
4651
- withdrawStakeInstruction = _b.sent();
4571
+ case 2:
4572
+ withdrawStakeInstruction = _a.sent();
4652
4573
  instructions.push(withdrawStakeInstruction);
4653
- return [3, 6];
4654
- case 5:
4655
- err_14 = _b.sent();
4574
+ return [3, 4];
4575
+ case 3:
4576
+ err_14 = _a.sent();
4656
4577
  console.log("perpClient withdrawStake error:: ", err_14);
4657
4578
  throw err_14;
4658
- case 6: return [2, {
4579
+ case 4: return [2, {
4659
4580
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4660
4581
  additionalSigners: additionalSigners
4661
4582
  }];
@@ -4669,10 +4590,10 @@ var PerpetualsClient = (function () {
4669
4590
  args_1[_i - 3] = arguments[_i];
4670
4591
  }
4671
4592
  return __awaiter(_this, __spreadArray([rewardSymbol_1, poolConfig_1, tokenStakeAccount_1], args_1, true), void 0, function (rewardSymbol, poolConfig, tokenStakeAccount, createUserATA) {
4672
- var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, _a, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4593
+ var publicKey, rewardCustodyMint, rewardCustodyConfig, preInstructions, instructions, postInstructions, additionalSigners, pool, flpStakeAccount, receivingTokenAccount, tokenStakeAccounts, withdrawStakeInstruction, err_15;
4673
4594
  if (createUserATA === void 0) { createUserATA = true; }
4674
- return __generator(this, function (_b) {
4675
- switch (_b.label) {
4595
+ return __generator(this, function (_a) {
4596
+ switch (_a.label) {
4676
4597
  case 0:
4677
4598
  publicKey = this.provider.wallet.publicKey;
4678
4599
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
@@ -4681,22 +4602,13 @@ var PerpetualsClient = (function () {
4681
4602
  instructions = [];
4682
4603
  postInstructions = [];
4683
4604
  additionalSigners = [];
4684
- _b.label = 1;
4605
+ _a.label = 1;
4685
4606
  case 1:
4686
- _b.trys.push([1, 5, , 6]);
4607
+ _a.trys.push([1, 3, , 4]);
4687
4608
  pool = poolConfig.poolAddress;
4688
4609
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), pool.toBuffer()], this.program.programId)[0];
4689
4610
  receivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, publicKey, true);
4690
- _a = createUserATA;
4691
- if (!_a) return [3, 3];
4692
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
4693
- case 2:
4694
- _a = !(_b.sent());
4695
- _b.label = 3;
4696
- case 3:
4697
- if (_a) {
4698
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint));
4699
- }
4611
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
4700
4612
  tokenStakeAccounts = [];
4701
4613
  if (tokenStakeAccount) {
4702
4614
  tokenStakeAccounts.push({
@@ -4725,15 +4637,15 @@ var PerpetualsClient = (function () {
4725
4637
  })
4726
4638
  .remainingAccounts(__spreadArray([], tokenStakeAccounts, true))
4727
4639
  .instruction()];
4728
- case 4:
4729
- withdrawStakeInstruction = _b.sent();
4640
+ case 2:
4641
+ withdrawStakeInstruction = _a.sent();
4730
4642
  instructions.push(withdrawStakeInstruction);
4731
- return [3, 6];
4732
- case 5:
4733
- err_15 = _b.sent();
4643
+ return [3, 4];
4644
+ case 3:
4645
+ err_15 = _a.sent();
4734
4646
  console.log("perpClient withdrawStake error:: ", err_15);
4735
4647
  throw err_15;
4736
- case 6: return [2, {
4648
+ case 4: return [2, {
4737
4649
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4738
4650
  additionalSigners: additionalSigners
4739
4651
  }];
@@ -4792,29 +4704,21 @@ var PerpetualsClient = (function () {
4792
4704
  isWritable: false,
4793
4705
  });
4794
4706
  }
4795
- return [4, (0, utils_1.checkIfAccountExists)(lpTokenAccount, this.provider.connection)];
4796
- case 1:
4797
- if (!(_f.sent())) {
4798
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint));
4799
- }
4800
- return [4, (0, utils_1.checkIfAccountExists)(compoundingTokenAccount, this.provider.connection)];
4801
- case 2:
4802
- if (!(_f.sent())) {
4803
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint));
4804
- }
4805
- if (!(inTokenSymbol == 'SOL')) return [3, 5];
4707
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, lpTokenAccount, publicKey, poolConfig.stakedLpTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4708
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoundingTokenAccount, publicKey, poolConfig.compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
4709
+ if (!(inTokenSymbol == 'SOL')) return [3, 3];
4806
4710
  console.log("inTokenSymbol === SOL", inTokenSymbol);
4807
4711
  lamports = amountIn.add(new anchor_1.BN(this.minimumBalanceForRentExemptAccountLamports));
4808
- if (!!skipBalanceChecks) return [3, 4];
4712
+ if (!!skipBalanceChecks) return [3, 2];
4809
4713
  _e = anchor_1.BN.bind;
4810
4714
  return [4, this.provider.connection.getBalance(publicKey)];
4811
- case 3:
4715
+ case 1:
4812
4716
  unWrappedSolBalance = new (_e.apply(anchor_1.BN, [void 0, _f.sent()]))();
4813
4717
  if (unWrappedSolBalance.lt(lamports)) {
4814
4718
  throw "Insufficient SOL Funds";
4815
4719
  }
4816
- _f.label = 4;
4817
- case 4:
4720
+ _f.label = 2;
4721
+ case 2:
4818
4722
  if (!ephemeralSignerPubkey) {
4819
4723
  wrappedSolAccount = new web3_js_1.Keypair();
4820
4724
  additionalSigners.push(wrappedSolAccount);
@@ -4832,17 +4736,17 @@ var PerpetualsClient = (function () {
4832
4736
  postInstructions = [
4833
4737
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4834
4738
  ];
4835
- return [3, 7];
4836
- case 5:
4837
- if (!!skipBalanceChecks) return [3, 7];
4739
+ return [3, 5];
4740
+ case 3:
4741
+ if (!!skipBalanceChecks) return [3, 5];
4838
4742
  return [4, (0, utils_1.checkIfAccountExists)(fundingAccount, this.provider.connection)];
4839
- case 6:
4743
+ case 4:
4840
4744
  if (!(_f.sent())) {
4841
4745
  throw "Insufficient Funds , token Account doesn't exist";
4842
4746
  }
4843
- _f.label = 7;
4844
- case 7:
4845
- _f.trys.push([7, 9, , 10]);
4747
+ _f.label = 5;
4748
+ case 5:
4749
+ _f.trys.push([5, 7, , 8]);
4846
4750
  if (enableHeapSizeIx) {
4847
4751
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4848
4752
  bytes: 64 * 1024,
@@ -4884,15 +4788,15 @@ var PerpetualsClient = (function () {
4884
4788
  })
4885
4789
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
4886
4790
  .instruction()];
4887
- case 8:
4791
+ case 6:
4888
4792
  addCompoundingLiquidity = _f.sent();
4889
4793
  instructions.push(addCompoundingLiquidity);
4890
- return [3, 10];
4891
- case 9:
4794
+ return [3, 8];
4795
+ case 7:
4892
4796
  err_16 = _f.sent();
4893
4797
  console.log("perpClient addCompoundingLiquidity error:: ", err_16);
4894
- return [3, 10];
4895
- case 10: return [2, {
4798
+ return [3, 8];
4799
+ case 8: return [2, {
4896
4800
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
4897
4801
  additionalSigners: additionalSigners
4898
4802
  }];
@@ -4906,14 +4810,14 @@ var PerpetualsClient = (function () {
4906
4810
  args_1[_i - 5] = arguments[_i];
4907
4811
  }
4908
4812
  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) {
4909
- 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;
4813
+ var publicKey, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, outCustodyConfig, lpTokenMint, compoundingTokenMint, lamports, custodyAccountMetas, custodyOracleAccountMetas, markets, _a, _b, custody, _c, _d, market, compoundingTokenAccount, heapSizeIx, whitelistPda, whitelistMeta, removeCompoundingLiquidity, err_17;
4910
4814
  if (createUserATA === void 0) { createUserATA = true; }
4911
4815
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
4912
4816
  if (userPublicKey === void 0) { userPublicKey = undefined; }
4913
4817
  if (enableHeapSizeIx === void 0) { enableHeapSizeIx = true; }
4914
4818
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
4915
- return __generator(this, function (_f) {
4916
- switch (_f.label) {
4819
+ return __generator(this, function (_e) {
4820
+ switch (_e.label) {
4917
4821
  case 0:
4918
4822
  publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
4919
4823
  preInstructions = [];
@@ -4924,45 +4828,35 @@ var PerpetualsClient = (function () {
4924
4828
  outCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(outTokenSymbol).mintKey); });
4925
4829
  lpTokenMint = poolConfig.stakedLpTokenMint;
4926
4830
  compoundingTokenMint = poolConfig.compoundingTokenMint;
4927
- if (!(outCustodyConfig.symbol == 'SOL')) return [3, 1];
4928
- lamports = this.minimumBalanceForRentExemptAccountLamports;
4929
- if (!ephemeralSignerPubkey) {
4930
- wrappedSolAccount = new web3_js_1.Keypair();
4931
- additionalSigners.push(wrappedSolAccount);
4831
+ if (outCustodyConfig.symbol == 'SOL') {
4832
+ lamports = this.minimumBalanceForRentExemptAccountLamports;
4833
+ if (!ephemeralSignerPubkey) {
4834
+ wrappedSolAccount = new web3_js_1.Keypair();
4835
+ additionalSigners.push(wrappedSolAccount);
4836
+ }
4837
+ preInstructions = [
4838
+ web3_js_1.SystemProgram.createAccount({
4839
+ fromPubkey: publicKey,
4840
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4841
+ lamports: lamports,
4842
+ space: 165,
4843
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
4844
+ }),
4845
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4846
+ ];
4847
+ postInstructions = [
4848
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4849
+ ];
4932
4850
  }
4933
- preInstructions = [
4934
- web3_js_1.SystemProgram.createAccount({
4935
- fromPubkey: publicKey,
4936
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
4937
- lamports: lamports,
4938
- space: 165,
4939
- programId: spl_token_1.TOKEN_PROGRAM_ID,
4940
- }),
4941
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
4942
- ];
4943
- postInstructions = [
4944
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
4945
- ];
4946
- return [3, 4];
4947
- case 1:
4948
- 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);
4949
- _a = createUserATA;
4950
- if (!_a) return [3, 3];
4951
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
4952
- case 2:
4953
- _a = !(_f.sent());
4954
- _f.label = 3;
4955
- case 3:
4956
- if (_a) {
4957
- 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));
4851
+ else {
4852
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(outCustodyConfig.mintKey, publicKey, true, poolConfig.getTokenFromSymbol(outTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
4853
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, outCustodyConfig.mintKey, poolConfig.getTokenFromSymbol(outTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
4958
4854
  }
4959
- _f.label = 4;
4960
- case 4:
4961
4855
  custodyAccountMetas = [];
4962
4856
  custodyOracleAccountMetas = [];
4963
4857
  markets = [];
4964
- for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4965
- custody = _c[_b];
4858
+ for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
4859
+ custody = _b[_a];
4966
4860
  custodyAccountMetas.push({
4967
4861
  pubkey: custody.custodyAccount,
4968
4862
  isSigner: false,
@@ -4974,8 +4868,8 @@ var PerpetualsClient = (function () {
4974
4868
  isWritable: false,
4975
4869
  });
4976
4870
  }
4977
- for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4978
- market = _e[_d];
4871
+ for (_c = 0, _d = poolConfig.markets; _c < _d.length; _c++) {
4872
+ market = _d[_c];
4979
4873
  markets.push({
4980
4874
  pubkey: market.marketAccount,
4981
4875
  isSigner: false,
@@ -4983,9 +4877,9 @@ var PerpetualsClient = (function () {
4983
4877
  });
4984
4878
  }
4985
4879
  compoundingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
4986
- _f.label = 5;
4987
- case 5:
4988
- _f.trys.push([5, 7, , 8]);
4880
+ _e.label = 1;
4881
+ case 1:
4882
+ _e.trys.push([1, 3, , 4]);
4989
4883
  if (enableHeapSizeIx) {
4990
4884
  heapSizeIx = web3_js_1.ComputeBudgetProgram.requestHeapFrame({
4991
4885
  bytes: 64 * 1024,
@@ -5027,15 +4921,15 @@ var PerpetualsClient = (function () {
5027
4921
  })
5028
4922
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
5029
4923
  .instruction()];
5030
- case 6:
5031
- removeCompoundingLiquidity = _f.sent();
4924
+ case 2:
4925
+ removeCompoundingLiquidity = _e.sent();
5032
4926
  instructions.push(removeCompoundingLiquidity);
5033
- return [3, 8];
5034
- case 7:
5035
- err_17 = _f.sent();
4927
+ return [3, 4];
4928
+ case 3:
4929
+ err_17 = _e.sent();
5036
4930
  console.log("perpClient removeCompoundingLiquidity error:: ", err_17);
5037
- return [3, 8];
5038
- case 8: return [2, {
4931
+ return [3, 4];
4932
+ case 4: return [2, {
5039
4933
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5040
4934
  additionalSigners: additionalSigners
5041
4935
  }];
@@ -5048,13 +4942,13 @@ var PerpetualsClient = (function () {
5048
4942
  for (var _i = 3; _i < arguments.length; _i++) {
5049
4943
  args_1[_i - 3] = arguments[_i];
5050
4944
  }
5051
- return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA) {
5052
- 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;
4945
+ return __awaiter(_this, __spreadArray([amount_1, rewardTokenMint_1, poolConfig_1], args_1, true), void 0, function (amount, rewardTokenMint, poolConfig, createUserATA, userPublicKey) {
4946
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, tokenStakeAccount, tokenStakeAccounts, _a, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _b, _c, custody, _d, _e, market, migrateStake, err_18;
5053
4947
  if (createUserATA === void 0) { createUserATA = true; }
5054
- return __generator(this, function (_g) {
5055
- switch (_g.label) {
4948
+ return __generator(this, function (_f) {
4949
+ switch (_f.label) {
5056
4950
  case 0:
5057
- publicKey = this.provider.wallet.publicKey;
4951
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5058
4952
  preInstructions = [];
5059
4953
  instructions = [];
5060
4954
  postInstructions = [];
@@ -5063,27 +4957,18 @@ var PerpetualsClient = (function () {
5063
4957
  lpTokenMint = poolConfig.stakedLpTokenMint;
5064
4958
  compoundingTokenMint = poolConfig.compoundingTokenMint;
5065
4959
  compoudingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(compoundingTokenMint, publicKey, true);
5066
- _a = createUserATA;
5067
- if (!_a) return [3, 2];
5068
- return [4, (0, utils_1.checkIfAccountExists)(compoudingTokenAccount, this.provider.connection)];
5069
- case 1:
5070
- _a = !(_g.sent());
5071
- _g.label = 2;
5072
- case 2:
5073
- if (_a) {
5074
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint));
5075
- }
4960
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, compoudingTokenAccount, publicKey, compoundingTokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5076
4961
  flpStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("stake"), publicKey.toBuffer(), poolConfig.poolAddress.toBuffer()], this.programId)[0];
5077
4962
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), publicKey.toBuffer()], this.programId)[0];
5078
4963
  tokenStakeAccounts = [];
5079
- _b = tokenStakeAccount;
5080
- if (!_b) return [3, 4];
4964
+ _a = tokenStakeAccount;
4965
+ if (!_a) return [3, 2];
5081
4966
  return [4, (0, utils_1.checkIfAccountExists)(tokenStakeAccount, this.provider.connection)];
5082
- case 3:
5083
- _b = (_g.sent());
5084
- _g.label = 4;
5085
- case 4:
5086
- if (_b) {
4967
+ case 1:
4968
+ _a = (_f.sent());
4969
+ _f.label = 2;
4970
+ case 2:
4971
+ if (_a) {
5087
4972
  tokenStakeAccounts.push({
5088
4973
  pubkey: tokenStakeAccount,
5089
4974
  isSigner: false,
@@ -5094,8 +4979,8 @@ var PerpetualsClient = (function () {
5094
4979
  custodyAccountMetas = [];
5095
4980
  custodyOracleAccountMetas = [];
5096
4981
  markets = [];
5097
- for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
5098
- custody = _d[_c];
4982
+ for (_b = 0, _c = poolConfig.custodies; _b < _c.length; _b++) {
4983
+ custody = _c[_b];
5099
4984
  custodyAccountMetas.push({
5100
4985
  pubkey: custody.custodyAccount,
5101
4986
  isSigner: false,
@@ -5107,17 +4992,17 @@ var PerpetualsClient = (function () {
5107
4992
  isWritable: false,
5108
4993
  });
5109
4994
  }
5110
- for (_e = 0, _f = poolConfig.markets; _e < _f.length; _e++) {
5111
- market = _f[_e];
4995
+ for (_d = 0, _e = poolConfig.markets; _d < _e.length; _d++) {
4996
+ market = _e[_d];
5112
4997
  markets.push({
5113
4998
  pubkey: market.marketAccount,
5114
4999
  isSigner: false,
5115
5000
  isWritable: false,
5116
5001
  });
5117
5002
  }
5118
- _g.label = 5;
5119
- case 5:
5120
- _g.trys.push([5, 7, , 8]);
5003
+ _f.label = 3;
5004
+ case 3:
5005
+ _f.trys.push([3, 5, , 6]);
5121
5006
  return [4, this.program.methods
5122
5007
  .migrateStake({
5123
5008
  amount: amount
@@ -5142,15 +5027,15 @@ var PerpetualsClient = (function () {
5142
5027
  })
5143
5028
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), markets, true), tokenStakeAccounts, true))
5144
5029
  .instruction()];
5145
- case 6:
5146
- migrateStake = _g.sent();
5030
+ case 4:
5031
+ migrateStake = _f.sent();
5147
5032
  instructions.push(migrateStake);
5148
- return [3, 8];
5149
- case 7:
5150
- err_18 = _g.sent();
5033
+ return [3, 6];
5034
+ case 5:
5035
+ err_18 = _f.sent();
5151
5036
  console.log("perpClient migrateStake error:: ", err_18);
5152
- return [3, 8];
5153
- case 8: return [2, {
5037
+ return [3, 6];
5038
+ case 6: return [2, {
5154
5039
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5155
5040
  additionalSigners: additionalSigners
5156
5041
  }];
@@ -5158,12 +5043,12 @@ var PerpetualsClient = (function () {
5158
5043
  });
5159
5044
  });
5160
5045
  };
5161
- this.migrateFlp = function (amount, rewardTokenMint, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
5046
+ this.migrateFlp = function (amount, rewardTokenMint, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
5162
5047
  var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustody, lpTokenMint, compoundingTokenMint, compoudingTokenAccount, flpStakeAccount, poolStakedLpVault, custodyAccountMetas, custodyOracleAccountMetas, markets, _i, _a, custody, _b, _c, market, migrateFlp, err_19;
5163
5048
  return __generator(this, function (_d) {
5164
5049
  switch (_d.label) {
5165
5050
  case 0:
5166
- publicKey = this.provider.wallet.publicKey;
5051
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5167
5052
  preInstructions = [];
5168
5053
  instructions = [];
5169
5054
  postInstructions = [];
@@ -5327,14 +5212,10 @@ var PerpetualsClient = (function () {
5327
5212
  additionalSigners = [];
5328
5213
  _a.label = 1;
5329
5214
  case 1:
5330
- _a.trys.push([1, 4, , 5]);
5215
+ _a.trys.push([1, 3, , 4]);
5331
5216
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5332
5217
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5333
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5334
- case 2:
5335
- if (!(_a.sent())) {
5336
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint));
5337
- }
5218
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(feePayer, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5338
5219
  return [4, this.program.methods
5339
5220
  .depositTokenStake({
5340
5221
  depositAmount: depositAmount
@@ -5354,15 +5235,15 @@ var PerpetualsClient = (function () {
5354
5235
  tokenMint: poolConfig.tokenMint,
5355
5236
  })
5356
5237
  .instruction()];
5357
- case 3:
5238
+ case 2:
5358
5239
  depositTokenStakeInstruction = _a.sent();
5359
5240
  instructions.push(depositTokenStakeInstruction);
5360
- return [3, 5];
5361
- case 4:
5241
+ return [3, 4];
5242
+ case 3:
5362
5243
  err_21 = _a.sent();
5363
5244
  console.log("perpClient depositStakingInstruction error:: ", err_21);
5364
5245
  throw err_21;
5365
- case 5: return [2, {
5246
+ case 4: return [2, {
5366
5247
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5367
5248
  additionalSigners: additionalSigners
5368
5249
  }];
@@ -5420,14 +5301,10 @@ var PerpetualsClient = (function () {
5420
5301
  additionalSigners = [];
5421
5302
  _a.label = 1;
5422
5303
  case 1:
5423
- _a.trys.push([1, 4, , 5]);
5304
+ _a.trys.push([1, 3, , 4]);
5424
5305
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5425
5306
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5426
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5427
- case 2:
5428
- if (!(_a.sent())) {
5429
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5430
- }
5307
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5431
5308
  return [4, this.program.methods
5432
5309
  .unstakeTokenInstant({
5433
5310
  unstakeAmount: unstakeAmount
@@ -5446,15 +5323,15 @@ var PerpetualsClient = (function () {
5446
5323
  tokenMint: poolConfig.tokenMint,
5447
5324
  })
5448
5325
  .instruction()];
5449
- case 3:
5326
+ case 2:
5450
5327
  unstakeTokenInstantInstruction = _a.sent();
5451
5328
  instructions.push(unstakeTokenInstantInstruction);
5452
- return [3, 5];
5453
- case 4:
5329
+ return [3, 4];
5330
+ case 3:
5454
5331
  err_23 = _a.sent();
5455
5332
  console.log("perpClient unstakeTokenInstantInstruction error:: ", err_23);
5456
5333
  throw err_23;
5457
- case 5: return [2, {
5334
+ case 4: return [2, {
5458
5335
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5459
5336
  additionalSigners: additionalSigners
5460
5337
  }];
@@ -5472,14 +5349,10 @@ var PerpetualsClient = (function () {
5472
5349
  additionalSigners = [];
5473
5350
  _a.label = 1;
5474
5351
  case 1:
5475
- _a.trys.push([1, 4, , 5]);
5352
+ _a.trys.push([1, 3, , 4]);
5476
5353
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5477
5354
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5478
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5479
- case 2:
5480
- if (!(_a.sent())) {
5481
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint));
5482
- }
5355
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(this.provider.wallet.publicKey, userTokenAccount, owner, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5483
5356
  return [4, this.program.methods
5484
5357
  .withdrawToken({
5485
5358
  withdrawRequestId: withdrawRequestId
@@ -5498,15 +5371,15 @@ var PerpetualsClient = (function () {
5498
5371
  tokenMint: poolConfig.tokenMint,
5499
5372
  })
5500
5373
  .instruction()];
5501
- case 3:
5374
+ case 2:
5502
5375
  withdrawTokenInstruction = _a.sent();
5503
5376
  instructions.push(withdrawTokenInstruction);
5504
- return [3, 5];
5505
- case 4:
5377
+ return [3, 4];
5378
+ case 3:
5506
5379
  err_24 = _a.sent();
5507
5380
  console.log("perpClient withdrawTokenInstruction error:: ", err_24);
5508
5381
  throw err_24;
5509
- case 5: return [2, {
5382
+ case 4: return [2, {
5510
5383
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5511
5384
  additionalSigners: additionalSigners
5512
5385
  }];
@@ -5558,32 +5431,23 @@ var PerpetualsClient = (function () {
5558
5431
  for (var _i = 2; _i < arguments.length; _i++) {
5559
5432
  args_1[_i - 2] = arguments[_i];
5560
5433
  }
5561
- return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA) {
5562
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, _a, collectTokenRewardInstruction, err_26;
5434
+ return __awaiter(_this, __spreadArray([owner_1, poolConfig_1], args_1, true), void 0, function (owner, poolConfig, createUserATA, userPublicKey) {
5435
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, tokenStakeAccount, userTokenAccount, collectTokenRewardInstruction, err_26;
5563
5436
  if (createUserATA === void 0) { createUserATA = true; }
5564
- return __generator(this, function (_b) {
5565
- switch (_b.label) {
5437
+ return __generator(this, function (_a) {
5438
+ switch (_a.label) {
5566
5439
  case 0:
5567
- publicKey = this.provider.wallet.publicKey;
5440
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5568
5441
  preInstructions = [];
5569
5442
  instructions = [];
5570
5443
  postInstructions = [];
5571
5444
  additionalSigners = [];
5572
- _b.label = 1;
5445
+ _a.label = 1;
5573
5446
  case 1:
5574
- _b.trys.push([1, 5, , 6]);
5447
+ _a.trys.push([1, 3, , 4]);
5575
5448
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5576
5449
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.tokenMint, owner, true);
5577
- _a = createUserATA;
5578
- if (!_a) return [3, 3];
5579
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5580
- case 2:
5581
- _a = !(_b.sent());
5582
- _b.label = 3;
5583
- case 3:
5584
- if (_a) {
5585
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint));
5586
- }
5450
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
5587
5451
  return [4, this.program.methods
5588
5452
  .collectTokenReward({})
5589
5453
  .accounts({
@@ -5600,15 +5464,15 @@ var PerpetualsClient = (function () {
5600
5464
  tokenMint: poolConfig.tokenMint,
5601
5465
  })
5602
5466
  .instruction()];
5603
- case 4:
5604
- collectTokenRewardInstruction = _b.sent();
5467
+ case 2:
5468
+ collectTokenRewardInstruction = _a.sent();
5605
5469
  instructions.push(collectTokenRewardInstruction);
5606
- return [3, 6];
5607
- case 5:
5608
- err_26 = _b.sent();
5470
+ return [3, 4];
5471
+ case 3:
5472
+ err_26 = _a.sent();
5609
5473
  console.log("perpClient collectTokenRewardInstruction error:: ", err_26);
5610
5474
  throw err_26;
5611
- case 6: return [2, {
5475
+ case 4: return [2, {
5612
5476
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5613
5477
  additionalSigners: additionalSigners
5614
5478
  }];
@@ -5622,32 +5486,23 @@ var PerpetualsClient = (function () {
5622
5486
  args_1[_i - 3] = arguments[_i];
5623
5487
  }
5624
5488
  return __awaiter(_this, __spreadArray([owner_1, rewardSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rewardSymbol, poolConfig, createUserATA) {
5625
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, _a, collectRevenueInstruction, err_27;
5489
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rewardCustodyMint, tokenStakeAccount, userTokenAccount, collectRevenueInstruction, err_27;
5626
5490
  if (createUserATA === void 0) { createUserATA = true; }
5627
- return __generator(this, function (_b) {
5628
- switch (_b.label) {
5491
+ return __generator(this, function (_a) {
5492
+ switch (_a.label) {
5629
5493
  case 0:
5630
5494
  publicKey = this.provider.wallet.publicKey;
5631
5495
  preInstructions = [];
5632
5496
  instructions = [];
5633
5497
  postInstructions = [];
5634
5498
  additionalSigners = [];
5635
- _b.label = 1;
5499
+ _a.label = 1;
5636
5500
  case 1:
5637
- _b.trys.push([1, 5, , 6]);
5501
+ _a.trys.push([1, 3, , 4]);
5638
5502
  rewardCustodyMint = poolConfig.getTokenFromSymbol(rewardSymbol).mintKey;
5639
5503
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5640
5504
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rewardCustodyMint, owner, true);
5641
- _a = createUserATA;
5642
- if (!_a) return [3, 3];
5643
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5644
- case 2:
5645
- _a = !(_b.sent());
5646
- _b.label = 3;
5647
- case 3:
5648
- if (_a) {
5649
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint));
5650
- }
5505
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rewardCustodyMint, spl_token_1.TOKEN_PROGRAM_ID));
5651
5506
  return [4, this.program.methods
5652
5507
  .collectRevenue({})
5653
5508
  .accounts({
@@ -5664,15 +5519,15 @@ var PerpetualsClient = (function () {
5664
5519
  receivingTokenMint: rewardCustodyMint,
5665
5520
  })
5666
5521
  .instruction()];
5667
- case 4:
5668
- collectRevenueInstruction = _b.sent();
5522
+ case 2:
5523
+ collectRevenueInstruction = _a.sent();
5669
5524
  instructions.push(collectRevenueInstruction);
5670
- return [3, 6];
5671
- case 5:
5672
- err_27 = _b.sent();
5525
+ return [3, 4];
5526
+ case 3:
5527
+ err_27 = _a.sent();
5673
5528
  console.log("perpClient collectRevenueInstruction error:: ", err_27);
5674
5529
  throw err_27;
5675
- case 6: return [2, {
5530
+ case 4: return [2, {
5676
5531
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5677
5532
  additionalSigners: additionalSigners
5678
5533
  }];
@@ -5686,32 +5541,23 @@ var PerpetualsClient = (function () {
5686
5541
  args_1[_i - 3] = arguments[_i];
5687
5542
  }
5688
5543
  return __awaiter(_this, __spreadArray([owner_1, rebateSymbol_1, poolConfig_1], args_1, true), void 0, function (owner, rebateSymbol, poolConfig, createUserATA) {
5689
- var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, _a, collectRebateInstruction, err_28;
5544
+ var publicKey, preInstructions, instructions, postInstructions, additionalSigners, rebateMint, tokenStakeAccount, userTokenAccount, collectRebateInstruction, err_28;
5690
5545
  if (createUserATA === void 0) { createUserATA = true; }
5691
- return __generator(this, function (_b) {
5692
- switch (_b.label) {
5546
+ return __generator(this, function (_a) {
5547
+ switch (_a.label) {
5693
5548
  case 0:
5694
5549
  publicKey = this.provider.wallet.publicKey;
5695
5550
  preInstructions = [];
5696
5551
  instructions = [];
5697
5552
  postInstructions = [];
5698
5553
  additionalSigners = [];
5699
- _b.label = 1;
5554
+ _a.label = 1;
5700
5555
  case 1:
5701
- _b.trys.push([1, 5, , 6]);
5556
+ _a.trys.push([1, 3, , 4]);
5702
5557
  rebateMint = poolConfig.getTokenFromSymbol(rebateSymbol).mintKey;
5703
5558
  tokenStakeAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("token_stake"), owner.toBuffer()], this.programId)[0];
5704
5559
  userTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(rebateMint, owner, true);
5705
- _a = createUserATA;
5706
- if (!_a) return [3, 3];
5707
- return [4, (0, utils_1.checkIfAccountExists)(userTokenAccount, this.provider.connection)];
5708
- case 2:
5709
- _a = !(_b.sent());
5710
- _b.label = 3;
5711
- case 3:
5712
- if (_a) {
5713
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userTokenAccount, publicKey, rebateMint));
5714
- }
5560
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userTokenAccount, publicKey, rebateMint, spl_token_1.TOKEN_PROGRAM_ID));
5715
5561
  return [4, this.program.methods
5716
5562
  .collectRebate()
5717
5563
  .accounts({
@@ -5728,15 +5574,15 @@ var PerpetualsClient = (function () {
5728
5574
  receivingTokenMint: rebateMint,
5729
5575
  })
5730
5576
  .instruction()];
5731
- case 4:
5732
- collectRebateInstruction = _b.sent();
5577
+ case 2:
5578
+ collectRebateInstruction = _a.sent();
5733
5579
  instructions.push(collectRebateInstruction);
5734
- return [3, 6];
5735
- case 5:
5736
- err_28 = _b.sent();
5580
+ return [3, 4];
5581
+ case 3:
5582
+ err_28 = _a.sent();
5737
5583
  console.log("perpClient collectRebateInstruction error:: ", err_28);
5738
5584
  throw err_28;
5739
- case 6: return [2, {
5585
+ case 4: return [2, {
5740
5586
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
5741
5587
  additionalSigners: additionalSigners
5742
5588
  }];
@@ -5796,14 +5642,14 @@ var PerpetualsClient = (function () {
5796
5642
  for (var _i = 11; _i < arguments.length; _i++) {
5797
5643
  args_1[_i - 11] = arguments[_i];
5798
5644
  }
5799
- 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) {
5645
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, reserveSymbol_1, receiveSymbol_1, side_1, limitPrice_1, reserveAmount_1, sizeAmount_1, stopLossPrice_1, takeProfitPrice_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, reserveSymbol, receiveSymbol, side, limitPrice, reserveAmount, sizeAmount, stopLossPrice, takeProfitPrice, poolConfig, skipBalanceChecks, ephemeralSignerPubkey, userPublicKey) {
5800
5646
  var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, userReserveTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, accCreationLamports, lamports, unWrappedSolBalance, _a, tokenAccountBalance, _b, positionAccount, orderAccount, placeLimitOrder, err_30;
5801
5647
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
5802
5648
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5803
5649
  return __generator(this, function (_c) {
5804
5650
  switch (_c.label) {
5805
5651
  case 0:
5806
- publicKey = this.provider.wallet.publicKey;
5652
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5807
5653
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5808
5654
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5809
5655
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5919,14 +5765,14 @@ var PerpetualsClient = (function () {
5919
5765
  for (var _i = 11; _i < arguments.length; _i++) {
5920
5766
  args_1[_i - 11] = arguments[_i];
5921
5767
  }
5922
- 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) {
5923
- var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, _a, positionAccount, orderAccount, editLimitOrder, err_31;
5768
+ return __awaiter(_this, __spreadArray([targetSymbol_1, collateralSymbol_1, reserveSymbol_1, receiveSymbol_1, side_1, orderId_1, limitPrice_1, sizeAmount_1, stopLossPrice_1, takeProfitPrice_1, poolConfig_1], args_1, true), void 0, function (targetSymbol, collateralSymbol, reserveSymbol, receiveSymbol, side, orderId, limitPrice, sizeAmount, stopLossPrice, takeProfitPrice, poolConfig, createUserATA, ephemeralSignerPubkey, userPublicKey) {
5769
+ var publicKey, targetCustodyConfig, reserveCustodyConfig, collateralCustodyConfig, receiveCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, wrappedSolAccount, userReceivingTokenAccount, lamports, positionAccount, orderAccount, editLimitOrder, err_31;
5924
5770
  if (createUserATA === void 0) { createUserATA = true; }
5925
5771
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
5926
- return __generator(this, function (_b) {
5927
- switch (_b.label) {
5772
+ return __generator(this, function (_a) {
5773
+ switch (_a.label) {
5928
5774
  case 0:
5929
- publicKey = this.provider.wallet.publicKey;
5775
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
5930
5776
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
5931
5777
  reserveCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey); });
5932
5778
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
@@ -5936,43 +5782,33 @@ var PerpetualsClient = (function () {
5936
5782
  instructions = [];
5937
5783
  postInstructions = [];
5938
5784
  additionalSigners = [];
5939
- _b.label = 1;
5785
+ _a.label = 1;
5940
5786
  case 1:
5941
- _b.trys.push([1, 7, , 8]);
5942
- if (!(reserveSymbol == 'SOL')) return [3, 2];
5943
- lamports = (this.minimumBalanceForRentExemptAccountLamports);
5944
- if (!ephemeralSignerPubkey) {
5945
- wrappedSolAccount = new web3_js_1.Keypair();
5946
- additionalSigners.push(wrappedSolAccount);
5787
+ _a.trys.push([1, 3, , 4]);
5788
+ if (reserveSymbol == 'SOL') {
5789
+ lamports = (this.minimumBalanceForRentExemptAccountLamports);
5790
+ if (!ephemeralSignerPubkey) {
5791
+ wrappedSolAccount = new web3_js_1.Keypair();
5792
+ additionalSigners.push(wrappedSolAccount);
5793
+ }
5794
+ preInstructions = [
5795
+ web3_js_1.SystemProgram.createAccount({
5796
+ fromPubkey: publicKey,
5797
+ newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5798
+ lamports: lamports,
5799
+ space: 165,
5800
+ programId: spl_token_1.TOKEN_PROGRAM_ID,
5801
+ }),
5802
+ (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5803
+ ];
5804
+ postInstructions = [
5805
+ (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5806
+ ];
5947
5807
  }
5948
- preInstructions = [
5949
- web3_js_1.SystemProgram.createAccount({
5950
- fromPubkey: publicKey,
5951
- newAccountPubkey: (ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey),
5952
- lamports: lamports,
5953
- space: 165,
5954
- programId: spl_token_1.TOKEN_PROGRAM_ID,
5955
- }),
5956
- (0, spl_token_1.createInitializeAccount3Instruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), spl_token_1.NATIVE_MINT, publicKey),
5957
- ];
5958
- postInstructions = [
5959
- (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
5960
- ];
5961
- return [3, 5];
5962
- case 2:
5963
- 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);
5964
- _a = createUserATA;
5965
- if (!_a) return [3, 4];
5966
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
5967
- case 3:
5968
- _a = !(_b.sent());
5969
- _b.label = 4;
5970
- case 4:
5971
- if (_a) {
5972
- 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));
5808
+ else {
5809
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(reserveSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(reserveSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
5810
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(reserveSymbol).mintKey, poolConfig.getTokenFromSymbol(reserveSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
5973
5811
  }
5974
- _b.label = 5;
5975
- case 5:
5976
5812
  positionAccount = poolConfig.getPositionFromMarketPk(publicKey, marketAccount);
5977
5813
  orderAccount = poolConfig.getOrderFromMarketPk(publicKey, marketAccount);
5978
5814
  return [4, this.program.methods
@@ -6006,15 +5842,15 @@ var PerpetualsClient = (function () {
6006
5842
  receivingMint: poolConfig.getTokenFromSymbol(reserveSymbol).mintKey
6007
5843
  })
6008
5844
  .instruction()];
6009
- case 6:
6010
- editLimitOrder = _b.sent();
5845
+ case 2:
5846
+ editLimitOrder = _a.sent();
6011
5847
  instructions.push(editLimitOrder);
6012
- return [3, 8];
6013
- case 7:
6014
- err_31 = _b.sent();
5848
+ return [3, 4];
5849
+ case 3:
5850
+ err_31 = _a.sent();
6015
5851
  console.log("perpClient editLimitOrder error:: ", err_31);
6016
5852
  throw err_31;
6017
- case 8: return [2, {
5853
+ case 4: return [2, {
6018
5854
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6019
5855
  additionalSigners: additionalSigners
6020
5856
  }];
@@ -6163,12 +5999,12 @@ var PerpetualsClient = (function () {
6163
5999
  });
6164
6000
  });
6165
6001
  };
6166
- this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6002
+ this.placeTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6167
6003
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, placeTriggerOrder, err_34;
6168
6004
  return __generator(this, function (_a) {
6169
6005
  switch (_a.label) {
6170
6006
  case 0:
6171
- publicKey = this.provider.wallet.publicKey;
6007
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6172
6008
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6173
6009
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6174
6010
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6222,12 +6058,12 @@ var PerpetualsClient = (function () {
6222
6058
  }
6223
6059
  });
6224
6060
  }); };
6225
- this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6061
+ this.editTriggerOrder = function (targetSymbol, collateralSymbol, receiveSymbol, side, orderId, triggerPrice, deltaSizeAmount, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6226
6062
  var publicKey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, editTriggerOrder, err_35;
6227
6063
  return __generator(this, function (_a) {
6228
6064
  switch (_a.label) {
6229
6065
  case 0:
6230
- publicKey = this.provider.wallet.publicKey;
6066
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6231
6067
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6232
6068
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6233
6069
  receivingCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receiveSymbol).mintKey); });
@@ -6280,12 +6116,12 @@ var PerpetualsClient = (function () {
6280
6116
  }
6281
6117
  });
6282
6118
  }); };
6283
- this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6119
+ this.cancelTriggerOrder = function (targetSymbol, collateralSymbol, side, orderId, isStopLoss, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6284
6120
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, cancelTriggerOrder, err_36;
6285
6121
  return __generator(this, function (_a) {
6286
6122
  switch (_a.label) {
6287
6123
  case 0:
6288
- publicKey = this.provider.wallet.publicKey;
6124
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6289
6125
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6290
6126
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6291
6127
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6324,12 +6160,12 @@ var PerpetualsClient = (function () {
6324
6160
  }
6325
6161
  });
6326
6162
  }); };
6327
- this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig) { return __awaiter(_this, void 0, void 0, function () {
6163
+ this.cancelAllTriggerOrders = function (targetSymbol, collateralSymbol, side, poolConfig, userPublicKey) { return __awaiter(_this, void 0, void 0, function () {
6328
6164
  var publicKey, targetCustodyConfig, collateralCustodyConfig, marketAccount, preInstructions, instructions, postInstructions, additionalSigners, orderAccount, positionAccount, cancelAllTriggerOrders, err_37;
6329
6165
  return __generator(this, function (_a) {
6330
6166
  switch (_a.label) {
6331
6167
  case 0:
6332
- publicKey = this.provider.wallet.publicKey;
6168
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6333
6169
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
6334
6170
  collateralCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey); });
6335
6171
  marketAccount = poolConfig.getMarketPk(targetCustodyConfig.custodyAccount, collateralCustodyConfig.custodyAccount, side);
@@ -6372,13 +6208,13 @@ var PerpetualsClient = (function () {
6372
6208
  args_1[_i - 9] = arguments[_i];
6373
6209
  }
6374
6210
  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) {
6375
- 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;
6211
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, receivingCustodyConfig, marketAccount, userReceivingTokenAccount, userReceivingTokenAccountCollateral, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, collateralToken, receivingToken, positionAccount, orderAccount, custodyAccountMetas, custodyOracleAccountMetas, _a, _b, custody, executeTriggerWithSwap, err_38;
6376
6212
  if (createUserATA === void 0) { createUserATA = true; }
6377
6213
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6378
6214
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6379
6215
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6380
- return __generator(this, function (_e) {
6381
- switch (_e.label) {
6216
+ return __generator(this, function (_c) {
6217
+ switch (_c.label) {
6382
6218
  case 0:
6383
6219
  payerPubkey = this.provider.wallet.publicKey;
6384
6220
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6391,42 +6227,23 @@ var PerpetualsClient = (function () {
6391
6227
  additionalSigners = [];
6392
6228
  collateralToken = poolConfig.getTokenFromSymbol(collateralSymbol);
6393
6229
  receivingToken = poolConfig.getTokenFromSymbol(receivingSymbol);
6394
- _e.label = 1;
6230
+ _c.label = 1;
6395
6231
  case 1:
6396
- _e.trys.push([1, 9, , 10]);
6397
- if (!false) return [3, 2];
6398
- return [3, 7];
6399
- case 2:
6400
- 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);
6401
- _a = createUserATA;
6402
- if (!_a) return [3, 4];
6403
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6404
- case 3:
6405
- _a = !(_e.sent());
6406
- _e.label = 4;
6407
- case 4:
6408
- if (_a) {
6409
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey));
6232
+ _c.trys.push([1, 3, , 4]);
6233
+ if (false) {
6410
6234
  }
6411
- 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);
6412
- _b = createUserATA;
6413
- if (!_b) return [3, 6];
6414
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccountCollateral, this.provider.connection)];
6415
- case 5:
6416
- _b = !(_e.sent());
6417
- _e.label = 6;
6418
- case 6:
6419
- if (_b) {
6420
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey));
6235
+ else {
6236
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, owner, true, receivingToken.isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6237
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(receivingSymbol).mintKey, spl_token_1.TOKEN_PROGRAM_ID));
6238
+ userReceivingTokenAccountCollateral = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, owner, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6239
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccountCollateral, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6421
6240
  }
6422
- _e.label = 7;
6423
- case 7:
6424
6241
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6425
6242
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6426
6243
  custodyAccountMetas = [];
6427
6244
  custodyOracleAccountMetas = [];
6428
- for (_c = 0, _d = poolConfig.custodies; _c < _d.length; _c++) {
6429
- custody = _d[_c];
6245
+ for (_a = 0, _b = poolConfig.custodies; _a < _b.length; _a++) {
6246
+ custody = _b[_a];
6430
6247
  custodyAccountMetas.push({
6431
6248
  pubkey: custody.custodyAccount,
6432
6249
  isSigner: false,
@@ -6473,15 +6290,15 @@ var PerpetualsClient = (function () {
6473
6290
  })
6474
6291
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6475
6292
  .instruction()];
6476
- case 8:
6477
- executeTriggerWithSwap = _e.sent();
6293
+ case 2:
6294
+ executeTriggerWithSwap = _c.sent();
6478
6295
  instructions.push(executeTriggerWithSwap);
6479
- return [3, 10];
6480
- case 9:
6481
- err_38 = _e.sent();
6296
+ return [3, 4];
6297
+ case 3:
6298
+ err_38 = _c.sent();
6482
6299
  console.log("perpClient executeTriggerWithSwap error:: ", err_38);
6483
6300
  throw err_38;
6484
- case 10: return [2, {
6301
+ case 4: return [2, {
6485
6302
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6486
6303
  additionalSigners: additionalSigners
6487
6304
  }];
@@ -6495,13 +6312,13 @@ var PerpetualsClient = (function () {
6495
6312
  args_1[_i - 8] = arguments[_i];
6496
6313
  }
6497
6314
  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) {
6498
- var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, _a, positionAccount, orderAccount, executeTriggerOrder, err_39;
6315
+ var payerPubkey, targetCustodyConfig, collateralCustodyConfig, marketAccount, userReceivingTokenAccount, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, positionAccount, orderAccount, executeTriggerOrder, err_39;
6499
6316
  if (createUserATA === void 0) { createUserATA = true; }
6500
6317
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6501
6318
  if (tokenStakeAccount === void 0) { tokenStakeAccount = web3_js_1.PublicKey.default; }
6502
6319
  if (userReferralAccount === void 0) { userReferralAccount = web3_js_1.PublicKey.default; }
6503
- return __generator(this, function (_b) {
6504
- switch (_b.label) {
6320
+ return __generator(this, function (_a) {
6321
+ switch (_a.label) {
6505
6322
  case 0:
6506
6323
  payerPubkey = this.provider.wallet.publicKey;
6507
6324
  targetCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(targetSymbol).mintKey); });
@@ -6511,25 +6328,15 @@ var PerpetualsClient = (function () {
6511
6328
  instructions = [];
6512
6329
  postInstructions = [];
6513
6330
  additionalSigners = [];
6514
- _b.label = 1;
6331
+ _a.label = 1;
6515
6332
  case 1:
6516
- _b.trys.push([1, 7, , 8]);
6517
- if (!false) return [3, 2];
6518
- return [3, 5];
6519
- case 2:
6520
- 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);
6521
- _a = createUserATA;
6522
- if (!_a) return [3, 4];
6523
- return [4, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
6524
- case 3:
6525
- _a = !(_b.sent());
6526
- _b.label = 4;
6527
- case 4:
6528
- if (_a) {
6529
- 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));
6333
+ _a.trys.push([1, 3, , 4]);
6334
+ if (false) {
6335
+ }
6336
+ else {
6337
+ userReceivingTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, owner, true, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID);
6338
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payerPubkey, userReceivingTokenAccount, owner, poolConfig.getTokenFromSymbol(collateralSymbol).mintKey, poolConfig.getTokenFromSymbol(collateralSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6530
6339
  }
6531
- _b.label = 5;
6532
- case 5:
6533
6340
  positionAccount = poolConfig.getPositionFromMarketPk(owner, marketAccount);
6534
6341
  orderAccount = poolConfig.getOrderFromMarketPk(owner, marketAccount);
6535
6342
  return [4, this.program.methods
@@ -6561,15 +6368,15 @@ var PerpetualsClient = (function () {
6561
6368
  })
6562
6369
  .remainingAccounts(__spreadArray([], (0, getReferralAccounts_1.getReferralAccounts)(tokenStakeAccount, userReferralAccount, privilege), true))
6563
6370
  .instruction()];
6564
- case 6:
6565
- executeTriggerOrder = _b.sent();
6371
+ case 2:
6372
+ executeTriggerOrder = _a.sent();
6566
6373
  instructions.push(executeTriggerOrder);
6567
- return [3, 8];
6568
- case 7:
6569
- err_39 = _b.sent();
6374
+ return [3, 4];
6375
+ case 3:
6376
+ err_39 = _a.sent();
6570
6377
  console.log("perpClient executeTriggerOrder error:: ", err_39);
6571
6378
  throw err_39;
6572
- case 8: return [2, {
6379
+ case 4: return [2, {
6573
6380
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6574
6381
  additionalSigners: additionalSigners
6575
6382
  }];
@@ -6582,16 +6389,16 @@ var PerpetualsClient = (function () {
6582
6389
  for (var _i = 5; _i < arguments.length; _i++) {
6583
6390
  args_1[_i - 5] = arguments[_i];
6584
6391
  }
6585
- 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) {
6586
- 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;
6392
+ return __awaiter(_this, __spreadArray([userInputTokenSymbol_1, userOutputTokenSymbol_1, amountIn_1, minAmountOut_1, poolConfig_1], args_1, true), void 0, function (userInputTokenSymbol, userOutputTokenSymbol, amountIn, minAmountOut, poolConfig, useFeesPool, createUserATA, unWrapSol, skipBalanceChecks, ephemeralSignerPubkey, isWhitelistedUser, userPublicKey) {
6393
+ var userInputCustodyConfig, userOutputCustodyConfig, publicKey, wrappedSolAccount, preInstructions, instructions, postInstructions, additionalSigners, userOutputTokenAccount, userInputTokenAccount, wsolAssociatedTokenAccount, unWrappedSolBalance, _a, wsolAssociatedTokenAccount, closeWsolATAIns, accCreationLamports, lamports, unWrappedSolBalance, _b, tokenAccountBalance, _c, lamports, custodyAccountMetas, custodyOracleAccountMetas, _d, _e, custody, whitelistPda, whitelistMeta, params, inx, closeWsolATAIns, err_40;
6587
6394
  if (useFeesPool === void 0) { useFeesPool = false; }
6588
6395
  if (createUserATA === void 0) { createUserATA = true; }
6589
6396
  if (unWrapSol === void 0) { unWrapSol = false; }
6590
6397
  if (skipBalanceChecks === void 0) { skipBalanceChecks = false; }
6591
6398
  if (ephemeralSignerPubkey === void 0) { ephemeralSignerPubkey = undefined; }
6592
6399
  if (isWhitelistedUser === void 0) { isWhitelistedUser = false; }
6593
- return __generator(this, function (_g) {
6594
- switch (_g.label) {
6400
+ return __generator(this, function (_f) {
6401
+ switch (_f.label) {
6595
6402
  case 0:
6596
6403
  userInputCustodyConfig = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(userInputTokenSymbol).mintKey); });
6597
6404
  if (!userInputCustodyConfig) {
@@ -6601,31 +6408,26 @@ var PerpetualsClient = (function () {
6601
6408
  if (!userOutputCustodyConfig) {
6602
6409
  throw "userOutputCustodyConfig not found";
6603
6410
  }
6604
- publicKey = this.provider.wallet.publicKey;
6411
+ publicKey = userPublicKey !== null && userPublicKey !== void 0 ? userPublicKey : this.provider.wallet.publicKey;
6605
6412
  preInstructions = [];
6606
6413
  instructions = [];
6607
6414
  postInstructions = [];
6608
6415
  additionalSigners = [];
6609
- if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 5];
6416
+ if (!(userInputTokenSymbol == 'SOL' && userOutputTokenSymbol == 'WSOL')) return [3, 4];
6610
6417
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(spl_token_1.NATIVE_MINT, publicKey, true)];
6611
6418
  case 1:
6612
- wsolAssociatedTokenAccount = _g.sent();
6613
- return [4, (0, utils_1.checkIfAccountExists)(wsolAssociatedTokenAccount, this.provider.connection)];
6614
- case 2:
6615
- wsolATAExist = _g.sent();
6616
- if (!wsolATAExist) {
6617
- instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT));
6618
- }
6619
- if (!!skipBalanceChecks) return [3, 4];
6419
+ wsolAssociatedTokenAccount = _f.sent();
6420
+ instructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, wsolAssociatedTokenAccount, publicKey, spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID));
6421
+ if (!!skipBalanceChecks) return [3, 3];
6620
6422
  _a = anchor_1.BN.bind;
6621
6423
  return [4, this.provider.connection.getBalance(publicKey)];
6622
- case 3:
6623
- unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _g.sent()]))();
6424
+ case 2:
6425
+ unWrappedSolBalance = new (_a.apply(anchor_1.BN, [void 0, _f.sent()]))();
6624
6426
  if (unWrappedSolBalance.lt(amountIn)) {
6625
6427
  throw "Insufficient SOL Funds";
6626
6428
  }
6627
- _g.label = 4;
6628
- case 4:
6429
+ _f.label = 3;
6430
+ case 3:
6629
6431
  instructions.push(web3_js_1.SystemProgram.transfer({
6630
6432
  fromPubkey: publicKey,
6631
6433
  toPubkey: wsolAssociatedTokenAccount,
@@ -6635,7 +6437,7 @@ var PerpetualsClient = (function () {
6635
6437
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6636
6438
  additionalSigners: additionalSigners
6637
6439
  }];
6638
- case 5:
6440
+ case 4:
6639
6441
  if (userInputTokenSymbol == 'WSOL' && userOutputTokenSymbol == 'SOL') {
6640
6442
  console.log("WSOL=> SOL : NOTE : ONLY WAY IS TO CLOSE THE WSOL ATA and GET ALL SOL ");
6641
6443
  wsolAssociatedTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spl_token_1.NATIVE_MINT, publicKey, true);
@@ -6646,20 +6448,20 @@ var PerpetualsClient = (function () {
6646
6448
  additionalSigners: additionalSigners
6647
6449
  }];
6648
6450
  }
6649
- _g.label = 6;
6650
- case 6:
6651
- _g.trys.push([6, 19, , 20]);
6652
- if (!(userInputTokenSymbol == 'SOL')) return [3, 9];
6451
+ _f.label = 5;
6452
+ case 5:
6453
+ _f.trys.push([5, 16, , 17]);
6454
+ if (!(userInputTokenSymbol == 'SOL')) return [3, 8];
6653
6455
  console.log("userInputTokenSymbol === sol", userInputTokenSymbol);
6654
6456
  return [4, (0, spl_token_1.getMinimumBalanceForRentExemptAccount)(this.provider.connection)];
6655
- case 7:
6656
- accCreationLamports = (_g.sent());
6457
+ case 6:
6458
+ accCreationLamports = (_f.sent());
6657
6459
  console.log("accCreationLamports:", accCreationLamports);
6658
6460
  lamports = amountIn.add(new anchor_1.BN(accCreationLamports));
6659
6461
  _b = anchor_1.BN.bind;
6660
6462
  return [4, this.provider.connection.getBalance(publicKey)];
6661
- case 8:
6662
- unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _g.sent()]))();
6463
+ case 7:
6464
+ unWrappedSolBalance = new (_b.apply(anchor_1.BN, [void 0, _f.sent()]))();
6663
6465
  if (unWrappedSolBalance.lt(amountIn)) {
6664
6466
  throw "Insufficient SOL Funds";
6665
6467
  }
@@ -6681,25 +6483,25 @@ var PerpetualsClient = (function () {
6681
6483
  postInstructions = [
6682
6484
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6683
6485
  ];
6684
- return [3, 12];
6685
- case 9:
6486
+ return [3, 11];
6487
+ case 8:
6686
6488
  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);
6687
6489
  return [4, (0, utils_1.checkIfAccountExists)(userInputTokenAccount, this.provider.connection)];
6688
- case 10:
6689
- if (!(_g.sent())) {
6490
+ case 9:
6491
+ if (!(_f.sent())) {
6690
6492
  throw "Insufficient Funds , Token Account doesn't exist";
6691
6493
  }
6692
- if (!!skipBalanceChecks) return [3, 12];
6494
+ if (!!skipBalanceChecks) return [3, 11];
6693
6495
  _c = anchor_1.BN.bind;
6694
6496
  return [4, this.provider.connection.getTokenAccountBalance(userInputTokenAccount)];
6695
- case 11:
6696
- tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_g.sent()).value.amount]))();
6497
+ case 10:
6498
+ tokenAccountBalance = new (_c.apply(anchor_1.BN, [void 0, (_f.sent()).value.amount]))();
6697
6499
  if (tokenAccountBalance.lt(amountIn)) {
6698
6500
  throw "Insufficient Funds need more ".concat(amountIn.sub(tokenAccountBalance), " tokens");
6699
6501
  }
6700
- _g.label = 12;
6701
- case 12:
6702
- if (!(userOutputTokenSymbol == 'SOL')) return [3, 13];
6502
+ _f.label = 11;
6503
+ case 11:
6504
+ if (!(userOutputTokenSymbol == 'SOL')) return [3, 12];
6703
6505
  lamports = (this.minimumBalanceForRentExemptAccountLamports);
6704
6506
  if (!ephemeralSignerPubkey) {
6705
6507
  wrappedSolAccount = new web3_js_1.Keypair();
@@ -6719,26 +6521,17 @@ var PerpetualsClient = (function () {
6719
6521
  postInstructions = [
6720
6522
  (0, spl_token_1.createCloseAccountInstruction)((ephemeralSignerPubkey ? ephemeralSignerPubkey : wrappedSolAccount.publicKey), publicKey, publicKey),
6721
6523
  ];
6722
- return [3, 17];
6723
- 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)];
6524
+ return [3, 14];
6525
+ case 12: return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, publicKey, true, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID)];
6526
+ case 13:
6527
+ userOutputTokenAccount = _f.sent();
6528
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, userOutputTokenAccount, publicKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).mintKey, poolConfig.getTokenFromSymbol(userOutputTokenSymbol).isToken2022 ? spl_token_1.TOKEN_2022_PROGRAM_ID : spl_token_1.TOKEN_PROGRAM_ID));
6529
+ _f.label = 14;
6724
6530
  case 14:
6725
- userOutputTokenAccount = _g.sent();
6726
- _d = createUserATA;
6727
- if (!_d) return [3, 16];
6728
- return [4, (0, utils_1.checkIfAccountExists)(userOutputTokenAccount, this.provider.connection)];
6729
- case 15:
6730
- _d = !(_g.sent());
6731
- _g.label = 16;
6732
- case 16:
6733
- if (_d) {
6734
- 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));
6735
- }
6736
- _g.label = 17;
6737
- case 17:
6738
6531
  custodyAccountMetas = [];
6739
6532
  custodyOracleAccountMetas = [];
6740
- for (_e = 0, _f = poolConfig.custodies; _e < _f.length; _e++) {
6741
- custody = _f[_e];
6533
+ for (_d = 0, _e = poolConfig.custodies; _d < _e.length; _d++) {
6534
+ custody = _e[_d];
6742
6535
  custodyAccountMetas.push({
6743
6536
  pubkey: custody.custodyAccount,
6744
6537
  isSigner: false,
@@ -6786,19 +6579,19 @@ var PerpetualsClient = (function () {
6786
6579
  })
6787
6580
  .remainingAccounts(__spreadArray(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true), (isWhitelistedUser ? [whitelistMeta] : []), true))
6788
6581
  .instruction()];
6789
- case 18:
6790
- inx = _g.sent();
6582
+ case 15:
6583
+ inx = _f.sent();
6791
6584
  instructions.push(inx);
6792
6585
  if (userOutputTokenSymbol == 'SOL' && unWrapSol) {
6793
6586
  closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userOutputTokenAccount, publicKey, publicKey);
6794
6587
  instructions.push(closeWsolATAIns);
6795
6588
  }
6796
- return [3, 20];
6797
- case 19:
6798
- err_40 = _g.sent();
6589
+ return [3, 17];
6590
+ case 16:
6591
+ err_40 = _f.sent();
6799
6592
  console.error("perpClient Swap error:: ", err_40);
6800
6593
  throw err_40;
6801
- case 20: return [2, {
6594
+ case 17: return [2, {
6802
6595
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
6803
6596
  additionalSigners: additionalSigners
6804
6597
  }];
@@ -7840,15 +7633,11 @@ var PerpetualsClient = (function () {
7840
7633
  additionalSigners = [];
7841
7634
  _a.label = 1;
7842
7635
  case 1:
7843
- _a.trys.push([1, 5, , 6]);
7636
+ _a.trys.push([1, 4, , 5]);
7844
7637
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7845
7638
  case 2:
7846
7639
  receivingTokenAccount = _a.sent();
7847
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7848
- case 3:
7849
- if (!(_a.sent())) {
7850
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7851
- }
7640
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7852
7641
  return [4, this.program.methods
7853
7642
  .withdrawInstantFees({})
7854
7643
  .accounts({
@@ -7863,15 +7652,15 @@ var PerpetualsClient = (function () {
7863
7652
  receivingTokenMint: poolConfig.tokenMint,
7864
7653
  })
7865
7654
  .instruction()];
7866
- case 4:
7655
+ case 3:
7867
7656
  withdrawInstantFeeInstruction = _a.sent();
7868
7657
  instructions.push(withdrawInstantFeeInstruction);
7869
- return [3, 6];
7870
- case 5:
7658
+ return [3, 5];
7659
+ case 4:
7871
7660
  err_57 = _a.sent();
7872
7661
  console.log("perpClient withdrawInstantFeeInstruction error:: ", err_57);
7873
7662
  throw err_57;
7874
- case 6: return [2, {
7663
+ case 5: return [2, {
7875
7664
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7876
7665
  additionalSigners: additionalSigners
7877
7666
  }];
@@ -7890,15 +7679,11 @@ var PerpetualsClient = (function () {
7890
7679
  additionalSigners = [];
7891
7680
  _a.label = 1;
7892
7681
  case 1:
7893
- _a.trys.push([1, 5, , 6]);
7682
+ _a.trys.push([1, 4, , 5]);
7894
7683
  return [4, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.tokenMint, publicKey, true)];
7895
7684
  case 2:
7896
7685
  receivingTokenAccount = _a.sent();
7897
- return [4, (0, utils_1.checkIfAccountExists)(receivingTokenAccount, this.provider.connection)];
7898
- case 3:
7899
- if (!(_a.sent())) {
7900
- preInstructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint));
7901
- }
7686
+ preInstructions.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(publicKey, receivingTokenAccount, publicKey, poolConfig.tokenMint, spl_token_1.TOKEN_PROGRAM_ID));
7902
7687
  return [4, this.program.methods
7903
7688
  .withdrawUnclaimedTokens({})
7904
7689
  .accounts({
@@ -7913,15 +7698,15 @@ var PerpetualsClient = (function () {
7913
7698
  receivingTokenMint: poolConfig.tokenMint,
7914
7699
  })
7915
7700
  .instruction()];
7916
- case 4:
7701
+ case 3:
7917
7702
  withdrawUnclaimedTokensInstruction = _a.sent();
7918
7703
  instructions.push(withdrawUnclaimedTokensInstruction);
7919
- return [3, 6];
7920
- case 5:
7704
+ return [3, 5];
7705
+ case 4:
7921
7706
  err_58 = _a.sent();
7922
7707
  console.log("perpClient withdrawUnclaimedTokensInstruction error:: ", err_58);
7923
7708
  throw err_58;
7924
- case 6: return [2, {
7709
+ case 5: return [2, {
7925
7710
  instructions: __spreadArray(__spreadArray(__spreadArray([], preInstructions, true), instructions, true), postInstructions, true),
7926
7711
  additionalSigners: additionalSigners
7927
7712
  }];