Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

feat: add minContextSlot configuration to (almost) all web3.js methods #26296

Merged
merged 15 commits into from
Jun 29, 2022
Merged
Changes from 1 commit
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
45 changes: 41 additions & 4 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ export type BlockheightBasedTransactionConfirmationStrategy = {
signature: TransactionSignature;
} & BlockhashWithExpiryBlockHeight;

/** @internal */
function extractCommitmentFromConfig<TConfig>(
commitmentOrConfig?: Commitment | ({commitment?: Commitment} & TConfig),
) {
let commitment: Commitment | undefined;
let config: Omit<TConfig, 'commitment'> | undefined;
if (typeof commitmentOrConfig === 'string') {
commitment = commitmentOrConfig;
} else if (commitmentOrConfig) {
const {commitment: specifiedCommitment, ...specifiedConfig} =
commitmentOrConfig;
commitment = specifiedCommitment;
config = specifiedConfig;
}
return {commitment, config};
}
Comment on lines +307 to +321
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been in a separate commit for the purpose of review, sorry.


/**
* @internal
*/
Expand Down Expand Up @@ -399,6 +416,16 @@ export type Finality = 'confirmed' | 'finalized';
*/
export type LargestAccountsFilter = 'circulating' | 'nonCirculating';

/**
* Configuration object for changing `getAccountInfo` query behavior
*/
export type GetAccountInfoConfig = {
/** The level of commitment desired */
commitment?: Commitment;
/** The minimum slot that the request can be evaluated at */
minContextSlot?: number;
};

/**
* Configuration object for changing `getLargestAccounts` query behavior
*/
Expand Down Expand Up @@ -2616,9 +2643,16 @@ export class Connection {
*/
async getAccountInfoAndContext(
publicKey: PublicKey,
commitment?: Commitment,
commitmentOrConfig?: Commitment | GetAccountInfoConfig,
): Promise<RpcResponseAndContext<AccountInfo<Buffer> | null>> {
const args = this._buildArgs([publicKey.toBase58()], commitment, 'base64');
const {commitment, config} =
extractCommitmentFromConfig(commitmentOrConfig);
const args = this._buildArgs(
[publicKey.toBase58()],
commitment,
'base64',
config,
);
const unsafeRes = await this._rpcRequest('getAccountInfo', args);
const res = create(
unsafeRes,
Expand Down Expand Up @@ -2670,10 +2704,13 @@ export class Connection {
*/
async getAccountInfo(
publicKey: PublicKey,
commitment?: Commitment,
commitmentOrConfig?: Commitment | GetAccountInfoConfig,
): Promise<AccountInfo<Buffer> | null> {
try {
const res = await this.getAccountInfoAndContext(publicKey, commitment);
const res = await this.getAccountInfoAndContext(
publicKey,
commitmentOrConfig,
);
return res.value;
} catch (e) {
throw new Error(
Expand Down