Replies: 1 comment
-
|
In ethers v6, gas estimation for contract methods uses the Basic gas estimationimport { Contract, JsonRpcProvider } from 'ethers';
const provider = new JsonRpcProvider('http://localhost:8545');
const contract = new Contract(contractAddress, abi, provider);
// Estimate gas for a specific function call
const gasEstimate = await contract.transfer.estimateGas(
'0xRecipient',
ethers.parseUnits('100', 18)
);
console.log('Gas estimate:', gasEstimate.toString());With a signer (for state-changing calls)const signer = await provider.getSigner();
const contract = new Contract(contractAddress, abi, signer);
const gas = await contract.swap.estimateGas(
tokenIn,
tokenOut,
amountIn,
minAmountOut,
{ value: ethers.parseEther('0.1') } // if payable
);Dry-run first with
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,I have a hardhat project that have a file that does this
const arbitrage = new ethers.Contract(config.PROJECT_SETTINGS.ARBITRAGE_ADDRESS, IArbitrage.abi, provider);and it is imported in another js file and that file has a function that wants to estimate gas for a specific function inside the contract.sol file:I have logged the method
estimateGas()and theexecuteTrade()they both existI also checked for
executeTrade()method in my abi and it exist but I'm getting this error:⚠️ Profitability check failed: Cannot read properties of undefined (reading 'executeTrade')"},I'm usingEthers version: 6.15.0,I logged it to make sure that I'm correct.I'm usingexecuteTrade()in an another function in the same file and I'm not getting any error from it.Now what is the correct way to
estimateGas()?Beta Was this translation helpful? Give feedback.
All reactions