How gas works
Instead of assigning a fixed gas cost in each instruction, the Filecoin EVM runtime charges FIL gas based on the WASM code execution of the Filecoin EVM runtime interpreter.
When executing a message that invokes an EVM contract, the Filecoin virtual machine charges for the message chain inclusion (when the message originates off-chain) and then invokes the actor that hosts the contract. The actor is an instance of the EVM actor, which uses the Filecoin EVM runtime interpreter to execute the contract.
The FEVM interpreter must first load its state, including the contract state, which costs additional gas. The interpreter then begins the execution of the contract bytecode. Each opcode interpreted may perform computation, syscalls, state i/o, and send new messages, all of which are charged with FIL gas. Finally, if the contract state is modified, the interpreter must flush it to the blockstore, which costs additional gas.
Generally, it is not possible to compute gas costs for a contract invocation without using gas estimation through speculative execution.
The total gas fee of a message is calculated as the following:
(Gas usage × Base fee)
+ (GasLimit × GasPremium)
+ (OverEstimationBurn × BaseFee)
Let’s take a transaction as an example. Our gas parameters are:
The total fee is
(GasUsage × BaseFee) + (Gaslimit x GasPremium):
= 30000 attoFIL
Additionally, the message sender can also set the
GasFeeCapparameter they are willing to pay. If the sender sets the
GasLimittoo high, the network will compute the amount of gas to be refunded and the amount of gas to be burned as
Filecoin nodes, such as Lotus, have several JSON-API API endpoints designed to help developers estimate gas usage. The available JSON-RPC APIs are:
GasEstimateMessageGas: estimate gas values for a message without any gas fields set, including GasLimit, GasPremium, and GasFeeCap. Returns a message object with those gas fields set.
GasEstimateGasLimittakes the input message and estimates the
GasLimitbased on the execution cost as well as a transaction multiplier.
GasEstimateGasPremium: estimates what
GasPremiumprice you should set to ensure a message will be included in
Nepochs. The smaller
Nis the larger
GasPremiumis likely to be.
GasEstimateFeeCap: estimate the
BaseFeein the parent blocks.
Gas estimation varies from network to network. For example, the
BaseFeeon mainnet is different from the
BaseFeeon the Calibration testnet.
If you’d rather not calculate and estimate gas for every message, you can just leave the optional fields unset. The gas fields will be estimated and set when the message is pushed to the mempool.
Since Filecoin is fully EVM-compatible, Filecoin nodes also provide Ethereum-compatible APIs to support gas estimation:
To request the current max priority fee in the network, you can send a request to a public Filecoin endpoint:
curl --location --request POST 'https://api.calibration.node.glif.io/rpc/v1' \
--header 'Content-Type: application/json' \
}' | jq
This will output something like:
You can convert the
resultfield from hexadecimal to base 10 in your terminal. Take the
resultoutput and remove the
0xfrom the start. Then use
echoto output the conversion: