Skip to content

Commit c87faa9

Browse files
Thunkarbenesjan
authored andcommitted
chore: cache chainInfo in embeddedwallet (#22592)
A wallet is instantiated for a particular chain, and if an app supports multiple it is expected to recreate the whole thing. We are calling `getChainInfo` a surprising amount of times, so it makes sense to cache it for performance. --------- Co-authored-by: benesjan <janbenes1234@gmail.com>
1 parent 3ac1a74 commit c87faa9

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

yarn-project/wallet-sdk/src/base-wallet/base_wallet.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import type { AuthWitness } from '@aztec/stdlib/auth-witness';
4343
import { AztecAddress } from '@aztec/stdlib/aztec-address';
4444
import {
4545
type ContractInstanceWithAddress,
46+
type NodeInfo,
4647
computePartialAddress,
4748
getContractClassFromArtifact,
4849
} from '@aztec/stdlib/contract';
@@ -107,6 +108,9 @@ export type CompleteFeeOptionsConfig = {
107108
export abstract class BaseWallet implements Wallet {
108109
protected minFeePadding = 0.5;
109110
protected cancellableTransactions = false;
111+
// A wallet is instantiated for a particular chain, so chain info never changes during its lifetime.
112+
// We cache it here because getChainInfo is called frequently (every tx simulation, send, auth wit, etc.).
113+
private nodeInfoPromise: Promise<NodeInfo> | undefined;
110114

111115
// Protected because we want to force wallets to instantiate their own PXE.
112116
protected constructor(
@@ -138,7 +142,10 @@ export abstract class BaseWallet implements Wallet {
138142
}
139143

140144
async getChainInfo(): Promise<ChainInfo> {
141-
const { l1ChainId, rollupVersion } = await this.aztecNode.getNodeInfo();
145+
if (!this.nodeInfoPromise) {
146+
this.nodeInfoPromise = this.aztecNode.getNodeInfo();
147+
}
148+
const { l1ChainId, rollupVersion } = await this.nodeInfoPromise;
142149
return { chainId: new Fr(l1ChainId), version: new Fr(rollupVersion) };
143150
}
144151

0 commit comments

Comments
 (0)