@drift-labs/sdk 2.53.0-beta.0 → 2.53.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.53.0-beta.0
1
+ 2.53.0-beta.2
@@ -11,7 +11,7 @@ export declare class FastSingleTxSender extends BaseTxSender {
11
11
  blockhashRefreshInterval: number;
12
12
  additionalConnections: Connection[];
13
13
  timoutCount: number;
14
- recentBlockhash: string;
14
+ blockhashQueue: string[];
15
15
  constructor({ connection, wallet, opts, timeout, blockhashRefreshInterval, additionalConnections, }: {
16
16
  connection: Connection;
17
17
  wallet: IWallet;
@@ -21,6 +21,7 @@ export declare class FastSingleTxSender extends BaseTxSender {
21
21
  additionalConnections?: any;
22
22
  });
23
23
  startBlockhashRefreshLoop(): void;
24
+ addBlockhashToQueue(blockhash: string): void;
24
25
  prepareTx(tx: Transaction, additionalSigners: Array<Signer>, opts: ConfirmOptions): Promise<Transaction>;
25
26
  getVersionedTransaction(ixs: TransactionInstruction[], lookupTableAccounts: AddressLookupTableAccount[], additionalSigners?: Array<Signer>, opts?: ConfirmOptions): Promise<VersionedTransaction>;
26
27
  sendRawTransaction(rawTransaction: Buffer | Uint8Array, opts: ConfirmOptions): Promise<TxSigAndSlot>;
@@ -5,11 +5,13 @@ const web3_js_1 = require("@solana/web3.js");
5
5
  const anchor_1 = require("@coral-xyz/anchor");
6
6
  const baseTxSender_1 = require("./baseTxSender");
7
7
  const DEFAULT_TIMEOUT = 35000;
8
- const DEFAULT_BLOCKHASH_REFRESH = 10000;
8
+ const DEFAULT_BLOCKHASH_REFRESH = 500;
9
+ const MAX_BLOCKHASH_QUEUE_LENGTH = 100;
9
10
  class FastSingleTxSender extends baseTxSender_1.BaseTxSender {
10
11
  constructor({ connection, wallet, opts = anchor_1.AnchorProvider.defaultOptions(), timeout = DEFAULT_TIMEOUT, blockhashRefreshInterval = DEFAULT_BLOCKHASH_REFRESH, additionalConnections = new Array(), }) {
11
12
  super({ connection, wallet, opts, timeout, additionalConnections });
12
13
  this.timoutCount = 0;
14
+ this.blockhashQueue = [];
13
15
  this.connection = connection;
14
16
  this.wallet = wallet;
15
17
  this.opts = opts;
@@ -21,18 +23,29 @@ class FastSingleTxSender extends baseTxSender_1.BaseTxSender {
21
23
  startBlockhashRefreshLoop() {
22
24
  setInterval(async () => {
23
25
  try {
24
- this.recentBlockhash = (await this.connection.getLatestBlockhash(this.opts)).blockhash;
26
+ const blockhash = (await this.connection.getLatestBlockhash(this.opts))
27
+ .blockhash;
28
+ this.addBlockhashToQueue(blockhash);
25
29
  }
26
30
  catch (e) {
27
31
  console.error('Error in startBlockhashRefreshLoop: ', e);
28
32
  }
29
33
  }, this.blockhashRefreshInterval);
30
34
  }
35
+ addBlockhashToQueue(blockhash) {
36
+ if (blockhash !== this.blockhashQueue[0]) {
37
+ this.blockhashQueue.push(blockhash);
38
+ return;
39
+ }
40
+ if (this.blockhashQueue.length > MAX_BLOCKHASH_QUEUE_LENGTH) {
41
+ this.blockhashQueue.shift();
42
+ }
43
+ }
31
44
  async prepareTx(tx, additionalSigners, opts) {
32
45
  var _a;
33
46
  tx.feePayer = this.wallet.publicKey;
34
47
  tx.recentBlockhash =
35
- (_a = this.recentBlockhash) !== null && _a !== void 0 ? _a : (await this.connection.getLatestBlockhash(opts.preflightCommitment))
48
+ (_a = this.blockhashQueue.shift()) !== null && _a !== void 0 ? _a : (await this.connection.getLatestBlockhash(opts.preflightCommitment))
36
49
  .blockhash;
37
50
  additionalSigners
38
51
  .filter((s) => s !== undefined)
@@ -52,7 +65,7 @@ class FastSingleTxSender extends baseTxSender_1.BaseTxSender {
52
65
  }
53
66
  const message = new web3_js_1.TransactionMessage({
54
67
  payerKey: this.wallet.publicKey,
55
- recentBlockhash: (_a = this.recentBlockhash) !== null && _a !== void 0 ? _a : (await this.connection.getLatestBlockhash(opts.preflightCommitment))
68
+ recentBlockhash: (_a = this.blockhashQueue.shift()) !== null && _a !== void 0 ? _a : (await this.connection.getLatestBlockhash(opts.preflightCommitment))
56
69
  .blockhash,
57
70
  instructions: ixs,
58
71
  }).compileToV0Message(lookupTableAccounts);
@@ -18,6 +18,7 @@ export declare class UserMap implements UserMapInterface {
18
18
  private connection;
19
19
  private commitment;
20
20
  private includeIdle;
21
+ private disableSyncOnTotalAccountsChange;
21
22
  private lastNumberOfSubAccounts;
22
23
  private subscription;
23
24
  private stateAccountUpdateCallback;
@@ -13,7 +13,7 @@ class UserMap {
13
13
  * Constructs a new UserMap instance.
14
14
  */
15
15
  constructor(config) {
16
- var _a, _b, _c;
16
+ var _a, _b, _c, _d;
17
17
  this.userMap = new Map();
18
18
  this.stateAccountUpdateCallback = async (state) => {
19
19
  if (!state.numberOfSubAccounts.eq(this.lastNumberOfSubAccounts)) {
@@ -32,8 +32,10 @@ class UserMap {
32
32
  this.commitment =
33
33
  (_a = config.subscriptionConfig.commitment) !== null && _a !== void 0 ? _a : this.driftClient.opts.commitment;
34
34
  this.includeIdle = (_b = config.includeIdle) !== null && _b !== void 0 ? _b : false;
35
+ this.disableSyncOnTotalAccountsChange =
36
+ (_c = config.disableSyncOnTotalAccountsChange) !== null && _c !== void 0 ? _c : false;
35
37
  let decodeFn;
36
- if ((_c = config.fastDecode) !== null && _c !== void 0 ? _c : true) {
38
+ if ((_d = config.fastDecode) !== null && _d !== void 0 ? _d : true) {
37
39
  decodeFn = (name, buffer) => (0, user_1.decodeUser)(buffer);
38
40
  }
39
41
  else {
@@ -65,7 +67,9 @@ class UserMap {
65
67
  await this.driftClient.subscribe();
66
68
  this.lastNumberOfSubAccounts =
67
69
  this.driftClient.getStateAccount().numberOfSubAccounts;
68
- this.driftClient.eventEmitter.on('stateAccountUpdate', this.stateAccountUpdateCallback);
70
+ if (!this.disableSyncOnTotalAccountsChange) {
71
+ this.driftClient.eventEmitter.on('stateAccountUpdate', this.stateAccountUpdateCallback);
72
+ }
69
73
  await this.subscription.subscribe();
70
74
  }
71
75
  async addPubkey(userAccountPublicKey, userAccount, slot) {
@@ -260,7 +264,9 @@ class UserMap {
260
264
  this.userMap.delete(key);
261
265
  }
262
266
  if (this.lastNumberOfSubAccounts) {
263
- this.driftClient.eventEmitter.removeListener('stateAccountUpdate', this.stateAccountUpdateCallback);
267
+ if (!this.disableSyncOnTotalAccountsChange) {
268
+ this.driftClient.eventEmitter.removeListener('stateAccountUpdate', this.stateAccountUpdateCallback);
269
+ }
264
270
  this.lastNumberOfSubAccounts = undefined;
265
271
  }
266
272
  }
@@ -18,4 +18,5 @@ export type UserMapConfig = {
18
18
  skipInitialLoad?: boolean;
19
19
  includeIdle?: boolean;
20
20
  fastDecode?: boolean;
21
+ disableSyncOnTotalAccountsChange?: boolean;
21
22
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.53.0-beta.0",
3
+ "version": "2.53.0-beta.2",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -15,7 +15,8 @@ import { IWallet } from '../types';
15
15
  import { BaseTxSender } from './baseTxSender';
16
16
 
17
17
  const DEFAULT_TIMEOUT = 35000;
18
- const DEFAULT_BLOCKHASH_REFRESH = 10000;
18
+ const DEFAULT_BLOCKHASH_REFRESH = 500;
19
+ const MAX_BLOCKHASH_QUEUE_LENGTH = 100;
19
20
 
20
21
  export class FastSingleTxSender extends BaseTxSender {
21
22
  connection: Connection;
@@ -25,7 +26,7 @@ export class FastSingleTxSender extends BaseTxSender {
25
26
  blockhashRefreshInterval: number;
26
27
  additionalConnections: Connection[];
27
28
  timoutCount = 0;
28
- recentBlockhash: string;
29
+ blockhashQueue: string[] = [];
29
30
 
30
31
  public constructor({
31
32
  connection,
@@ -55,15 +56,25 @@ export class FastSingleTxSender extends BaseTxSender {
55
56
  startBlockhashRefreshLoop(): void {
56
57
  setInterval(async () => {
57
58
  try {
58
- this.recentBlockhash = (
59
- await this.connection.getLatestBlockhash(this.opts)
60
- ).blockhash;
59
+ const blockhash = (await this.connection.getLatestBlockhash(this.opts))
60
+ .blockhash;
61
+ this.addBlockhashToQueue(blockhash);
61
62
  } catch (e) {
62
63
  console.error('Error in startBlockhashRefreshLoop: ', e);
63
64
  }
64
65
  }, this.blockhashRefreshInterval);
65
66
  }
66
67
 
68
+ addBlockhashToQueue(blockhash: string): void {
69
+ if (blockhash !== this.blockhashQueue[0]) {
70
+ this.blockhashQueue.push(blockhash);
71
+ return;
72
+ }
73
+ if (this.blockhashQueue.length > MAX_BLOCKHASH_QUEUE_LENGTH) {
74
+ this.blockhashQueue.shift();
75
+ }
76
+ }
77
+
67
78
  async prepareTx(
68
79
  tx: Transaction,
69
80
  additionalSigners: Array<Signer>,
@@ -72,7 +83,7 @@ export class FastSingleTxSender extends BaseTxSender {
72
83
  tx.feePayer = this.wallet.publicKey;
73
84
 
74
85
  tx.recentBlockhash =
75
- this.recentBlockhash ??
86
+ this.blockhashQueue.shift() ??
76
87
  (await this.connection.getLatestBlockhash(opts.preflightCommitment))
77
88
  .blockhash;
78
89
 
@@ -103,7 +114,7 @@ export class FastSingleTxSender extends BaseTxSender {
103
114
  const message = new TransactionMessage({
104
115
  payerKey: this.wallet.publicKey,
105
116
  recentBlockhash:
106
- this.recentBlockhash ??
117
+ this.blockhashQueue.shift() ??
107
118
  (await this.connection.getLatestBlockhash(opts.preflightCommitment))
108
119
  .blockhash,
109
120
  instructions: ixs,
@@ -51,6 +51,7 @@ export class UserMap implements UserMapInterface {
51
51
  private connection: Connection;
52
52
  private commitment: Commitment;
53
53
  private includeIdle: boolean;
54
+ private disableSyncOnTotalAccountsChange: boolean;
54
55
  private lastNumberOfSubAccounts: BN;
55
56
  private subscription: PollingSubscription | WebsocketSubscription;
56
57
  private stateAccountUpdateCallback = async (state: StateAccount) => {
@@ -78,6 +79,8 @@ export class UserMap implements UserMapInterface {
78
79
  this.commitment =
79
80
  config.subscriptionConfig.commitment ?? this.driftClient.opts.commitment;
80
81
  this.includeIdle = config.includeIdle ?? false;
82
+ this.disableSyncOnTotalAccountsChange =
83
+ config.disableSyncOnTotalAccountsChange ?? false;
81
84
 
82
85
  let decodeFn;
83
86
  if (config.fastDecode ?? true) {
@@ -115,10 +118,12 @@ export class UserMap implements UserMapInterface {
115
118
  await this.driftClient.subscribe();
116
119
  this.lastNumberOfSubAccounts =
117
120
  this.driftClient.getStateAccount().numberOfSubAccounts;
118
- this.driftClient.eventEmitter.on(
119
- 'stateAccountUpdate',
120
- this.stateAccountUpdateCallback
121
- );
121
+ if (!this.disableSyncOnTotalAccountsChange) {
122
+ this.driftClient.eventEmitter.on(
123
+ 'stateAccountUpdate',
124
+ this.stateAccountUpdateCallback
125
+ );
126
+ }
122
127
 
123
128
  await this.subscription.subscribe();
124
129
  }
@@ -363,10 +368,13 @@ export class UserMap implements UserMapInterface {
363
368
  }
364
369
 
365
370
  if (this.lastNumberOfSubAccounts) {
366
- this.driftClient.eventEmitter.removeListener(
367
- 'stateAccountUpdate',
368
- this.stateAccountUpdateCallback
369
- );
371
+ if (!this.disableSyncOnTotalAccountsChange) {
372
+ this.driftClient.eventEmitter.removeListener(
373
+ 'stateAccountUpdate',
374
+ this.stateAccountUpdateCallback
375
+ );
376
+ }
377
+
370
378
  this.lastNumberOfSubAccounts = undefined;
371
379
  }
372
380
  }
@@ -31,4 +31,8 @@ export type UserMapConfig = {
31
31
 
32
32
  // Whether to skip loading available perp/spot positions and open orders
33
33
  fastDecode?: boolean;
34
+
35
+ // If true, will not do a full sync whenever StateAccount.numberOfSubAccounts changes.
36
+ // default behavior is to do a full sync on changes.
37
+ disableSyncOnTotalAccountsChange?: boolean;
34
38
  };