Estimate gas
Overview
Ethereum (ETH) fees, also referred to as gas
and gasPrice
, are calculated based on the amount of network activity and the network resources required.
The fee rate dictates the speed at which your transaction will be picked up and confirmed by the blockchain. You can either use a custom fee to set the amount of gas
and gasPrice
manually in the transaction parameters, or let the SDK estimate gas automatically when creating a transaction. When setting a tentative fee rate, the fee amount is an estimate only. Gas fee calculation is not finalized until your transaction is signed.
MPC also supports the EIP-1559 standard for fee estimation:
- Maximum priority fee: Transaction time takes priority over gas fee price.
- Max gas fee: The gas fee max price is preset and takes priority over transaction time.
Estimate network fee
- Web
- Unity
You can see the network fee estimates in your fee values before creating transaction data.
This example shows how to estimate the gasPrice network fee for the Saigon testnet using the getNetworkFee
method:
...
// Get gasPrice
const gasPrice : string = await mpcCore.getNetworkFee();
console.log(gasPrice); // 0x4a817c800 (20 Gwei) in Saigon testnet
...
To calculate gas
and gasPrice
, mpcCore
uses the blockchain network specified during initialization.
The gasPrice
network fee value is returned together with gas
when calling the functions for estimating transaction fees.
Estimate transaction fee
- Web
- Unity
To estimate gas of a transaction, use the estimateGas
method:
const gas: string = await mpcCore.estimateGas(tx);
//...
This example shows how to estimate gas for a transaction:
const tx = {
"data": "0x",
"to": "0xedb40e7abaa613a0b06d86260dd55c7eb2df2447",
"value": "0x16345785d8a0000"
};
const gas: string = await mpcCore.estimateGas(tx);
console.log(gas); // 0xa410
//...
Within the Ronin network, it's typical to require a gas limit that exceeds the estimateGas
standard. This often involves using a coefficient that's 1.5 or 2 times larger than the standard estimate. In complex transaction calls to a contract, you should increase the gas 2 times or more to ensure transaction execution.
This example shows how to increase a gas limit:
import { JsonRpcProvider } from "@ethersproject/providers";
// Connect to provider - Saigon testnet
const rpc = new JsonRpcProvider(https://saigon-testnet.roninchain.com/rpc);
// Example transaction that sends 0.1 RON
const tx = {
"chainId": "0x7e5",
"from": "0x2082d1fedd8c2d2408c22869c49000f5e375ddbb",
"data": "0x",
"nonce": "0x5d",
"r": "0x0",
"s": "0x0",
"to": "0xedb40e7abaa613a0b06d86260dd55c7eb2df2447",
"v": "0x0",
"value": "0x16345785d8a0000"
};
// Estimate gas in BigNumber
const rawGas = await rpc.estimateGas(tx);
// Increase gas in testnet
const rawSafeGas = rawGas.mul(3); // Warning: multiply by 3 to overestimate gas
// Convert BigNumber to HexString
const gas = ethers.utils.hexValue(rawSafeGas);
Base method:
var (data, error) = await MPC.EstimateGas(from, to, value, data);
This example shows how to estimate gas for a transaction using EstimateGas
:
string from = "0xB980D30Dc0c318BccFf6C760eDf465Fa9D2950Bd";
string to = "0xaeD7F0FaEc15981835aaAeEd9c8D21327385743e";
string value = "0xde0b6b3a7640000"; // 1 RON
string data = "0x314da1c4000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001";
var (data, error) = await MPC.EstimateGas(from, to, value, data);
Estimate gas for transferring RON:
var (data, error) = await MPC.EstimateTransferNativeTokenGas(to, amount);
This estimate shows how to estimate gas for transferring 1 RON:
string to = "0xcd3cf91e7f0601ab98c95dd18b4f99221bcf0b20";
string amount = "0xde0b6b3a7640000"; // 1 RON
var (data, error) = await MPC.EstimateTransferNativeTokenGas(to, amount);
Estimate gas for transferring an ERC-20 token:
var (data, error) = await MPC.EstimateERC20TokenGas(selector, tokenAddress, to, amount);
This example shows how to estimate gas for transferring 1 AXS:
string selector = MPCConstants.kSignatureTransfer; // Function selector for transfer(address,uint256)
string tokenAddress = "0x3c4e17b9056272ce1b49f6900d8cfd6171a1869d";
string to = "0xcd3cf91e7f0601ab98c95dd18b4f99221bcf0b20";
string amount = "0xde0b6b3a7640000"; // 1 AXS
var (data, error) = await MPC.EstimateERC20TokenGas(MPCConstants.kSignatureTransfer, tokenAddress, to, amount);
This example shows how to estimate gas for authorizing a specific smart contract to spend 1 AXS:
string selector = MPCConstants.kSignatureApprove; // Function selector for approve(address,uint256)
string tokenAddress = "0x3c4e17b9056272ce1b49f6900d8cfd6171a1869d";
string to = "0xcd3cf91e7f0601ab98c95dd18b4f99221bcf0b20";
string amount = "0xde0b6b3a7640000"; // 1 AXS
var (data, error) = await MPC.EstimateERC20TokenGas(MPCConstants.kSignatureApprove, tokenAddress, to, amount);