Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(cast): get logs #5042

Merged
merged 10 commits into from
Jun 3, 2023
Merged

feat(cast): get logs #5042

merged 10 commits into from
Jun 3, 2023

Conversation

bernard-wagner
Copy link
Contributor

@bernard-wagner bernard-wagner commented May 25, 2023

Motivation

It is often very useful to watch for events or get logs using specific topics.

Solution

Added a command that works in two ways.

cast logs --from-block 1234122 --to-block latest --address 0x0000000 SIG [ARGS]
cast logs --from-block 1234122 --to-block latest --address somename.eth TOPIC_0 [TOPICS]

Example:

cast logs --from-block 15537393 --to-block latest 'Transfer (address indexed from, address indexed to, uint256 value)' 0x2e8ABfE042886E4938201101A63730D04F160A82

- address: 0xdAC17F958D2ee523a2206206994597C13D831ec7
  blockHash: 0xa23f0f6f8cd60e489a5a9853b56dbef9deabe3cd41c03431e5cbc981a3c29581
  blockNumber: 16895421
  data: 0x0000000000000000000000000000000000000000000000000000000028fa46c3
  logIndex: 350
  removed: false
  topics: [
  	0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
  	0x0000000000000000000000002e8abfe042886e4938201101a63730d04f160a82
  	0x000000000000000000000000e16dc9e130239d096d554a04ce6ddff5a78a774a
  ]
  transactionHash: 0xed77e9d350b15695f1d5e3a213c3aafee7055c422671e2779003f34cbebe1b19
  transactionIndex: 225
- address: 0xdAC17F958D2ee523a2206206994597C13D831ec7
  blockHash: 0x4e6b93493ce559a8264c82c8b3f8914c8ff0d381fd82c9e856c706d121c772b4
  blockNumber: 16895424
  data: 0x0000000000000000000000000000000000000000000000000000000000000000
  logIndex: 12
  removed: false
  topics: [
  	0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
  	0x0000000000000000000000002e8abfe042886e4938201101a63730d04f160a82
  	0x000000000000000000000000e16adb7bcdf5df419fdd48cd092a058f68a4774a
  ]
  transactionHash: 0x3f83518d1b4d2b83f98df18c26dc3387731a50bf56421b9177b4a9f39a78f1eb
  transactionIndex: 12


Equivalent command using just topics:

cast logs --from-block 15537393 --to-block latest 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef 0x0000000000000000000000002e8abfe042886e4938201101a63730d04f160a82

Logic is that the signature of the event is TOPIC_0 anyways. So the position argument still is kind of consistent. The arguments for when specifying a signature are the indexed arguments of the signature and are converted to topic hashes.

I am still up skilling with Rust so opening a draft PR to get feedback while I work on adding some tests.

Also realised I still need to handle empty arguments (any) when using a signature.

@tynes
Copy link
Contributor

tynes commented May 25, 2023

It would be nice to have a --json flag to make it easy to pipe to jq, other commands have the --json flag

@tynes
Copy link
Contributor

tynes commented May 25, 2023

A few other thoughts:

  • It would be nice to abi decode things, not sure if that should be the default behavior or behind a --pretty flag
  • seth has the ability to watch for new logs, it probably uses eth_subscribe under the hood, not sure how useful that is in practice and probably adds complexity to the implementation

@bernard-wagner
Copy link
Contributor Author

A few other thoughts:

  • It would be nice to abi decode things, not sure if that should be the default behavior or behind a --pretty flag
  • seth has the ability to watch for new logs, it probably uses eth_subscribe under the hood, not sure how useful that is in practice and probably adds complexity to the implementation

@bernard-wagner bernard-wagner marked this pull request as ready for review May 26, 2023 11:29
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supportive, a few nits

cast/src/lib.rs Outdated Show resolved Hide resolved
}
}

async fn get_block_number<M: Middleware>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would like a one line here what this does: converting the BlockId

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to convert_block_number to prevent overlap with existing functions in the repo. Also added a bit of documentation.

Comment on lines +114 to +120
fn build_filter(
from_block: Option<BlockNumber>,
to_block: Option<BlockNumber>,
address: Option<Address>,
sig_or_topic: Option<String>,
topics_or_args: Vec<String>,
) -> Result<Filter, eyre::Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda know what this does, but this is difficult to understand.

we need a few docs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split the logic into new functions and added some description, hopefully that makes it easier to follow.

cast/src/lib.rs Outdated Show resolved Hide resolved
@mattsse mattsse added T-feature Type: feature C-cast Command: cast labels Jun 1, 2023
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

this is now much easier to understand

@mattsse mattsse merged commit 7fbe257 into foundry-rs:master Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cast Command: cast T-feature Type: feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants