Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 7fb9636

Browse files
committed
Add Access List Docs
1 parent dd4aec0 commit 7fb9636

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

docs/docs/guides/04_transactions/gas-and-fees.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ With the above in mind, the total cost of fees associated with a transaction are
1313

1414
## Estimating Gas
1515

16-
The Ethereum JSON-RPC specifies the [eth_estimateGas](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_estimategas) method, which accepts a transaction and returns an estimate of the amount of gas required to execute that transaction. The transaction will not be executed and added to the blockchain. The estimate may be different (typically more) than the amount of gas actually used by the transaction for a variety of reasons, including EVM mechanics, node performance, and changes to the state of a smart contract. To invoke the `eth_estimateGas` RPC method, use the [`Web3Eth.estimateGas` method](/api/web3-eth/class/Web3Eth#estimateGas) and provide the [`Transaction`](/api/web3-types/interface/Transaction) for which to estimate gas.
16+
The Ethereum JSON-RPC specifies the [`eth_estimateGas`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_estimategas) method, which accepts a transaction and returns an estimate of the amount of gas required to execute that transaction. The transaction will not be executed and added to the blockchain. The estimate may be different (typically more) than the amount of gas actually used by the transaction for a variety of reasons, including EVM mechanics, node performance, and changes to the state of a smart contract. To invoke the `eth_estimateGas` RPC method, use the [`Web3Eth.estimateGas` method](/api/web3-eth/class/Web3Eth#estimateGas) and provide the [`Transaction`](/api/web3-types/interface/Transaction) for which to estimate gas.
1717

1818
Web3.js transactions may specifying a gas limit (the maximum amount of gas they are able to consume) by providing the [`Transaction.gas` property](/api/web3/namespace/types#gas). If the specified gas limit is less than the actual amount of gas required to execute the transaction, the transaction will consume an amount of gas equal to the gas limit, which is not refunded, before failing and reverting any state changes made by the transaction.
1919

@@ -50,3 +50,34 @@ const feeData: FeeData = await web3.eth.calculateFeeData();
5050
transaction.maxFeePerGas = feeData.maxFeePerGas;
5151
transaction.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas;
5252
```
53+
54+
## Generating Access Lists
55+
56+
An access list specifies the addresses and storage keys that a transaction plans to access. Specifying these elements in advance makes a transaction's gas costs more predictable.
57+
58+
The Ethereum JSON-RPC specifies the [`eth_createAccessList`](https://github.com/ethereum/execution-apis/blob/4140e528360fea53c34a766d86a000c6c039100e/src/eth/execute.yaml#L54-L97) method, which accepts a transaction and returns an object that lists the addresses and storage keys that the transaction will access, as well as the approximate gas cost for the transaction if the access list is included. The transaction will not be executed and added to the blockchain. To invoke the `eth_createAccessList` RPC method, use the [`Web3Eth.createAccessList` method](/api/web3-eth/function/createAccessList) and provide the [`TransactionForAccessList`](/api/web3-types/interface/TransactionForAccessList) for which to generate the access list.
59+
60+
Web3.js transactions may specifying an access list by providing the [`Transaction.accessList` property](/api/web3/namespace/types#accessList).
61+
62+
```ts
63+
const transaction: TransactionForAccessList = {
64+
from: '<SENDER ADDRESS>',
65+
to: '<RECEIVER ADDRESS>',
66+
value: web3.utils.ethUnitMap.ether,
67+
};
68+
69+
const accessListResult: AccessListResult = await web3.eth.createAccessList(transaction);
70+
transaction.accessList = accessListResult.accessList;
71+
transaction.gas = accessListResult.gasUsed;
72+
```
73+
74+
The following example demonstrates creating an access list for a transaction that invokes a smart contract function:
75+
76+
```ts
77+
const transfer: NonPayableMethodObject = erc20.methods.transfer('<RECEIVER ADDRESS>', 1);
78+
const transferOpts: NonPayableCallOptions = { from: '<SENDER ADDRESS>' };
79+
const transferTxn: Transaction = transfer.populateTransaction(transferOpts);
80+
const accessListResult: AccessListResult = await transfer.createAccessList(transferOpts);
81+
transferTxn.gas = accessListResult.gasUsed;
82+
transferTxn.accessList = accessListResult.accessList;
83+
```

0 commit comments

Comments
 (0)