0

I am using REMIX IDE, and running the following codes to learn about the gas limit.

GAS LIMIT IS 3000000

First I ran this code,

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.3;

contract gas{
    uint public i=0;

    function runforever() public{
        while(true){
            i=i+1;  // infinite loop
        }
    } 
}

It took about few minutes then got output as

transact to gas.runforever pending ... 
transact to gas.runforever errored: VM error: out of gas.

out of gas
    The transaction ran out of gas. Please increase the Gas Limit.

Debug the transaction to get more information.
[vm]from: 0x5B3...eddC4to: gas.runforever() 0xd91...39138value: 0 weidata: 0x9ac...cf33elogs: 0hash: 0x476...4df65
status  false Transaction mined but execution failed
transaction hash    0x47641a740be683f1bb332bfa1912ad56ecbc20eeb117720228fb12f517d4df65
block hash  0x8db7f3a41ec75424681633f64942dc8a4d78583fbc6e76dc8207e00150233836
block number    2
from    0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
to  gas.runforever() 0xd9145CCE52D386f254917e481eB44e9943F39138
gas 3000000 gas
transaction cost    3000000 gas 
execution cost  2978936 gas 
input   0x9ac...cf33e
decoded input   {}
decoded output  {}
logs    []
val 0 wei

Again, I ran the following code

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.3;

contract infiView{
    function loop(uint x) public pure{
        while(true){
            x=x+1;
        }
    }
}

Now I got the output in few seconds

call to infiView.loop
CALL
[call]from: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4to: infiView.loop(uint256)data: 0x0b7...00001
from    0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
to  infiView.loop(uint256) 0xf8e81D47203A594245E36C48e151709F0C19fBe8
execution cost  2978796 gas (Cost only applies when called by a contract)
input   0x0b7...00001
decoded input   {
    "uint256 x": "1"
}
decoded output  {}
logs    []
call to infiView.loop errored: VM error: out of gas.

out of gas
    The transaction ran out of gas. Please increase the Gas Limit.

Debug the transaction to get more information.

But to my surprise, I found that no amount of ethers has been deducted from the account, but earlier in the first code there was a significant deduction in ethers. Also, according to this answer pure functions also cost gas fee, when they execute Ethereum opcodes.

  1. Why there is no deduction in amount of Ethers in case of second code?
  2. Why there is difference in the amount of time, both code take to get executed?

1 Answer 1

1
  1. Pure functions only cost gas if you are calling them from inside another write function. Imagine you have a write function as mint and you call the above-mentioned loop function inside the mint then it will cost gas. But if you directly call the loop function then it won't cost any gas.

  2. There's a difference in time because, in the first function, you are reading a state variable. and in the second one, you don't.

3
  • The pure function didn't cost any gas as it is not called from inside another write function, that's why there is no deduction in total ethers. But why in the output of second code it is mentioned the transaction ran out of gas ? Aug 10 at 6:32
  • If that answers your Question then make sure to mark it as answer. @NiteshModi Aug 11 at 3:16
  • The computation cost gas but is not deducted from the wallet. So who pays for the gas. Aug 11 at 8:41

Your Answer

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

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