I have a function that iterate through an array, which could cost a lot of gas. But I still want to test out the estimate gas cost, then decide if I should maintain the design or not.
function giveAwayDividend(uint amount) onlyOwner payable {
for(uint i=0;i<size();i++){
customerAddress[i].call.value((balances[customerAddress[i]] * amount * 100) / totalSupply)();
}}
If I test out on testnet, I have to manually create over 1 thousand user account and send each of them some token, which seems stupid. Is there a better way to calculate the gas cost?
For example, if the cost is linear, I could calculate the cost for one customer then multiple by the number of customers. Question is, I dont think its linear, can someone shed some light on this?
Best Answer
Using Truffle and testrpc. It's actually pretty easy to build a development environment and test different use cases.
For the gas estimation, it's mostly based on Web3 native functions:
You can retrieve the gas price (in wei) using
web3.eth.getGasPrice
The function
estimateGas
will give the gas estimation for a function (with the parameters passed)Multiply number of gas by gas price to get the gas cost estimation.
For example
Result in my case (private network):