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.
- Why there is no deduction in amount of Ethers in case of second code?
- Why there is difference in the amount of time, both code take to get executed?