Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: add sync methods UserMap #395

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- sdk: add sync to UserMap and UserStatsMap ([#395](https://github.com/drift-labs/protocol-v2/pull/395))
- sdk: add option to lazy decode user data ([#393](https://github.com/drift-labs/protocol-v2/pull/393))
- program: revert fill ix ([#391](https://github.com/drift-labs/protocol-v2/pull/391))
- program: flag users as idle on-chain ([#386](https://github.com/drift-labs/protocol-v2/pull/386))
Expand Down
53 changes: 52 additions & 1 deletion sdk/src/userMap/userMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
LPRecord,
} from '..';

import { PublicKey } from '@solana/web3.js';
import { AccountInfo, PublicKey } from '@solana/web3.js';
import { Buffer } from 'buffer';
import bs58 from 'bs58';

export interface UserMapInterface {
fetchAllUsers(): Promise<void>;
Expand Down Expand Up @@ -177,4 +179,53 @@ export class UserMap implements UserMapInterface {
public size(): number {
return this.userMap.size;
}

public async sync(includeIdle = true) {
let filters = undefined;
if (!includeIdle) {
filters = [
{
memcmp: {
offset: 4350,
bytes: bs58.encode(Uint8Array.from([0])),
},
},
];
}

const programAccounts =
await this.driftClient.connection.getProgramAccounts(
this.driftClient.program.programId,
{
commitment: this.driftClient.connection.commitment,
filters: [
{
memcmp: this.driftClient.program.coder.accounts.memcmp('User'),
},
...(Array.isArray(filters) ? filters : []),
],
}
);

const programAccountMap = new Map<string, AccountInfo<Buffer>>();
for (const programAccount of programAccounts) {
programAccountMap.set(
programAccount.pubkey.toString(),
programAccount.account
);
}

for (const key of programAccountMap.keys()) {
if (!this.has(key)) {
await this.addPubkey(new PublicKey(key));
}
}

for (const [key, user] of this.userMap.entries()) {
if (!programAccountMap.has(key)) {
await user.unsubscribe();
this.userMap.delete(key);
}
}
}
}
33 changes: 32 additions & 1 deletion sdk/src/userMap/userStatsMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import {
InsuranceFundStakeRecord,
} from '..';
import { ProgramAccount } from '@project-serum/anchor';
import { PublicKey } from '@solana/web3.js';
import { AccountInfo, PublicKey } from '@solana/web3.js';

import { UserMap } from './userMap';
import { Buffer } from 'buffer';

export class UserStatsMap {
/**
Expand Down Expand Up @@ -190,4 +191,34 @@ export class UserStatsMap {
public size(): number {
return this.userStatsMap.size;
}

public async sync() {
const programAccounts =
await this.driftClient.connection.getProgramAccounts(
this.driftClient.program.programId,
{
commitment: this.driftClient.connection.commitment,
filters: [
{
memcmp:
this.driftClient.program.coder.accounts.memcmp('UserStats'),
},
],
}
);

const programAccountMap = new Map<string, AccountInfo<Buffer>>();
for (const programAccount of programAccounts) {
programAccountMap.set(
new PublicKey(programAccount.account.data.slice(8, 40)).toString(),
programAccount.account
);
}

for (const key of programAccountMap.keys()) {
if (!this.has(key)) {
await this.addUserStat(new PublicKey(key));
}
}
}
}