Skip to content

feat(cheatcode): add vm.waitForTransaction for getting tx and receipt info in scripts #3589

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
mds1 opened this issue Oct 31, 2022 · 3 comments
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge T-feature Type: feature

Comments

@mds1
Copy link
Collaborator

mds1 commented Oct 31, 2022

Component

Forge

Describe the feature you would like

Problems

First problem: In scripts, there's a frontrunning footgun you have to be careful of. Here's an example with pseudocode:

  • Transaction 1: Call gnosisSafeFactory.createSafe() to deploy a safe with CREATE (so contract nonce matters)
  • Transaction 2: Transfer 10 DAI to the safe

Forge will simulate tx1, get the address of the safe, and send tokens to that in tx2. But if someone else deploys a gnosis safe in between, the address of your safe changes and you send tokens to the wrong address. I've encountered this on mainnet, so it's not just theoretical 🙂

(Note that an alternative solution to the first problem is to have --slow wait for the tx to be mined, then requery for all data before the next transaction)

Second problem: The default broadcast artifacts are not ideal for everyone. For example, if you have upgradeable contracts, you may want <latest-ContractX>.json for all your contract deploys, instead of one JSON file for each script execution. This is doable by using the file writing cheats within a script, but currently you can't access tx and receipt info so you have more limited logging capabilities than forge itself.

Solution

function waitForTransaction() external returns (Tx memory tx, Receipt memory receipt);

(or if ethers-rs has a function of the same purpose with a different name, the cheatcode can match that name)

This cheatcode can be called after each broadcasted transaction. It will wait for the transaction to be mined, return the tx and receipt data, then continue.

One issue here is the returned structs will differ depending on if the tx is type 0, 1, or 2. As a result we probably need to just return bytes memory for both tx and receipt, and use the existing JSON helper structs with forge-std methods to help decode the transactions, which is ok.

One other issue is that this is the first cheatcode that would be "script only", i.e. this wouldn't make much sense in tests, though I guess it could work there too but for all transactions?

Additional context

No response

@PatrickAlphaC
Copy link

Additionally, this would be helpful for verifiers. Right now a forge script waits for 1 transaction receipt, but I often find that 6 is a safer amount for a block explorer to have indexed the contract& transaction.

@PatrickAlphaC
Copy link

PatrickAlphaC commented Apr 15, 2023

Or, I'd love to see this option too:

forge script script/MyScript.s.sol --confirmations 6

and it waits 6 confirmations before trying to verify

@mds1
Copy link
Collaborator Author

mds1 commented Apr 16, 2023

Just noting that we're now tracking that confirmations suggestion in #4748 (thanks for creating the issue!)

@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@zerosnacks zerosnacks changed the title feat: vm.waitForTransaction for getting tx and receipt info in scripts feat(cheatcode): add vm.waitForTransaction for getting tx and receipt info in scripts Aug 7, 2024
@grandizzy grandizzy removed this from the v1.0.0 milestone Oct 17, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-cheatcodes Area: cheatcodes C-forge Command: forge T-feature Type: feature
Projects
Archived in project
Development

No branches or pull requests

5 participants