Skip to content

Commit

Permalink
feat: add deserialize static method to NameRegistryState
Browse files Browse the repository at this point in the history
  • Loading branch information
dr497 committed Jan 8, 2024
1 parent 98f7a63 commit b230e6b
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions js/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,20 @@ export class NameRegistryState {
this.class = new PublicKey(obj.class);
}

static deserialize(data: Buffer) {
let res: NameRegistryState = deserializeUnchecked(
this.schema,
NameRegistryState,
data,
);

res.data = data?.slice(this.HEADER_LEN);
return res;
}

public static async retrieve(
connection: Connection,
nameAccountKey: PublicKey
nameAccountKey: PublicKey,
) {
const nameAccount = await connection.getAccountInfo(nameAccountKey);
if (!nameAccount) {
Expand All @@ -46,7 +57,7 @@ export class NameRegistryState {
let res: NameRegistryState = deserializeUnchecked(
this.schema,
NameRegistryState,
nameAccount.data
nameAccount.data,
);

res.data = nameAccount.data?.slice(this.HEADER_LEN);
Expand All @@ -58,17 +69,16 @@ export class NameRegistryState {

static async _retrieveBatch(
connection: Connection,
nameAccountKeys: PublicKey[]
nameAccountKeys: PublicKey[],
) {
const nameAccounts = await connection.getMultipleAccountsInfo(
nameAccountKeys
);
const nameAccounts =
await connection.getMultipleAccountsInfo(nameAccountKeys);
const fn = (data: Buffer | undefined) => {
if (!data) return undefined;
const res: NameRegistryState = deserializeUnchecked(
this.schema,
NameRegistryState,
data
data,
);
res.data = data?.slice(this.HEADER_LEN);
return res;
Expand All @@ -78,13 +88,13 @@ export class NameRegistryState {

public static async retrieveBatch(
connection: Connection,
nameAccountKeys: PublicKey[]
nameAccountKeys: PublicKey[],
) {
let result: (NameRegistryState | undefined)[] = [];
const keys = [...nameAccountKeys];
while (keys.length > 0) {
result.push(
...(await this._retrieveBatch(connection, keys.splice(0, 100)))
...(await this._retrieveBatch(connection, keys.splice(0, 100))),
);
}
return result;
Expand Down

0 comments on commit b230e6b

Please sign in to comment.