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(provider): multicall #2010

Merged
merged 54 commits into from
Feb 11, 2025
Merged

feat(provider): multicall #2010

merged 54 commits into from
Feb 11, 2025

Conversation

yash-atreya
Copy link
Member

@yash-atreya yash-atreya commented Feb 5, 2025

Motivation

Implementing a Multicall3 builder type

Solution

Uses a tuple based approach to add numerous (different) calls to the builder with type inference.

sol! {
     #[sol(rpc)]
     #[derive(Debug)]
     interface ERC20 {
         function totalSupply() external view returns (uint256 totalSupply);
         function balanceOf(address owner) external view returns (uint256 balance);
     }
}

let weth = address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
let provider = ProviderBuilder::new().on_anvil_with_config(|a| a.fork(FORK_URL));

let erc20 = ERC20::new(weth, &provider);
let owner = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let multicall = provider.multicall().add(erc20.totalSupply()).add(erc20.balanceOf(owner));

let (total_supply, balance) = multicall.aggregate().await.unwrap();
  • fix return type decoding - make it a flat tuple
  • aggregate
  • aggregate3
  • aggregate3Value
  • try* aggregate methods
  • other util methods for fetching block context

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@DaniPopes
Copy link
Member

FYI alloy-rs/core#492 contains possibly useful stuff like TuplePush to flatten the tuple return type

crates/contract/src/multicall.rs Outdated Show resolved Hide resolved
crates/contract/src/multicall.rs Outdated Show resolved Hide resolved
crates/contract/src/multicall.rs Outdated Show resolved Hide resolved
crates/contract/src/multicall.rs Outdated Show resolved Hide resolved
crates/contract/src/multicall.rs Outdated Show resolved Hide resolved
async fn build_and_call<M: SolCall>(
&self,
call_type: M,
value: Option<U256>,
Copy link
Member

Choose a reason for hiding this comment

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

why does this need a standalone value arg?

crates/contract/src/multicall/mod.rs Outdated Show resolved Hide resolved
crates/contract/src/multicall/mod.rs Outdated Show resolved Hide resolved
crates/contract/src/multicall/mod.rs Outdated Show resolved Hide resolved
crates/contract/src/multicall/mod.rs Outdated Show resolved Hide resolved
crates/contract/src/multicall/mod.rs Outdated Show resolved Hide resolved
@yash-atreya yash-atreya requested review from klkvr and mattsse February 10, 2025 12:36
@yash-atreya yash-atreya self-assigned this Feb 10, 2025
Comment on lines 816 to 818
function fail() external {
revert("fail");
}
Copy link
Member

@zerosnacks zerosnacks Feb 10, 2025

Choose a reason for hiding this comment

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

fyi: multicall3 does not support bubbling up of errors (or custom error forwarding for that matter) which will make it more difficult to debug

related: mds1/multicall#146

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.

lgtm, pending @DaniPopes and @klkvr

Comment on lines +223 to +224
where
Self: Sized,
Copy link
Member

Choose a reason for hiding this comment

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

hmm, this is the first time we do it like this and not use the client.

for mutlicall we def need the provider because we need its functions

@yash-atreya yash-atreya linked an issue Feb 11, 2025 that may be closed by this pull request
Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

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

lgtm!

///
/// ## Example
///
/// ```ignore
Copy link
Member

@DaniPopes DaniPopes Feb 11, 2025

Choose a reason for hiding this comment

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

should change the doctests to no_run so we make sure they compile

ah actually these would have a circular dep

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep! Won't compile

@yash-atreya yash-atreya merged commit b56175d into main Feb 11, 2025
27 checks passed
@yash-atreya yash-atreya deleted the yash/multicall branch February 11, 2025 14:05
@yash-atreya yash-atreya mentioned this pull request Feb 11, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[Feature] Multicall Support
5 participants