foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.11k stars 1.67k forks source link

feat(cast): add `cast events <txHash>` to fetch and format events from receipt #2340

Open zobront opened 2 years ago

zobront commented 2 years ago

Component

Cast

Describe the feature you would like

I'm able to call cast receipt [txHash] logs to get the full logs of a transaction, but it's time consuming to manually parse the logs into legible events.

Seems helpful to be able to call cast events [txHash] to get a nicely formatted view of the events.

I'd love to build this out, as soon as we have agreement on the plan below.

Rough Plan

1) Grab the logs using cast.receipt() with field set to logs.

2) For each, grab topic[0] and run it against 4byte. If that fails, try pulling interface and comparing to event signatures from abi. If neither works, error out.

3) Convert topics & data into types defined by the event.

4) Print pretty output with event signature and correct data.

Additional context

No response

mds1 commented 2 years ago

Just noting this is similar to seth events, though that requires specifying an address, and optionally a block or --follow mode, instead of a transaction hash. I think supporting all of that would be great if feasible, but ok to split it over multiple PRs too. cc'ing @tynes here in case you've used seth events (I haven't) and have any thoughts.

zobront commented 2 years ago

Thanks @mds1 — that's a great point. Pulling from address & block or address & contract would be useful too. I'll look into the seth interface to see how they organized it.

Seems reasonable to include it all in this PR, except maybe the follow mode.

tynes commented 2 years ago

I haven't ever had luck with seth events actually working as expected. I do think that there is a good opportunity for creating a cast events command. There are times when I want to find an event by topic and want to see the abi decoded event data. There are other times when I want to see all of the events that a transaction logged. I also would find it useful to be able to get all events that match a boolean expression in a block range

zobront commented 2 years ago

It sounds like the ways we'd like to be able to filter are:

My instinct is with 4 inputs but no one that's required, the best option is to just have them all as options, and build validation for valid combinations into the function. For example:

cast events --txhash 0xd420fab45481f6337e06d0e0f503edc69e49781feee1062f33bd901a6981cdd5

cast events --contract 0x78D72E60BaE892F97b97fEBAE5886DaB2eF0cbC8 --start-block 15135280 --end-block 15135300

cast events --contract 0x78D72E60BaE892F97b97fEBAE5886DaB2eF0cbC8 --filter topic0=0x9d9af8e38d66c62e2c12f0225249fd9d721c54b83f48d9352c97c6cacdcb6f31

cast events --contract 0x78D72E60BaE892F97b97fEBAE5886DaB2eF0cbC8 --filter sig="Transfer(address,address,uint256)" --start-block 15135280 --end-block 15135300

Thoughts?

mds1 commented 2 years ago

That seems good to me!

I also would find it useful to be able to get all events that match a boolean expression in a block range

@tynes Do you have an example of what you mean here?