ts: add optional commitment arg (#1171)
This commit is contained in:
parent
8aab5b4634
commit
af926876c5
|
@ -19,6 +19,7 @@ incremented for features.
|
||||||
|
|
||||||
* lang: Add `programdata_address: Option<Pubkey>` field to `Program` account. Will be populated if account is a program owned by the upgradable bpf loader ([#1125](https://github.com/project-serum/anchor/pull/1125))
|
* lang: Add `programdata_address: Option<Pubkey>` field to `Program` account. Will be populated if account is a program owned by the upgradable bpf loader ([#1125](https://github.com/project-serum/anchor/pull/1125))
|
||||||
* lang,ts,ci,cli,docs: update solana toolchain to version 1.8.5([#1133](https://github.com/project-serum/anchor/pull/1133))
|
* lang,ts,ci,cli,docs: update solana toolchain to version 1.8.5([#1133](https://github.com/project-serum/anchor/pull/1133))
|
||||||
|
* ts: Add optional commitment argument to `fetch` and `fetchMultiple` ([#1171](https://github.com/project-serum/anchor/pull/1171))
|
||||||
|
|
||||||
### Breaking
|
### Breaking
|
||||||
|
|
||||||
|
|
|
@ -136,8 +136,11 @@ export class AccountClient<
|
||||||
*
|
*
|
||||||
* @param address The address of the account to fetch.
|
* @param address The address of the account to fetch.
|
||||||
*/
|
*/
|
||||||
async fetchNullable(address: Address): Promise<T | null> {
|
async fetchNullable(
|
||||||
const accountInfo = await this.getAccountInfo(address);
|
address: Address,
|
||||||
|
commitment?: Commitment
|
||||||
|
): Promise<T | null> {
|
||||||
|
const accountInfo = await this.getAccountInfo(address, commitment);
|
||||||
if (accountInfo === null) {
|
if (accountInfo === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -161,8 +164,8 @@ export class AccountClient<
|
||||||
*
|
*
|
||||||
* @param address The address of the account to fetch.
|
* @param address The address of the account to fetch.
|
||||||
*/
|
*/
|
||||||
async fetch(address: Address): Promise<T> {
|
async fetch(address: Address, commitment?: Commitment): Promise<T> {
|
||||||
const data = await this.fetchNullable(address);
|
const data = await this.fetchNullable(address, commitment);
|
||||||
if (data === null) {
|
if (data === null) {
|
||||||
throw new Error(`Account does not exist ${address.toString()}`);
|
throw new Error(`Account does not exist ${address.toString()}`);
|
||||||
}
|
}
|
||||||
|
@ -175,10 +178,14 @@ export class AccountClient<
|
||||||
*
|
*
|
||||||
* @param addresses The addresses of the accounts to fetch.
|
* @param addresses The addresses of the accounts to fetch.
|
||||||
*/
|
*/
|
||||||
async fetchMultiple(addresses: Address[]): Promise<(Object | null)[]> {
|
async fetchMultiple(
|
||||||
|
addresses: Address[],
|
||||||
|
commitment?: Commitment
|
||||||
|
): Promise<(Object | null)[]> {
|
||||||
const accounts = await rpcUtil.getMultipleAccounts(
|
const accounts = await rpcUtil.getMultipleAccounts(
|
||||||
this._provider.connection,
|
this._provider.connection,
|
||||||
addresses.map((address) => translateAddress(address))
|
addresses.map((address) => translateAddress(address)),
|
||||||
|
commitment
|
||||||
);
|
);
|
||||||
|
|
||||||
const discriminator = AccountsCoder.accountDiscriminator(
|
const discriminator = AccountsCoder.accountDiscriminator(
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
TransactionSignature,
|
TransactionSignature,
|
||||||
Transaction,
|
Transaction,
|
||||||
TransactionInstruction,
|
TransactionInstruction,
|
||||||
|
Commitment,
|
||||||
} from "@solana/web3.js";
|
} from "@solana/web3.js";
|
||||||
import { chunks } from "../utils/common.js";
|
import { chunks } from "../utils/common.js";
|
||||||
import { Address, translateAddress } from "../program/common.js";
|
import { Address, translateAddress } from "../program/common.js";
|
||||||
|
@ -44,29 +45,44 @@ const GET_MULTIPLE_ACCOUNTS_LIMIT: number = 99;
|
||||||
|
|
||||||
export async function getMultipleAccounts(
|
export async function getMultipleAccounts(
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
publicKeys: PublicKey[]
|
publicKeys: PublicKey[],
|
||||||
|
commitment?: Commitment
|
||||||
): Promise<
|
): Promise<
|
||||||
Array<null | { publicKey: PublicKey; account: AccountInfo<Buffer> }>
|
Array<null | { publicKey: PublicKey; account: AccountInfo<Buffer> }>
|
||||||
> {
|
> {
|
||||||
if (publicKeys.length <= GET_MULTIPLE_ACCOUNTS_LIMIT) {
|
if (publicKeys.length <= GET_MULTIPLE_ACCOUNTS_LIMIT) {
|
||||||
return await getMultipleAccountsCore(connection, publicKeys);
|
return await getMultipleAccountsCore(connection, publicKeys, commitment);
|
||||||
} else {
|
} else {
|
||||||
const batches = chunks(publicKeys, GET_MULTIPLE_ACCOUNTS_LIMIT);
|
const batches = chunks(publicKeys, GET_MULTIPLE_ACCOUNTS_LIMIT);
|
||||||
const results = await Promise.all<
|
const results = await Promise.all<
|
||||||
Array<null | { publicKey: PublicKey; account: AccountInfo<Buffer> }>
|
Array<null | { publicKey: PublicKey; account: AccountInfo<Buffer> }>
|
||||||
>(batches.map((batch) => getMultipleAccountsCore(connection, batch)));
|
>(
|
||||||
|
batches.map((batch) =>
|
||||||
|
getMultipleAccountsCore(connection, batch, commitment)
|
||||||
|
)
|
||||||
|
);
|
||||||
return results.flat();
|
return results.flat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getMultipleAccountsCore(
|
async function getMultipleAccountsCore(
|
||||||
connection: Connection,
|
connection: Connection,
|
||||||
publicKeys: PublicKey[]
|
publicKeys: PublicKey[],
|
||||||
|
commitmentOverride?: Commitment
|
||||||
): Promise<
|
): Promise<
|
||||||
Array<null | { publicKey: PublicKey; account: AccountInfo<Buffer> }>
|
Array<null | { publicKey: PublicKey; account: AccountInfo<Buffer> }>
|
||||||
> {
|
> {
|
||||||
const args = [publicKeys.map((k) => k.toBase58()), { commitment: "recent" }];
|
const commitment = commitmentOverride ?? connection.commitment;
|
||||||
// @ts-ignore
|
const args: (
|
||||||
|
| string[]
|
||||||
|
| {
|
||||||
|
commitment: Commitment;
|
||||||
|
}
|
||||||
|
)[] = [publicKeys.map((k) => k.toBase58())];
|
||||||
|
if (commitment) {
|
||||||
|
args.push({ commitment });
|
||||||
|
}
|
||||||
|
// @ts-expect-error
|
||||||
const res = await connection._rpcRequest("getMultipleAccounts", args);
|
const res = await connection._rpcRequest("getMultipleAccounts", args);
|
||||||
if (res.error) {
|
if (res.error) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|
Loading…
Reference in New Issue