Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions crates/cast/src/cmd/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ pub struct LogsArgs {
to_block: Option<BlockId>,

/// The contract address to filter on.
#[arg(
long,
value_parser = NameOrAddress::from_str
)]
address: Option<NameOrAddress>,
#[arg(long, value_parser = NameOrAddress::from_str)]
address: Option<Vec<NameOrAddress>>,

/// The signature of the event to filter logs by which will be converted to the first topic or
/// a topic to filter on.
Expand Down Expand Up @@ -61,9 +58,14 @@ impl LogsArgs {
let provider = utils::get_provider(&config)?;

let cast = Cast::new(&provider);

let address = match address {
Some(address) => Some(address.resolve(&provider).await?),
let addresses = match address {
Some(addresses) => Some(
futures::future::try_join_all(addresses.into_iter().map(|address| {
let provider = provider.clone();
async move { address.resolve(&provider).await }
}))
.await?,
),
None => None,
};

Expand All @@ -72,7 +74,7 @@ impl LogsArgs {
let to_block =
cast.convert_block_number(Some(to_block.unwrap_or_else(BlockId::latest))).await?;

let filter = build_filter(from_block, to_block, address, sig_or_topic, topics_or_args)?;
let filter = build_filter(from_block, to_block, addresses, sig_or_topic, topics_or_args)?;

if !subscribe {
let logs = cast.filter_logs(filter).await?;
Expand Down Expand Up @@ -101,7 +103,7 @@ impl LogsArgs {
fn build_filter(
from_block: Option<BlockNumberOrTag>,
to_block: Option<BlockNumberOrTag>,
address: Option<Address>,
address: Option<Vec<Address>>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit, can we rename to addresses

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it becomes a breaking change if we do that, and also --addresses <address> --addresses <address> would be a bit weird

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah, right, missed that, think it's ok then, looking for one more opinion before merging

sig_or_topic: Option<String>,
topics_or_args: Vec<String>,
) -> Result<Filter, eyre::Error> {
Expand Down Expand Up @@ -223,7 +225,9 @@ mod tests {
address: ValueOrArray::Value(address.unwrap()).into(),
topics: [vec![].into(), vec![].into(), vec![].into(), vec![].into()],
};
let filter = build_filter(from_block, to_block, address, None, vec![]).unwrap();
let filter =
build_filter(from_block, to_block, address.map(|addr| vec![addr]), None, vec![])
.unwrap();
assert_eq!(filter, expected)
}

Expand Down Expand Up @@ -365,6 +369,29 @@ mod tests {
assert_eq!(filter, expected)
}

#[test]
fn test_build_filter_with_multiple_addresses() {
let expected = Filter {
block_option: FilterBlockOption::Range { from_block: None, to_block: None },
address: vec![Address::ZERO, ADDRESS.parse().unwrap()].into(),
topics: [
vec![TRANSFER_TOPIC.parse().unwrap()].into(),
vec![].into(),
vec![].into(),
vec![].into(),
],
};
let filter = build_filter(
None,
None,
Some(vec![Address::ZERO, ADDRESS.parse().unwrap()]),
Some(TRANSFER_TOPIC.to_string()),
vec![],
)
.unwrap();
assert_eq!(filter, expected)
}

#[test]
fn test_build_filter_sig_with_mismatched_argument() {
let err = build_filter(
Expand Down
Loading