Skip to content

Commit

Permalink
sdk: add sync methods UserMap (#395)
Browse files Browse the repository at this point in the history
* sdk: add sync methods UserMap

* CHANGELOG
  • Loading branch information
crispheaney committed Mar 16, 2023
1 parent 0da09be commit 248415e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
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));
}
}
}
}

0 comments on commit 248415e

Please sign in to comment.