@drift-labs/sdk 2.30.0-beta.1 → 2.30.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.
Files changed (42) hide show
  1. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
  2. package/lib/accounts/pollingTokenAccountSubscriber.js +5 -2
  3. package/lib/accounts/types.d.ts +3 -3
  4. package/lib/constants/spotMarkets.js +10 -0
  5. package/lib/dlob/DLOB.d.ts +20 -1
  6. package/lib/dlob/DLOB.js +39 -0
  7. package/lib/driftClient.d.ts +13 -8
  8. package/lib/driftClient.js +32 -27
  9. package/lib/examples/makeTradeExample.js +1 -1
  10. package/lib/idl/drift.json +23 -1
  11. package/lib/jupiter/jupiterClient.d.ts +1 -1
  12. package/lib/jupiter/jupiterClient.js +7 -2
  13. package/lib/math/spotBalance.d.ts +41 -0
  14. package/lib/math/spotBalance.js +41 -0
  15. package/lib/token/index.d.ts +3 -2
  16. package/lib/token/index.js +9 -32
  17. package/lib/tokenFaucet.d.ts +3 -3
  18. package/lib/tokenFaucet.js +4 -9
  19. package/lib/tx/retryTxSender.js +3 -0
  20. package/lib/types.d.ts +15 -2
  21. package/lib/types.js +6 -1
  22. package/lib/wallet.d.ts +5 -3
  23. package/lib/wallet.js +19 -7
  24. package/package.json +2 -2
  25. package/src/accounts/pollingTokenAccountSubscriber.ts +8 -5
  26. package/src/accounts/types.ts +3 -3
  27. package/src/constants/spotMarkets.ts +11 -0
  28. package/src/dlob/DLOB.ts +75 -0
  29. package/src/driftClient.ts +57 -53
  30. package/src/examples/makeTradeExample.ts +2 -4
  31. package/src/idl/drift.json +23 -1
  32. package/src/jupiter/jupiterClient.ts +10 -2
  33. package/src/math/spotBalance.ts +41 -0
  34. package/src/token/index.ts +12 -36
  35. package/src/tokenFaucet.ts +15 -34
  36. package/src/tx/retryTxSender.ts +4 -0
  37. package/src/types.ts +16 -2
  38. package/src/wallet.ts +34 -12
  39. package/tests/dlob/test.ts +218 -40
  40. package/lib/util/getTokenAddress.d.ts +0 -2
  41. package/lib/util/getTokenAddress.js +0 -9
  42. package/src/util/getTokenAddress.ts +0 -18
@@ -8,7 +8,10 @@ import {
8
8
  import bs58 from 'bs58';
9
9
  import {
10
10
  ASSOCIATED_TOKEN_PROGRAM_ID,
11
- Token,
11
+ createAssociatedTokenAccountInstruction,
12
+ createCloseAccountInstruction,
13
+ createInitializeAccountInstruction,
14
+ getAssociatedTokenAddress,
12
15
  TOKEN_PROGRAM_ID,
13
16
  } from '@solana/spl-token';
14
17
  import {
@@ -39,6 +42,7 @@ import {
39
42
  ModifyOrderParams,
40
43
  PhoenixV1FulfillmentConfigAccount,
41
44
  ModifyOrderPolicy,
45
+ SwapReduceOnly,
42
46
  } from './types';
43
47
  import * as anchor from '@coral-xyz/anchor';
44
48
  import driftIDL from './idl/drift.json';
@@ -113,7 +117,7 @@ import { isSpotPositionAvailable } from './math/spotPosition';
113
117
  import { calculateMarketMaxAvailableInsurance } from './math/market';
114
118
  import { fetchUserStatsAccount } from './accounts/fetch';
115
119
  import { castNumberToSpotPrecision } from './math/spotMarket';
116
- import { JupiterClient } from './jupiter/jupiterClient';
120
+ import { JupiterClient, Route, SwapMode } from './jupiter/jupiterClient';
117
121
 
118
122
  type RemainingAccountParams = {
119
123
  userAccounts: UserAccount[];
@@ -720,9 +724,7 @@ export class DriftClient {
720
724
 
721
725
  const state = this.getStateAccount();
722
726
  if (!state.whitelistMint.equals(PublicKey.default)) {
723
- const associatedTokenPublicKey = await Token.getAssociatedTokenAddress(
724
- ASSOCIATED_TOKEN_PROGRAM_ID,
725
- TOKEN_PROGRAM_ID,
727
+ const associatedTokenPublicKey = await getAssociatedTokenAddress(
726
728
  state.whitelistMint,
727
729
  this.wallet.publicKey
728
730
  );
@@ -1489,20 +1491,15 @@ export class DriftClient {
1489
1491
  return this.wallet.publicKey;
1490
1492
  }
1491
1493
  const mint = spotMarket.mint;
1492
- return await Token.getAssociatedTokenAddress(
1493
- ASSOCIATED_TOKEN_PROGRAM_ID,
1494
- TOKEN_PROGRAM_ID,
1495
- mint,
1496
- this.wallet.publicKey
1497
- );
1494
+ return await getAssociatedTokenAddress(mint, this.wallet.publicKey);
1498
1495
  }
1499
1496
 
1500
- public async createAssociatedTokenAccountIdempotentInstruction(
1497
+ public createAssociatedTokenAccountIdempotentInstruction(
1501
1498
  account: PublicKey,
1502
1499
  payer: PublicKey,
1503
1500
  owner: PublicKey,
1504
1501
  mint: PublicKey
1505
- ): Promise<TransactionInstruction> {
1502
+ ): TransactionInstruction {
1506
1503
  return new TransactionInstruction({
1507
1504
  keys: [
1508
1505
  { pubkey: payer, isSigner: true, isWritable: true },
@@ -1510,7 +1507,7 @@ export class DriftClient {
1510
1507
  { pubkey: owner, isSigner: false, isWritable: false },
1511
1508
  { pubkey: mint, isSigner: false, isWritable: false },
1512
1509
  {
1513
- pubkey: SystemProgram.programId,
1510
+ pubkey: anchor.web3.SystemProgram.programId,
1514
1511
  isSigner: false,
1515
1512
  isWritable: false,
1516
1513
  },
@@ -1582,8 +1579,7 @@ export class DriftClient {
1582
1579
  // Close the wrapped sol account at the end of the transaction
1583
1580
  if (createWSOLTokenAccount) {
1584
1581
  tx.add(
1585
- Token.createCloseAccountInstruction(
1586
- TOKEN_PROGRAM_ID,
1582
+ createCloseAccountInstruction(
1587
1583
  associatedTokenAccount,
1588
1584
  signerAuthority,
1589
1585
  signerAuthority,
@@ -1696,10 +1692,9 @@ export class DriftClient {
1696
1692
  );
1697
1693
 
1698
1694
  result.ixs.push(
1699
- Token.createInitAccountInstruction(
1700
- TOKEN_PROGRAM_ID,
1701
- WRAPPED_SOL_MINT,
1695
+ createInitializeAccountInstruction(
1702
1696
  wrappedSolAccount.publicKey,
1697
+ WRAPPED_SOL_MINT,
1703
1698
  authority
1704
1699
  )
1705
1700
  );
@@ -1713,17 +1708,12 @@ export class DriftClient {
1713
1708
  tokenMintAddress: PublicKey,
1714
1709
  associatedTokenAddress: PublicKey
1715
1710
  ): anchor.web3.TransactionInstruction {
1716
- const createAssociatedAccountIx =
1717
- Token.createAssociatedTokenAccountInstruction(
1718
- ASSOCIATED_TOKEN_PROGRAM_ID,
1719
- TOKEN_PROGRAM_ID,
1720
- tokenMintAddress,
1721
- associatedTokenAddress,
1722
- this.wallet.publicKey,
1723
- this.wallet.publicKey
1724
- );
1725
-
1726
- return createAssociatedAccountIx;
1711
+ return createAssociatedTokenAccountInstruction(
1712
+ this.wallet.publicKey,
1713
+ associatedTokenAddress,
1714
+ this.wallet.publicKey,
1715
+ tokenMintAddress
1716
+ );
1727
1717
  }
1728
1718
 
1729
1719
  /**
@@ -1825,8 +1815,7 @@ export class DriftClient {
1825
1815
  // Close the wrapped sol account at the end of the transaction
1826
1816
  if (createWSOLTokenAccount) {
1827
1817
  tx.add(
1828
- Token.createCloseAccountInstruction(
1829
- TOKEN_PROGRAM_ID,
1818
+ createCloseAccountInstruction(
1830
1819
  userTokenAccount,
1831
1820
  authority,
1832
1821
  authority,
@@ -1965,8 +1954,7 @@ export class DriftClient {
1965
1954
  // Close the wrapped sol account at the end of the transaction
1966
1955
  if (createWSOLTokenAccount) {
1967
1956
  tx.add(
1968
- Token.createCloseAccountInstruction(
1969
- TOKEN_PROGRAM_ID,
1957
+ createCloseAccountInstruction(
1970
1958
  associatedTokenAddress,
1971
1959
  authority,
1972
1960
  authority,
@@ -3300,6 +3288,7 @@ export class DriftClient {
3300
3288
  * @param inAssociatedTokenAccount the token account to
3301
3289
  * @param amount the amount of the token to sell
3302
3290
  * @param slippageBps the max slippage passed to jupiter api
3291
+ * @param route the jupiter route to use for the swap
3303
3292
  * @param txParams
3304
3293
  */
3305
3294
  public async swap({
@@ -3310,32 +3299,42 @@ export class DriftClient {
3310
3299
  inAssociatedTokenAccount,
3311
3300
  amount,
3312
3301
  slippageBps,
3302
+ swapMode,
3303
+ route,
3304
+ reduceOnly,
3313
3305
  txParams,
3314
3306
  }: {
3315
3307
  jupiterClient: JupiterClient;
3316
3308
  outMarketIndex: number;
3317
3309
  inMarketIndex: number;
3318
- outAssociatedTokenAccount: PublicKey;
3319
- inAssociatedTokenAccount: PublicKey;
3310
+ outAssociatedTokenAccount?: PublicKey;
3311
+ inAssociatedTokenAccount?: PublicKey;
3320
3312
  amount: BN;
3321
- slippageBps: number;
3313
+ slippageBps?: number;
3314
+ swapMode?: SwapMode;
3315
+ route?: Route;
3316
+ reduceOnly?: SwapReduceOnly;
3322
3317
  txParams?: TxParams;
3323
3318
  }): Promise<TransactionSignature> {
3324
3319
  const outMarket = this.getSpotMarketAccount(outMarketIndex);
3325
3320
  const inMarket = this.getSpotMarketAccount(inMarketIndex);
3326
3321
 
3327
- const routes = await jupiterClient.getRoutes({
3328
- inputMint: inMarket.mint,
3329
- outputMint: outMarket.mint,
3330
- amount,
3331
- slippageBps,
3332
- });
3322
+ if (!route) {
3323
+ const routes = await jupiterClient.getRoutes({
3324
+ inputMint: inMarket.mint,
3325
+ outputMint: outMarket.mint,
3326
+ amount,
3327
+ slippageBps,
3328
+ swapMode,
3329
+ });
3330
+
3331
+ if (!routes || routes.length === 0) {
3332
+ throw new Error('No jupiter routes found');
3333
+ }
3333
3334
 
3334
- if (!routes || routes.length === 0) {
3335
- throw new Error('No jupiter routes found');
3335
+ route = routes[0];
3336
3336
  }
3337
3337
 
3338
- const route = routes[0];
3339
3338
  const transaction = await jupiterClient.getSwapTransaction({
3340
3339
  route,
3341
3340
  userPublicKey: this.provider.wallet.publicKey,
@@ -3356,7 +3355,8 @@ export class DriftClient {
3356
3355
  const preInstructions = [];
3357
3356
  if (!outAssociatedTokenAccount) {
3358
3357
  outAssociatedTokenAccount = await this.getAssociatedTokenAccount(
3359
- outMarket.marketIndex
3358
+ outMarket.marketIndex,
3359
+ false
3360
3360
  );
3361
3361
 
3362
3362
  const accountInfo = await this.connection.getAccountInfo(
@@ -3376,7 +3376,8 @@ export class DriftClient {
3376
3376
 
3377
3377
  if (!inAssociatedTokenAccount) {
3378
3378
  inAssociatedTokenAccount = await this.getAssociatedTokenAccount(
3379
- inMarket.marketIndex
3379
+ inMarket.marketIndex,
3380
+ false
3380
3381
  );
3381
3382
 
3382
3383
  const accountInfo = await this.connection.getAccountInfo(
@@ -3400,6 +3401,7 @@ export class DriftClient {
3400
3401
  amountIn: amount,
3401
3402
  inTokenAccount: inAssociatedTokenAccount,
3402
3403
  outTokenAccount: outAssociatedTokenAccount,
3404
+ reduceOnly,
3403
3405
  });
3404
3406
 
3405
3407
  const instructions = [
@@ -3440,6 +3442,7 @@ export class DriftClient {
3440
3442
  inTokenAccount,
3441
3443
  outTokenAccount,
3442
3444
  limitPrice,
3445
+ reduceOnly,
3443
3446
  }: {
3444
3447
  outMarketIndex: number;
3445
3448
  inMarketIndex: number;
@@ -3447,6 +3450,7 @@ export class DriftClient {
3447
3450
  inTokenAccount: PublicKey;
3448
3451
  outTokenAccount: PublicKey;
3449
3452
  limitPrice?: BN;
3453
+ reduceOnly?: SwapReduceOnly;
3450
3454
  }): Promise<{
3451
3455
  beginSwapIx: TransactionInstruction;
3452
3456
  endSwapIx: TransactionInstruction;
@@ -3487,6 +3491,7 @@ export class DriftClient {
3487
3491
  inMarketIndex,
3488
3492
  outMarketIndex,
3489
3493
  limitPrice ?? null,
3494
+ reduceOnly ?? null,
3490
3495
  {
3491
3496
  accounts: {
3492
3497
  state: await this.getStatePublicKey(),
@@ -5011,8 +5016,7 @@ export class DriftClient {
5011
5016
 
5012
5017
  if (createWSOLTokenAccount) {
5013
5018
  tx.add(
5014
- Token.createCloseAccountInstruction(
5015
- TOKEN_PROGRAM_ID,
5019
+ createCloseAccountInstruction(
5016
5020
  tokenAccount,
5017
5021
  this.wallet.publicKey,
5018
5022
  this.wallet.publicKey,
@@ -5162,8 +5166,7 @@ export class DriftClient {
5162
5166
  // Close the wrapped sol account at the end of the transaction
5163
5167
  if (createWSOLTokenAccount) {
5164
5168
  tx.add(
5165
- Token.createCloseAccountInstruction(
5166
- TOKEN_PROGRAM_ID,
5169
+ createCloseAccountInstruction(
5167
5170
  tokenAccount,
5168
5171
  this.wallet.publicKey,
5169
5172
  this.wallet.publicKey,
@@ -5364,7 +5367,8 @@ export class DriftClient {
5364
5367
  allIx.push(instructions);
5365
5368
  }
5366
5369
 
5367
- if (this.txVersion === 'legacy') {
5370
+ txVersion = txVersion ?? this.txVersion;
5371
+ if (txVersion === 'legacy') {
5368
5372
  return new Transaction().add(...allIx);
5369
5373
  } else {
5370
5374
  const marketLookupTable = await this.fetchMarketLookupTableAccount();
@@ -5,7 +5,7 @@ import {
5
5
  getMarketOrderParams,
6
6
  Wallet,
7
7
  } from '..';
8
- import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';
8
+ import { getAssociatedTokenAddress } from '@solana/spl-token';
9
9
  import { Connection, Keypair, PublicKey } from '@solana/web3.js';
10
10
  import {
11
11
  DriftClient,
@@ -25,9 +25,7 @@ export const getTokenAddress = (
25
25
  mintAddress: string,
26
26
  userPubKey: string
27
27
  ): Promise<PublicKey> => {
28
- return Token.getAssociatedTokenAddress(
29
- new PublicKey(`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`),
30
- TOKEN_PROGRAM_ID,
28
+ return getAssociatedTokenAddress(
31
29
  new PublicKey(mintAddress),
32
30
  new PublicKey(userPubKey)
33
31
  );
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.30.0-beta.1",
2
+ "version": "2.30.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -863,6 +863,14 @@
863
863
  "type": {
864
864
  "option": "u64"
865
865
  }
866
+ },
867
+ {
868
+ "name": "reduceOnly",
869
+ "type": {
870
+ "option": {
871
+ "defined": "SwapReduceOnly"
872
+ }
873
+ }
866
874
  }
867
875
  ]
868
876
  },
@@ -7449,6 +7457,20 @@
7449
7457
  ]
7450
7458
  }
7451
7459
  },
7460
+ {
7461
+ "name": "SwapReduceOnly",
7462
+ "type": {
7463
+ "kind": "enum",
7464
+ "variants": [
7465
+ {
7466
+ "name": "In"
7467
+ },
7468
+ {
7469
+ "name": "Out"
7470
+ }
7471
+ ]
7472
+ }
7473
+ },
7452
7474
  {
7453
7475
  "name": "TwapPeriod",
7454
7476
  "type": {
@@ -89,7 +89,7 @@ export class JupiterClient {
89
89
  /**
90
90
  * Get a swap transaction for a route
91
91
  * @param route the route to perform swap
92
- * @param userPublicKey the user's wallet public key
92
+ * @param userPublicKey the signer's wallet public key
93
93
  * @param slippageBps the slippage tolerance in basis points
94
94
  */
95
95
  public async getSwapTransaction({
@@ -143,7 +143,9 @@ export class JupiterClient {
143
143
  )
144
144
  ).filter((lookup) => lookup);
145
145
 
146
- const transactionMessage = TransactionMessage.decompile(message);
146
+ const transactionMessage = TransactionMessage.decompile(message, {
147
+ addressLookupTableAccounts: lookupTables,
148
+ });
147
149
  return {
148
150
  transactionMessage,
149
151
  lookupTables,
@@ -190,6 +192,12 @@ export class JupiterClient {
190
192
  return false;
191
193
  }
192
194
 
195
+ if (
196
+ instruction.programId.toString() === '11111111111111111111111111111111'
197
+ ) {
198
+ return false;
199
+ }
200
+
193
201
  if (
194
202
  instruction.programId.toString() ===
195
203
  'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'
@@ -23,6 +23,15 @@ import { OraclePriceData } from '../oracles/types';
23
23
  import { PERCENTAGE_PRECISION } from '../constants/numericConstants';
24
24
  import { divCeil } from './utils';
25
25
 
26
+ /**
27
+ * Calculates the balance of a given token amount including any accumulated interest. This
28
+ * is the same as `SpotPosition.scaledBalance`.
29
+ *
30
+ * @param {BN} tokenAmount - the amount of tokens
31
+ * @param {SpotMarketAccount} spotMarket - the spot market account
32
+ * @param {SpotBalanceType} balanceType - the balance type ('deposit' or 'borrow')
33
+ * @return {BN} the calculated balance, scaled by `SPOT_MARKET_BALANCE_PRECISION`
34
+ */
26
35
  export function getBalance(
27
36
  tokenAmount: BN,
28
37
  spotMarket: SpotMarketAccount,
@@ -43,6 +52,14 @@ export function getBalance(
43
52
  return balance;
44
53
  }
45
54
 
55
+ /**
56
+ * Calculates the spot token amount including any accumulated interest.
57
+ *
58
+ * @param {BN} balanceAmount - The balance amount, typically from `SpotPosition.scaledBalance`
59
+ * @param {SpotMarketAccount} spotMarket - The spot market account details
60
+ * @param {SpotBalanceType} balanceType - The balance type to be used for calculation
61
+ * @returns {BN} The calculated token amount, scaled by `SpotMarketConfig.precision`
62
+ */
46
63
  export function getTokenAmount(
47
64
  balanceAmount: BN,
48
65
  spotMarket: SpotMarketAccount,
@@ -62,6 +79,13 @@ export function getTokenAmount(
62
79
  }
63
80
  }
64
81
 
82
+ /**
83
+ * Returns the signed (positive for deposit,negative for borrow) token amount based on the balance type.
84
+ *
85
+ * @param {BN} tokenAmount - The token amount to convert (from `getTokenAmount`)
86
+ * @param {SpotBalanceType} balanceType - The balance type to determine the sign of the token amount.
87
+ * @returns {BN} - The signed token amount, scaled by `SpotMarketConfig.precision`
88
+ */
65
89
  export function getSignedTokenAmount(
66
90
  tokenAmount: BN,
67
91
  balanceType: SpotBalanceType
@@ -73,6 +97,15 @@ export function getSignedTokenAmount(
73
97
  }
74
98
  }
75
99
 
100
+ /**
101
+ * Calculates the value of a given token amount using the worst of the provided oracle price and its TWAP.
102
+ *
103
+ * @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
104
+ * @param {number} spotDecimals - The number of decimals in the token.
105
+ * @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
106
+ * @param {BN} oraclePriceTwap - The Time-Weighted Average Price of the oracle.
107
+ * @return {BN} The calculated value of the given token amount, scaled by `PRICE_PRECISION`
108
+ */
76
109
  export function getStrictTokenValue(
77
110
  tokenAmount: BN,
78
111
  spotDecimals: number,
@@ -95,6 +128,14 @@ export function getStrictTokenValue(
95
128
  return tokenAmount.mul(price).div(precisionDecrease);
96
129
  }
97
130
 
131
+ /**
132
+ * Calculates the value of a given token amount in relation to an oracle price data
133
+ *
134
+ * @param {BN} tokenAmount - The amount of tokens to calculate the value for (from `getTokenAmount`)
135
+ * @param {number} spotDecimals - The number of decimal places of the token.
136
+ * @param {OraclePriceData} oraclePriceData - The oracle price data (typically a token/USD oracle).
137
+ * @return {BN} The value of the token based on the oracle, scaled by `PRICE_PRECISION`
138
+ */
98
139
  export function getTokenValue(
99
140
  tokenAmount: BN,
100
141
  spotDecimals: number,
@@ -1,37 +1,13 @@
1
- import { AccountInfo, AccountLayout, u64 } from '@solana/spl-token';
2
- import { PublicKey } from '@solana/web3.js';
3
-
4
- export function parseTokenAccount(data: Buffer): AccountInfo {
5
- const accountInfo = AccountLayout.decode(data);
6
- accountInfo.mint = new PublicKey(accountInfo.mint);
7
- accountInfo.owner = new PublicKey(accountInfo.owner);
8
- accountInfo.amount = u64.fromBuffer(accountInfo.amount);
9
-
10
- if (accountInfo.delegateOption === 0) {
11
- accountInfo.delegate = null;
12
- // eslint-disable-next-line new-cap
13
- accountInfo.delegatedAmount = u64.fromBuffer(Buffer.from('0'));
14
- } else {
15
- accountInfo.delegate = new PublicKey(accountInfo.delegate);
16
- accountInfo.delegatedAmount = u64.fromBuffer(accountInfo.delegatedAmount);
17
- }
18
-
19
- accountInfo.isInitialized = accountInfo.state !== 0;
20
- accountInfo.isFrozen = accountInfo.state === 2;
21
-
22
- if (accountInfo.isNativeOption === 1) {
23
- accountInfo.rentExemptReserve = u64.fromBuffer(accountInfo.isNative);
24
- accountInfo.isNative = true;
25
- } else {
26
- accountInfo.rentExemptReserve = null;
27
- accountInfo.isNative = false;
28
- }
29
-
30
- if (accountInfo.closeAuthorityOption === 0) {
31
- accountInfo.closeAuthority = null;
32
- } else {
33
- accountInfo.closeAuthority = new PublicKey(accountInfo.closeAuthority);
34
- }
35
-
36
- return accountInfo;
1
+ import { Account, TOKEN_PROGRAM_ID, unpackAccount } from '@solana/spl-token';
2
+ import { PublicKey, AccountInfo } from '@solana/web3.js';
3
+
4
+ export function parseTokenAccount(data: Buffer, pubkey: PublicKey): Account {
5
+ // mock AccountInfo so unpackAccount can be used
6
+ const accountInfo: AccountInfo<Buffer> = {
7
+ data,
8
+ owner: TOKEN_PROGRAM_ID,
9
+ executable: false,
10
+ lamports: 0,
11
+ };
12
+ return unpackAccount(pubkey, accountInfo, TOKEN_PROGRAM_ID);
37
13
  }
@@ -1,10 +1,11 @@
1
1
  import * as anchor from '@coral-xyz/anchor';
2
2
  import { AnchorProvider, Idl, Program } from '@coral-xyz/anchor';
3
3
  import {
4
- AccountInfo,
5
- ASSOCIATED_TOKEN_PROGRAM_ID,
6
- Token,
7
4
  TOKEN_PROGRAM_ID,
5
+ getAccount,
6
+ Account,
7
+ createAssociatedTokenAccountInstruction,
8
+ getAssociatedTokenAddress,
8
9
  } from '@solana/spl-token';
9
10
  import {
10
11
  ConfirmOptions,
@@ -174,15 +175,12 @@ export class TokenFaucet {
174
175
  { userPubKey: userPublicKey }
175
176
  );
176
177
 
177
- const createAssociatedAccountIx =
178
- Token.createAssociatedTokenAccountInstruction(
179
- ASSOCIATED_TOKEN_PROGRAM_ID,
180
- TOKEN_PROGRAM_ID,
181
- state.mint,
182
- associateTokenPublicKey,
183
- userPublicKey,
184
- this.wallet.publicKey
185
- );
178
+ const createAssociatedAccountIx = createAssociatedTokenAccountInstruction(
179
+ this.wallet.publicKey,
180
+ associateTokenPublicKey,
181
+ userPublicKey,
182
+ state.mint
183
+ );
186
184
 
187
185
  const mintToIx = await this.mintToUserIx(associateTokenPublicKey, amount);
188
186
 
@@ -194,36 +192,19 @@ export class TokenFaucet {
194
192
  }): Promise<anchor.web3.PublicKey> {
195
193
  const state: any = await this.fetchState();
196
194
 
197
- return Token.getAssociatedTokenAddress(
198
- ASSOCIATED_TOKEN_PROGRAM_ID,
199
- TOKEN_PROGRAM_ID,
200
- state.mint,
201
- props.userPubKey
202
- );
195
+ return getAssociatedTokenAddress(state.mint, props.userPubKey);
203
196
  }
204
197
 
205
198
  public async getTokenAccountInfo(props: {
206
199
  userPubKey: PublicKey;
207
- }): Promise<AccountInfo> {
208
- const assosciatedKey = await this.getAssosciatedMockUSDMintAddress(props);
209
-
210
- const state: any = await this.fetchState();
211
-
212
- const token = new Token(
213
- this.connection,
214
- state.mint,
215
- TOKEN_PROGRAM_ID,
216
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
217
- // @ts-ignore
218
- this.provider.payer
219
- );
220
-
221
- return await token.getAccountInfo(assosciatedKey);
200
+ }): Promise<Account> {
201
+ const associatedKey = await this.getAssosciatedMockUSDMintAddress(props);
202
+ return await getAccount(this.connection, associatedKey);
222
203
  }
223
204
 
224
205
  public async subscribeToTokenAccount(props: {
225
206
  userPubKey: PublicKey;
226
- callback: (accountInfo: AccountInfo) => void;
207
+ callback: (accountInfo: Account) => void;
227
208
  }): Promise<boolean> {
228
209
  try {
229
210
  const tokenAccountKey = await this.getAssosciatedMockUSDMintAddress(
@@ -122,6 +122,10 @@ export class RetryTxSender implements TxSender {
122
122
  // @ts-ignore
123
123
  tx.sign((additionalSigners ?? []).concat(this.provider.wallet.payer));
124
124
 
125
+ if (opts === undefined) {
126
+ opts = this.provider.opts;
127
+ }
128
+
125
129
  return this.sendRawTransaction(tx.serialize(), opts);
126
130
  }
127
131
 
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PublicKey, Transaction } from '@solana/web3.js';
1
+ import { PublicKey, Transaction, VersionedTransaction } from '@solana/web3.js';
2
2
  import { BN, ZERO } from '.';
3
3
 
4
4
  // # Utility Types / Enums / Constants
@@ -506,7 +506,7 @@ export type OrderActionRecord = {
506
506
  };
507
507
 
508
508
  export type SwapRecord = {
509
- ts: number;
509
+ ts: BN;
510
510
  user: PublicKey;
511
511
  amountOut: BN;
512
512
  amountIn: BN;
@@ -951,12 +951,26 @@ export type TxParams = {
951
951
  computeUnitsPrice?: number;
952
952
  };
953
953
 
954
+ export class SwapReduceOnly {
955
+ static readonly In = { in: {} };
956
+ static readonly Out = { out: {} };
957
+ }
958
+
954
959
  // # Misc Types
955
960
  export interface IWallet {
956
961
  signTransaction(tx: Transaction): Promise<Transaction>;
957
962
  signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
958
963
  publicKey: PublicKey;
959
964
  }
965
+ export interface IVersionedWallet {
966
+ signVersionedTransaction(
967
+ tx: VersionedTransaction
968
+ ): Promise<VersionedTransaction>;
969
+ signAllVersionedTransactions(
970
+ txs: VersionedTransaction[]
971
+ ): Promise<VersionedTransaction[]>;
972
+ publicKey: PublicKey;
973
+ }
960
974
 
961
975
  export type FeeStructure = {
962
976
  feeTiers: FeeTier[];