Skip to content

Commit

Permalink
ts-sdk: allow disabling userMapSync on state account updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wphan committed Dec 26, 2023
1 parent 277f499 commit af51b52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 16 additions & 8 deletions sdk/src/userMap/userMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class UserMap implements UserMapInterface {
private connection: Connection;
private commitment: Commitment;
private includeIdle: boolean;
private disableSyncOnTotalAccountsChange: boolean;
private lastNumberOfSubAccounts: BN;
private subscription: PollingSubscription | WebsocketSubscription;
private stateAccountUpdateCallback = async (state: StateAccount) => {
Expand Down Expand Up @@ -78,6 +79,8 @@ export class UserMap implements UserMapInterface {
this.commitment =
config.subscriptionConfig.commitment ?? this.driftClient.opts.commitment;
this.includeIdle = config.includeIdle ?? false;
this.disableSyncOnTotalAccountsChange =
config.disableSyncOnTotalAccountsChange ?? false;

let decodeFn;
if (config.fastDecode ?? true) {
Expand Down Expand Up @@ -115,10 +118,12 @@ export class UserMap implements UserMapInterface {
await this.driftClient.subscribe();
this.lastNumberOfSubAccounts =
this.driftClient.getStateAccount().numberOfSubAccounts;
this.driftClient.eventEmitter.on(
'stateAccountUpdate',
this.stateAccountUpdateCallback
);
if (!this.disableSyncOnTotalAccountsChange) {
this.driftClient.eventEmitter.on(
'stateAccountUpdate',
this.stateAccountUpdateCallback
);
}

await this.subscription.subscribe();
}
Expand Down Expand Up @@ -363,10 +368,13 @@ export class UserMap implements UserMapInterface {
}

if (this.lastNumberOfSubAccounts) {
this.driftClient.eventEmitter.removeListener(
'stateAccountUpdate',
this.stateAccountUpdateCallback
);
if (!this.disableSyncOnTotalAccountsChange) {
this.driftClient.eventEmitter.removeListener(
'stateAccountUpdate',
this.stateAccountUpdateCallback
);
}

this.lastNumberOfSubAccounts = undefined;
}
}
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/userMap/userMapConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ export type UserMapConfig = {

// Whether to skip loading available perp/spot positions and open orders
fastDecode?: boolean;

// If true, will not do a full sync whenever StateAccount.numberOfSubAccounts changes.
// default behavior is to do a full sync on changes.
disableSyncOnTotalAccountsChange?: boolean;
};

0 comments on commit af51b52

Please sign in to comment.