contract Counter {
uint256 value = 0;
function addOne() external {
value += 1;
}
}
contract MyContract {
Counter a = .....;
Counter b = .....;
// .......
function myFunc() external {
// ......
a.addOne();
b.addOne();
}
}
What will happen if gas uses out while on executing b.addOne()
?
If a.value
will add one?
How can I avoid it?
Gas is the currency used to incentivize miners to execute OPCODEs on the EVM. Each transaction on Ethereum is ATOMic, meaning every OPCODE in the transaction must run otherwise the entire transaction is rejected. So if you run out of gas, your transaction will simply revert
.
You can avoid this by using a gas estimator (e.g. Remix has this integrate out of the box) or doing it by hand (since every OPCODE's gas requirement is known, and so is the gas price, even if it does fluctuate moment to moment).
Then when you make the call, you specify how much gas to use eg: a.addOne{gas: 69696969 }()
If you send too much, the unspent gas will be returned to the caller.