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

Generate a Libraries struct #255

Merged
merged 4 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions examples/linked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ async fn run() {
.deploy()
.await
.expect("library deployment failure");
let instance = LinkedContract::builder(&web3, library.address(), 1337.into())
.gas(4_712_388.into())
.deploy()
.await
.expect("contract deployment failure");
let instance = LinkedContract::builder(
&web3,
linked_contract::Libraries {
simple_library: library.address(),
},
1337.into(),
)
.gas(4_712_388.into())
.deploy()
.await
.expect("contract deployment failure");

println!(
"The value is {}",
Expand Down
61 changes: 34 additions & 27 deletions generate/src/contract/deployment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::contract::{methods, Context};
use crate::util;
use anyhow::{Context as _, Result};
use ethcontract_common::abi::{Param, ParamType};
use ethcontract_common::Address;
use inflector::Inflector;
use proc_macro2::{Literal, TokenStream};
Expand Down Expand Up @@ -99,41 +98,49 @@ fn expand_deploy(cx: &Context) -> Result<TokenStream> {
None => (quote! {}, quote! {()}),
};

let lib_params: Vec<_> = cx
.artifact
.bytecode
.undefined_libraries()
.map(|name| Param {
name: name.to_snake_case(),
kind: ParamType::Address,
})
.collect();
let lib_input = methods::expand_inputs(&lib_params)?;

let link = if !lib_params.is_empty() {
let link_libraries = cx
.artifact
.bytecode
.undefined_libraries()
.zip(lib_params.iter())
.map(|(name, lib_param)| {
let name = Literal::string(&name);
let address = util::ident(&lib_param.name);
let libs: Vec<_> = cx.artifact.bytecode.undefined_libraries().collect();
let (lib_struct, lib_input, link) = if !libs.is_empty() {
let lib_struct = {
let lib_struct_fields = libs.iter().map(|name| {
let doc = util::expand_doc(&format!("Address of the `{}` library.", name));
let field = util::safe_ident(&name.to_snake_case());

quote! { #doc pub #field: self::ethcontract::Address }
});

quote! {
/// Undefinied libraries in the contract bytecode that are
/// required for linking in order to deploy.
pub struct Libraries {
#( #lib_struct_fields, )*
}
}
};

let link = {
let link_libraries = libs.iter().map(|name| {
let name_lit = Literal::string(&name);
let field = util::safe_ident(&name.to_snake_case());
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: since this logic is shared with the loop above, how about making the one above return a pair of (lib_struct_fields, link_libraries)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!


quote! {
bytecode.link(#name, #address).expect("valid library");
bytecode.link(#name_lit, libs.#field).expect("valid library");
}
});

quote! {
let mut bytecode = bytecode;
#( #link_libraries )*
}
quote! {
let mut bytecode = bytecode;
#( #link_libraries )*
}
};

(lib_struct, quote! { , libs: Libraries }, link)
} else {
quote! {}
Default::default()
};

Ok(quote! {
#lib_struct

impl Contract {
#doc
pub fn builder<F, T>(
Expand Down