@drift-labs/sdk 2.85.0-beta.1 → 2.85.0-beta.10

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 (41) hide show
  1. package/VERSION +1 -1
  2. package/bun.lockb +0 -0
  3. package/lib/accounts/bulkAccountLoader.d.ts +3 -3
  4. package/lib/accounts/pollingDriftClientAccountSubscriber.js +10 -2
  5. package/lib/accounts/pollingUserAccountSubscriber.d.ts +4 -3
  6. package/lib/accounts/pollingUserAccountSubscriber.js +7 -6
  7. package/lib/accounts/testBulkAccountLoader.d.ts +4 -0
  8. package/lib/accounts/testBulkAccountLoader.js +45 -0
  9. package/lib/bankrun/bankrunConnection.d.ts +71 -0
  10. package/lib/bankrun/bankrunConnection.js +285 -0
  11. package/lib/blockhashSubscriber/BlockhashSubscriber.js +22 -15
  12. package/lib/constants/perpMarkets.js +10 -0
  13. package/lib/constants/spotMarkets.js +10 -0
  14. package/lib/driftClient.d.ts +1 -1
  15. package/lib/driftClient.js +8 -2
  16. package/lib/events/eventSubscriber.js +12 -4
  17. package/lib/idl/drift.json +28 -8
  18. package/lib/testClient.js +1 -2
  19. package/lib/tokenFaucet.d.ts +3 -1
  20. package/lib/tokenFaucet.js +41 -8
  21. package/lib/tx/txParamProcessor.d.ts +2 -1
  22. package/lib/tx/txParamProcessor.js +7 -3
  23. package/lib/types.d.ts +1 -0
  24. package/lib/user.js +1 -1
  25. package/package.json +3 -1
  26. package/src/accounts/bulkAccountLoader.ts +3 -2
  27. package/src/accounts/pollingDriftClientAccountSubscriber.ts +16 -3
  28. package/src/accounts/pollingUserAccountSubscriber.ts +13 -12
  29. package/src/accounts/testBulkAccountLoader.ts +53 -0
  30. package/src/bankrun/bankrunConnection.ts +466 -0
  31. package/src/blockhashSubscriber/BlockhashSubscriber.ts +24 -19
  32. package/src/constants/perpMarkets.ts +10 -0
  33. package/src/constants/spotMarkets.ts +10 -0
  34. package/src/driftClient.ts +12 -3
  35. package/src/events/eventSubscriber.ts +5 -0
  36. package/src/idl/drift.json +28 -8
  37. package/src/testClient.ts +1 -2
  38. package/src/tokenFaucet.ts +49 -12
  39. package/src/tx/txParamProcessor.ts +11 -2
  40. package/src/types.ts +1 -0
  41. package/src/user.ts +5 -2
package/src/testClient.ts CHANGED
@@ -10,8 +10,6 @@ export class TestClient extends AdminClient {
10
10
  throw new Error('Test client must be polling');
11
11
  }
12
12
  super(config);
13
- // @ts-ignore
14
- this.txHandler.blockhashCommitment = 'recent';
15
13
  }
16
14
 
17
15
  async sendTransaction(
@@ -30,6 +28,7 @@ export class TestClient extends AdminClient {
30
28
  let lastFetchedSlot = (
31
29
  this.accountSubscriber as PollingDriftClientAccountSubscriber
32
30
  ).accountLoader.mostRecentSlot;
31
+ await this.fetchAccounts();
33
32
  while (lastFetchedSlot < slot) {
34
33
  await this.fetchAccounts();
35
34
  lastFetchedSlot = (
@@ -2,10 +2,10 @@ import * as anchor from '@coral-xyz/anchor';
2
2
  import { AnchorProvider, Idl, Program } from '@coral-xyz/anchor';
3
3
  import {
4
4
  TOKEN_PROGRAM_ID,
5
- getAccount,
6
5
  Account,
7
6
  createAssociatedTokenAccountInstruction,
8
7
  getAssociatedTokenAddress,
8
+ getAccount,
9
9
  } from '@solana/spl-token';
10
10
  import {
11
11
  ConfirmOptions,
@@ -19,8 +19,10 @@ import {
19
19
  import { BN } from '.';
20
20
  import tokenFaucet from './idl/token_faucet.json';
21
21
  import { IWallet } from './types';
22
+ import { BankrunContextWrapper } from './bankrun/bankrunConnection';
22
23
 
23
24
  export class TokenFaucet {
25
+ context?: BankrunContextWrapper;
24
26
  connection: Connection;
25
27
  wallet: IWallet;
26
28
  public program: Program;
@@ -33,13 +35,20 @@ export class TokenFaucet {
33
35
  wallet: IWallet,
34
36
  programId: PublicKey,
35
37
  mint: PublicKey,
36
- opts?: ConfirmOptions
38
+ opts?: ConfirmOptions,
39
+ context?: BankrunContextWrapper
37
40
  ) {
38
41
  this.connection = connection;
42
+ this.context = context;
39
43
  this.wallet = wallet;
40
44
  this.opts = opts || AnchorProvider.defaultOptions();
41
45
  // @ts-ignore
42
- const provider = new AnchorProvider(connection, wallet, this.opts);
46
+ const provider = new AnchorProvider(
47
+ context ? context.connection.toConnection() : this.connection,
48
+ // @ts-ignore
49
+ wallet,
50
+ this.opts
51
+ );
43
52
  this.provider = provider;
44
53
  this.program = new Program(tokenFaucet as Idl, programId, provider);
45
54
  this.mint = mint;
@@ -76,7 +85,7 @@ export class TokenFaucet {
76
85
  public async initialize(): Promise<TransactionSignature> {
77
86
  const [faucetConfigPublicKey] =
78
87
  await this.getFaucetConfigPublicKeyAndNonce();
79
- return await this.program.rpc.initialize({
88
+ const ix = this.program.instruction.initialize({
80
89
  accounts: {
81
90
  faucetConfig: faucetConfigPublicKey,
82
91
  admin: this.wallet.publicKey,
@@ -86,6 +95,9 @@ export class TokenFaucet {
86
95
  tokenProgram: TOKEN_PROGRAM_ID,
87
96
  },
88
97
  });
98
+ const tx = new Transaction().add(ix);
99
+ const txSig = await this.context.sendTransaction(tx);
100
+ return txSig;
89
101
  }
90
102
 
91
103
  public async fetchState(): Promise<any> {
@@ -114,12 +126,29 @@ export class TokenFaucet {
114
126
 
115
127
  const tx = new Transaction().add(mintIx);
116
128
 
117
- const txSig = await this.program.provider.sendAndConfirm(tx, [], this.opts);
118
-
119
- return txSig;
129
+ if (this.context) {
130
+ return await this.context.sendTransaction(tx);
131
+ } else {
132
+ return await this.program.provider.sendAndConfirm(tx, [], this.opts);
133
+ }
120
134
  }
121
135
 
122
136
  public async transferMintAuthority(): Promise<TransactionSignature> {
137
+ if (this.context) {
138
+ const ix = this.program.instruction.transferMintAuthority({
139
+ accounts: {
140
+ faucetConfig: await this.getFaucetConfigPublicKey(),
141
+ mintAccount: this.mint,
142
+ mintAuthority: await this.getMintAuthority(),
143
+ tokenProgram: TOKEN_PROGRAM_ID,
144
+ admin: this.wallet.publicKey,
145
+ },
146
+ });
147
+ const tx = new Transaction().add(ix);
148
+ const txSig = await this.context.sendTransaction(tx);
149
+ return txSig;
150
+ }
151
+
123
152
  return await this.program.rpc.transferMintAuthority({
124
153
  accounts: {
125
154
  faucetConfig: await this.getFaucetConfigPublicKey(),
@@ -146,9 +175,8 @@ export class TokenFaucet {
146
175
  let associatedTokenAccountExists = false;
147
176
 
148
177
  try {
149
- const assosciatedTokenAccount = await this.connection.getAccountInfo(
150
- associatedTokenPublicKey
151
- );
178
+ const assosciatedTokenAccount =
179
+ await this.context.connection.getAccountInfo(associatedTokenPublicKey);
152
180
 
153
181
  associatedTokenAccountExists = !!assosciatedTokenAccount;
154
182
  } catch (e) {
@@ -162,7 +190,13 @@ export class TokenFaucet {
162
190
 
163
191
  tx.add(mintToTx);
164
192
 
165
- const txSig = await this.program.provider.sendAndConfirm(tx, [], this.opts);
193
+ let txSig;
194
+ if (this.context) {
195
+ txSig = await this.context.sendTransaction(tx);
196
+ } else {
197
+ txSig = await this.program.provider.sendAndConfirm(tx, [], this.opts);
198
+ }
199
+
166
200
  return [associatedTokenPublicKey, txSig];
167
201
  }
168
202
 
@@ -200,6 +234,9 @@ export class TokenFaucet {
200
234
  userPubKey: PublicKey;
201
235
  }): Promise<Account> {
202
236
  const associatedKey = await this.getAssosciatedMockUSDMintAddress(props);
237
+ if (this.context) {
238
+ return await this.context.connection.getTokenAccount(associatedKey);
239
+ }
203
240
  return await getAccount(this.connection, associatedKey);
204
241
  }
205
242
 
@@ -215,7 +252,7 @@ export class TokenFaucet {
215
252
  props.callback(await this.getTokenAccountInfo(props));
216
253
 
217
254
  // Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
218
- this.connection.onAccountChange(
255
+ this.context.connection.onAccountChange(
219
256
  tokenAccountKey,
220
257
  async (
221
258
  _accountInfo /* accountInfo is a buffer which we don't know how to deserialize */
@@ -32,7 +32,8 @@ export class TransactionParamProcessor {
32
32
  public static async getTxSimComputeUnits(
33
33
  tx: VersionedTransaction,
34
34
  connection: Connection,
35
- bufferMultiplier: number // Making this a mandatory param to force the user to remember that simulated CU's can be inaccurate and a buffer should be applied
35
+ bufferMultiplier: number, // Making this a mandatory param to force the user to remember that simulated CU's can be inaccurate and a buffer should be applied
36
+ lowerBoundCu?: number
36
37
  ): Promise<{ success: boolean; computeUnits: number }> {
37
38
  try {
38
39
  if (TEST_SIMS_ALWAYS_FAIL)
@@ -49,10 +50,18 @@ export class TransactionParamProcessor {
49
50
  const computeUnits = await this.getComputeUnitsFromSim(simTxResult);
50
51
 
51
52
  // Apply the buffer, but round down to the MAX_COMPUTE_UNITS, and round up to the nearest whole number
52
- const bufferedComputeUnits = Math.ceil(
53
+ let bufferedComputeUnits = Math.ceil(
53
54
  Math.min(computeUnits * bufferMultiplier, MAX_COMPUTE_UNITS)
54
55
  );
55
56
 
57
+ // If a lower bound CU is passed then enforce it
58
+ if (lowerBoundCu) {
59
+ bufferedComputeUnits = Math.max(
60
+ bufferedComputeUnits,
61
+ Math.min(lowerBoundCu, MAX_COMPUTE_UNITS)
62
+ );
63
+ }
64
+
56
65
  return {
57
66
  success: true,
58
67
  computeUnits: bufferedComputeUnits,
package/src/types.ts CHANGED
@@ -1042,6 +1042,7 @@ export type ProcessingTxParams = {
1042
1042
  computeUnitsBufferMultiplier?: number;
1043
1043
  useSimulatedComputeUnitsForCUPriceCalculation?: boolean;
1044
1044
  getCUPriceFromComputeUnits?: (computeUnits: number) => number;
1045
+ lowerBoundCu?: number;
1045
1046
  };
1046
1047
 
1047
1048
  export type TxParams = BaseTxParams & ProcessingTxParams;
package/src/user.ts CHANGED
@@ -108,9 +108,12 @@ export class User {
108
108
  this.userAccountPublicKey = config.userAccountPublicKey;
109
109
  if (config.accountSubscription?.type === 'polling') {
110
110
  this.accountSubscriber = new PollingUserAccountSubscriber(
111
- config.driftClient.program,
111
+ config.driftClient.connection,
112
112
  config.userAccountPublicKey,
113
- config.accountSubscription.accountLoader
113
+ config.accountSubscription.accountLoader,
114
+ this.driftClient.program.account.user.coder.accounts.decodeUnchecked.bind(
115
+ this.driftClient.program.account.user.coder.accounts
116
+ )
114
117
  );
115
118
  } else if (config.accountSubscription?.type === 'custom') {
116
119
  this.accountSubscriber = config.accountSubscription.userAccountSubscriber;