@drift-labs/sdk 2.85.0-beta.4 → 2.85.0-beta.6

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.
@@ -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 */
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;