I would like to write a test in Foundry but I need to be able to first read data from a previous transaction that was submitted in some point in the past.
For example, the transaction I'm interested in was to some contract, the function called in that transaction was to deposit(address token, uint256 amount)
.
Is there a way for me to test:
- submit a transaction that will be tracked
- get that transaction's receipt/hash
- I know the transaction function signature (i.e.
deposit(address,uint256)
), look at what arguments the transaction was sent with. - Arguments are what I expected, e.g.
AssertEq(arg1, expectedAddress)
,AssertEq(arg2, expectedUint)
This is for a POC I'm trying to write while learning Foundry and writing tests.
Best Answer
Assuming that you are referring to a transaction that was included in previous blocks for a particular chain, you can get the transaction receipt using
cast
and decode the transaction data within the test itself (refer here). This coupled with foundryvm.ffi
will give you the test you need. For Eg : Here I have shown how to decode a simple ERC20transfer
transaction using cast within a forge test and then assert the decoded output.