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: alloy-dyn-contract #149

Merged
merged 18 commits into from
Jan 29, 2024
Merged

feat: alloy-dyn-contract #149

merged 18 commits into from
Jan 29, 2024

Conversation

onbjerg
Copy link
Member

@onbjerg onbjerg commented Jan 24, 2024

Motivation

We want something like BaseContract/ContractInstance in ethers-rs for dynamic ABI in alloy.

Solution

Port over what we can of those existing APIs to alloy (i.e. really only call) and simplify the CallBuilder a bit

In ethers there are 2 call builders, a main one, FunctionCall, and a secondary one you can get from FunctionCall (called RawCall) which you can use to set state overrides. I inlined the state override API into CallBuilder instead, and opted to only add the 3rd parameter for eth_call if state overrides are set, as this is not supported by all nodes.

I'm not sure if there is any risk in sending a third "bogus" parameter in eth_call, but if so, maybe the API in this PR could be updated to split out the eth_call + state override to a different function in the provider.

This also does not include sending transactions (that part is a todo!()) because that flow is not really done yet in the provider or the signer.

A separate pr for "static" contracts should be opened

This PR does also not differentiate between legacy/EIP-1559 tx's as we don't have a "typed tx" construct in Alloy yet

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@onbjerg onbjerg added the enhancement New feature or request label Jan 24, 2024
crates/alloy-dyn-contract/src/call.rs Outdated Show resolved Hide resolved
crates/alloy-dyn-contract/src/call.rs Outdated Show resolved Hide resolved
crates/providers/src/provider.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

looks great, some comments

crates/alloy-dyn-contract/src/call.rs Outdated Show resolved Hide resolved
pub struct CallBuilder<P> {
// todo: this will not work with `send_transaction` and does not differentiate between EIP-1559
// and legacy tx
request: CallRequest,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think having access to the CallRequest would be ideal. This way we can get around not having send_transaction on the provider by transferring the fields manually to a signable/rlp-able tx type from alloy consensus e.g TxLegacy

crates/alloy-dyn-contract/src/call.rs Outdated Show resolved Hide resolved
Comment on lines 11 to 12
/// A smart contract interface.
#[derive(Debug, Clone)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add a bit more docs on why it's necessary/more ergonomic to have this type vs extending JsonAbi?

}
}

// TODO: events/errors
Copy link
Contributor

Choose a reason for hiding this comment

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

just flagging :D

Copy link
Member Author

Choose a reason for hiding this comment

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

yea we don't have the facilities rn for events so it's more of a follow up to do

Copy link
Contributor

Choose a reason for hiding this comment

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

sweet np, let's open an issue for this and roll

crates/providers/src/provider.rs Outdated Show resolved Hide resolved
Copy link
Member

@gakonst gakonst left a comment

Choose a reason for hiding this comment

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

nice

@@ -0,0 +1,123 @@
use std::collections::HashMap;
Copy link
Member

Choose a reason for hiding this comment

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

This file LGTM. I trust its functionality and would defer writing tests, open as issue and we can pass it to contributor as this is code that just wraps around JsonABI.

crates/alloy-dyn-contract/src/instance.rs Outdated Show resolved Hide resolved
/// If there are multiple functions with the same name due to overloading, consider using
/// the [`ContractInstance::function_from_selector`] method instead, since this will use the
/// first match.
pub fn function(&self, name: &str, args: &[DynSolValue]) -> Result<CallBuilder<P>> {
Copy link
Member

Choose a reason for hiding this comment

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

I would love if we had a great UX about this. Right now it'd require the user to manually do DynSolValue::X for each argument. The way we did it with the generics Tokenize in Ethers-rs was nice, but understand if it's a PITA to work with.

cc @DaniPopes

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to convey that at last sync but there did not seem to be a lot of agreement

pub async fn call(&self) -> Result<Vec<DynSolValue>> {
let bytes = self
.provider
.call(self.request.clone(), self.block, self.state.clone())
Copy link
Member

Choose a reason for hiding this comment

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

@DaniPopes @mattsse is there a world where we can get rid of these clones and use refs under the hood

crates/alloy-dyn-contract/src/call.rs Outdated Show resolved Hide resolved
crates/alloy-dyn-contract/Cargo.toml Outdated Show resolved Hide resolved
crates/alloy-dyn-contract/src/call.rs Outdated Show resolved Hide resolved
crates/alloy-dyn-contract/src/error.rs Outdated Show resolved Hide resolved
crates/alloy-dyn-contract/src/call.rs Outdated Show resolved Hide resolved
crates/alloy-dyn-contract/src/interface.rs Outdated Show resolved Hide resolved
Copy link
Member

@DaniPopes DaniPopes left a comment

Choose a reason for hiding this comment

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

lgtm

block: Option<BlockId>,
state: Option<StateOverride>,
provider: P,
// todo: only used to decode - should it be some type D to dedupe with `sol!` contracts?
Copy link
Member

Choose a reason for hiding this comment

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

Will need a follow-up for this so we can also use it in sol!

@onbjerg
Copy link
Member Author

onbjerg commented Jan 29, 2024

will merge as is and we can make improvements on top, we're prob bound to have missed something from the old api that someone has a use case for

@onbjerg onbjerg merged commit ba70833 into main Jan 29, 2024
14 checks passed
@onbjerg onbjerg deleted the onbjerg/alloy-dyn-contract branch January 29, 2024 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants