Is it possible to check the overall gas price of a transaction before executing the contract? I'm trying to implement some arbitrage trade through a smart contract that would find me price discrepancies between sushiswap and uniswap. But I need to check before running the swap if the profit - the gas fees would be in the green, and if it's not, just do not run the transaction. Any advice would be greatly appreciated
1 Answer
You can use a library like ethers.js or web3.js to estimate the gas usage and cost of the transaction.
A sample code to achieve that using ethers.js:
Using ethers.js v5:
const { ethers } = require('ethers');
const contractAddress = "YOUR_CONTRACT_ADDRESS";
const contractAbi = [
/* YOUR_CONTRACT_ABI */
];
const provider = new ethers.providers.JsonRpcProvider("YOUR_RPC_URL");
const privateKey = "YOUR_ACCOUNT_PRIVATE_KEY";
const wallet = new ethers.Wallet(privateKey, provider);
const contract = new ethers.Contract(contractAddress, contractAbi, wallet);
contract.estimateGas.yourMethod(params).then(async (gasEstimate) => {
console.log('Gas Usage Estimate In Wei: ', parseFloat(gasEstimate.toString()));
const gasEstimateInEther = ethers.utils.formatUnits(gasEstimate);
console.log('Gas Usage Estimate In Ether: ', gasEstimateInEther);
const gasData = await wallet.getFeeData();
const estimatedCostInWei = (gasData.maxFeePerGas.sub(gasData.lastBaseFeePerGas)).mul(gasEstimate);
console.log('Tx Cost Estimate In Wei: ', parseFloat(estimatedCostInWei.toString()));
const estimatedCostInEth = ethers.utils.formatUnits(estimatedCostInWei);
console.log('Tx Cost Estimate In Ether: ', estimatedCostInEth);
// Actual call to the contract function
contract.yourMethod(params, {
gasPrice: gasData.gasPrice,
gasLimit: gasEstimate
}).then(async (tx) => {
const receipt = await tx.wait();
console.log(receipt.hash)
});
});
Using ethers.js v6:
const { ethers } = require('ethers');
const contractAddress = "YOUR_CONTRACT_ADDRESS";
const contractAbi = [
/* YOUR_CONTRACT_ABI */
];
const provider = new ethers.JsonRpcProvider("YOUR_RPC_URL");
const privateKey = "YOUR_ACCOUNT_PRIVATE_KEY";
const wallet = new ethers.Wallet(privateKey, provider);
const contract = new ethers.Contract(contractAddress, contractAbi, wallet);
contract.yourMethod.estimateGas(params).then(async (gasEstimate) => {
console.log('Gas Usage Estimate In Wei: ', parseFloat(gasEstimate.toString()));
const gasEstimateInEther = ethers.formatUnits(gasEstimate);
console.log('Gas Usage Estimate In Ether: ', gasEstimateInEther);
const gasData = await provider.getFeeData();
const estimatedCostInWei = gasData.gasPrice * gasEstimate;
console.log('Tx Cost Estimate In Wei: ', parseFloat(estimatedCostInWei.toString()));
const estimatedCostInEth = ethers.formatUnits(estimatedCostInWei);
console.log('Tx Cost Estimate In Ether: ', estimatedCostInEth);
// Actual call to the contract function
contract.yourMethod(params, {
gasPrice: gasData.gasPrice,
gasLimit: gasEstimate
}).then(async (tx) => {
const receipt = await tx.wait();
console.log(receipt.hash)
});
});
-
1So i see where my issue was, I was looking at the v5 version of ethers when I was using the v6 version. The the estimateGas function is now called after the function name. So the proper way now to call is
myContract.myContractsFunction.estimateGas
– Esko918Commented Mar 21 at 21:42