Skip to content

Commit f96b1d8

Browse files
committed
simplify acc & wallets
1 parent d066d51 commit f96b1d8

File tree

6 files changed

+94
-117
lines changed

6 files changed

+94
-117
lines changed

yarn-project/circuit-types/src/interfaces/aztec-node.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
L1_TO_L2_MSG_TREE_HEIGHT,
1010
NOTE_HASH_TREE_HEIGHT,
1111
NULLIFIER_TREE_HEIGHT,
12+
type NodeInfo,
1213
PUBLIC_DATA_TREE_HEIGHT,
1314
type ProtocolContractAddresses,
1415
ProtocolContractsNames,
@@ -155,6 +156,11 @@ describe('AztecNodeApiSchema', () => {
155156
expect(response).toBe(true);
156157
});
157158

159+
it('getNodeInfo', async () => {
160+
const response = await context.client.getNodeInfo();
161+
expect(response).toEqual(await handler.getNodeInfo());
162+
});
163+
158164
it('getBlocks', async () => {
159165
const response = await context.client.getBlocks(1, 1);
160166
expect(response).toHaveLength(1);
@@ -404,6 +410,20 @@ class MockAztecNode implements AztecNode {
404410
isReady(): Promise<boolean> {
405411
return Promise.resolve(true);
406412
}
413+
getNodeInfo(): Promise<NodeInfo> {
414+
return Promise.resolve({
415+
nodeVersion: '1.0',
416+
l1ChainId: 1,
417+
protocolVersion: 1,
418+
enr: 'enr',
419+
l1ContractAddresses: Object.fromEntries(
420+
L1ContractsNames.map(name => [name, EthAddress.random()]),
421+
) as L1ContractAddresses,
422+
protocolContractAddresses: Object.fromEntries(
423+
ProtocolContractsNames.map(name => [name, AztecAddress.random()]),
424+
) as ProtocolContractAddresses,
425+
});
426+
}
407427
getBlocks(from: number, limit: number): Promise<L2Block[]> {
408428
return Promise.resolve(times(limit, i => L2Block.random(from + i)));
409429
}

yarn-project/circuit-types/src/interfaces/aztec-node.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
L1_TO_L2_MSG_TREE_HEIGHT,
99
NOTE_HASH_TREE_HEIGHT,
1010
NULLIFIER_TREE_HEIGHT,
11-
NodeInfo,
11+
type NodeInfo,
12+
NodeInfoSchema,
1213
PUBLIC_DATA_TREE_HEIGHT,
1314
type ProtocolContractAddresses,
1415
ProtocolContractAddressesSchema,
@@ -464,6 +465,8 @@ export const AztecNodeApiSchema: ApiSchemaFor<AztecNode> = {
464465

465466
isReady: z.function().returns(z.boolean()),
466467

468+
getNodeInfo: z.function().returns(NodeInfoSchema),
469+
467470
getBlocks: z.function().args(z.number(), z.number()).returns(z.array(L2Block.schema)),
468471

469472
getNodeVersion: z.function().returns(z.string()),

yarn-project/ethereum/src/deploy_l1_contracts.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ export async function deployL1Contract(
617617
let txHash: Hex | undefined = undefined;
618618
let resultingAddress: Hex | null | undefined = undefined;
619619

620-
const gasUtils = new GasUtils(publicClient, logger);
620+
const gasUtils = new GasUtils(publicClient, walletClient, logger);
621621

622622
if (libraries) {
623623
// @note Assumes that we wont have nested external libraries.
@@ -667,7 +667,7 @@ export async function deployL1Contract(
667667
const existing = await publicClient.getBytecode({ address: resultingAddress });
668668

669669
if (existing === undefined || existing === '0x') {
670-
const receipt = await gasUtils.sendAndMonitorTransaction(walletClient, walletClient.account!, {
670+
const receipt = await gasUtils.sendAndMonitorTransaction({
671671
to: deployer,
672672
data: concatHex([salt, calldata]),
673673
});
@@ -680,7 +680,7 @@ export async function deployL1Contract(
680680
} else {
681681
// Regular deployment path
682682
const deployData = encodeDeployData({ abi, bytecode, args });
683-
const receipt = await gasUtils.sendAndMonitorTransaction(walletClient, walletClient.account!, {
683+
const receipt = await gasUtils.sendAndMonitorTransaction({
684684
to: '0x', // Contract creation
685685
data: deployData,
686686
});

yarn-project/ethereum/src/gas_utils.test.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ describe('GasUtils', () => {
2929
let gasUtils: GasUtils;
3030
let publicClient: any;
3131
let walletClient: any;
32-
let account: any;
3332
let anvil: Anvil;
3433
let initialBaseFee: bigint;
3534
const logger = createDebugLogger('l1_gas_test');
@@ -43,7 +42,7 @@ describe('GasUtils', () => {
4342
throw new Error('Failed to get private key');
4443
}
4544
const privKey = Buffer.from(privKeyRaw).toString('hex');
46-
account = privateKeyToAccount(`0x${privKey}`);
45+
const account = privateKeyToAccount(`0x${privKey}`);
4746

4847
publicClient = createPublicClient({
4948
transport: http(rpcUrl),
@@ -58,6 +57,7 @@ describe('GasUtils', () => {
5857

5958
gasUtils = new GasUtils(
6059
publicClient,
60+
walletClient,
6161
logger,
6262
{
6363
bufferPercentage: 20n,
@@ -77,7 +77,7 @@ describe('GasUtils', () => {
7777
}, 5000);
7878

7979
it('sends and monitors a simple transaction', async () => {
80-
const receipt = await gasUtils.sendAndMonitorTransaction(walletClient, account, {
80+
const receipt = await gasUtils.sendAndMonitorTransaction({
8181
to: '0x1234567890123456789012345678901234567890',
8282
data: '0x',
8383
value: 0n,
@@ -92,7 +92,7 @@ describe('GasUtils', () => {
9292
initialBaseFee = initialBlock.baseFeePerGas ?? 0n;
9393

9494
// Start a transaction
95-
const sendPromise = gasUtils.sendAndMonitorTransaction(walletClient, account, {
95+
const sendPromise = gasUtils.sendAndMonitorTransaction({
9696
to: '0x1234567890123456789012345678901234567890',
9797
data: '0x',
9898
value: 0n,
@@ -131,7 +131,7 @@ describe('GasUtils', () => {
131131
params: [],
132132
});
133133

134-
const receipt = await gasUtils.sendAndMonitorTransaction(walletClient, account, {
134+
const receipt = await gasUtils.sendAndMonitorTransaction({
135135
to: '0x1234567890123456789012345678901234567890',
136136
data: '0x',
137137
value: 0n,
@@ -154,6 +154,7 @@ describe('GasUtils', () => {
154154
// First deploy without any buffer
155155
const baselineGasUtils = new GasUtils(
156156
publicClient,
157+
walletClient,
157158
logger,
158159
{
159160
bufferPercentage: 0n,
@@ -168,7 +169,7 @@ describe('GasUtils', () => {
168169
},
169170
);
170171

171-
const baselineTx = await baselineGasUtils.sendAndMonitorTransaction(walletClient, account, {
172+
const baselineTx = await baselineGasUtils.sendAndMonitorTransaction({
172173
to: EthAddress.ZERO.toString(),
173174
data: SIMPLE_CONTRACT_BYTECODE,
174175
});
@@ -181,6 +182,7 @@ describe('GasUtils', () => {
181182
// Now deploy with 20% buffer
182183
const bufferedGasUtils = new GasUtils(
183184
publicClient,
185+
walletClient,
184186
logger,
185187
{
186188
bufferPercentage: 20n,
@@ -195,7 +197,7 @@ describe('GasUtils', () => {
195197
},
196198
);
197199

198-
const bufferedTx = await bufferedGasUtils.sendAndMonitorTransaction(walletClient, account, {
200+
const bufferedTx = await bufferedGasUtils.sendAndMonitorTransaction({
199201
to: EthAddress.ZERO.toString(),
200202
data: SIMPLE_CONTRACT_BYTECODE,
201203
});

yarn-project/ethereum/src/gas_utils.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { sleep } from '@aztec/foundation/sleep';
44
import {
55
type Account,
66
type Address,
7+
type Chain,
78
type Hex,
9+
type HttpTransport,
810
type PublicClient,
911
type TransactionReceipt,
1012
type WalletClient,
@@ -80,6 +82,7 @@ export class GasUtils {
8082

8183
constructor(
8284
private readonly publicClient: PublicClient,
85+
private readonly walletClient: WalletClient<HttpTransport, Chain, Account>,
8386
private readonly logger?: DebugLogger,
8487
gasConfig?: GasConfig,
8588
monitorConfig?: L1TxMonitorConfig,
@@ -102,25 +105,21 @@ export class GasUtils {
102105
* @returns The hash of the successful transaction
103106
*/
104107
public async sendAndMonitorTransaction(
105-
walletClient: WalletClient,
106-
account: Account,
107108
request: L1TxRequest,
108109
_gasConfig?: Partial<GasConfig>,
109110
_monitorConfig?: Partial<L1TxMonitorConfig>,
110111
): Promise<TransactionReceipt> {
111112
const monitorConfig = { ...this.monitorConfig, ..._monitorConfig };
112113
const gasConfig = { ...this.gasConfig, ..._gasConfig };
113-
114+
const account = this.walletClient.account;
114115
// Estimate gas
115116
const gasLimit = await this.estimateGas(account, request);
116117

117118
const gasPrice = await this.getGasPrice(gasConfig);
118119
const nonce = await this.publicClient.getTransactionCount({ address: account.address });
119120

120121
// Send initial tx
121-
const txHash = await walletClient.sendTransaction({
122-
chain: null,
123-
account,
122+
const txHash = await this.walletClient.sendTransaction({
124123
...request,
125124
gas: gasLimit,
126125
maxFeePerGas: gasPrice,
@@ -163,15 +162,19 @@ export class GasUtils {
163162

164163
// Check if current tx is pending
165164
const tx = await this.publicClient.getTransaction({ hash: currentTxHash });
166-
if (tx) {
167-
this.logger?.debug(`L1 Transaction ${currentTxHash} pending`);
165+
166+
// Get time passed
167+
const timePassed = Date.now() - lastSeen;
168+
169+
if (tx && timePassed < monitorConfig.stallTimeMs) {
170+
this.logger?.debug(`L1 Transaction ${currentTxHash} pending. Time passed: ${timePassed}ms`);
168171
lastSeen = Date.now();
169172
await sleep(monitorConfig.checkIntervalMs);
170173
continue;
171174
}
172175

173-
// tx not found and enough time has passed - might be stuck
174-
if (Date.now() - lastSeen > monitorConfig.stallTimeMs && attempts < monitorConfig.maxAttempts) {
176+
// Enough time has passed - might be stuck
177+
if (timePassed > monitorConfig.stallTimeMs && attempts < monitorConfig.maxAttempts) {
175178
attempts++;
176179
const newGasPrice = await this.getGasPrice();
177180

@@ -181,9 +184,7 @@ export class GasUtils {
181184
);
182185

183186
// Send replacement tx with higher gas price
184-
currentTxHash = await walletClient.sendTransaction({
185-
chain: null,
186-
account,
187+
currentTxHash = await this.walletClient.sendTransaction({
187188
...request,
188189
nonce,
189190
gas: gasLimit,
@@ -211,7 +212,7 @@ export class GasUtils {
211212
private async getGasPrice(_gasConfig?: GasConfig): Promise<bigint> {
212213
const gasConfig = { ...this.gasConfig, ..._gasConfig };
213214
const block = await this.publicClient.getBlock({ blockTag: 'latest' });
214-
const baseFee = block.baseFeePerGas ?? 0n; // Keep in Wei
215+
const baseFee = block.baseFeePerGas ?? 0n;
215216
const priorityFee = gasConfig.priorityFeeGwei * WEI_CONST;
216217

217218
// First ensure we're at least meeting the base fee

0 commit comments

Comments
 (0)