Estimating fees
The Etherlink gas price (and therefore the fee for a given transaction) varies based on the activity on the chain. As activity increases, fees increase, and vice versa.
As described in Fee structure, Etherlink fees include the cost of running the transaction and writing the transaction to layer 1 but not a voluntary tip.
You can use libraries such as ethers.js to estimate transaction fees. For an example, see Sending transactions.
Etherlink supports the standard EVM eth_gasPrice
endpoint to provide the current gas price, as in this example:
curl --request POST \
--url https://node.ghostnet.etherlink.com \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_gasPrice"
}
'
It returns the gas price plus a safety margin as a hexadecimal number, as in this example:
{"jsonrpc": "2.0", "result": "0x3b9aca00", "id": 1}
In this response, the hex number 0x3b9aca00
corresponds to the decimal number 1,000,000,000.
The gas price is given in units that are equivalent to wei on Ethereum, or 1-18 XTZ, which means that the current gas price is 1-9 XTZ, or 0.000000001 XTZ (equivalent to 1 gwei in Ethereum terms).
This gas price is the cost per unit of computation required by a transaction.
Versions of the EVM node before 0.25 had an issue with gas price estimation.
As intended by the EVM specification, the node's implementation of the eth_gasPrice
endpoint provides the current gas price.
If a wallet uses that information to calculate the cost of a transaction and the gas price goes up, the wallet might not include enough of a transaction fee.
In this case, the sequencer silently rejects the transaction.
Starting with version 0.25, the EVM node's implementation of the eth_gasPrice
endpoint includes an increased safety margin to help ensure that the transaction fee is sufficient.
However, wallets may still not include a high enough transaction fee if the gas price increases rapidly or if they do not check the gas price often enough.
For greater control, wallets and dApps can call the eth_getBlockByNumber
endpoint, which includes the base fee for transactions in the baseFeePerGas
field.
If you are using an earlier version of the EVM node, you can increase the transaction fee to ensure that the sequencer will accept your transaction.
Because the sequencer rejects these transactions silently, wallets may resubmit the transaction automatically, still with insufficient transaction fees.
Most wallets periodically check the status of submitted transactions via the eth_getTransactionByHash
and eth_getTransactionReceipt
endpoints and update their information to show that they have failed, but it can take time before the wallet shows that the transaction has failed.
If the gas price drops, the sequencer may eventually accept the transaction, but the better solution is to use the wallet's "speed up" function (available in most popular supported wallets), and increase the maximum base gas fee.
To calculate the fee estimate for a given transaction, you can send the transaction to the eth_estimateGas
endpoint to obtain the estimated gas usage for the the transaction (as in this example), and then calculate the total expected gas fee:
curl --request POST \
--url https://node.ghostnet.etherlink.com \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_estimateGas",
"params": [
{
"to": "0x46899d4FA5Ba90E3ef3B7aE8aae053C662c1Ca1d",
"gas": "0x0",
"gasPrice": "0x3b9aca00",
"value": "0xde0b6b3a7640000",
"data": "0x"
}
]
}
'
The response is the estimated gas units consumed by the transaction in hexadecimal format, as in this example:
{
"jsonrpc": "2.0",
"result": "0x98496",
"id": 1
}
In this case, the estimated gas usage for the transaction is 623766 gas units. To get the total fee, multiply the gas price by this gas usage estimate. If the gas price is 1 gwei, the total fee estimate is 1-9 x 623766, or 0.000623766 XTZ. This fee includes all fees described in Fee structure, but you can set a higher amount for the transaction's gas consumption as a safety margin (sometimes known as setting the gas limit) to ensure that if the transaction requires more gas, it can use it.
You can also set a maximum base gas price for the transaction when submitting the transaction to increase the chances that it will be included in a block or to make it fail if the gas price goes up.
The example in Sending XTZ sets a maximum gas price by setting the maxFeePerGas
field on the transaction object.