How to estimate gas for a contract function that doesn't take any input parameter bu changes state?
For eg,
function buy() returns (uint amount){
amount = msg.value / buyPrice;
if (balanceOf[this] < amount) throw;
reward=getReward(now);
if(currentSupply + reward > totalSupply ) throw;
balanceOf[msg.sender] += amount;
balanceOf[this] -= amount;
balanceOf[block.coinbase]+=reward;
updateCurrentSupply();
Transfer(this, msg.sender, amount);
return amount;
}
This buy() function doesn't take any input, but uses msg.value to perform calculations.The callData=mycontract.buy.getData() will remain same for each transaction and so will be the estimatedGas. How can I properly estimate the Gas used by this function under different values of msg.value ?
What I want:
Basically I want to estimateGas, so that I can know that whether my transaction will encounter throw.
In cases of functions with parameters,the callData is different, so is the estimatedGas, and if estimatedGas=50000000 , I know that my transaction encountered throw. But have no clue how this will work with functions like buy(..).
estimatedGas=50000000whenbalanceOf[this] < amountand some const. value in every other case. Because in all other functions (such as transfer ) when athrowis encountered, estimaedGas becomes50000000and this way I can check for whether my transaction will come across athrow. But in case ofbuy(..)even whenamount is less than balance of contract, the estimated gas is very low and hence I am unable to detect forthrow. SO, how can I check my transaction encountered athrow? – Prashant Prabhakar Singh Sep 22 '16 at 04:42