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

migrate(forge-bind): to alloy #7919

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open

Conversation

yash-atreya
Copy link
Collaborator

@yash-atreya yash-atreya commented May 13, 2024

Motivation

Final part of alloy migration

Solution

In order to do this, we have to make use of parts of code that make up the sol! macro to create a SolMacroGen type that is similar to the Abigen type in ethers.

We're reading the JSON abi files (the artifacts) and retrieving the ABI from it then converting that to a Solidity TokenStream and ultimately convert that to a SolInput that should be passed to the expander that is used by the sol! macro here: https://github.com/alloy-rs/core/blob/85aecdf54c56da20f274a21f372d3d37b8c118ca/crates/sol-macro/src/lib.rs#L281.

However the issue is that the expand module is private in the sol-macro crate and cannot be made public since it's not a macro.

@DaniPopes @mattsse I can move the expand module to a new crate sol-macro-expander, make it public and use it in foundry??

TODO

  • Artifacts and files filter
  • Make expand public by moving to a new crate.
  • Write to crate and module

@yash-atreya yash-atreya marked this pull request as draft May 13, 2024 14:54
@yash-atreya
Copy link
Collaborator Author

Proposing to make the utils method tokens_for_sol https://github.com/alloy-rs/core/blob/85aecdf54c56da20f274a21f372d3d37b8c118ca/crates/sol-macro-input/src/json.rs#L74 also public in alloy-rs/core, we can then remove the syn, proc_macro2 deps.

@yash-atreya
Copy link
Collaborator Author

yash-atreya commented May 15, 2024

TODO:

  • Filter for selecting and excluding ABIs in alloy
  • Adding derive and sol attributes
  • Cleanup nits and error handling todos

@yash-atreya yash-atreya marked this pull request as ready for review May 21, 2024 07:53
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.

@DaniPopes is best equipped to give directions here, but I think this sounds reasonable

crates/forge/bin/cmd/bind.rs Outdated Show resolved Hide resolved
crates/forge/src/sol_macro_gen.rs Outdated Show resolved Hide resolved
crates/forge/src/sol_macro_gen.rs Outdated Show resolved Hide resolved
crates/forge/src/sol_macro_gen.rs Outdated Show resolved Hide resolved
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.

pedantic doc nits

pending @DaniPopes

crates/forge/src/sol_macro_gen.rs Outdated Show resolved Hide resolved
@yash-atreya
Copy link
Collaborator Author

@DaniPopes please drop your final review whenever possible

crates/forge/bin/cmd/bind.rs Show resolved Hide resolved
@@ -158,9 +159,29 @@ impl BindArgs {
.into()
}

fn get_alloy_filter(&self) -> Filter {
Copy link
Member

Choose a reason for hiding this comment

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

is this necessary? The filtering and file finding behavior should be the same

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, so get_filter returns ContractFilter which is specific to ethers. These methods just return the regex that is then used in a unified setting in get_json_files to actually get the files.

crates/forge/bin/cmd/bind.rs Outdated Show resolved Hide resolved
crates/forge/bin/cmd/bind.rs Outdated Show resolved Hide resolved
crates/forge/Cargo.toml Outdated Show resolved Hide resolved
crates/forge/src/lib.rs Outdated Show resolved Hide resolved
crates/forge/src/sol_macro_gen.rs Outdated Show resolved Hide resolved
crates/forge/src/sol_macro_gen.rs Outdated Show resolved Hide resolved
crates/forge/src/sol_macro_gen.rs Outdated Show resolved Hide resolved
@yash-atreya
Copy link
Collaborator Author

@DaniPopes ptal

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.

can you run this on a few popular users (just search "forge bind" on github) and check if the output makes sense

crates/sol-macro-gen/src/sol_macro_gen.rs Outdated Show resolved Hide resolved
crates/sol-macro-gen/src/sol_macro_gen.rs Show resolved Hide resolved
yash-atreya and others added 3 commits June 4, 2024 20:53
Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>

let tokens = quote::quote! {
#[derive(Debug)]
#[sol(rpc)]
Copy link
Member

Choose a reason for hiding this comment

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

This is missing bytecode attributes

"#,
instance.name.to_lowercase()
);
let contents = "//! This module was autogenerated by the alloy sol!.\n//! More information can be found here <https://docs.rs/alloy-sol-macro/latest/alloy_sol_macro/macro.sol.html>.\n".to_string() + &instance.expansion.as_ref().unwrap().to_string();
Copy link
Member

Choose a reason for hiding this comment

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

use format! and split to multiple lines like above

cargo_toml_contents.contains(alloy_contract_check);
eyre::ensure!(
toml_consistent,
format!("The contents of Cargo.toml do not match the expected output of the newest `ethers::Abigen` version.\
Copy link
Member

Choose a reason for hiding this comment

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

this error message can be 2 lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants