@drift-labs/sdk 2.94.0-beta.0 → 2.94.0-beta.2

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.94.0-beta.0
1
+ 2.94.0-beta.2
@@ -19,7 +19,7 @@ export declare class BankrunContextWrapper {
19
19
  readonly context: ProgramTestContext;
20
20
  readonly provider: BankrunProvider;
21
21
  readonly commitment: Commitment;
22
- constructor(context: ProgramTestContext);
22
+ constructor(context: ProgramTestContext, verifySignatures?: boolean);
23
23
  sendTransaction(tx: Transaction, additionalSigners?: Keypair[]): Promise<TransactionSignature>;
24
24
  getMinimumBalanceForRentExemption(_: number): Promise<number>;
25
25
  fundKeypair(keypair: Keypair | Wallet, lamports: number | bigint): Promise<TransactionSignature>;
@@ -36,7 +36,8 @@ export declare class BankrunConnection {
36
36
  private nextClientSubscriptionId;
37
37
  private onLogCallbacks;
38
38
  private onAccountChangeCallbacks;
39
- constructor(banksClient: BanksClient, context: ProgramTestContext);
39
+ private verifySignatures;
40
+ constructor(banksClient: BanksClient, context: ProgramTestContext, verifySignatures?: boolean);
40
41
  getSlot(): Promise<bigint>;
41
42
  toConnection(): SolanaConnection;
42
43
  getTokenAccount(publicKey: PublicKey): Promise<Account>;
@@ -11,11 +11,11 @@ const bs58_1 = __importDefault(require("bs58"));
11
11
  const anchor_1 = require("@coral-xyz/anchor");
12
12
  const spl_token_1 = require("@solana/spl-token");
13
13
  class BankrunContextWrapper {
14
- constructor(context) {
14
+ constructor(context, verifySignatures = true) {
15
15
  this.commitment = 'confirmed';
16
16
  this.context = context;
17
17
  this.provider = new anchor_bankrun_1.BankrunProvider(context);
18
- this.connection = new BankrunConnection(this.context.banksClient, this.context);
18
+ this.connection = new BankrunConnection(this.context.banksClient, this.context, verifySignatures);
19
19
  }
20
20
  async sendTransaction(tx, additionalSigners) {
21
21
  tx.recentBlockhash = (await this.getLatestBlockhash()).toString();
@@ -62,13 +62,14 @@ class BankrunContextWrapper {
62
62
  }
63
63
  exports.BankrunContextWrapper = BankrunContextWrapper;
64
64
  class BankrunConnection {
65
- constructor(banksClient, context) {
65
+ constructor(banksClient, context, verifySignatures = true) {
66
66
  this.transactionToMeta = new Map();
67
67
  this.nextClientSubscriptionId = 0;
68
68
  this.onLogCallbacks = new Map();
69
69
  this.onAccountChangeCallbacks = new Map();
70
70
  this._banksClient = banksClient;
71
71
  this.context = context;
72
+ this.verifySignatures = verifySignatures;
72
73
  }
73
74
  getSlot() {
74
75
  return this._banksClient.getSlot();
@@ -103,7 +104,15 @@ class BankrunConnection {
103
104
  return signature;
104
105
  }
105
106
  async sendTransaction(tx) {
106
- const banksTransactionMeta = await this._banksClient.tryProcessTransaction(tx);
107
+ const serialized = tx.serialize({
108
+ verifySignatures: this.verifySignatures,
109
+ });
110
+ // @ts-ignore
111
+ const internal = this._banksClient.inner;
112
+ const inner = tx instanceof web3_js_1.Transaction
113
+ ? await internal.tryProcessLegacyTransaction(serialized)
114
+ : await internal.tryProcessVersionedTransaction(serialized);
115
+ const banksTransactionMeta = new solana_bankrun_1.BanksTransactionResultWithMeta(inner);
107
116
  if (banksTransactionMeta.result) {
108
117
  throw new Error(banksTransactionMeta.result);
109
118
  }
@@ -121,12 +121,19 @@ export declare class DriftClient {
121
121
  * @param includeDelegates
122
122
  */
123
123
  updateWallet(newWallet: IWallet, subAccountIds?: number[], activeSubAccountId?: number, includeDelegates?: boolean, authoritySubaccountMap?: Map<string, number[]>): Promise<boolean>;
124
+ /**
125
+ * Update the subscribed accounts to a given authority, while leaving the
126
+ * connected wallet intact. This allows a user to emulate another user's
127
+ * account on the UI and sign permissionless transactions with their own wallet.
128
+ * @param emulateAuthority
129
+ */
130
+ emulateAccount(emulateAuthority: PublicKey): Promise<boolean>;
124
131
  switchActiveUser(subAccountId: number, authority?: PublicKey): Promise<void>;
125
132
  addUser(subAccountId: number, authority?: PublicKey, userAccount?: UserAccount): Promise<boolean>;
126
133
  /**
127
134
  * Adds and subscribes to users based on params set by the constructor or by updateWallet.
128
135
  */
129
- addAndSubscribeToUsers(): Promise<boolean>;
136
+ addAndSubscribeToUsers(authority?: PublicKey): Promise<boolean>;
130
137
  initializeUserAccount(subAccountId?: number, name?: string, referrerInfo?: ReferrerInfo, txParams?: TxParams): Promise<[TransactionSignature, PublicKey]>;
131
138
  getInitializeUserInstructions(subAccountId?: number, name?: string, referrerInfo?: ReferrerInfo): Promise<[PublicKey, TransactionInstruction]>;
132
139
  getInitializeUserStatsIx(): Promise<TransactionInstruction>;
@@ -407,6 +407,43 @@ class DriftClient {
407
407
  }
408
408
  return success;
409
409
  }
410
+ /**
411
+ * Update the subscribed accounts to a given authority, while leaving the
412
+ * connected wallet intact. This allows a user to emulate another user's
413
+ * account on the UI and sign permissionless transactions with their own wallet.
414
+ * @param emulateAuthority
415
+ */
416
+ async emulateAccount(emulateAuthority) {
417
+ var _a, _b, _c;
418
+ this.skipLoadUsers = false;
419
+ // Update provider for txSender with new wallet details
420
+ this.authority = emulateAuthority;
421
+ this.userStatsAccountPublicKey = undefined;
422
+ this.includeDelegates = true;
423
+ const walletSupportsVersionedTxns =
424
+ //@ts-ignore
425
+ (_b = (_a = this.wallet.supportedTransactionVersions) === null || _a === void 0 ? void 0 : _a.size) !== null && _b !== void 0 ? _b : 0 > 1;
426
+ this.txVersion = walletSupportsVersionedTxns ? 0 : 'legacy';
427
+ this.authoritySubAccountMap = new Map();
428
+ /* Reset user stats account */
429
+ if ((_c = this.userStats) === null || _c === void 0 ? void 0 : _c.isSubscribed) {
430
+ await this.userStats.unsubscribe();
431
+ }
432
+ this.userStats = undefined;
433
+ this.userStats = new userStats_1.UserStats({
434
+ driftClient: this,
435
+ userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
436
+ accountSubscription: this.userStatsAccountSubscriptionConfig,
437
+ });
438
+ await this.userStats.subscribe();
439
+ let success = true;
440
+ if (this.isSubscribed) {
441
+ await Promise.all(this.unsubscribeUsers());
442
+ this.users.clear();
443
+ success = await this.addAndSubscribeToUsers(emulateAuthority);
444
+ }
445
+ return success;
446
+ }
410
447
  async switchActiveUser(subAccountId, authority) {
411
448
  var _a;
412
449
  const authorityChanged = authority && !((_a = this.authority) === null || _a === void 0 ? void 0 : _a.equals(authority));
@@ -445,7 +482,7 @@ class DriftClient {
445
482
  /**
446
483
  * Adds and subscribes to users based on params set by the constructor or by updateWallet.
447
484
  */
448
- async addAndSubscribeToUsers() {
485
+ async addAndSubscribeToUsers(authority) {
449
486
  var _a, _b, _c, _d, _e, _f, _g;
450
487
  // save the rpc calls if driftclient is initialized without a real wallet
451
488
  if (this.skipLoadUsers)
@@ -465,9 +502,9 @@ class DriftClient {
465
502
  else {
466
503
  let userAccounts = [];
467
504
  let delegatedAccounts = [];
468
- const userAccountsPromise = this.getUserAccountsForAuthority(this.wallet.publicKey);
505
+ const userAccountsPromise = this.getUserAccountsForAuthority(authority !== null && authority !== void 0 ? authority : this.wallet.publicKey);
469
506
  if (this.includeDelegates) {
470
- const delegatedAccountsPromise = this.getUserAccountsForDelegate(this.wallet.publicKey);
507
+ const delegatedAccountsPromise = this.getUserAccountsForDelegate(authority !== null && authority !== void 0 ? authority : this.wallet.publicKey);
471
508
  [userAccounts, delegatedAccounts] = await Promise.all([
472
509
  userAccountsPromise,
473
510
  delegatedAccountsPromise,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.94.0-beta.0",
3
+ "version": "2.94.0-beta.2",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -55,12 +55,13 @@ export class BankrunContextWrapper {
55
55
  public readonly provider: BankrunProvider;
56
56
  public readonly commitment: Commitment = 'confirmed';
57
57
 
58
- constructor(context: ProgramTestContext) {
58
+ constructor(context: ProgramTestContext, verifySignatures = true) {
59
59
  this.context = context;
60
60
  this.provider = new BankrunProvider(context);
61
61
  this.connection = new BankrunConnection(
62
62
  this.context.banksClient,
63
- this.context
63
+ this.context,
64
+ verifySignatures
64
65
  );
65
66
  }
66
67
 
@@ -149,9 +150,16 @@ export class BankrunConnection {
149
150
  [PublicKey, AccountChangeCallback]
150
151
  >();
151
152
 
152
- constructor(banksClient: BanksClient, context: ProgramTestContext) {
153
+ private verifySignatures: boolean;
154
+
155
+ constructor(
156
+ banksClient: BanksClient,
157
+ context: ProgramTestContext,
158
+ verifySignatures = true
159
+ ) {
153
160
  this._banksClient = banksClient;
154
161
  this.context = context;
162
+ this.verifySignatures = verifySignatures;
155
163
  }
156
164
 
157
165
  getSlot(): Promise<bigint> {
@@ -205,9 +213,17 @@ export class BankrunConnection {
205
213
  }
206
214
 
207
215
  async sendTransaction(tx: Transaction): Promise<TransactionSignature> {
208
- const banksTransactionMeta = await this._banksClient.tryProcessTransaction(
209
- tx
210
- );
216
+ const serialized = tx.serialize({
217
+ verifySignatures: this.verifySignatures,
218
+ });
219
+ // @ts-ignore
220
+ const internal = this._banksClient.inner;
221
+ const inner =
222
+ tx instanceof Transaction
223
+ ? await internal.tryProcessLegacyTransaction(serialized)
224
+ : await internal.tryProcessVersionedTransaction(serialized);
225
+ const banksTransactionMeta = new BanksTransactionResultWithMeta(inner);
226
+
211
227
  if (banksTransactionMeta.result) {
212
228
  throw new Error(banksTransactionMeta.result);
213
229
  }
@@ -714,6 +714,51 @@ export class DriftClient {
714
714
  return success;
715
715
  }
716
716
 
717
+ /**
718
+ * Update the subscribed accounts to a given authority, while leaving the
719
+ * connected wallet intact. This allows a user to emulate another user's
720
+ * account on the UI and sign permissionless transactions with their own wallet.
721
+ * @param emulateAuthority
722
+ */
723
+ public async emulateAccount(emulateAuthority: PublicKey): Promise<boolean> {
724
+ this.skipLoadUsers = false;
725
+ // Update provider for txSender with new wallet details
726
+ this.authority = emulateAuthority;
727
+ this.userStatsAccountPublicKey = undefined;
728
+ this.includeDelegates = true;
729
+ const walletSupportsVersionedTxns =
730
+ //@ts-ignore
731
+ this.wallet.supportedTransactionVersions?.size ?? 0 > 1;
732
+ this.txVersion = walletSupportsVersionedTxns ? 0 : 'legacy';
733
+
734
+ this.authoritySubAccountMap = new Map<string, number[]>();
735
+
736
+ /* Reset user stats account */
737
+ if (this.userStats?.isSubscribed) {
738
+ await this.userStats.unsubscribe();
739
+ }
740
+
741
+ this.userStats = undefined;
742
+
743
+ this.userStats = new UserStats({
744
+ driftClient: this,
745
+ userStatsAccountPublicKey: this.getUserStatsAccountPublicKey(),
746
+ accountSubscription: this.userStatsAccountSubscriptionConfig,
747
+ });
748
+
749
+ await this.userStats.subscribe();
750
+
751
+ let success = true;
752
+
753
+ if (this.isSubscribed) {
754
+ await Promise.all(this.unsubscribeUsers());
755
+ this.users.clear();
756
+ success = await this.addAndSubscribeToUsers(emulateAuthority);
757
+ }
758
+
759
+ return success;
760
+ }
761
+
717
762
  public async switchActiveUser(subAccountId: number, authority?: PublicKey) {
718
763
  const authorityChanged = authority && !this.authority?.equals(authority);
719
764
 
@@ -771,7 +816,7 @@ export class DriftClient {
771
816
  /**
772
817
  * Adds and subscribes to users based on params set by the constructor or by updateWallet.
773
818
  */
774
- public async addAndSubscribeToUsers(): Promise<boolean> {
819
+ public async addAndSubscribeToUsers(authority?: PublicKey): Promise<boolean> {
775
820
  // save the rpc calls if driftclient is initialized without a real wallet
776
821
  if (this.skipLoadUsers) return true;
777
822
 
@@ -799,12 +844,12 @@ export class DriftClient {
799
844
  let delegatedAccounts = [];
800
845
 
801
846
  const userAccountsPromise = this.getUserAccountsForAuthority(
802
- this.wallet.publicKey
847
+ authority ?? this.wallet.publicKey
803
848
  );
804
849
 
805
850
  if (this.includeDelegates) {
806
851
  const delegatedAccountsPromise = this.getUserAccountsForDelegate(
807
- this.wallet.publicKey
852
+ authority ?? this.wallet.publicKey
808
853
  );
809
854
  [userAccounts, delegatedAccounts] = await Promise.all([
810
855
  userAccountsPromise,