How to estimate gas cost of the function


I checked the following link: how to estimate gas cost?

My contract is:

pragma solidity ^0.5.1;
contract TransferGC{
uint public testVal = 97 ether;
function testFunc(address payable addr) public returns (uint) {
return testVal;}
function deposit() payable public{}

I did the following:

TGC ='0xE4F90Ef2e5815cf24eea956b1f5781601859cb90')
truffle(development)> await web3.eth.getGasPrice()
await TGS.testFunc('0x3e1D6ef576d19A6D28f3755acDb754E37aFe6E4A').estimateGas(1)

I am getting the following error:

Thrown: TypeError: TGC.testFunc(…).estimateGas is not a function

Somebody please guide me.

I think what does the method estimateGas do is to simulate the real action, it is just equal to web3.eth.sendTransaction, the only difference between them is estimateGas does not send the transaction out actually, and if your operation will fail when call a function, you can not get a valid result by estimateGas.

So does your contract have 97 ether?


Thanks for your response. How you know that the contract has 97 Ether? I actually I transferred 97 Ether from account1(acc1) yesterday and then locked my computer through out the rest of the day but today when I checked, I found that the acc1 has 99.999Ether.
Any way I am able to run estimateGas:

Truffle(development)> await TGC.testFunc.estimateGas('0x3e1D6ef576d19A6D28f3755acDb754E37aFe6E4A')
truffle(development)> balance = await web3.eth.getBalance(TGC.address)
truffle(development)> web3.utils.fromWei(balance, "ether")
truffle(development)> b1 = await web3.eth.getBalance(acc1)
truffle(development)> web3.utils.fromWei(b1, "ether")

So the situation is that the contract has 97Ether and the gas Estimate is 32204? Can the Smart Contract run the testFunc(…)?

Please guide me?