How to estimate gas cost of the function

Hi,

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) {
addr.transfer(testVal);
return testVal;}
function deposit() payable public{}
}

I did the following:

TGC = TransferGC.at('0xE4F90Ef2e5815cf24eea956b1f5781601859cb90')
truffle(development)> await web3.eth.getGasPrice()
'20000000000'
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.
Zulfi.

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?

2 Likes

Hi,
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')
32204
truffle(development)> balance = await web3.eth.getBalance(TGC.address)
undefined
truffle(development)> web3.utils.fromWei(balance, "ether")
'97'
truffle(development)> b1 = await web3.eth.getBalance(acc1)
undefined
truffle(development)> web3.utils.fromWei(b1, "ether")
'99.99957588'

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?

Zulfi.