Skip to content

Commit

Permalink
fix(multicall): impl Error for Failure + clear returns Empty bu…
Browse files Browse the repository at this point in the history
…ilder. (#2043)

* fix: implement Error for `Failure`

* fix: clear should return Empty builder

* fix clear test
  • Loading branch information
yash-atreya authored Feb 11, 2025
1 parent b56175d commit 56c21da
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/contract/src/multicall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ mod tests {
let provider = ProviderBuilder::new().on_anvil();

let erc20 = ERC20::new(weth, &provider);
let mut multicall = provider
let multicall = provider
.multicall()
.add(erc20.totalSupply())
.add(erc20.balanceOf(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045")));
assert_eq!(multicall.len(), 2);
multicall.clear();
let multicall = multicall.clear();
assert_eq!(multicall.len(), 0);
}

Expand Down
5 changes: 3 additions & 2 deletions crates/provider/src/provider/multicall/inner_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use thiserror::Error;
/// Result type for multicall operations.
pub type Result<T, E = MulticallError> = core::result::Result<T, E>;

/// A struct to representing a failure in a multicall
#[derive(Debug, Clone)]
/// A struct representing a failure in a multicall
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("Call failed at index {idx} with return data: {return_data:?}")]
pub struct Failure {
/// The index-position of the call that failed
pub idx: usize,
Expand Down
15 changes: 12 additions & 3 deletions crates/provider/src/provider/multicall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,18 @@ where
self.add_call(call)
}

/// Clear the calls from the builder
pub fn clear(&mut self) {
self.calls.clear();
/// Returns an [`Empty`] builder
///
/// Retains previously set provider, address, block and state_override settings.
pub fn clear(self) -> MulticallBuilder<Empty, P, N> {
MulticallBuilder {
calls: Vec::new(),
provider: self.provider,
block: self.block,
state_override: self.state_override,
address: self.address,
_pd: Default::default(),
}
}

/// Get the number of calls in the builder
Expand Down

0 comments on commit 56c21da

Please sign in to comment.