ts: Add fetchMultiple to compliment rpc getMultiple (#761)

This commit is contained in:
Dana Hanna 2021-09-21 15:15:43 -04:00 committed by GitHub
parent 5bd0309f66
commit d40400fff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import Coder, {
import { Subscription, Address, translateAddress } from "../common";
import { getProvider } from "../../";
import * as pubkeyUtil from "../../utils/pubkey";
import * as rpcUtil from "../../utils/rpc";
export default class AccountFactory {
public static build(
@ -152,6 +153,34 @@ export class AccountClient<T = any> {
return data;
}
/**
* Returns multiple deserialized accounts.
* Accounts not found or with wrong discriminator are returned as null.
*
* @param addresses The addresses of the accounts to fetch.
*/
async fetchMultiple(addresses: Address[]): Promise<(Object | null)[]> {
const accounts = await rpcUtil.getMultipleAccounts(
this._provider.connection,
addresses.map((address) => translateAddress(address))
);
const discriminator = await accountDiscriminator(this._idlAccount.name);
// Decode accounts where discriminator is correct, null otherwise
return accounts.map((account) => {
if (account == null) {
return null;
}
if (discriminator.compare(account?.account.data.slice(0, 8))) {
return null;
}
return this._coder.accounts.decode(
this._idlAccount.name,
account?.account.data
);
});
}
/**
* Returns all instances of this account type for the program.
*/