1

I have a payable function in solidity which distributes the ether to various accounts. The Ether flow works as Follows

MetaMask(Ether in M.M)--->SmartContract(goes to the smart contract )---->To all the accounts

I saw that the await contract2.estimateGas.myPayableFunction() in ethers.js shows different values when i execute it before transferring the ethers in the smart contract and after transferring the ethers.(it is run on the same function which is myPayableFunction())

Before transferring the eth to the contract await contract2.estimateGas.myPayableFunction() 70116 Gas

After transferring the eth to the contract await contract2.estimateGas.myPayableFunction() 117666.85 Gas

I am not really clear , why is it so happens.

1 Answer 1

0

The gas consumed depends on the contract execution and the contract state. For example if you perform some action depending on the contract's balance it will give you different gas consumption if you received an ether deposit or not.

function foo() public {
    if (address(this).balance > 0) {
        for (int i=0; i<100; i+=1) {
            // Do something expensive
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.