Skip to content

Commit

Permalink
refactor(sdk): use UserMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ChesterSim committed Jan 15, 2024
1 parent 780b965 commit 86cdb0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Binary file modified ts/sdk/bun.lockb
Binary file not shown.
24 changes: 21 additions & 3 deletions ts/sdk/src/vaultClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getUserAccountPublicKeySync,
getUserStatsAccountPublicKey,
User,
UserMap,
} from '@drift-labs/sdk';
import { BorshAccountsCoder, Program, ProgramAccount } from '@coral-xyz/anchor';
import { DriftVaults } from './types/drift_vaults';
Expand Down Expand Up @@ -36,6 +37,7 @@ import {
} from '@solana/spl-token';
import { Vault, VaultDepositor, WithdrawUnit } from './types/types';
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
import { UserMapConfig } from '@drift-labs/sdk/lib/userMap/userMapConfig';

export class VaultClient {
driftClient: DriftClient;
Expand All @@ -45,20 +47,35 @@ export class VaultClient {
/**
* Cache map of drift user accounts of vaults.
*/
readonly vaultUsers: Map<string, User> = new Map<string, User>();
readonly vaultUsers: UserMap;

constructor({
driftClient,
program,
cliMode,
userMapConfig
}: {
driftClient: DriftClient;
program: Program<DriftVaults>;
cliMode?: boolean;
userMapConfig?: UserMapConfig;
}) {
this.driftClient = driftClient;
this.program = program;
this.cliMode = !!cliMode;

if (!userMapConfig) {
this.vaultUsers = new UserMap({
driftClient: driftClient,
subscriptionConfig: {
type: 'polling',
frequency: 1000,
commitment: 'processed'
}
});
} else {
this.vaultUsers = new UserMap(userMapConfig);
}
}

public async getVault(vault: PublicKey): Promise<Vault> {
Expand Down Expand Up @@ -139,8 +156,9 @@ export class VaultClient {
driftClient: this.driftClient,
userAccountPublicKey: vaultDriftUserAccountPubKey,
});
await vaultUser.subscribe();
this.vaultUsers.set(vaultDriftUserAccountPubKey.toBase58(), vaultUser);
await this.vaultUsers.addPubkey(
vaultDriftUserAccountPubKey,
);
}

if (!vaultUser?.isSubscribed) {
Expand Down

0 comments on commit 86cdb0f

Please sign in to comment.