@drift-labs/sdk 2.28.0-beta.4 → 2.28.0-beta.5

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.
@@ -56,6 +56,9 @@ import {
56
56
  SystemProgram,
57
57
  ComputeBudgetProgram,
58
58
  AddressLookupTableAccount,
59
+ TransactionVersion,
60
+ VersionedTransaction,
61
+ TransactionMessage,
59
62
  } from '@solana/web3.js';
60
63
 
61
64
  import { TokenFaucet } from './tokenFaucet';
@@ -82,9 +85,14 @@ import {
82
85
  } from './accounts/types';
83
86
  import { TxSender, TxSigAndSlot } from './tx/types';
84
87
  import { wrapInTx } from './tx/utils';
85
- import { QUOTE_SPOT_MARKET_INDEX, ZERO } from './constants/numericConstants';
88
+ import {
89
+ BASE_PRECISION,
90
+ PRICE_PRECISION,
91
+ QUOTE_SPOT_MARKET_INDEX,
92
+ ZERO,
93
+ } from './constants/numericConstants';
86
94
  import { findDirectionToClose, positionIsAvailable } from './math/position';
87
- import { getTokenAmount } from './math/spotBalance';
95
+ import { getSignedTokenAmount, getTokenAmount } from './math/spotBalance';
88
96
  import { decodeName, DEFAULT_USER_NAME, encodeName } from './userName';
89
97
  import { OraclePriceData } from './oracles/types';
90
98
  import { DriftClientConfig } from './driftClientConfig';
@@ -93,11 +101,17 @@ import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDrift
93
101
  import { RetryTxSender } from './tx/retryTxSender';
94
102
  import { User } from './user';
95
103
  import { UserSubscriptionConfig } from './userConfig';
96
- import { configs, getMarketsAndOraclesForSubscription } from './config';
104
+ import {
105
+ configs,
106
+ DRIFT_PROGRAM_ID,
107
+ getMarketsAndOraclesForSubscription,
108
+ } from './config';
97
109
  import { WRAPPED_SOL_MINT } from './constants/spotMarkets';
98
110
  import { UserStats } from './userStats';
99
111
  import { isSpotPositionAvailable } from './math/spotPosition';
100
112
  import { calculateMarketMaxAvailableInsurance } from './math/market';
113
+ import { fetchUserStatsAccount } from './accounts/fetch';
114
+ import { castNumberToSpotPrecision } from './math/spotMarket';
101
115
 
102
116
  type RemainingAccountParams = {
103
117
  userAccounts: UserAccount[];
@@ -134,6 +148,7 @@ export class DriftClient {
134
148
  includeDelegates?: boolean;
135
149
  authoritySubAccountMap?: Map<string, number[]>;
136
150
  skipLoadUsers?: boolean;
151
+ txVersion: TransactionVersion;
137
152
 
138
153
  public get isSubscribed() {
139
154
  return this._isSubscribed && this.accountSubscriber.isSubscribed;
@@ -154,13 +169,14 @@ export class DriftClient {
154
169
  );
155
170
  this.program = new Program(
156
171
  driftIDL as Idl,
157
- config.programID,
172
+ config.programID ?? new PublicKey(DRIFT_PROGRAM_ID),
158
173
  this.provider
159
174
  );
160
175
 
161
176
  this.authority = config.authority ?? this.wallet.publicKey;
162
177
  this.activeSubAccountId = config.activeSubAccountId ?? 0;
163
178
  this.skipLoadUsers = config.skipLoadUsers ?? false;
179
+ this.txVersion = config.txVersion ?? 'legacy';
164
180
 
165
181
  if (config.includeDelegates && config.subAccountIds) {
166
182
  throw new Error(
@@ -746,6 +762,21 @@ export class DriftClient {
746
762
  });
747
763
  }
748
764
 
765
+ async getNextSubAccountId(): Promise<number> {
766
+ const userStats = this.getUserStats();
767
+ let userStatsAccount: UserStatsAccount;
768
+ if (!userStats) {
769
+ userStatsAccount = await fetchUserStatsAccount(
770
+ this.connection,
771
+ this.program,
772
+ this.wallet.publicKey
773
+ );
774
+ } else {
775
+ userStatsAccount = userStats.getAccount();
776
+ }
777
+ return userStatsAccount.numberOfSubAccountsCreated;
778
+ }
779
+
749
780
  public async initializeReferrerName(
750
781
  name: string
751
782
  ): Promise<TransactionSignature> {
@@ -1002,7 +1033,7 @@ export class DriftClient {
1002
1033
  });
1003
1034
 
1004
1035
  const { txSig } = await this.sendTransaction(
1005
- wrapInTx(ix, txParams?.computeUnits, txParams?.computeUnitsPrice),
1036
+ await this.buildTransaction(ix, txParams),
1006
1037
  [],
1007
1038
  this.opts
1008
1039
  );
@@ -1107,28 +1138,58 @@ export class DriftClient {
1107
1138
  }
1108
1139
 
1109
1140
  public getQuoteAssetTokenAmount(): BN {
1110
- const spotMarket = this.getSpotMarketAccount(QUOTE_SPOT_MARKET_INDEX);
1111
- const spotPosition = this.getSpotPosition(QUOTE_SPOT_MARKET_INDEX);
1112
- return getTokenAmount(
1113
- spotPosition.scaledBalance,
1114
- spotMarket,
1115
- spotPosition.balanceType
1116
- );
1141
+ return this.getTokenAmount(QUOTE_SPOT_MARKET_INDEX);
1117
1142
  }
1118
1143
 
1144
+ /**
1145
+ * Returns the token amount for a given market. The spot market precision is based on the token mint decimals.
1146
+ * Positive if it is a deposit, negative if it is a borrow.
1147
+ * @param marketIndex
1148
+ */
1119
1149
  public getTokenAmount(marketIndex: number): BN {
1120
1150
  const spotPosition = this.getSpotPosition(marketIndex);
1121
1151
  if (spotPosition === undefined) {
1122
1152
  return ZERO;
1123
1153
  }
1124
1154
  const spotMarket = this.getSpotMarketAccount(marketIndex);
1125
- return getTokenAmount(
1126
- spotPosition.scaledBalance,
1127
- spotMarket,
1155
+ return getSignedTokenAmount(
1156
+ getTokenAmount(
1157
+ spotPosition.scaledBalance,
1158
+ spotMarket,
1159
+ spotPosition.balanceType
1160
+ ),
1128
1161
  spotPosition.balanceType
1129
1162
  );
1130
1163
  }
1131
1164
 
1165
+ /**
1166
+ * Converts an amount to the spot precision for a given market. The spot market precision is based on the token mint decimals.
1167
+ * @param marketIndex
1168
+ * @param amount
1169
+ */
1170
+ public convertToSpotPrecision(marketIndex: number, amount: BN | number): BN {
1171
+ const spotMarket = this.getSpotMarketAccount(marketIndex);
1172
+ return castNumberToSpotPrecision(amount, spotMarket);
1173
+ }
1174
+
1175
+ /**
1176
+ * Converts an amount to the perp precision. The perp market precision is {@link BASE_PRECISION} (1e9).
1177
+ * @param amount
1178
+ */
1179
+ public convertToPerpPrecision(amount: BN | number): BN {
1180
+ amount = typeof amount === 'number' ? new BN(amount) : amount;
1181
+ return amount.mul(BASE_PRECISION);
1182
+ }
1183
+
1184
+ /**
1185
+ * Converts an amount to the price precision. The perp market precision is {@link PRICE_PRECISION} (1e6).
1186
+ * @param amount
1187
+ */
1188
+ public convertToPricePrecision(amount: BN | number): BN {
1189
+ amount = typeof amount === 'number' ? new BN(amount) : amount;
1190
+ return amount.mul(PRICE_PRECISION);
1191
+ }
1192
+
1132
1193
  getRemainingAccounts(params: RemainingAccountParams): AccountMeta[] {
1133
1194
  const { oracleAccountMap, spotMarketAccountMap, perpMarketAccountMap } =
1134
1195
  this.getRemainingAccountMapsForUsers(params.userAccounts);
@@ -1412,10 +1473,41 @@ export class DriftClient {
1412
1473
  );
1413
1474
  }
1414
1475
 
1476
+ /**
1477
+ * Get the associated token address for the given spot market
1478
+ * @param marketIndex
1479
+ * @param useNative
1480
+ */
1481
+ public async getAssociatedTokenAccount(
1482
+ marketIndex: number,
1483
+ useNative = true
1484
+ ): Promise<PublicKey> {
1485
+ const spotMarket = this.getSpotMarketAccount(marketIndex);
1486
+ if (useNative && spotMarket.mint.equals(WRAPPED_SOL_MINT)) {
1487
+ return this.wallet.publicKey;
1488
+ }
1489
+ const mint = spotMarket.mint;
1490
+ return await Token.getAssociatedTokenAddress(
1491
+ ASSOCIATED_TOKEN_PROGRAM_ID,
1492
+ TOKEN_PROGRAM_ID,
1493
+ mint,
1494
+ this.wallet.publicKey
1495
+ );
1496
+ }
1497
+
1498
+ /**
1499
+ * Deposit funds into the given spot market
1500
+ *
1501
+ * @param amount
1502
+ * @param marketIndex
1503
+ * @param associatedTokenAccount can be the wallet public key if using native sol
1504
+ * @param subAccountId
1505
+ * @param reduceOnly
1506
+ */
1415
1507
  public async deposit(
1416
1508
  amount: BN,
1417
1509
  marketIndex: number,
1418
- collateralAccountPublicKey: PublicKey,
1510
+ associatedTokenAccount: PublicKey,
1419
1511
  subAccountId?: number,
1420
1512
  reduceOnly = false
1421
1513
  ): Promise<TransactionSignature> {
@@ -1435,13 +1527,13 @@ export class DriftClient {
1435
1527
  const signerAuthority = this.wallet.publicKey;
1436
1528
 
1437
1529
  const createWSOLTokenAccount =
1438
- isSolMarket && collateralAccountPublicKey.equals(signerAuthority);
1530
+ isSolMarket && associatedTokenAccount.equals(signerAuthority);
1439
1531
 
1440
1532
  if (createWSOLTokenAccount) {
1441
1533
  const { ixs, signers, pubkey } =
1442
1534
  await this.getWrappedSolAccountCreationIxs(amount, true);
1443
1535
 
1444
- collateralAccountPublicKey = pubkey;
1536
+ associatedTokenAccount = pubkey;
1445
1537
 
1446
1538
  ixs.forEach((ix) => {
1447
1539
  tx.add(ix);
@@ -1453,7 +1545,7 @@ export class DriftClient {
1453
1545
  const depositCollateralIx = await this.getDepositInstruction(
1454
1546
  amount,
1455
1547
  marketIndex,
1456
- collateralAccountPublicKey,
1548
+ associatedTokenAccount,
1457
1549
  subAccountId,
1458
1550
  reduceOnly,
1459
1551
  true
@@ -1466,7 +1558,7 @@ export class DriftClient {
1466
1558
  tx.add(
1467
1559
  Token.createCloseAccountInstruction(
1468
1560
  TOKEN_PROGRAM_ID,
1469
- collateralAccountPublicKey,
1561
+ associatedTokenAccount,
1470
1562
  signerAuthority,
1471
1563
  signerAuthority,
1472
1564
  []
@@ -1777,10 +1869,17 @@ export class DriftClient {
1777
1869
  return [txSig, userAccountPublicKey];
1778
1870
  }
1779
1871
 
1872
+ /**
1873
+ * Withdraws from a user account. If deposit doesn't already exist, creates a borrow
1874
+ * @param amount
1875
+ * @param marketIndex
1876
+ * @param associatedTokenAddress - the token account to withdraw to. can be the wallet public key if using native sol
1877
+ * @param reduceOnly
1878
+ */
1780
1879
  public async withdraw(
1781
1880
  amount: BN,
1782
1881
  marketIndex: number,
1783
- userTokenAccount: PublicKey,
1882
+ associatedTokenAddress: PublicKey,
1784
1883
  reduceOnly = false
1785
1884
  ): Promise<TransactionSignature> {
1786
1885
  const tx = new Transaction();
@@ -1799,13 +1898,13 @@ export class DriftClient {
1799
1898
  const authority = this.wallet.publicKey;
1800
1899
 
1801
1900
  const createWSOLTokenAccount =
1802
- isSolMarket && userTokenAccount.equals(authority);
1901
+ isSolMarket && associatedTokenAddress.equals(authority);
1803
1902
 
1804
1903
  if (createWSOLTokenAccount) {
1805
1904
  const { ixs, signers, pubkey } =
1806
1905
  await this.getWrappedSolAccountCreationIxs(amount, false);
1807
1906
 
1808
- userTokenAccount = pubkey;
1907
+ associatedTokenAddress = pubkey;
1809
1908
 
1810
1909
  ixs.forEach((ix) => {
1811
1910
  tx.add(ix);
@@ -1813,13 +1912,15 @@ export class DriftClient {
1813
1912
 
1814
1913
  signers.forEach((signer) => additionalSigners.push(signer));
1815
1914
  } else {
1816
- const accountExists = await this.checkIfAccountExists(userTokenAccount);
1915
+ const accountExists = await this.checkIfAccountExists(
1916
+ associatedTokenAddress
1917
+ );
1817
1918
 
1818
1919
  if (!accountExists) {
1819
1920
  const createAssociatedTokenAccountIx =
1820
1921
  this.getAssociatedTokenAccountCreationIx(
1821
1922
  spotMarketAccount.mint,
1822
- userTokenAccount
1923
+ associatedTokenAddress
1823
1924
  );
1824
1925
 
1825
1926
  tx.add(createAssociatedTokenAccountIx);
@@ -1829,7 +1930,7 @@ export class DriftClient {
1829
1930
  const withdrawCollateral = await this.getWithdrawIx(
1830
1931
  amount,
1831
1932
  spotMarketAccount.marketIndex,
1832
- userTokenAccount,
1933
+ associatedTokenAddress,
1833
1934
  reduceOnly
1834
1935
  );
1835
1936
 
@@ -1840,7 +1941,7 @@ export class DriftClient {
1840
1941
  tx.add(
1841
1942
  Token.createCloseAccountInstruction(
1842
1943
  TOKEN_PROGRAM_ID,
1843
- userTokenAccount,
1944
+ associatedTokenAddress,
1844
1945
  authority,
1845
1946
  authority,
1846
1947
  []
@@ -1895,6 +1996,14 @@ export class DriftClient {
1895
1996
  );
1896
1997
  }
1897
1998
 
1999
+ /**
2000
+ * Withdraws from the fromSubAccount and deposits into the toSubAccount
2001
+ * @param amount
2002
+ * @param marketIndex
2003
+ * @param fromSubAccountId
2004
+ * @param toSubAccountId
2005
+ * @param txParams
2006
+ */
1898
2007
  public async transferDeposit(
1899
2008
  amount: BN,
1900
2009
  marketIndex: number,
@@ -1903,15 +2012,14 @@ export class DriftClient {
1903
2012
  txParams?: TxParams
1904
2013
  ): Promise<TransactionSignature> {
1905
2014
  const { txSig, slot } = await this.sendTransaction(
1906
- wrapInTx(
2015
+ await this.buildTransaction(
1907
2016
  await this.getTransferDepositIx(
1908
2017
  amount,
1909
2018
  marketIndex,
1910
2019
  fromSubAccountId,
1911
2020
  toSubAccountId
1912
2021
  ),
1913
- txParams?.computeUnits,
1914
- txParams?.computeUnitsPrice
2022
+ txParams
1915
2023
  ),
1916
2024
  [],
1917
2025
  this.opts
@@ -1989,10 +2097,9 @@ export class DriftClient {
1989
2097
  txParams?: TxParams
1990
2098
  ): Promise<TransactionSignature> {
1991
2099
  const { txSig } = await this.sendTransaction(
1992
- wrapInTx(
2100
+ await this.buildTransaction(
1993
2101
  await this.updateSpotMarketCumulativeInterestIx(marketIndex),
1994
- txParams?.computeUnits,
1995
- txParams?.computeUnitsPrice
2102
+ txParams
1996
2103
  ),
1997
2104
  [],
1998
2105
  this.opts
@@ -2019,10 +2126,9 @@ export class DriftClient {
2019
2126
  txParams?: TxParams
2020
2127
  ): Promise<TransactionSignature> {
2021
2128
  const { txSig } = await this.sendTransaction(
2022
- wrapInTx(
2129
+ await this.buildTransaction(
2023
2130
  await this.settleLPIx(settleeUserAccountPublicKey, marketIndex),
2024
- txParams?.computeUnits,
2025
- txParams?.computeUnitsPrice
2131
+ txParams
2026
2132
  ),
2027
2133
  [],
2028
2134
  this.opts
@@ -2058,10 +2164,9 @@ export class DriftClient {
2058
2164
  txParams?: TxParams
2059
2165
  ): Promise<TransactionSignature> {
2060
2166
  const { txSig } = await this.sendTransaction(
2061
- wrapInTx(
2167
+ await this.buildTransaction(
2062
2168
  await this.getRemovePerpLpSharesIx(marketIndex, sharesToBurn),
2063
- txParams?.computeUnits,
2064
- txParams?.computeUnitsPrice
2169
+ txParams
2065
2170
  ),
2066
2171
  [],
2067
2172
  this.opts
@@ -2076,14 +2181,13 @@ export class DriftClient {
2076
2181
  txParams?: TxParams
2077
2182
  ): Promise<TransactionSignature> {
2078
2183
  const { txSig } = await this.sendTransaction(
2079
- wrapInTx(
2184
+ await this.buildTransaction(
2080
2185
  await this.getRemovePerpLpSharesInExpiringMarket(
2081
2186
  marketIndex,
2082
2187
  userAccountPublicKey,
2083
2188
  sharesToBurn
2084
2189
  ),
2085
- txParams?.computeUnits,
2086
- txParams?.computeUnitsPrice
2190
+ txParams
2087
2191
  ),
2088
2192
  [],
2089
2193
  this.opts
@@ -2168,10 +2272,9 @@ export class DriftClient {
2168
2272
  txParams?: TxParams
2169
2273
  ): Promise<TransactionSignature> {
2170
2274
  const { txSig, slot } = await this.sendTransaction(
2171
- wrapInTx(
2275
+ await this.buildTransaction(
2172
2276
  await this.getAddPerpLpSharesIx(amount, marketIndex),
2173
- txParams?.computeUnits,
2174
- txParams?.computeUnitsPrice
2277
+ txParams
2175
2278
  ),
2176
2279
  [],
2177
2280
  this.opts
@@ -2201,6 +2304,9 @@ export class DriftClient {
2201
2304
  });
2202
2305
  }
2203
2306
 
2307
+ /**
2308
+ * @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
2309
+ */
2204
2310
  public async openPosition(
2205
2311
  direction: PositionDirection,
2206
2312
  amount: BN,
@@ -2351,10 +2457,9 @@ export class DriftClient {
2351
2457
  txParams?: TxParams
2352
2458
  ): Promise<TransactionSignature> {
2353
2459
  const { txSig, slot } = await this.sendTransaction(
2354
- wrapInTx(
2460
+ await this.buildTransaction(
2355
2461
  await this.getPlacePerpOrderIx(orderParams),
2356
- txParams?.computeUnits,
2357
- txParams?.computeUnitsPrice
2462
+ txParams
2358
2463
  ),
2359
2464
  [],
2360
2465
  this.opts
@@ -2400,10 +2505,9 @@ export class DriftClient {
2400
2505
  txParams?: TxParams
2401
2506
  ): Promise<TransactionSignature> {
2402
2507
  const { txSig } = await this.sendTransaction(
2403
- wrapInTx(
2508
+ await this.buildTransaction(
2404
2509
  await this.getUpdateAMMsIx(marketIndexes),
2405
- txParams?.computeUnits,
2406
- txParams?.computeUnitsPrice
2510
+ txParams
2407
2511
  ),
2408
2512
  [],
2409
2513
  this.opts
@@ -2450,10 +2554,9 @@ export class DriftClient {
2450
2554
  txParams?: TxParams
2451
2555
  ): Promise<TransactionSignature> {
2452
2556
  const { txSig } = await this.sendTransaction(
2453
- wrapInTx(
2557
+ await this.buildTransaction(
2454
2558
  await this.getSettleExpiredMarketIx(marketIndex),
2455
- txParams?.computeUnits,
2456
- txParams?.computeUnitsPrice
2559
+ txParams
2457
2560
  ),
2458
2561
  [],
2459
2562
  this.opts
@@ -2523,7 +2626,7 @@ export class DriftClient {
2523
2626
  });
2524
2627
 
2525
2628
  const { txSig } = await this.sendTransaction(
2526
- wrapInTx(ix, txParams?.computeUnits, txParams?.computeUnitsPrice),
2629
+ await this.buildTransaction(ix, txParams),
2527
2630
  [],
2528
2631
  this.opts
2529
2632
  );
@@ -2536,10 +2639,9 @@ export class DriftClient {
2536
2639
  txParams?: TxParams
2537
2640
  ): Promise<TransactionSignature> {
2538
2641
  const { txSig } = await this.sendTransaction(
2539
- wrapInTx(
2642
+ await this.buildTransaction(
2540
2643
  await this.getCancelOrderIx(orderId),
2541
- txParams?.computeUnits,
2542
- txParams?.computeUnitsPrice
2644
+ txParams
2543
2645
  ),
2544
2646
  [],
2545
2647
  this.opts
@@ -2572,10 +2674,9 @@ export class DriftClient {
2572
2674
  txParams?: TxParams
2573
2675
  ): Promise<TransactionSignature> {
2574
2676
  const { txSig } = await this.sendTransaction(
2575
- wrapInTx(
2677
+ await this.buildTransaction(
2576
2678
  await this.getCancelOrderByUserIdIx(userOrderId),
2577
- txParams?.computeUnits,
2578
- txParams?.computeUnitsPrice
2679
+ txParams
2579
2680
  ),
2580
2681
  [],
2581
2682
  this.opts
@@ -2614,10 +2715,9 @@ export class DriftClient {
2614
2715
  txParams?: TxParams
2615
2716
  ): Promise<TransactionSignature> {
2616
2717
  const { txSig } = await this.sendTransaction(
2617
- wrapInTx(
2718
+ await this.buildTransaction(
2618
2719
  await this.getCancelOrdersIx(marketType, marketIndex, direction),
2619
- txParams?.computeUnits,
2620
- txParams?.computeUnitsPrice
2720
+ txParams
2621
2721
  ),
2622
2722
  [],
2623
2723
  this.opts
@@ -2710,7 +2810,7 @@ export class DriftClient {
2710
2810
  txParams?: TxParams
2711
2811
  ): Promise<TransactionSignature> {
2712
2812
  const { txSig } = await this.sendTransaction(
2713
- wrapInTx(
2813
+ await this.buildTransaction(
2714
2814
  await this.getFillPerpOrderIx(
2715
2815
  userAccountPublicKey,
2716
2816
  user,
@@ -2718,8 +2818,7 @@ export class DriftClient {
2718
2818
  makerInfo,
2719
2819
  referrerInfo
2720
2820
  ),
2721
- txParams?.computeUnits,
2722
- txParams?.computeUnitsPrice
2821
+ txParams
2723
2822
  ),
2724
2823
  [],
2725
2824
  this.opts
@@ -2827,10 +2926,9 @@ export class DriftClient {
2827
2926
  txParams?: TxParams
2828
2927
  ): Promise<TransactionSignature> {
2829
2928
  const { txSig, slot } = await this.sendTransaction(
2830
- wrapInTx(
2929
+ await this.buildTransaction(
2831
2930
  await this.getPlaceSpotOrderIx(orderParams),
2832
- txParams?.computeUnits,
2833
- txParams?.computeUnitsPrice
2931
+ txParams
2834
2932
  ),
2835
2933
  [],
2836
2934
  this.opts
@@ -2878,7 +2976,7 @@ export class DriftClient {
2878
2976
  txParams?: TxParams
2879
2977
  ): Promise<TransactionSignature> {
2880
2978
  const { txSig } = await this.sendTransaction(
2881
- wrapInTx(
2979
+ await this.buildTransaction(
2882
2980
  await this.getFillSpotOrderIx(
2883
2981
  userAccountPublicKey,
2884
2982
  user,
@@ -2887,8 +2985,7 @@ export class DriftClient {
2887
2985
  makerInfo,
2888
2986
  referrerInfo
2889
2987
  ),
2890
- txParams?.computeUnits,
2891
- txParams?.computeUnitsPrice
2988
+ txParams
2892
2989
  ),
2893
2990
  [],
2894
2991
  this.opts
@@ -3174,10 +3271,9 @@ export class DriftClient {
3174
3271
  txParams?: TxParams
3175
3272
  ): Promise<TransactionSignature> {
3176
3273
  const { txSig } = await this.sendTransaction(
3177
- wrapInTx(
3274
+ await this.buildTransaction(
3178
3275
  await this.getTriggerOrderIx(userAccountPublicKey, user, order),
3179
- txParams?.computeUnits,
3180
- txParams?.computeUnitsPrice
3276
+ txParams
3181
3277
  ),
3182
3278
  [],
3183
3279
  this.opts
@@ -3227,10 +3323,9 @@ export class DriftClient {
3227
3323
  txParams?: TxParams
3228
3324
  ): Promise<TransactionSignature> {
3229
3325
  const { txSig } = await this.sendTransaction(
3230
- wrapInTx(
3326
+ await this.buildTransaction(
3231
3327
  await this.getForceCancelOrdersIx(userAccountPublicKey, user),
3232
- txParams?.computeUnits,
3233
- txParams?.computeUnitsPrice
3328
+ txParams
3234
3329
  ),
3235
3330
  [],
3236
3331
  this.opts
@@ -3266,10 +3361,9 @@ export class DriftClient {
3266
3361
  txParams?: TxParams
3267
3362
  ): Promise<TransactionSignature> {
3268
3363
  const { txSig } = await this.sendTransaction(
3269
- wrapInTx(
3364
+ await this.buildTransaction(
3270
3365
  await this.getUpdateUserIdleIx(userAccountPublicKey, user),
3271
- txParams?.computeUnits,
3272
- txParams?.computeUnitsPrice
3366
+ txParams
3273
3367
  ),
3274
3368
  [],
3275
3369
  this.opts
@@ -3305,14 +3399,13 @@ export class DriftClient {
3305
3399
  txParams?: TxParams
3306
3400
  ): Promise<TransactionSignature> {
3307
3401
  const { txSig, slot } = await this.sendTransaction(
3308
- wrapInTx(
3402
+ await this.buildTransaction(
3309
3403
  await this.getPlaceAndTakePerpOrderIx(
3310
3404
  orderParams,
3311
3405
  makerInfo,
3312
3406
  referrerInfo
3313
3407
  ),
3314
- txParams?.computeUnits,
3315
- txParams?.computeUnitsPrice
3408
+ txParams
3316
3409
  ),
3317
3410
  [],
3318
3411
  this.opts
@@ -3400,14 +3493,13 @@ export class DriftClient {
3400
3493
  txParams?: TxParams
3401
3494
  ): Promise<TransactionSignature> {
3402
3495
  const { txSig, slot } = await this.sendTransaction(
3403
- wrapInTx(
3496
+ await this.buildTransaction(
3404
3497
  await this.getPlaceAndMakePerpOrderIx(
3405
3498
  orderParams,
3406
3499
  takerInfo,
3407
3500
  referrerInfo
3408
3501
  ),
3409
- txParams?.computeUnits,
3410
- txParams?.computeUnitsPrice
3502
+ txParams
3411
3503
  ),
3412
3504
  [],
3413
3505
  this.opts
@@ -3472,15 +3564,14 @@ export class DriftClient {
3472
3564
  txParams?: TxParams
3473
3565
  ): Promise<TransactionSignature> {
3474
3566
  const { txSig, slot } = await this.sendTransaction(
3475
- wrapInTx(
3567
+ await this.buildTransaction(
3476
3568
  await this.getPlaceAndTakeSpotOrderIx(
3477
3569
  orderParams,
3478
3570
  fulfillmentConfig,
3479
3571
  makerInfo,
3480
3572
  referrerInfo
3481
3573
  ),
3482
- txParams?.computeUnits,
3483
- txParams?.computeUnitsPrice
3574
+ txParams
3484
3575
  ),
3485
3576
  [],
3486
3577
  this.opts
@@ -3571,15 +3662,14 @@ export class DriftClient {
3571
3662
  txParams?: TxParams
3572
3663
  ): Promise<TransactionSignature> {
3573
3664
  const { txSig, slot } = await this.sendTransaction(
3574
- wrapInTx(
3665
+ await this.buildTransaction(
3575
3666
  await this.getPlaceAndMakeSpotOrderIx(
3576
3667
  orderParams,
3577
3668
  takerInfo,
3578
3669
  fulfillmentConfig,
3579
3670
  referrerInfo
3580
3671
  ),
3581
- txParams?.computeUnits,
3582
- txParams?.computeUnitsPrice
3672
+ txParams
3583
3673
  ),
3584
3674
  [],
3585
3675
  this.opts
@@ -3647,9 +3737,7 @@ export class DriftClient {
3647
3737
  }
3648
3738
 
3649
3739
  /**
3650
- * Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
3651
- * @param marketIndex
3652
- * @returns
3740
+ * @deprecated use {@link placePerpOrder} or {@link placeAndTakePerpOrder} instead
3653
3741
  */
3654
3742
  public async closePosition(
3655
3743
  marketIndex: number,
@@ -3783,10 +3871,9 @@ export class DriftClient {
3783
3871
  };
3784
3872
 
3785
3873
  const { txSig } = await this.sendTransaction(
3786
- wrapInTx(
3874
+ await this.buildTransaction(
3787
3875
  await this.getModifyOrderIx(orderId, orderParams),
3788
- txParams?.computeUnits,
3789
- txParams?.computeUnitsPrice
3876
+ txParams
3790
3877
  ),
3791
3878
  [],
3792
3879
  this.opts
@@ -3883,10 +3970,9 @@ export class DriftClient {
3883
3970
  };
3884
3971
 
3885
3972
  const { txSig } = await this.sendTransaction(
3886
- wrapInTx(
3973
+ await this.buildTransaction(
3887
3974
  await this.getModifyOrderByUserIdIx(userOrderId, orderParams),
3888
- txParams?.computeUnits,
3889
- txParams?.computeUnitsPrice
3975
+ txParams
3890
3976
  ),
3891
3977
  [],
3892
3978
  this.opts
@@ -3959,14 +4045,13 @@ export class DriftClient {
3959
4045
  txParams?: TxParams
3960
4046
  ): Promise<TransactionSignature> {
3961
4047
  const { txSig } = await this.sendTransaction(
3962
- wrapInTx(
4048
+ await this.buildTransaction(
3963
4049
  await this.settlePNLIx(
3964
4050
  settleeUserAccountPublicKey,
3965
4051
  settleeUserAccount,
3966
4052
  marketIndex
3967
4053
  ),
3968
- txParams?.computeUnits,
3969
- txParams?.computeUnitsPrice
4054
+ txParams
3970
4055
  ),
3971
4056
  [],
3972
4057
  this.opts
@@ -4005,7 +4090,7 @@ export class DriftClient {
4005
4090
  txParams?: TxParams
4006
4091
  ): Promise<TransactionSignature> {
4007
4092
  const { txSig, slot } = await this.sendTransaction(
4008
- wrapInTx(
4093
+ await this.buildTransaction(
4009
4094
  await this.getLiquidatePerpIx(
4010
4095
  userAccountPublicKey,
4011
4096
  userAccount,
@@ -4013,8 +4098,7 @@ export class DriftClient {
4013
4098
  maxBaseAssetAmount,
4014
4099
  limitPrice
4015
4100
  ),
4016
- txParams?.computeUnits,
4017
- txParams?.computeUnitsPrice
4101
+ txParams
4018
4102
  ),
4019
4103
  [],
4020
4104
  this.opts
@@ -4072,7 +4156,7 @@ export class DriftClient {
4072
4156
  txParams?: TxParams
4073
4157
  ): Promise<TransactionSignature> {
4074
4158
  const { txSig, slot } = await this.sendTransaction(
4075
- wrapInTx(
4159
+ await this.buildTransaction(
4076
4160
  await this.getLiquidateSpotIx(
4077
4161
  userAccountPublicKey,
4078
4162
  userAccount,
@@ -4081,8 +4165,7 @@ export class DriftClient {
4081
4165
  maxLiabilityTransfer,
4082
4166
  limitPrice
4083
4167
  ),
4084
- txParams?.computeUnits,
4085
- txParams?.computeUnitsPrice
4168
+ txParams
4086
4169
  ),
4087
4170
  [],
4088
4171
  this.opts
@@ -4143,7 +4226,7 @@ export class DriftClient {
4143
4226
  txParams?: TxParams
4144
4227
  ): Promise<TransactionSignature> {
4145
4228
  const { txSig, slot } = await this.sendTransaction(
4146
- wrapInTx(
4229
+ await this.buildTransaction(
4147
4230
  await this.getLiquidateBorrowForPerpPnlIx(
4148
4231
  userAccountPublicKey,
4149
4232
  userAccount,
@@ -4152,8 +4235,7 @@ export class DriftClient {
4152
4235
  maxLiabilityTransfer,
4153
4236
  limitPrice
4154
4237
  ),
4155
- txParams?.computeUnits,
4156
- txParams?.computeUnitsPrice
4238
+ txParams
4157
4239
  ),
4158
4240
  [],
4159
4241
  this.opts
@@ -4214,7 +4296,7 @@ export class DriftClient {
4214
4296
  txParams?: TxParams
4215
4297
  ): Promise<TransactionSignature> {
4216
4298
  const { txSig, slot } = await this.sendTransaction(
4217
- wrapInTx(
4299
+ await this.buildTransaction(
4218
4300
  await this.getLiquidatePerpPnlForDepositIx(
4219
4301
  userAccountPublicKey,
4220
4302
  userAccount,
@@ -4223,8 +4305,7 @@ export class DriftClient {
4223
4305
  maxPnlTransfer,
4224
4306
  limitPrice
4225
4307
  ),
4226
- txParams?.computeUnits,
4227
- txParams?.computeUnitsPrice
4308
+ txParams
4228
4309
  ),
4229
4310
  [],
4230
4311
  this.opts
@@ -4282,14 +4363,13 @@ export class DriftClient {
4282
4363
  txParams?: TxParams
4283
4364
  ): Promise<TransactionSignature> {
4284
4365
  const { txSig } = await this.sendTransaction(
4285
- wrapInTx(
4366
+ await this.buildTransaction(
4286
4367
  await this.getResolvePerpBankruptcyIx(
4287
4368
  userAccountPublicKey,
4288
4369
  userAccount,
4289
4370
  marketIndex
4290
4371
  ),
4291
- txParams?.computeUnits,
4292
- txParams?.computeUnitsPrice
4372
+ txParams
4293
4373
  ),
4294
4374
  [],
4295
4375
  this.opts
@@ -4346,14 +4426,13 @@ export class DriftClient {
4346
4426
  txParams?: TxParams
4347
4427
  ): Promise<TransactionSignature> {
4348
4428
  const { txSig } = await this.sendTransaction(
4349
- wrapInTx(
4429
+ await this.buildTransaction(
4350
4430
  await this.getResolveSpotBankruptcyIx(
4351
4431
  userAccountPublicKey,
4352
4432
  userAccount,
4353
4433
  marketIndex
4354
4434
  ),
4355
- txParams?.computeUnits,
4356
- txParams?.computeUnitsPrice
4435
+ txParams
4357
4436
  ),
4358
4437
  [],
4359
4438
  this.opts
@@ -4404,10 +4483,9 @@ export class DriftClient {
4404
4483
  txParams?: TxParams
4405
4484
  ): Promise<TransactionSignature> {
4406
4485
  const { txSig } = await this.sendTransaction(
4407
- wrapInTx(
4486
+ await this.buildTransaction(
4408
4487
  await this.getUpdateFundingRateIx(perpMarketIndex, oracle),
4409
- txParams?.computeUnits,
4410
- txParams?.computeUnitsPrice
4488
+ txParams
4411
4489
  ),
4412
4490
  [],
4413
4491
  this.opts
@@ -4437,10 +4515,9 @@ export class DriftClient {
4437
4515
  txParams?: TxParams
4438
4516
  ): Promise<TransactionSignature> {
4439
4517
  const { txSig } = await this.sendTransaction(
4440
- wrapInTx(
4518
+ await this.buildTransaction(
4441
4519
  await this.getSettleFundingPaymentIx(userAccountPublicKey),
4442
- txParams?.computeUnits,
4443
- txParams?.computeUnitsPrice
4520
+ txParams
4444
4521
  ),
4445
4522
  [],
4446
4523
  this.opts
@@ -4499,10 +4576,9 @@ export class DriftClient {
4499
4576
  txParams?: TxParams
4500
4577
  ): Promise<TransactionSignature> {
4501
4578
  const { txSig } = await this.sendTransaction(
4502
- wrapInTx(
4579
+ await this.buildTransaction(
4503
4580
  await this.getInitializeInsuranceFundStakeIx(marketIndex),
4504
- txParams?.computeUnits,
4505
- txParams?.computeUnitsPrice
4581
+ txParams
4506
4582
  ),
4507
4583
  [],
4508
4584
  this.opts
@@ -4859,10 +4935,9 @@ export class DriftClient {
4859
4935
  txParams?: TxParams
4860
4936
  ): Promise<TransactionSignature> {
4861
4937
  const { txSig } = await this.sendTransaction(
4862
- wrapInTx(
4938
+ await this.buildTransaction(
4863
4939
  await this.getResolvePerpPnlDeficitIx(spotMarketIndex, perpMarketIndex),
4864
- txParams?.computeUnits,
4865
- txParams?.computeUnitsPrice
4940
+ txParams
4866
4941
  ),
4867
4942
  [],
4868
4943
  this.opts
@@ -4956,11 +5031,71 @@ export class DriftClient {
4956
5031
  }
4957
5032
 
4958
5033
  sendTransaction(
4959
- tx: Transaction,
5034
+ tx: Transaction | VersionedTransaction,
4960
5035
  additionalSigners?: Array<Signer>,
4961
5036
  opts?: ConfirmOptions,
4962
5037
  preSigned?: boolean
4963
5038
  ): Promise<TxSigAndSlot> {
4964
- return this.txSender.send(tx, additionalSigners, opts, preSigned);
5039
+ // @ts-ignore
5040
+ if (!tx.message) {
5041
+ return this.txSender.send(
5042
+ tx as Transaction,
5043
+ additionalSigners,
5044
+ opts,
5045
+ preSigned
5046
+ );
5047
+ } else {
5048
+ return this.txSender.sendVersionedTransaction(
5049
+ tx as VersionedTransaction,
5050
+ additionalSigners,
5051
+ opts
5052
+ );
5053
+ }
5054
+ }
5055
+
5056
+ async buildTransaction(
5057
+ instructions: TransactionInstruction | TransactionInstruction[],
5058
+ txParams?: TxParams
5059
+ ): Promise<Transaction | VersionedTransaction> {
5060
+ const allIx = [];
5061
+ const computeUnits = txParams?.computeUnits ?? 600_000;
5062
+ if (computeUnits !== 200_000) {
5063
+ allIx.push(
5064
+ ComputeBudgetProgram.setComputeUnitLimit({
5065
+ units: computeUnits,
5066
+ })
5067
+ );
5068
+ }
5069
+ const computeUnitsPrice = txParams?.computeUnitsPrice ?? 0;
5070
+ if (computeUnitsPrice !== 0) {
5071
+ allIx.push(
5072
+ ComputeBudgetProgram.setComputeUnitPrice({
5073
+ microLamports: computeUnitsPrice,
5074
+ })
5075
+ );
5076
+ }
5077
+
5078
+ if (Array.isArray(instructions)) {
5079
+ allIx.push(...instructions);
5080
+ } else {
5081
+ allIx.push(instructions);
5082
+ }
5083
+
5084
+ if (this.txVersion === 'legacy') {
5085
+ return new Transaction().add(...allIx);
5086
+ } else {
5087
+ const marketLookupTable = await this.fetchMarketLookupTableAccount();
5088
+ const message = new TransactionMessage({
5089
+ payerKey: this.provider.wallet.publicKey,
5090
+ recentBlockhash: (
5091
+ await this.provider.connection.getRecentBlockhash(
5092
+ this.opts.preflightCommitment
5093
+ )
5094
+ ).blockhash,
5095
+ instructions: allIx,
5096
+ }).compileToV0Message([marketLookupTable]);
5097
+
5098
+ return new VersionedTransaction(message);
5099
+ }
4965
5100
  }
4966
5101
  }