On the Ethereum network, the gas fee is composed of a base fee and a priority fee.
The base fee is retrieved from the network and is based on the network workload. MetaMask will call the eth_getBlockByNumber
method, this gives a lot of information about the block, including the base fee. You can retrieve the base fee also by calling the eth_gasPriceeth_getGasPrice
method.
Then the priority fee is what goes to the miner. Generally, 2 Gwei is considered the minimum needed for a miner to pick it up.
Then the acual ETH value used for gas is calculated by doing:
(base fee + priority fee) * gas limit
The gas limit in 21000 units for a regular transfer on the ETH network, but it can be retrieved from the network using the eth_estimateGas
method.
I have a repo that shows all this in Python using web3.py if you want to see how it works!