foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Allow setting gas limits for individual transactions inside scripts using solidities `{gas: x}` syntax

Open ckoopmann opened this issue 2 years ago • 4 comments

Component

Forge

Describe the feature you would like

Summary

Allow the user to hardcode gas limits on individual transactions inside a forge-script using standard solidity syntax. This hardcoded value should override / have preference over existing gas estimation logic.

Example

In the below example the first call to someFunction should be executed with exactly the specified gas limit both in simulation as well as on broadcast.

The second call to this function (as well as the contract deployment) should have its gas limit estimated the same way as usual.

contract SomeContract {
    function someFunction() external {
    }
}

contract SomeScript is Script {

       function run() public {
            vm.startBroadcast();
            SomeContract someContract = new SomeContract();
            someContract.someFunction{gas: 12345}();
            someContract.someFunction();
            vm.stopBroadcast()
       }

}

Additional context

No response

ckoopmann avatar Aug 05 '22 03:08 ckoopmann

+1 to the issue, but instead of introducing new syntax, perhaps vm can respect the ether balance of an address?

For e.g., following should throw meaningful "not enough gas"

contract SomeContract {
    function someFunction() external {
    }
}

contract SomeScript is Script {
       function run() public {
           SomeContract someContract = new SomeContract();
            vm.deal(address(0), 0);
            vm.prank(address(0));
            someContract.someFunction();
       }
}

1-om avatar Aug 07 '22 23:08 1-om

These are different: your example gives the account a balance, @ckoopmann's example uses native Solidity syntax to specify the gas limit of a transaction

onbjerg avatar Aug 08 '22 17:08 onbjerg

I had a problem today with the lack of this feature. The pattern checks for enough remaining gas in someFunction, and it passes the trace and the on-chain simulation but fails on the broadcast. For now, I solved the problem by specifying --gas-estimate-multiplier (#2524) at runtime.

minaminao avatar Aug 16 '22 16:08 minaminao

+1 would love to have this!

jpiabrantes avatar Sep 16 '22 11:09 jpiabrantes

This would be nice when using --skip-simulation and gas estimation fails. Gas estimation failure happens frequently with txs that may fail due to randomness, etc. but you may want to attempt the submission regardless.

plotchy avatar Sep 22 '22 05:09 plotchy