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 all commits
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
58 changes: 35 additions & 23 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 @@ -110,41 +109,54 @@ fn expand_deploy(cx: &Context) -> Result<TokenStream> {
None => (quote! {}, quote! {()}),
};

let lib_params: Vec<_> = cx
let libs: Vec<_> = cx
.artifact
.bytecode
.undefined_libraries()
.map(|name| Param {
name: name.to_snake_case(),
kind: ParamType::Address,
})
.map(|name| (name, util::safe_ident(&name.to_snake_case())))
.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 (lib_struct, lib_input, link) = if !libs.is_empty() {
let lib_struct = {
let lib_struct_fields = libs.iter().map(|(name, field)| {
let doc = util::expand_doc(&format!("Address of the `{}` library.", name));

quote! {
bytecode.link(#name, #address).expect("valid library");
#doc pub #field: self::ethcontract::Address
}
});

quote! {
let mut bytecode = bytecode;
#( #link_libraries )*
}
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, field)| {
let name_lit = Literal::string(&name);

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

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