Estimate gas and fees
viem's estimation actions work as on any chain, against your node.
Gas and fees
const gas = await anvil.testClient.estimateContractGas({
account: alice,
address: token,
abi: exampleToken.abi,
functionName: "transfer",
args: [bob, 1n],
});
const { maxFeePerGas, maxPriorityFeePerGas } =
await anvil.testClient.estimateFeesPerGas();
const history = await anvil.testClient.getFeeHistory({
blockCount: 3,
rewardPercentiles: [50],
});
expect(gas).toBeGreaterThan(21_000n);
expect(maxFeePerGas).toBeGreaterThan(maxPriorityFeePerGas);
expect(history.baseFeePerGas.length).toBeGreaterThan(0);
Tested in examples/test/e2e/simulation-and-gas.e2e.test.ts:214
Access lists (EIP-2930)
const data = encodeFunctionData({
abi: exampleToken.abi,
functionName: "transfer",
args: [bob, 1n],
});
const { accessList, gasUsed } = await anvil.testClient.createAccessList({
account: alice,
to: token,
data,
});
expect(accessList[0].address.toLowerCase()).toBe(token.toLowerCase());
expect(accessList[0].storageKeys.length).toBeGreaterThan(0); // both balances
expect(gasUsed).toBeGreaterThan(0n);
const receipt = await anvil.confirm(
anvil
.testClientFor(alice)
.sendTransaction({ to: token, data, accessList }),
);
expect(receipt.type).toBe("eip1559");
Tested in examples/test/e2e/simulation-and-gas.e2e.test.ts:188
Let the node fill a transaction
const { transaction } = await anvil.testClient.fillTransaction({
account: alice,
to: bob,
value: 1n,
});
expect(transaction).toMatchObject({
nonce: await anvil.testClient.getTransactionCount({ address: alice }),
chainId: anvil.chainId,
type: "eip1559",
});
expect(transaction.gas).toBe(21_000n);
Tested in examples/test/e2e/simulation-and-gas.e2e.test.ts:237
Storage proofs
const totalSupplySlot = numberToHex(2, { size: 32 }); // 3rd state variable
const proof = await anvil.testClient.getProof({
address: token,
storageKeys: [totalSupplySlot],
});
expect(proof.storageProof[0].value).toBe(parseEther("10"));
expect(proof.accountProof.length).toBeGreaterThan(0);
Tested in examples/test/e2e/simulation-and-gas.e2e.test.ts:253
To control the fees themselves (base fee, gas price, tips), see Build blocks and Configure the chain.