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