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(forge-bind): add_derives for serde in contract bindings #5836

Merged
merged 6 commits into from
Oct 10, 2023

Conversation

Autoparallel
Copy link
Contributor

Motivation

The change was made to close #5835 which would allow users to get more functionality out of enums/structs in contract bindings, e.g., parse contract events from a chain into a file format such as CSV.

Solution

This is a basic solution where we just add in the serde::Serialize and serde:Deserialize traits via the add_derives method on Abigen. This does not give the end user any option in deriving these traits and, perhaps they may not want them.

I am happy to provide flags as such described in #5835. Please let me know what you prefer.

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.

this looks fine, however, I think we're missing support for configuring additional Cargo.toml dependencies in Abigen.

ideally serde would be added to the generated Cargo.toml as well.

currently we're only setting ethers:

https://github.com/gakonst/ethers-rs/blob/c702e6cdece63f3c47f3476c626024c11f4b8cec/ethers-contract/ethers-contract-abigen/src/multi.rs#L612-L612

so we need a way to also configure additional deps.

I think this should go here:

https://github.com/gakonst/ethers-rs/blob/c702e6cdece63f3c47f3476c626024c11f4b8cec/ethers-contract/ethers-contract-abigen/src/multi.rs#L367-L368

something like dependencies: Vec<String>

I don't want to add this as an additional arg to write_to_crate since this would break.

@Autoparallel
Copy link
Contributor Author

Okay got it. Let me work on this.

@Autoparallel
Copy link
Contributor Author

Autoparallel commented Sep 15, 2023

Would it instead be a simpler change to do this:

https://github.com/gakonst/ethers-rs/blob/c702e6cdece63f3c47f3476c626024c11f4b8cec/ethers-contract/src/lib.rs#L20-L30

In this lib.rs, just add

pub use serde::{Serialize, Deserialize}

And then replace the

add_derive("serde::Serialize")

with

add_derive("::ethers::contract::Serialize")

and of course the respective change for Deserialize. This is pretty minimal change required.

Trying this on my end worked fine in both crate/module bindings.

@mattsse
Copy link
Member

mattsse commented Sep 15, 2023

public re-exports for third-party crates are hacky, really don't like this solution for this

@mattsse
Copy link
Member

mattsse commented Sep 15, 2023

another fix would be to append the serde dependency manually in forge bind, but ideal solution would be support in abigen

@Autoparallel
Copy link
Contributor Author

I was working on this for a while today inside ethers-rs. I am struggling to see a nice way to do it that doesn't involve more sweeping changes.

From what I can gather, there is a process like:

Abigen -> MultiAbigen -> MultiExpansion -> MultiExpansionResult -> MultiBindings -> MultiBindingsInner

where finally the Cargo.toml is written.

I considered adding a:

add_dependency()

for Abigen as this is where we would also be adding a derive as well. However, carrying this dependency throughout as a Vec<String> in each one of these structs seemed to lead to larger changes than I expected. I think this is doable, but does this seem like the approach we want?

Do you have more thoughts on this? I want to do it the right way here!

@mattsse
Copy link
Member

mattsse commented Sep 18, 2023

hmm,

I think we only need this here:

-> MultiBindings -> MultiBindingsInner

this is where it's written:

bindings.write_to_crate(
&self.crate_name,
&self.crate_version,
self.bindings_root(&artifacts),
self.single_file,
)?;

so similar to MultiBindings::format

@Autoparallel Autoparallel force-pushed the feat(forge)-serde-derives branch from 140b90a to 6c350e7 Compare September 20, 2023 22:02
@Autoparallel
Copy link
Contributor Author

I made a fix here that pushes the relevant dependency into the bindings when generated via a crate. It will have to come after the change in ethers-rs of course!

@Autoparallel
Copy link
Contributor Author

Once we get ethers-rs issue 2606 in, can we revisit this as well?

@gakonst
Copy link
Member

gakonst commented Oct 10, 2023

This has been fixed in Ethers

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.

ty for this!

sorry for the delay

@mattsse mattsse merged commit deae4f1 into foundry-rs:master Oct 10, 2023
16 of 17 checks passed
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.

forge feat: add serde::Serialize and serde::Deserialize traits upon abigen
3 participants