diff --git a/derive/src/contract.rs b/derive/src/contract.rs index ef51f237e..d07fe8b8b 100644 --- a/derive/src/contract.rs +++ b/derive/src/contract.rs @@ -21,10 +21,34 @@ pub struct Contract { impl<'a> From<&'a ethabi::Contract> for Contract { fn from(c: &'a ethabi::Contract) -> Self { + + let functions: Vec = c.functions + .iter() + .fold( + Vec::new(), + |mut functions, (method_name, fns)| { + if fns.len() == 1 { + functions.push(fns.get(0).unwrap().into()); + } else { + let mut i = 0; + for func in fns { + let mut func: Function = func.into(); + if i > 0 { + func.alias = format!("{}_{}", method_name, i); + } else { + func.alias = method_name.to_string(); + } + functions.push(func); + i += 1; + } + } + functions + }); + Contract { constructor: c.constructor.as_ref().map(Into::into), - functions: c.functions().map(Into::into).collect(), events: c.events().map(Into::into).collect(), + functions, } } } diff --git a/derive/src/function.rs b/derive/src/function.rs index 9bcd78fb2..6092f1a36 100644 --- a/derive/src/function.rs +++ b/derive/src/function.rs @@ -17,53 +17,55 @@ use super::{ to_ethabi_param_vec, to_token, }; -struct TemplateParam { +pub struct TemplateParam { /// Template param declaration. /// /// ```text /// [T0: Into, T1: Into, T2: IntoIterator, U2 = Into] /// ``` - declaration: TokenStream, + pub declaration: TokenStream, /// Template param definition. /// /// ```text /// [param0: T0, hello_world: T1, param2: T2] /// ``` - definition: TokenStream, + pub definition: TokenStream, } -struct Inputs { +pub struct Inputs { /// Collects template params into vector. /// /// ```text /// [Token::Uint(param0.into()), Token::Bytes(hello_world.into()), Token::Array(param2.into_iter().map(Into::into).collect())] /// ``` - tokenize: Vec, + pub tokenize: Vec, /// Template params. - template_params: Vec, + pub template_params: Vec, /// Quote used to recreate `Vec` - recreate_quote: TokenStream, + pub recreate_quote: TokenStream, } -struct Outputs { +pub struct Outputs { /// Decoding implementation. - implementation: TokenStream, + pub implementation: TokenStream, /// Decode result. - result: TokenStream, + pub result: TokenStream, /// Quote used to recreate `Vec`. - recreate_quote: TokenStream, + pub recreate_quote: TokenStream, } /// Structure used to generate contract's function interface. pub struct Function { + /// Function name alias. + pub alias: String, /// Function name. - name: String, + pub name: String, /// Function input params. - inputs: Inputs, + pub inputs: Inputs, /// Function output params. - outputs: Outputs, + pub outputs: Outputs, /// Constant function. - constant: bool, + pub constant: bool, } impl<'a> From<&'a ethabi::Function> for Function { @@ -125,6 +127,7 @@ impl<'a> From<&'a ethabi::Function> for Function { }; Function { + alias: f.name.clone(), name: f.name.clone(), inputs: Inputs { tokenize, template_params, recreate_quote: to_ethabi_param_vec(&f.inputs) }, outputs: Outputs { @@ -141,7 +144,7 @@ impl Function { /// Generates the interface for contract's function. pub fn generate(&self) -> TokenStream { let name = &self.name; - let module_name = syn::Ident::new(&self.name.to_snake_case(), Span::call_site()); + let module_name = syn::Ident::new(&self.alias.to_snake_case(), Span::call_site()); let tokenize = &self.inputs.tokenize; let declarations: &Vec<_> = &self.inputs.template_params.iter().map(|i| &i.declaration).collect(); let definitions: &Vec<_> = &self.inputs.template_params.iter().map(|i| &i.definition).collect();