@drift-labs/sdk 2.43.0-beta.10 → 2.43.0-beta.12

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.
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.43.0-beta.10
1
+ 2.43.0-beta.12
@@ -418,14 +418,15 @@ class DriftClient {
418
418
  return result;
419
419
  }
420
420
  async initializeUserAccount(subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, referrerInfo) {
421
+ const initializeIxs = [];
421
422
  const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
422
- const tx = new web3_js_1.Transaction();
423
423
  if (subAccountId === 0) {
424
424
  if (!(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))) {
425
- tx.add(await this.getInitializeUserStatsIx());
425
+ initializeIxs.push(await this.getInitializeUserStatsIx());
426
426
  }
427
427
  }
428
- tx.add(initializeUserAccountIx);
428
+ initializeIxs.push(initializeUserAccountIx);
429
+ const tx = await this.buildTransaction(initializeIxs);
429
430
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
430
431
  await this.addUser(subAccountId);
431
432
  return [txSig, userAccountPublicKey];
@@ -1052,18 +1053,19 @@ class DriftClient {
1052
1053
  */
1053
1054
  async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, marketIndex = 0, subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, fromSubAccountId, referrerInfo, txParams) {
1054
1055
  var _a;
1056
+ const ixs = [];
1055
1057
  const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
1056
1058
  const additionalSigners = [];
1057
1059
  const spotMarket = this.getSpotMarketAccount(marketIndex);
1058
1060
  const isSolMarket = spotMarket.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
1059
- const tx = new web3_js_1.Transaction();
1060
- tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
1061
- units: (_a = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits) !== null && _a !== void 0 ? _a : 600000,
1062
- }));
1061
+ let params = {
1062
+ computeUnits: (_a = txParams === null || txParams === void 0 ? void 0 : txParams.computeUnits) !== null && _a !== void 0 ? _a : 600000,
1063
+ };
1063
1064
  if (txParams === null || txParams === void 0 ? void 0 : txParams.computeUnitsPrice) {
1064
- tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
1065
- microLamports: txParams.computeUnitsPrice,
1066
- }));
1065
+ params = {
1066
+ ...params,
1067
+ computeUnitsPrice: txParams.computeUnitsPrice,
1068
+ };
1067
1069
  }
1068
1070
  const authority = this.wallet.publicKey;
1069
1071
  const isFromSubaccount = fromSubAccountId !== null &&
@@ -1073,40 +1075,41 @@ class DriftClient {
1073
1075
  if (createWSOLTokenAccount) {
1074
1076
  const { ixs: startIxs, pubkey } = await this.getWrappedSolAccountCreationIxs(amount, true);
1075
1077
  userTokenAccount = pubkey;
1076
- startIxs.forEach((ix) => {
1077
- tx.add(ix);
1078
- });
1078
+ ixs.push(...startIxs);
1079
1079
  }
1080
1080
  const depositCollateralIx = isFromSubaccount
1081
1081
  ? await this.getTransferDepositIx(amount, marketIndex, fromSubAccountId, subAccountId)
1082
1082
  : await this.getDepositInstruction(amount, marketIndex, userTokenAccount, subAccountId, false, false);
1083
1083
  if (subAccountId === 0) {
1084
1084
  if (!(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))) {
1085
- tx.add(await this.getInitializeUserStatsIx());
1085
+ ixs.push(await this.getInitializeUserStatsIx());
1086
1086
  }
1087
1087
  }
1088
- tx.add(initializeUserAccountIx).add(depositCollateralIx);
1088
+ ixs.push(initializeUserAccountIx, depositCollateralIx);
1089
1089
  // Close the wrapped sol account at the end of the transaction
1090
1090
  if (createWSOLTokenAccount) {
1091
- tx.add((0, spl_token_1.createCloseAccountInstruction)(userTokenAccount, authority, authority, []));
1091
+ ixs.push((0, spl_token_1.createCloseAccountInstruction)(userTokenAccount, authority, authority, []));
1092
1092
  }
1093
+ const tx = await this.buildTransaction(ixs, params);
1093
1094
  const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
1094
1095
  this.spotMarketLastSlotCache.set(marketIndex, slot);
1095
1096
  await this.addUser(subAccountId);
1096
1097
  return [txSig, userAccountPublicKey];
1097
1098
  }
1098
1099
  async initializeUserAccountForDevnet(subAccountId = 0, name = userName_1.DEFAULT_USER_NAME, marketIndex, tokenFaucet, amount, referrerInfo) {
1100
+ const ixs = [];
1099
1101
  const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] = await tokenFaucet.createAssociatedTokenAccountAndMintToInstructions(this.wallet.publicKey, amount);
1100
1102
  const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(subAccountId, name, referrerInfo);
1101
1103
  const depositCollateralIx = await this.getDepositInstruction(amount, marketIndex, associateTokenPublicKey, subAccountId, false, false);
1102
- const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToIx);
1104
+ ixs.push(createAssociatedAccountIx, mintToIx);
1103
1105
  if (subAccountId === 0) {
1104
1106
  if (!(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))) {
1105
- tx.add(await this.getInitializeUserStatsIx());
1107
+ ixs.push(await this.getInitializeUserStatsIx());
1106
1108
  }
1107
1109
  }
1108
- tx.add(initializeUserAccountIx).add(depositCollateralIx);
1109
- const txSig = await this.program.provider.sendAndConfirm(tx, []);
1110
+ ixs.push(initializeUserAccountIx, depositCollateralIx);
1111
+ const tx = await this.buildTransaction(ixs);
1112
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
1110
1113
  await this.addUser(subAccountId);
1111
1114
  return [txSig, userAccountPublicKey];
1112
1115
  }
@@ -1118,10 +1121,7 @@ class DriftClient {
1118
1121
  * @param reduceOnly
1119
1122
  */
1120
1123
  async withdraw(amount, marketIndex, associatedTokenAddress, reduceOnly = false) {
1121
- const tx = new web3_js_1.Transaction();
1122
- tx.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
1123
- units: 600000,
1124
- }));
1124
+ const withdrawIxs = [];
1125
1125
  const additionalSigners = [];
1126
1126
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
1127
1127
  const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
@@ -1130,23 +1130,24 @@ class DriftClient {
1130
1130
  if (createWSOLTokenAccount) {
1131
1131
  const { ixs, pubkey } = await this.getWrappedSolAccountCreationIxs(amount, false);
1132
1132
  associatedTokenAddress = pubkey;
1133
- ixs.forEach((ix) => {
1134
- tx.add(ix);
1135
- });
1133
+ withdrawIxs.push(...ixs);
1136
1134
  }
1137
1135
  else {
1138
1136
  const accountExists = await this.checkIfAccountExists(associatedTokenAddress);
1139
1137
  if (!accountExists) {
1140
1138
  const createAssociatedTokenAccountIx = this.getAssociatedTokenAccountCreationIx(spotMarketAccount.mint, associatedTokenAddress);
1141
- tx.add(createAssociatedTokenAccountIx);
1139
+ withdrawIxs.push(createAssociatedTokenAccountIx);
1142
1140
  }
1143
1141
  }
1144
- const withdrawCollateral = await this.getWithdrawIx(amount, spotMarketAccount.marketIndex, associatedTokenAddress, reduceOnly);
1145
- tx.add(withdrawCollateral);
1142
+ const withdrawCollateralIx = await this.getWithdrawIx(amount, spotMarketAccount.marketIndex, associatedTokenAddress, reduceOnly);
1143
+ withdrawIxs.push(withdrawCollateralIx);
1146
1144
  // Close the wrapped sol account at the end of the transaction
1147
1145
  if (createWSOLTokenAccount) {
1148
- tx.add((0, spl_token_1.createCloseAccountInstruction)(associatedTokenAddress, authority, authority, []));
1146
+ withdrawIxs.push((0, spl_token_1.createCloseAccountInstruction)(associatedTokenAddress, authority, authority, []));
1149
1147
  }
1148
+ const tx = await this.buildTransaction(withdrawIxs, {
1149
+ computeUnits: 600000,
1150
+ });
1150
1151
  const { txSig, slot } = await this.sendTransaction(tx, additionalSigners, this.opts);
1151
1152
  this.spotMarketLastSlotCache.set(marketIndex, slot);
1152
1153
  return txSig;
@@ -2698,11 +2699,7 @@ class DriftClient {
2698
2699
  }
2699
2700
  async settlePNLs(users, marketIndexes) {
2700
2701
  const ixs = await this.getSettlePNLsIxs(users, marketIndexes);
2701
- const tx = new web3_js_1.Transaction()
2702
- .add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
2703
- units: 1000000,
2704
- }))
2705
- .add(...ixs);
2702
+ const tx = await this.buildTransaction(ixs, { computeUnits: 1000000 });
2706
2703
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
2707
2704
  return txSig;
2708
2705
  }
@@ -3030,7 +3027,7 @@ class DriftClient {
3030
3027
  * Add to an insurance fund stake and optionally initialize the account
3031
3028
  */
3032
3029
  async addInsuranceFundStake({ marketIndex, amount, collateralAccountPublicKey, initializeStakeAccount, fromSubaccount, }) {
3033
- const tx = new web3_js_1.Transaction();
3030
+ const addIfStakeIxs = [];
3034
3031
  const additionalSigners = [];
3035
3032
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
3036
3033
  const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
@@ -3040,7 +3037,7 @@ class DriftClient {
3040
3037
  const { ixs, pubkey } = await this.getWrappedSolAccountCreationIxs(amount, true);
3041
3038
  tokenAccount = pubkey;
3042
3039
  ixs.forEach((ix) => {
3043
- tx.add(ix);
3040
+ addIfStakeIxs.push(ix);
3044
3041
  });
3045
3042
  }
3046
3043
  else {
@@ -3048,17 +3045,18 @@ class DriftClient {
3048
3045
  }
3049
3046
  if (fromSubaccount) {
3050
3047
  const withdrawIx = await this.getWithdrawIx(amount, marketIndex, tokenAccount);
3051
- tx.add(withdrawIx);
3048
+ addIfStakeIxs.push(withdrawIx);
3052
3049
  }
3053
3050
  if (initializeStakeAccount) {
3054
3051
  const initializeIx = await this.getInitializeInsuranceFundStakeIx(marketIndex);
3055
- tx.add(initializeIx);
3052
+ addIfStakeIxs.push(initializeIx);
3056
3053
  }
3057
3054
  const addFundsIx = await this.getAddInsuranceFundStakeIx(marketIndex, amount, tokenAccount);
3058
- tx.add(addFundsIx);
3055
+ addIfStakeIxs.push(addFundsIx);
3059
3056
  if (createWSOLTokenAccount) {
3060
- tx.add((0, spl_token_1.createCloseAccountInstruction)(tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
3057
+ addIfStakeIxs.push((0, spl_token_1.createCloseAccountInstruction)(tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
3061
3058
  }
3059
+ const tx = await this.buildTransaction(addIfStakeIxs);
3062
3060
  const { txSig } = await this.sendTransaction(tx, additionalSigners, this.opts);
3063
3061
  return txSig;
3064
3062
  }
@@ -3070,7 +3068,7 @@ class DriftClient {
3070
3068
  useMarketLastSlotCache: true,
3071
3069
  writableSpotMarketIndexes: [marketIndex],
3072
3070
  });
3073
- const tx = await this.program.transaction.requestRemoveInsuranceFundStake(marketIndex, amount, {
3071
+ const ix = await this.program.instruction.requestRemoveInsuranceFundStake(marketIndex, amount, {
3074
3072
  accounts: {
3075
3073
  state: await this.getStatePublicKey(),
3076
3074
  spotMarket: spotMarketAccount.pubkey,
@@ -3081,6 +3079,7 @@ class DriftClient {
3081
3079
  },
3082
3080
  remainingAccounts,
3083
3081
  });
3082
+ const tx = await this.buildTransaction(ix);
3084
3083
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
3085
3084
  return txSig;
3086
3085
  }
@@ -3092,7 +3091,7 @@ class DriftClient {
3092
3091
  useMarketLastSlotCache: true,
3093
3092
  writableSpotMarketIndexes: [marketIndex],
3094
3093
  });
3095
- const tx = await this.program.transaction.cancelRequestRemoveInsuranceFundStake(marketIndex, {
3094
+ const ix = await this.program.instruction.cancelRequestRemoveInsuranceFundStake(marketIndex, {
3096
3095
  accounts: {
3097
3096
  state: await this.getStatePublicKey(),
3098
3097
  spotMarket: spotMarketAccount.pubkey,
@@ -3103,11 +3102,12 @@ class DriftClient {
3103
3102
  },
3104
3103
  remainingAccounts,
3105
3104
  });
3105
+ const tx = await this.buildTransaction(ix);
3106
3106
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
3107
3107
  return txSig;
3108
3108
  }
3109
3109
  async removeInsuranceFundStake(marketIndex, collateralAccountPublicKey) {
3110
- const tx = new web3_js_1.Transaction();
3110
+ const removeIfStakeIxs = [];
3111
3111
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
3112
3112
  const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, marketIndex);
3113
3113
  const additionalSigners = [];
@@ -3118,7 +3118,7 @@ class DriftClient {
3118
3118
  const { ixs, pubkey } = await this.getWrappedSolAccountCreationIxs(numericConstants_1.ZERO, true);
3119
3119
  tokenAccount = pubkey;
3120
3120
  ixs.forEach((ix) => {
3121
- tx.add(ix);
3121
+ removeIfStakeIxs.push(ix);
3122
3122
  });
3123
3123
  }
3124
3124
  else {
@@ -3143,11 +3143,12 @@ class DriftClient {
3143
3143
  },
3144
3144
  remainingAccounts,
3145
3145
  });
3146
- tx.add(removeStakeIx);
3146
+ removeIfStakeIxs.push(removeStakeIx);
3147
3147
  // Close the wrapped sol account at the end of the transaction
3148
3148
  if (createWSOLTokenAccount) {
3149
- tx.add((0, spl_token_1.createCloseAccountInstruction)(tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
3149
+ removeIfStakeIxs.push((0, spl_token_1.createCloseAccountInstruction)(tokenAccount, this.wallet.publicKey, this.wallet.publicKey, []));
3150
3150
  }
3151
+ const tx = await this.buildTransaction(removeIfStakeIxs);
3151
3152
  const { txSig } = await this.sendTransaction(tx, additionalSigners, this.opts);
3152
3153
  return txSig;
3153
3154
  }
@@ -3158,7 +3159,7 @@ class DriftClient {
3158
3159
  useMarketLastSlotCache: true,
3159
3160
  writableSpotMarketIndexes: [marketIndex],
3160
3161
  });
3161
- const tx = await this.program.transaction.settleRevenueToInsuranceFund(marketIndex, {
3162
+ const ix = await this.program.instruction.settleRevenueToInsuranceFund(marketIndex, {
3162
3163
  accounts: {
3163
3164
  state: await this.getStatePublicKey(),
3164
3165
  spotMarket: spotMarketAccount.pubkey,
@@ -3169,6 +3170,7 @@ class DriftClient {
3169
3170
  },
3170
3171
  remainingAccounts,
3171
3172
  });
3173
+ const tx = await this.buildTransaction(ix);
3172
3174
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
3173
3175
  return txSig;
3174
3176
  }
package/lib/user.js CHANGED
@@ -1677,14 +1677,13 @@ class User {
1677
1677
  withdrawLimit = _1.BN.max(withdrawLimit, userDepositAmount);
1678
1678
  }
1679
1679
  const assetWeight = (0, spotBalance_1.calculateAssetWeight)(userDepositAmount, oracleData.price, spotMarket, 'Initial');
1680
- const amountWithdrawable = assetWeight.eq(numericConstants_1.ZERO)
1681
- ? userDepositAmount
1682
- : freeCollateral
1683
- .mul(numericConstants_1.MARGIN_PRECISION)
1684
- .div(assetWeight)
1685
- .mul(numericConstants_1.PRICE_PRECISION)
1686
- .div(oracleData.price)
1687
- .mul(precisionIncrease);
1680
+ let amountWithdrawable;
1681
+ if (assetWeight.eq(numericConstants_1.ZERO)) {
1682
+ amountWithdrawable = userDepositAmount;
1683
+ }
1684
+ else {
1685
+ amountWithdrawable = (0, _1.divCeil)((0, _1.divCeil)(freeCollateral.mul(numericConstants_1.MARGIN_PRECISION), assetWeight).mul(numericConstants_1.PRICE_PRECISION), oracleData.price).mul(precisionIncrease);
1686
+ }
1688
1687
  const maxWithdrawValue = _1.BN.min(_1.BN.min(amountWithdrawable, userDepositAmount), withdrawLimit.abs());
1689
1688
  if (reduceOnly) {
1690
1689
  return _1.BN.max(maxWithdrawValue, numericConstants_1.ZERO);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.43.0-beta.10",
3
+ "version": "2.43.0-beta.12",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -708,6 +708,8 @@ export class DriftClient {
708
708
  name = DEFAULT_USER_NAME,
709
709
  referrerInfo?: ReferrerInfo
710
710
  ): Promise<[TransactionSignature, PublicKey]> {
711
+ const initializeIxs = [];
712
+
711
713
  const [userAccountPublicKey, initializeUserAccountIx] =
712
714
  await this.getInitializeUserInstructions(
713
715
  subAccountId,
@@ -715,15 +717,16 @@ export class DriftClient {
715
717
  referrerInfo
716
718
  );
717
719
 
718
- const tx = new Transaction();
719
720
  if (subAccountId === 0) {
720
721
  if (
721
722
  !(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))
722
723
  ) {
723
- tx.add(await this.getInitializeUserStatsIx());
724
+ initializeIxs.push(await this.getInitializeUserStatsIx());
724
725
  }
725
726
  }
726
- tx.add(initializeUserAccountIx);
727
+ initializeIxs.push(initializeUserAccountIx);
728
+ const tx = await this.buildTransaction(initializeIxs);
729
+
727
730
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
728
731
 
729
732
  await this.addUser(subAccountId);
@@ -1800,6 +1803,8 @@ export class DriftClient {
1800
1803
  referrerInfo?: ReferrerInfo,
1801
1804
  txParams?: TxParams
1802
1805
  ): Promise<[TransactionSignature, PublicKey]> {
1806
+ const ixs = [];
1807
+
1803
1808
  const [userAccountPublicKey, initializeUserAccountIx] =
1804
1809
  await this.getInitializeUserInstructions(
1805
1810
  subAccountId,
@@ -1813,20 +1818,15 @@ export class DriftClient {
1813
1818
 
1814
1819
  const isSolMarket = spotMarket.mint.equals(WRAPPED_SOL_MINT);
1815
1820
 
1816
- const tx = new Transaction();
1817
-
1818
- tx.add(
1819
- ComputeBudgetProgram.setComputeUnitLimit({
1820
- units: txParams?.computeUnits ?? 600_000,
1821
- })
1822
- );
1821
+ let params: TxParams = {
1822
+ computeUnits: txParams?.computeUnits ?? 600_000,
1823
+ };
1823
1824
 
1824
1825
  if (txParams?.computeUnitsPrice) {
1825
- tx.add(
1826
- ComputeBudgetProgram.setComputeUnitPrice({
1827
- microLamports: txParams.computeUnitsPrice,
1828
- })
1829
- );
1826
+ params = {
1827
+ ...params,
1828
+ computeUnitsPrice: txParams.computeUnitsPrice,
1829
+ };
1830
1830
  }
1831
1831
 
1832
1832
  const authority = this.wallet.publicKey;
@@ -1845,9 +1845,7 @@ export class DriftClient {
1845
1845
 
1846
1846
  userTokenAccount = pubkey;
1847
1847
 
1848
- startIxs.forEach((ix) => {
1849
- tx.add(ix);
1850
- });
1848
+ ixs.push(...startIxs);
1851
1849
  }
1852
1850
 
1853
1851
  const depositCollateralIx = isFromSubaccount
@@ -1870,14 +1868,14 @@ export class DriftClient {
1870
1868
  if (
1871
1869
  !(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))
1872
1870
  ) {
1873
- tx.add(await this.getInitializeUserStatsIx());
1871
+ ixs.push(await this.getInitializeUserStatsIx());
1874
1872
  }
1875
1873
  }
1876
- tx.add(initializeUserAccountIx).add(depositCollateralIx);
1874
+ ixs.push(initializeUserAccountIx, depositCollateralIx);
1877
1875
 
1878
1876
  // Close the wrapped sol account at the end of the transaction
1879
1877
  if (createWSOLTokenAccount) {
1880
- tx.add(
1878
+ ixs.push(
1881
1879
  createCloseAccountInstruction(
1882
1880
  userTokenAccount,
1883
1881
  authority,
@@ -1887,6 +1885,8 @@ export class DriftClient {
1887
1885
  );
1888
1886
  }
1889
1887
 
1888
+ const tx = await this.buildTransaction(ixs, params);
1889
+
1890
1890
  const { txSig, slot } = await this.sendTransaction(
1891
1891
  tx,
1892
1892
  additionalSigners,
@@ -1907,6 +1907,8 @@ export class DriftClient {
1907
1907
  amount: BN,
1908
1908
  referrerInfo?: ReferrerInfo
1909
1909
  ): Promise<[TransactionSignature, PublicKey]> {
1910
+ const ixs = [];
1911
+
1910
1912
  const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] =
1911
1913
  await tokenFaucet.createAssociatedTokenAccountAndMintToInstructions(
1912
1914
  this.wallet.publicKey,
@@ -1929,18 +1931,20 @@ export class DriftClient {
1929
1931
  false
1930
1932
  );
1931
1933
 
1932
- const tx = new Transaction().add(createAssociatedAccountIx).add(mintToIx);
1934
+ ixs.push(createAssociatedAccountIx, mintToIx);
1933
1935
 
1934
1936
  if (subAccountId === 0) {
1935
1937
  if (
1936
1938
  !(await this.checkIfAccountExists(this.getUserStatsAccountPublicKey()))
1937
1939
  ) {
1938
- tx.add(await this.getInitializeUserStatsIx());
1940
+ ixs.push(await this.getInitializeUserStatsIx());
1939
1941
  }
1940
1942
  }
1941
- tx.add(initializeUserAccountIx).add(depositCollateralIx);
1943
+ ixs.push(initializeUserAccountIx, depositCollateralIx);
1944
+
1945
+ const tx = await this.buildTransaction(ixs);
1942
1946
 
1943
- const txSig = await this.program.provider.sendAndConfirm(tx, []);
1947
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
1944
1948
 
1945
1949
  await this.addUser(subAccountId);
1946
1950
 
@@ -1960,12 +1964,7 @@ export class DriftClient {
1960
1964
  associatedTokenAddress: PublicKey,
1961
1965
  reduceOnly = false
1962
1966
  ): Promise<TransactionSignature> {
1963
- const tx = new Transaction();
1964
- tx.add(
1965
- ComputeBudgetProgram.setComputeUnitLimit({
1966
- units: 600_000,
1967
- })
1968
- );
1967
+ const withdrawIxs = [];
1969
1968
 
1970
1969
  const additionalSigners: Array<Signer> = [];
1971
1970
 
@@ -1986,9 +1985,7 @@ export class DriftClient {
1986
1985
 
1987
1986
  associatedTokenAddress = pubkey;
1988
1987
 
1989
- ixs.forEach((ix) => {
1990
- tx.add(ix);
1991
- });
1988
+ withdrawIxs.push(...ixs);
1992
1989
  } else {
1993
1990
  const accountExists = await this.checkIfAccountExists(
1994
1991
  associatedTokenAddress
@@ -2001,22 +1998,22 @@ export class DriftClient {
2001
1998
  associatedTokenAddress
2002
1999
  );
2003
2000
 
2004
- tx.add(createAssociatedTokenAccountIx);
2001
+ withdrawIxs.push(createAssociatedTokenAccountIx);
2005
2002
  }
2006
2003
  }
2007
2004
 
2008
- const withdrawCollateral = await this.getWithdrawIx(
2005
+ const withdrawCollateralIx = await this.getWithdrawIx(
2009
2006
  amount,
2010
2007
  spotMarketAccount.marketIndex,
2011
2008
  associatedTokenAddress,
2012
2009
  reduceOnly
2013
2010
  );
2014
2011
 
2015
- tx.add(withdrawCollateral);
2012
+ withdrawIxs.push(withdrawCollateralIx);
2016
2013
 
2017
2014
  // Close the wrapped sol account at the end of the transaction
2018
2015
  if (createWSOLTokenAccount) {
2019
- tx.add(
2016
+ withdrawIxs.push(
2020
2017
  createCloseAccountInstruction(
2021
2018
  associatedTokenAddress,
2022
2019
  authority,
@@ -2026,6 +2023,9 @@ export class DriftClient {
2026
2023
  );
2027
2024
  }
2028
2025
 
2026
+ const tx = await this.buildTransaction(withdrawIxs, {
2027
+ computeUnits: 600_000,
2028
+ });
2029
2029
  const { txSig, slot } = await this.sendTransaction(
2030
2030
  tx,
2031
2031
  additionalSigners,
@@ -4758,13 +4758,8 @@ export class DriftClient {
4758
4758
  marketIndexes: number[]
4759
4759
  ): Promise<TransactionSignature> {
4760
4760
  const ixs = await this.getSettlePNLsIxs(users, marketIndexes);
4761
- const tx = new Transaction()
4762
- .add(
4763
- ComputeBudgetProgram.setComputeUnitLimit({
4764
- units: 1_000_000,
4765
- })
4766
- )
4767
- .add(...ixs);
4761
+
4762
+ const tx = await this.buildTransaction(ixs, { computeUnits: 1_000_000 });
4768
4763
 
4769
4764
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
4770
4765
  return txSig;
@@ -5484,7 +5479,7 @@ export class DriftClient {
5484
5479
  */
5485
5480
  fromSubaccount?: boolean;
5486
5481
  }): Promise<TransactionSignature> {
5487
- const tx = new Transaction();
5482
+ const addIfStakeIxs = [];
5488
5483
 
5489
5484
  const additionalSigners: Array<Signer> = [];
5490
5485
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
@@ -5501,7 +5496,7 @@ export class DriftClient {
5501
5496
  );
5502
5497
  tokenAccount = pubkey;
5503
5498
  ixs.forEach((ix) => {
5504
- tx.add(ix);
5499
+ addIfStakeIxs.push(ix);
5505
5500
  });
5506
5501
  } else {
5507
5502
  tokenAccount = collateralAccountPublicKey;
@@ -5513,14 +5508,14 @@ export class DriftClient {
5513
5508
  marketIndex,
5514
5509
  tokenAccount
5515
5510
  );
5516
- tx.add(withdrawIx);
5511
+ addIfStakeIxs.push(withdrawIx);
5517
5512
  }
5518
5513
 
5519
5514
  if (initializeStakeAccount) {
5520
5515
  const initializeIx = await this.getInitializeInsuranceFundStakeIx(
5521
5516
  marketIndex
5522
5517
  );
5523
- tx.add(initializeIx);
5518
+ addIfStakeIxs.push(initializeIx);
5524
5519
  }
5525
5520
 
5526
5521
  const addFundsIx = await this.getAddInsuranceFundStakeIx(
@@ -5529,10 +5524,10 @@ export class DriftClient {
5529
5524
  tokenAccount
5530
5525
  );
5531
5526
 
5532
- tx.add(addFundsIx);
5527
+ addIfStakeIxs.push(addFundsIx);
5533
5528
 
5534
5529
  if (createWSOLTokenAccount) {
5535
- tx.add(
5530
+ addIfStakeIxs.push(
5536
5531
  createCloseAccountInstruction(
5537
5532
  tokenAccount,
5538
5533
  this.wallet.publicKey,
@@ -5542,6 +5537,8 @@ export class DriftClient {
5542
5537
  );
5543
5538
  }
5544
5539
 
5540
+ const tx = await this.buildTransaction(addIfStakeIxs);
5541
+
5545
5542
  const { txSig } = await this.sendTransaction(
5546
5543
  tx,
5547
5544
  additionalSigners,
@@ -5568,7 +5565,7 @@ export class DriftClient {
5568
5565
  writableSpotMarketIndexes: [marketIndex],
5569
5566
  });
5570
5567
 
5571
- const tx = await this.program.transaction.requestRemoveInsuranceFundStake(
5568
+ const ix = await this.program.instruction.requestRemoveInsuranceFundStake(
5572
5569
  marketIndex,
5573
5570
  amount,
5574
5571
  {
@@ -5584,6 +5581,8 @@ export class DriftClient {
5584
5581
  }
5585
5582
  );
5586
5583
 
5584
+ const tx = await this.buildTransaction(ix);
5585
+
5587
5586
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
5588
5587
  return txSig;
5589
5588
  }
@@ -5604,8 +5603,8 @@ export class DriftClient {
5604
5603
  writableSpotMarketIndexes: [marketIndex],
5605
5604
  });
5606
5605
 
5607
- const tx =
5608
- await this.program.transaction.cancelRequestRemoveInsuranceFundStake(
5606
+ const ix =
5607
+ await this.program.instruction.cancelRequestRemoveInsuranceFundStake(
5609
5608
  marketIndex,
5610
5609
  {
5611
5610
  accounts: {
@@ -5620,6 +5619,8 @@ export class DriftClient {
5620
5619
  }
5621
5620
  );
5622
5621
 
5622
+ const tx = await this.buildTransaction(ix);
5623
+
5623
5624
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
5624
5625
  return txSig;
5625
5626
  }
@@ -5628,7 +5629,7 @@ export class DriftClient {
5628
5629
  marketIndex: number,
5629
5630
  collateralAccountPublicKey: PublicKey
5630
5631
  ): Promise<TransactionSignature> {
5631
- const tx = new Transaction();
5632
+ const removeIfStakeIxs = [];
5632
5633
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
5633
5634
  const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
5634
5635
  this.program.programId,
@@ -5650,7 +5651,7 @@ export class DriftClient {
5650
5651
  );
5651
5652
  tokenAccount = pubkey;
5652
5653
  ixs.forEach((ix) => {
5653
- tx.add(ix);
5654
+ removeIfStakeIxs.push(ix);
5654
5655
  });
5655
5656
  } else {
5656
5657
  tokenAccount = collateralAccountPublicKey;
@@ -5678,11 +5679,11 @@ export class DriftClient {
5678
5679
  remainingAccounts,
5679
5680
  });
5680
5681
 
5681
- tx.add(removeStakeIx);
5682
+ removeIfStakeIxs.push(removeStakeIx);
5682
5683
 
5683
5684
  // Close the wrapped sol account at the end of the transaction
5684
5685
  if (createWSOLTokenAccount) {
5685
- tx.add(
5686
+ removeIfStakeIxs.push(
5686
5687
  createCloseAccountInstruction(
5687
5688
  tokenAccount,
5688
5689
  this.wallet.publicKey,
@@ -5692,6 +5693,8 @@ export class DriftClient {
5692
5693
  );
5693
5694
  }
5694
5695
 
5696
+ const tx = await this.buildTransaction(removeIfStakeIxs);
5697
+
5695
5698
  const { txSig } = await this.sendTransaction(
5696
5699
  tx,
5697
5700
  additionalSigners,
@@ -5711,7 +5714,7 @@ export class DriftClient {
5711
5714
  writableSpotMarketIndexes: [marketIndex],
5712
5715
  });
5713
5716
 
5714
- const tx = await this.program.transaction.settleRevenueToInsuranceFund(
5717
+ const ix = await this.program.instruction.settleRevenueToInsuranceFund(
5715
5718
  marketIndex,
5716
5719
  {
5717
5720
  accounts: {
@@ -5726,6 +5729,8 @@ export class DriftClient {
5726
5729
  }
5727
5730
  );
5728
5731
 
5732
+ const tx = await this.buildTransaction(ix);
5733
+
5729
5734
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
5730
5735
  return txSig;
5731
5736
  }
package/src/user.ts CHANGED
@@ -48,6 +48,7 @@ import {
48
48
  calculateReservePrice,
49
49
  calculateSpotMarketMarginRatio,
50
50
  calculateUnrealizedAssetWeight,
51
+ divCeil,
51
52
  getBalance,
52
53
  getSignedTokenAmount,
53
54
  getStrictTokenValue,
@@ -3054,14 +3055,17 @@ export class User {
3054
3055
  'Initial'
3055
3056
  );
3056
3057
 
3057
- const amountWithdrawable = assetWeight.eq(ZERO)
3058
- ? userDepositAmount
3059
- : freeCollateral
3060
- .mul(MARGIN_PRECISION)
3061
- .div(assetWeight)
3062
- .mul(PRICE_PRECISION)
3063
- .div(oracleData.price)
3064
- .mul(precisionIncrease);
3058
+ let amountWithdrawable;
3059
+ if (assetWeight.eq(ZERO)) {
3060
+ amountWithdrawable = userDepositAmount;
3061
+ } else {
3062
+ amountWithdrawable = divCeil(
3063
+ divCeil(freeCollateral.mul(MARGIN_PRECISION), assetWeight).mul(
3064
+ PRICE_PRECISION
3065
+ ),
3066
+ oracleData.price
3067
+ ).mul(precisionIncrease);
3068
+ }
3065
3069
 
3066
3070
  const maxWithdrawValue = BN.min(
3067
3071
  BN.min(amountWithdrawable, userDepositAmount),