How to calculate gas fee for a smart-contract on Tezos
Predicting gas fee for a transfer has always been a question of doubt i.e there is no blog or perfect answer on any online medium or even the official developers portal of tezos. I did some digging and came up with the following
method to calculate the gas fee. Before moving further we need to understand what exactly is gas fee or why do we need it.
A gas fee is the fee that you pay the miner/baker to propagate and include operations into blocks, the baker and mempool filters require a minimal fee to propagate and include operations into blocks. This default is not set at the protocol level but rather in the configuration of the node and the baker. Bakers can thus decide of the settings that work best for them
The minimal fee depends on the operation sent (transaction, origination, revelation, etc)
When considering the injection of an operation in a block, the baker will check its size and gas and reject it if the associated fees are too low. The expected fees are computed using this formula:
fees >= (minimal_fees + minimal_nanotez_per_byte * size + minimal_nanotez_per_gas_unit * gas)
Where the size is the number of bytes of the complete serialized operation, i.e. including header and signature. When sending multiple transactions at once (i.e. packed operations), the baker will require the summed fees of all the operations to match the summed gas of all the operations and the total size of the packed operations, still including header and signature.
By default:
minimal_fees = 0.000 1ꜩ (100µꜩ)
minimal_nanotez_per_gas_unit = 100nꜩ/gu (0.000 000 1ꜩ/gu)
minimal_nanotez_per_byte = 1000nꜩ/B (0.000 001ꜩ/B)
For instance, a single transaction to an existing implicit address will require a transaction fee of at least 0.001 273ꜩ to be included by bakers who choose to follow the default settings.
Coming onto the coding part i.e how would we implement this logic in our code. I have written a simple JavaScript class to calculate gas fee for both transaction as well as for smart-contract origination operation. Follow the below link to implement it in your code-base.
https://github.com/agauti003/tezos-smart-contract-gas-fee/blob/main/gas-fee-calculator.js
Once you traverse to the the above link follow the below steps to calculate the gas fee for transfers/smart-contract origination operation.
- Examples of use, estimate a transfer operation Assuming that provider and signer are already configured.
const amount = 2;
const address = 'tz1h3rQ8wBxFd8L9B3d7Jhaawu6Z568XU3xY';
// Estimate gasLimit, storageLimit and fees for a transfer operation
const est = await Tezos.estimate.transfer({ to: address, amount: amount })
console.log(est.burnFeeMutez, est.gasLimit, est.minimalFeeMutez, est.storageLimit,
est.suggestedFeeMutez, est.totalCost, est.usingBaseFeeMutez)
- Examples of use, estimate a contract origination :
const est = await Tezos.estimate.originate({
code: michelsonCode,
storage: {
stored_counter: 0,
threshold: 1,
keys: ['edpkuLxx9PQD8fZ45eUzrK3BhfDZJHhBuK4Zi49DcEGANwd2rpX82t']
}
})
console.log(est.burnFeeMutez, est.gasLimit, est.minimalFeeMutez, est.storageLimit,
est.suggestedFeeMutez, est.totalCost, est.usingBaseFeeMutez)
That’s it it’s as simple as that. Please like and clap if you loved my article.