Skip to content

Commit cf637dc

Browse files
spypsyspalladino
andauthored
chore!: use single extended viem client (#13715)
Fixes #12254 Eliminates redundancy of passing 2 clients to a lot of classes, and just passes one instead that can do both public & wallet actions. --------- Co-authored-by: Santiago Palladino <[email protected]>
1 parent 0dd8a7e commit cf637dc

File tree

79 files changed

+1227
-1318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1227
-1318
lines changed

yarn-project/archiver/src/archiver/archiver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { RunningPromise, makeLoggingErrorHandler } from '@aztec/foundation/runni
77
import { sleep } from '@aztec/foundation/sleep';
88
import { count } from '@aztec/foundation/string';
99
import { elapsed } from '@aztec/foundation/timer';
10-
import { InboxAbi } from '@aztec/l1-artifacts';
10+
import { InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
1111
import {
1212
ContractClassRegisteredEvent,
1313
PrivateFunctionBroadcastedEvent,
@@ -531,7 +531,7 @@ export class Archiver extends EventEmitter implements ArchiveSource, Traceable {
531531

532532
// TODO(md): Retrieve from blob sink then from consensus client, then from peers
533533
const retrievedBlocks = await retrieveBlocksFromRollup(
534-
this.rollup.getContract(),
534+
this.rollup.getContract() as GetContractReturnType<typeof RollupAbi, ViemPublicClient>,
535535
this.publicClient,
536536
this.blobSinkClient,
537537
searchStartBlock, // TODO(palla/reorg): If the L2 reorg was due to an L1 reorg, we need to start search earlier

yarn-project/aztec-faucet/src/faucet.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ViemPublicClient, type ViemWalletClient, createEthereumChain } from '@aztec/ethereum';
1+
import { type ExtendedViemWalletClient, createEthereumChain, createExtendedL1Client } from '@aztec/ethereum';
22
import type { EthAddress } from '@aztec/foundation/eth-address';
33
import { createLogger } from '@aztec/foundation/log';
44
import { TestERC20Abi } from '@aztec/l1-artifacts';
@@ -10,12 +10,8 @@ import {
1010
type HttpTransport,
1111
type LocalAccount,
1212
type WalletClient,
13-
createPublicClient,
14-
createWalletClient,
15-
fallback,
1613
getContract,
1714
parseEther,
18-
http as viemHttp,
1915
} from 'viem';
2016
import { mnemonicToAccount } from 'viem/accounts';
2117

@@ -27,8 +23,7 @@ type L1Asset = {
2723
};
2824

2925
export class Faucet {
30-
private walletClient: ViemWalletClient;
31-
private publicClient: ViemPublicClient;
26+
private l1Client: ExtendedViemWalletClient;
3227

3328
private dripHistory = new Map<string, Map<string, number>>();
3429
private l1Assets = new Map<string, L1Asset>();
@@ -41,16 +36,7 @@ export class Faucet {
4136
) {
4237
const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
4338

44-
this.walletClient = createWalletClient({
45-
account: this.account,
46-
chain: chain.chainInfo,
47-
transport: fallback(chain.rpcUrls.map(url => viemHttp(url))),
48-
});
49-
50-
this.publicClient = createPublicClient({
51-
chain: chain.chainInfo,
52-
transport: fallback(chain.rpcUrls.map(url => viemHttp(url))),
53-
});
39+
this.l1Client = createExtendedL1Client(config.l1RpcUrls, this.account, chain.chainInfo);
5440
}
5541

5642
public static async create(config: FaucetConfig): Promise<Faucet> {
@@ -79,12 +65,12 @@ export class Faucet {
7965
public async sendEth(to: EthAddress): Promise<void> {
8066
this.checkThrottle(to, 'ETH');
8167

82-
const hash = await this.walletClient.sendTransaction({
68+
const hash = await this.l1Client.sendTransaction({
8369
account: this.account,
8470
to: to.toString(),
8571
value: parseEther(this.config.ethAmount),
8672
});
87-
await this.publicClient.waitForTransactionReceipt({ hash });
73+
await this.l1Client.waitForTransactionReceipt({ hash });
8874

8975
this.updateThrottle(to, 'ETH');
9076
this.log.info(`Sent ETH ${this.config.ethAmount} to ${to} in tx ${hash}`);
@@ -99,7 +85,7 @@ export class Faucet {
9985
this.checkThrottle(to, assetName);
10086

10187
const hash = await asset.contract.write.mint([to.toString(), asset.amount]);
102-
await this.publicClient.waitForTransactionReceipt({ hash });
88+
await this.l1Client.waitForTransactionReceipt({ hash });
10389

10490
this.updateThrottle(to, assetName);
10591

@@ -110,7 +96,7 @@ export class Faucet {
11096
const contract = getContract({
11197
abi: TestERC20Abi,
11298
address: l1AssetConfig.address.toString(),
113-
client: this.walletClient,
99+
client: this.l1Client,
114100
});
115101

116102
const [name, owner] = await Promise.all([contract.read.name(), contract.read.owner()]);

yarn-project/aztec.js/src/ethereum/portal_manager.ts

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ViemPublicClient, ViemWalletClient } from '@aztec/ethereum';
1+
import type { ExtendedViemWalletClient, ViemContract } from '@aztec/ethereum';
22
import { extractEvent } from '@aztec/ethereum/utils';
33
import { sha256ToField } from '@aztec/foundation/crypto';
44
import { EthAddress } from '@aztec/foundation/eth-address';
@@ -14,7 +14,7 @@ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
1414
import { computeSecretHash } from '@aztec/stdlib/hash';
1515
import type { PXE } from '@aztec/stdlib/interfaces/client';
1616

17-
import { type GetContractReturnType, type Hex, getContract, toFunctionSelector } from 'viem';
17+
import { type Hex, getContract, toFunctionSelector } from 'viem';
1818

1919
import type { Wallet } from '../index.js';
2020

@@ -57,28 +57,27 @@ export async function generateClaimSecret(logger?: Logger): Promise<[Fr, Fr]> {
5757

5858
/** Helper for managing an ERC20 on L1. */
5959
export class L1TokenManager {
60-
private contract: GetContractReturnType<typeof TestERC20Abi, ViemWalletClient>;
61-
private handler: GetContractReturnType<typeof FeeAssetHandlerAbi, ViemWalletClient> | undefined;
60+
private contract: ViemContract<typeof TestERC20Abi>;
61+
private handler: ViemContract<typeof FeeAssetHandlerAbi> | undefined;
6262

6363
public constructor(
6464
/** Address of the ERC20 contract. */
6565
public readonly tokenAddress: EthAddress,
6666
/** Address of the handler/faucet contract. */
6767
public readonly handlerAddress: EthAddress | undefined,
68-
private publicClient: ViemPublicClient,
69-
private walletClient: ViemWalletClient,
68+
private readonly extendedClient: ExtendedViemWalletClient,
7069
private logger: Logger,
7170
) {
7271
this.contract = getContract({
7372
address: this.tokenAddress.toString(),
7473
abi: TestERC20Abi,
75-
client: this.walletClient,
74+
client: this.extendedClient,
7675
});
7776
if (this.handlerAddress) {
7877
this.handler = getContract({
7978
address: this.handlerAddress.toString(),
8079
abi: FeeAssetHandlerAbi,
81-
client: this.walletClient,
80+
client: this.extendedClient,
8281
});
8382
}
8483
}
@@ -124,7 +123,7 @@ export class L1TokenManager {
124123
*/
125124
public async approve(amount: bigint, address: Hex, addressName = '') {
126125
this.logger.info(`Approving ${amount} tokens for ${stringifyEthAddress(address, addressName)}`);
127-
await this.publicClient.waitForTransactionReceipt({
126+
await this.extendedClient.waitForTransactionReceipt({
128127
hash: await this.contract.write.approve([address, amount]),
129128
});
130129
}
@@ -133,21 +132,20 @@ export class L1TokenManager {
133132
/** Helper for interacting with the FeeJuicePortal on L1. */
134133
export class L1FeeJuicePortalManager {
135134
private readonly tokenManager: L1TokenManager;
136-
private readonly contract: GetContractReturnType<typeof FeeJuicePortalAbi, ViemWalletClient>;
135+
private readonly contract: ViemContract<typeof FeeJuicePortalAbi>;
137136

138137
constructor(
139138
portalAddress: EthAddress,
140139
tokenAddress: EthAddress,
141140
handlerAddress: EthAddress,
142-
private readonly publicClient: ViemPublicClient,
143-
private readonly walletClient: ViemWalletClient,
141+
private readonly extendedClient: ExtendedViemWalletClient,
144142
private readonly logger: Logger,
145143
) {
146-
this.tokenManager = new L1TokenManager(tokenAddress, handlerAddress, publicClient, walletClient, logger);
144+
this.tokenManager = new L1TokenManager(tokenAddress, handlerAddress, extendedClient, logger);
147145
this.contract = getContract({
148146
address: portalAddress.toString(),
149147
abi: FeeJuicePortalAbi,
150-
client: this.walletClient,
148+
client: extendedClient,
151149
});
152150
}
153151

@@ -170,7 +168,7 @@ export class L1FeeJuicePortalManager {
170168
if (amountToBridge !== mintableAmount) {
171169
throw new Error(`Minting amount must be ${mintableAmount}`);
172170
}
173-
await this.tokenManager.mint(this.walletClient.account.address);
171+
await this.tokenManager.mint(this.extendedClient.account.address);
174172
}
175173

176174
await this.tokenManager.approve(amountToBridge, this.contract.address, 'FeeJuice Portal');
@@ -180,7 +178,7 @@ export class L1FeeJuicePortalManager {
180178

181179
await this.contract.simulate.depositToAztecPublic(args);
182180

183-
const txReceipt = await this.publicClient.waitForTransactionReceipt({
181+
const txReceipt = await this.extendedClient.waitForTransactionReceipt({
184182
hash: await this.contract.write.depositToAztecPublic(args),
185183
});
186184

@@ -210,14 +208,12 @@ export class L1FeeJuicePortalManager {
210208
/**
211209
* Creates a new instance
212210
* @param walletOrPxe - Wallet or PXE client used for retrieving the L1 contract addresses.
213-
* @param publicClient - L1 public client.
214-
* @param walletClient - L1 wallet client.
211+
* @param extendedClient - Wallet client, extended with public actions.
215212
* @param logger - Logger.
216213
*/
217214
public static async new(
218215
walletOrPxe: Wallet | PXE,
219-
publicClient: ViemPublicClient,
220-
walletClient: ViemWalletClient,
216+
extendedClient: ExtendedViemWalletClient,
221217
logger: Logger,
222218
): Promise<L1FeeJuicePortalManager> {
223219
const {
@@ -235,31 +231,29 @@ export class L1FeeJuicePortalManager {
235231
feeJuicePortalAddress,
236232
feeJuiceAddress,
237233
feeAssetHandlerAddress,
238-
publicClient,
239-
walletClient,
234+
extendedClient,
240235
logger,
241236
);
242237
}
243238
}
244239

245240
/** Helper for interacting with a test TokenPortal on L1 for sending tokens to L2. */
246241
export class L1ToL2TokenPortalManager {
247-
protected readonly portal: GetContractReturnType<typeof TokenPortalAbi, ViemWalletClient>;
242+
protected readonly portal: ViemContract<typeof TokenPortalAbi>;
248243
protected readonly tokenManager: L1TokenManager;
249244

250245
constructor(
251246
portalAddress: EthAddress,
252247
tokenAddress: EthAddress,
253248
handlerAddress: EthAddress | undefined,
254-
protected publicClient: ViemPublicClient,
255-
protected walletClient: ViemWalletClient,
249+
protected extendedClient: ExtendedViemWalletClient,
256250
protected logger: Logger,
257251
) {
258-
this.tokenManager = new L1TokenManager(tokenAddress, handlerAddress, publicClient, walletClient, logger);
252+
this.tokenManager = new L1TokenManager(tokenAddress, handlerAddress, extendedClient, logger);
259253
this.portal = getContract({
260254
address: portalAddress.toString(),
261255
abi: TokenPortalAbi,
262-
client: this.walletClient,
256+
client: extendedClient,
263257
});
264258
}
265259

@@ -284,8 +278,8 @@ export class L1ToL2TokenPortalManager {
284278
claimSecretHash.toString(),
285279
]);
286280

287-
const txReceipt = await this.publicClient.waitForTransactionReceipt({
288-
hash: await this.walletClient.writeContract(request),
281+
const txReceipt = await this.extendedClient.waitForTransactionReceipt({
282+
hash: await this.extendedClient.writeContract(request),
289283
});
290284

291285
const log = extractEvent(
@@ -327,8 +321,8 @@ export class L1ToL2TokenPortalManager {
327321
this.logger.info('Sending L1 tokens to L2 to be claimed privately');
328322
const { request } = await this.portal.simulate.depositToAztecPrivate([amount, claimSecretHash.toString()]);
329323

330-
const txReceipt = await this.publicClient.waitForTransactionReceipt({
331-
hash: await this.walletClient.writeContract(request),
324+
const txReceipt = await this.extendedClient.waitForTransactionReceipt({
325+
hash: await this.extendedClient.writeContract(request),
332326
});
333327

334328
const log = extractEvent(
@@ -360,7 +354,7 @@ export class L1ToL2TokenPortalManager {
360354
if (amount !== mintableAmount) {
361355
throw new Error(`Minting amount must be ${mintableAmount} for testing`);
362356
}
363-
await this.tokenManager.mint(this.walletClient.account.address);
357+
await this.tokenManager.mint(this.extendedClient.account.address);
364358
}
365359
await this.tokenManager.approve(amount, this.portal.address, 'TokenPortal');
366360
return generateClaimSecret();
@@ -369,22 +363,21 @@ export class L1ToL2TokenPortalManager {
369363

370364
/** Helper for interacting with a test TokenPortal on L1 for both withdrawing from and bridging to L2. */
371365
export class L1TokenPortalManager extends L1ToL2TokenPortalManager {
372-
private readonly outbox: GetContractReturnType<typeof OutboxAbi, ViemWalletClient>;
366+
private readonly outbox: ViemContract<typeof OutboxAbi>;
373367

374368
constructor(
375369
portalAddress: EthAddress,
376370
tokenAddress: EthAddress,
377371
handlerAddress: EthAddress | undefined,
378372
outboxAddress: EthAddress,
379-
publicClient: ViemPublicClient,
380-
walletClient: ViemWalletClient,
373+
extendedClient: ExtendedViemWalletClient,
381374
logger: Logger,
382375
) {
383-
super(portalAddress, tokenAddress, handlerAddress, publicClient, walletClient, logger);
376+
super(portalAddress, tokenAddress, handlerAddress, extendedClient, logger);
384377
this.outbox = getContract({
385378
address: outboxAddress.toString(),
386379
abi: OutboxAbi,
387-
client: walletClient,
380+
client: extendedClient,
388381
});
389382
}
390383

@@ -422,7 +415,9 @@ export class L1TokenPortalManager extends L1ToL2TokenPortalManager {
422415
siblingPath.toBufferArray().map((buf: Buffer): Hex => `0x${buf.toString('hex')}`),
423416
]);
424417

425-
await this.publicClient.waitForTransactionReceipt({ hash: await this.walletClient.writeContract(withdrawRequest) });
418+
await this.extendedClient.waitForTransactionReceipt({
419+
hash: await this.extendedClient.writeContract(withdrawRequest),
420+
});
426421

427422
const isConsumedAfter = await this.outbox.read.hasMessageBeenConsumedAtBlockAndIndex([blockNumber, messageIndex]);
428423
if (!isConsumedAfter) {
@@ -455,7 +450,7 @@ export class L1TokenPortalManager extends L1ToL2TokenPortalManager {
455450
l2Bridge.toBuffer(),
456451
new Fr(version).toBuffer(), // aztec version
457452
EthAddress.fromString(this.portal.address).toBuffer32() ?? Buffer.alloc(32, 0),
458-
new Fr(this.publicClient.chain.id).toBuffer(), // chain id
453+
new Fr(this.extendedClient.chain.id).toBuffer(), // chain id
459454
content.toBuffer(),
460455
]);
461456

yarn-project/aztec.js/src/test/anvil_test_watcher.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ViemPublicClient } from '@aztec/ethereum';
1+
import type { ViemClient } from '@aztec/ethereum';
22
import type { EthCheatCodes } from '@aztec/ethereum/eth-cheatcodes';
33
import type { EthAddress } from '@aztec/foundation/eth-address';
44
import { type Logger, createLogger } from '@aztec/foundation/log';
@@ -20,7 +20,7 @@ import { RollupCheatCodes } from './rollup_cheat_codes.js';
2020
export class AnvilTestWatcher {
2121
private isSandbox: boolean = false;
2222

23-
private rollup: GetContractReturnType<typeof RollupAbi, ViemPublicClient>;
23+
private rollup: GetContractReturnType<typeof RollupAbi, ViemClient>;
2424
private rollupCheatCodes: RollupCheatCodes;
2525

2626
private filledRunningPromise?: RunningPromise;
@@ -34,13 +34,13 @@ export class AnvilTestWatcher {
3434
constructor(
3535
private cheatcodes: EthCheatCodes,
3636
rollupAddress: EthAddress,
37-
publicClient: ViemPublicClient,
37+
l1Client: ViemClient,
3838
private dateProvider?: TestDateProvider,
3939
) {
4040
this.rollup = getContract({
4141
address: getAddress(rollupAddress.toString()),
4242
abi: RollupAbi,
43-
client: publicClient,
43+
client: l1Client,
4444
});
4545

4646
this.rollupCheatCodes = new RollupCheatCodes(this.cheatcodes, {

0 commit comments

Comments
 (0)