foundry: bug(`forge script`): tx succeeds during simulation but fails on any regular function call that occurs in a script after contract creation
Component
Anvil
Have you ensured that all of these are up to date?
- Foundry
- Foundryup
What version of Foundry are you on?
forge 0.2.0 (0962fd3 2022-06-16T18:19:05.497315Z)
What command(s) is the bug in?
No response
Operating System
macOS (Apple Silicon)
Describe the bug
Executing a contract func that updates storage on a contract that doesn’t have a constructor fails on a forked instance of optimism or arbitrum (using anvil). On mainnet and polygon forks, there’s no issue.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Contract {
address public test;
function initialize(address _test) external {
test = _test;
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "forge-std/Script.sol";
import {Contract} from "src/Contract.sol";
contract ContractScript is Script {
Contract public testContract;
function run() public {
vm.broadcast();
testContract = new Contract();
vm.broadcast();
testContract.initialize(address(1));
}
}
What’s interesting is the simulation is successful, but the actual broadcast tx fails:
$ forge script script/Contract.s.sol --rpc-url "http://127.0.0.1:8545" --private-key $ANVIL_ACCT_9 --broadcast -vvvv [14:41:16]
[⠒] Compiling...
Nothing to compile
Traces:
[93261] ContractScript::run()
├─ [0] VM::broadcast()
│ └─ ← ()
├─ [49699] → new Contract@0x700b6a60ce7eaaea56f065753d8dcb9653dbad35
│ └─ ← 248 bytes of code
├─ [0] VM::broadcast()
│ └─ ← ()
├─ [2502] Contract::initialize(0x0000000000000000000000000000000000000000)
│ └─ ← ()
└─ ← ()
Script ran successfully.
Gas used: 93261
==========================
Simulated On-chain Traces:
[106971] → new Contract@0x700b6a60ce7eaaea56f065753d8dcb9653dbad35
└─ ← 248 bytes of code
[23694] Contract::initialize(0x0000000000000000000000000000000000000000)
└─ ← ()
==========================
Estimated total gas used for script: 131151
Amount required: 0.000262302 ETH
==========================
###
Finding wallets for all the necessary addresses...
##
Sending transactions [0 - 1].
⠉ [00:00:00] [#################################################################################################################################################################################################################################################] 2/2 txes (0.0s)
Transactions saved to: broadcast/Contract.s.sol/10/run-latest.json
##
Waiting for receipts.
⠙ [00:00:07] [#############################################################################################################################################################################################################################################] 2/2 receipts (0.0s)
#####
✅ Hash: 0x616898701db8efc33e972bfb36467311b28cfc49ed863f70dfe2f4a1d48a4c71
Contract Address: 0x700b6a60ce7eaaea56f065753d8dcb9653dbad35
Block: 12064970
Paid: 0.000213942 ETH (106971 gas * 2 gwei)
#####
❌ Hash: 0x03db02d9ff00fb3f4e1c6550670aa6c57e8a9361d1e406a34e391e0de5ee0b21
Block: 12064971
Paid: 0.0000469 ETH (23450 gas * 2 gwei)
Transactions saved to: broadcast/Contract.s.sol/10/run-latest.json
Error:
0: ["Transaction Failure: 0x03db…0b21"]
Location:
cli/src/cmd/forge/script/receipts.rs:75
If you remove logic that updates any storage state, the tx succeeds.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 16 (7 by maintainers)
Per @mds1 suggestion let’s go with the following 2 steps:
1 Add a broadcast overload that lets you specify the gas limit for the tx,
vm.broadcast(uint256 gasLimit)andvm.broadcast(address sender, uint256 gasLimit)2. add bespoke logic for each L2, determined by chainId, this will need to be fork-aware when #1715 is mergedcc @joshieDo
+1 on @devanonon’s comment, haven’t gotten around to opening issues yet but here’s a related repro with other issues too: https://github.com/mds1/forge-trace-and-script-issue
Of the 4 items listed in my above comment, I believe 1 and 2 are implemented, 3 and 4 are not.
Just summarizing what we ended up on as the path forward:
has_different_gas_calcfunctionhttps://github.com/foundry-rs/foundry/blob/112bd440c7cc83480612c5e05d71229ceeba7178/cli/src/cmd/forge/script/broadcast.rs#L275-L277 https://github.com/foundry-rs/foundry/blob/123ad0a427dc938a0a911eae3332de6576370a51/cli/src/cmd/utils.rs#L199-L205
Force
--slowon Arbitrum and Optimism. Both of don’t have mempools like L1’s so if you send multiple txs they get rejected withnonce too low.Move RPC gas estimation right before sending the intended tx, and add a note to the output indicating the “total gas/ETH cost estimates” shown beforehand aren’t guaranteed to be accurate (since we don’t estimate Optimism costs correctly)
This one wasn’t settled on, but I think we should change the broadcast overloads to specify margin on the gas estimate, instead of absolute limits. So if I’m estimating gas for a uniswap v2 trade where cost varies by block position, I can do
vm.broadcast(20)to mean “add 20% margin to the gas estimate”. IMO this is better UX than needing to harcode gas limits directly.This issue still exists, and I believe it applies to any regular function call that occurs in a script after contract creation (i.e. not necessarily limited to an initialization call). I used
--with-gas-multiplierto resolve it.