2021-10-18 10:28:38 +00:00
|
|
|
//! Helper functions for deriving `EthCall`
|
|
|
|
|
2022-06-27 00:26:43 +00:00
|
|
|
use crate::{abi_ty, utils};
|
2021-11-05 13:00:01 +00:00
|
|
|
use ethers_core::{
|
2022-06-27 00:26:43 +00:00
|
|
|
abi::{Function, FunctionExt, HumanReadableParser},
|
2021-11-05 13:00:01 +00:00
|
|
|
macros::{ethers_contract_crate, ethers_core_crate},
|
|
|
|
};
|
2022-06-27 00:26:43 +00:00
|
|
|
use proc_macro2::{Span, TokenStream};
|
|
|
|
use quote::quote;
|
|
|
|
use syn::{parse::Error, spanned::Spanned as _, AttrStyle, DeriveInput, Lit, Meta, NestedMeta};
|
2021-10-18 10:28:38 +00:00
|
|
|
|
|
|
|
/// Generates the `ethcall` trait support
|
|
|
|
pub(crate) fn derive_eth_call_impl(input: DeriveInput) -> TokenStream {
|
|
|
|
let attributes = match parse_call_attributes(&input) {
|
|
|
|
Ok(attributes) => attributes,
|
|
|
|
Err(errors) => return errors,
|
|
|
|
};
|
|
|
|
|
2021-10-29 12:29:35 +00:00
|
|
|
let function_call_name =
|
|
|
|
attributes.name.map(|(s, _)| s).unwrap_or_else(|| input.ident.to_string());
|
2021-10-18 10:28:38 +00:00
|
|
|
|
|
|
|
let mut function = if let Some((src, span)) = attributes.abi {
|
2022-06-27 00:26:43 +00:00
|
|
|
let raw_function_sig = src.trim_start_matches("function ").trim_start();
|
2021-10-18 10:28:38 +00:00
|
|
|
// try to parse as solidity function
|
2022-06-27 00:26:43 +00:00
|
|
|
if let Ok(fun) = HumanReadableParser::parse_function(&src) {
|
2021-10-18 10:28:38 +00:00
|
|
|
fun
|
|
|
|
} else {
|
2022-06-27 00:26:43 +00:00
|
|
|
// try to determine the abi by using its fields at runtime
|
|
|
|
return match derive_trait_impls_with_abi_type(
|
|
|
|
&input,
|
|
|
|
&function_call_name,
|
|
|
|
Some(raw_function_sig),
|
|
|
|
) {
|
|
|
|
Ok(derived) => derived,
|
|
|
|
Err(err) => {
|
|
|
|
Error::new(span, format!("Unable to determine ABI for `{}` : {}", src, err))
|
|
|
|
.to_compile_error()
|
2021-12-04 04:19:00 +00:00
|
|
|
}
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-04 04:19:00 +00:00
|
|
|
// try to determine the abi by using its fields at runtime
|
2022-06-23 06:53:24 +00:00
|
|
|
return match derive_trait_impls_with_abi_type(&input, &function_call_name, None) {
|
2021-12-04 04:19:00 +00:00
|
|
|
Ok(derived) => derived,
|
|
|
|
Err(err) => err.to_compile_error(),
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
function.name = function_call_name.clone();
|
|
|
|
let abi = function.abi_signature();
|
|
|
|
let selector = utils::selector(function.selector());
|
2021-12-04 04:19:00 +00:00
|
|
|
let decode_impl = derive_decode_impl_from_function(&function);
|
|
|
|
|
|
|
|
derive_trait_impls(
|
|
|
|
&input,
|
|
|
|
&function_call_name,
|
|
|
|
quote! {#abi.into()},
|
|
|
|
Some(selector),
|
|
|
|
decode_impl,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates the EthCall implementation
|
|
|
|
pub fn derive_trait_impls(
|
|
|
|
input: &DeriveInput,
|
|
|
|
function_call_name: &str,
|
|
|
|
abi_signature: TokenStream,
|
|
|
|
selector: Option<TokenStream>,
|
|
|
|
decode_impl: TokenStream,
|
|
|
|
) -> TokenStream {
|
|
|
|
// the ethers crates to use
|
|
|
|
let core_crate = ethers_core_crate();
|
|
|
|
let contract_crate = ethers_contract_crate();
|
|
|
|
let struct_name = &input.ident;
|
2021-10-18 10:28:38 +00:00
|
|
|
|
2021-12-04 04:19:00 +00:00
|
|
|
let selector = selector.unwrap_or_else(|| {
|
|
|
|
quote! {
|
|
|
|
#core_crate::utils::id(Self::abi_signature())
|
|
|
|
}
|
|
|
|
});
|
2021-10-18 10:28:38 +00:00
|
|
|
|
|
|
|
let ethcall_impl = quote! {
|
2021-12-04 04:19:00 +00:00
|
|
|
impl #contract_crate::EthCall for #struct_name {
|
2021-10-18 10:28:38 +00:00
|
|
|
|
|
|
|
fn function_name() -> ::std::borrow::Cow<'static, str> {
|
|
|
|
#function_call_name.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn selector() -> #core_crate::types::Selector {
|
|
|
|
#selector
|
|
|
|
}
|
|
|
|
|
|
|
|
fn abi_signature() -> ::std::borrow::Cow<'static, str> {
|
2021-12-04 04:19:00 +00:00
|
|
|
#abi_signature
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 04:19:00 +00:00
|
|
|
impl #core_crate::abi::AbiDecode for #struct_name {
|
2022-07-28 18:06:06 +00:00
|
|
|
fn decode(bytes: impl AsRef<[u8]>) -> ::std::result::Result<Self, #core_crate::abi::AbiError> {
|
2021-10-18 10:28:38 +00:00
|
|
|
#decode_impl
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 22:07:24 +00:00
|
|
|
|
2021-12-04 04:19:00 +00:00
|
|
|
impl #core_crate::abi::AbiEncode for #struct_name {
|
2021-10-27 22:07:24 +00:00
|
|
|
fn encode(self) -> ::std::vec::Vec<u8> {
|
|
|
|
let tokens = #core_crate::abi::Tokenize::into_tokens(self);
|
|
|
|
let selector = <Self as #contract_crate::EthCall>::selector();
|
|
|
|
let encoded = #core_crate::abi::encode(&tokens);
|
|
|
|
selector
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.chain(encoded.into_iter())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 10:28:38 +00:00
|
|
|
};
|
2021-12-04 04:19:00 +00:00
|
|
|
let tokenize_impl = abi_ty::derive_tokenizeable_impl(input);
|
2021-10-18 10:28:38 +00:00
|
|
|
|
|
|
|
quote! {
|
|
|
|
#tokenize_impl
|
|
|
|
#ethcall_impl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 04:19:00 +00:00
|
|
|
/// Generates the decode implementation based on the function's input types
|
|
|
|
fn derive_decode_impl_from_function(function: &Function) -> TokenStream {
|
|
|
|
let datatypes = function.inputs.iter().map(|input| utils::param_type_quote(&input.kind));
|
|
|
|
let datatypes_array = quote! {[#( #datatypes ),*]};
|
|
|
|
derive_decode_impl(datatypes_array)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates the decode implementation based on the function's runtime `AbiType` impl
|
|
|
|
fn derive_decode_impl_with_abi_type(input: &DeriveInput) -> Result<TokenStream, Error> {
|
|
|
|
let datatypes_array = utils::derive_abi_parameters_array(input, "EthCall")?;
|
|
|
|
Ok(derive_decode_impl(datatypes_array))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn derive_decode_impl(datatypes_array: TokenStream) -> TokenStream {
|
2021-10-18 10:28:38 +00:00
|
|
|
let core_crate = ethers_core_crate();
|
|
|
|
let contract_crate = ethers_contract_crate();
|
2021-12-04 04:19:00 +00:00
|
|
|
let data_types_init = quote! {let data_types = #datatypes_array;};
|
2021-10-18 10:28:38 +00:00
|
|
|
|
|
|
|
quote! {
|
|
|
|
let bytes = bytes.as_ref();
|
|
|
|
if bytes.len() < 4 || bytes[..4] != <Self as #contract_crate::EthCall>::selector() {
|
|
|
|
return Err(#contract_crate::AbiError::WrongSelector);
|
|
|
|
}
|
|
|
|
#data_types_init
|
|
|
|
let data_tokens = #core_crate::abi::decode(&data_types, &bytes[4..])?;
|
|
|
|
Ok(<Self as #core_crate::abi::Tokenizable>::from_token( #core_crate::abi::Token::Tuple(data_tokens))?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 04:19:00 +00:00
|
|
|
/// Use the `AbiType` trait to determine the correct `ParamType` and signature at runtime
|
|
|
|
fn derive_trait_impls_with_abi_type(
|
|
|
|
input: &DeriveInput,
|
|
|
|
function_call_name: &str,
|
2022-06-23 06:53:24 +00:00
|
|
|
abi_signature: Option<&str>,
|
2021-12-04 04:19:00 +00:00
|
|
|
) -> Result<TokenStream, Error> {
|
2022-06-23 06:53:24 +00:00
|
|
|
let abi_signature = if let Some(abi) = abi_signature {
|
|
|
|
quote! {#abi}
|
|
|
|
} else {
|
|
|
|
utils::derive_abi_signature_with_abi_type(input, function_call_name, "EthCall")?
|
|
|
|
};
|
|
|
|
|
2021-12-04 04:19:00 +00:00
|
|
|
let abi_signature = quote! {
|
2022-06-23 06:53:24 +00:00
|
|
|
#abi_signature.into()
|
2021-10-18 10:28:38 +00:00
|
|
|
};
|
2021-12-04 04:19:00 +00:00
|
|
|
let decode_impl = derive_decode_impl_with_abi_type(input)?;
|
|
|
|
Ok(derive_trait_impls(input, function_call_name, abi_signature, None, decode_impl))
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// All the attributes the `EthCall` macro supports
|
|
|
|
#[derive(Default)]
|
|
|
|
struct EthCallAttributes {
|
|
|
|
name: Option<(String, Span)>,
|
|
|
|
abi: Option<(String, Span)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// extracts the attributes from the struct annotated with `EthCall`
|
|
|
|
fn parse_call_attributes(input: &DeriveInput) -> Result<EthCallAttributes, TokenStream> {
|
|
|
|
let mut result = EthCallAttributes::default();
|
|
|
|
for a in input.attrs.iter() {
|
|
|
|
if let AttrStyle::Outer = a.style {
|
|
|
|
if let Ok(Meta::List(meta)) = a.parse_meta() {
|
|
|
|
if meta.path.is_ident("ethcall") {
|
|
|
|
for n in meta.nested.iter() {
|
|
|
|
if let NestedMeta::Meta(meta) = n {
|
|
|
|
match meta {
|
|
|
|
Meta::Path(path) => {
|
|
|
|
return Err(Error::new(
|
|
|
|
path.span(),
|
|
|
|
"unrecognized ethcall parameter",
|
|
|
|
)
|
2021-10-29 12:29:35 +00:00
|
|
|
.to_compile_error())
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
Meta::List(meta) => {
|
|
|
|
return Err(Error::new(
|
|
|
|
meta.path.span(),
|
|
|
|
"unrecognized ethcall parameter",
|
|
|
|
)
|
2021-10-29 12:29:35 +00:00
|
|
|
.to_compile_error())
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
Meta::NameValue(meta) => {
|
|
|
|
if meta.path.is_ident("name") {
|
|
|
|
if let Lit::Str(ref lit_str) = meta.lit {
|
|
|
|
if result.name.is_none() {
|
|
|
|
result.name =
|
|
|
|
Some((lit_str.value(), lit_str.span()));
|
|
|
|
} else {
|
|
|
|
return Err(Error::new(
|
|
|
|
meta.span(),
|
|
|
|
"name already specified",
|
|
|
|
)
|
2021-10-29 12:29:35 +00:00
|
|
|
.to_compile_error())
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::new(
|
|
|
|
meta.span(),
|
|
|
|
"name must be a string",
|
|
|
|
)
|
2021-10-29 12:29:35 +00:00
|
|
|
.to_compile_error())
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
} else if meta.path.is_ident("abi") {
|
|
|
|
if let Lit::Str(ref lit_str) = meta.lit {
|
|
|
|
if result.abi.is_none() {
|
|
|
|
result.abi =
|
|
|
|
Some((lit_str.value(), lit_str.span()));
|
|
|
|
} else {
|
|
|
|
return Err(Error::new(
|
|
|
|
meta.span(),
|
|
|
|
"abi already specified",
|
|
|
|
)
|
2021-10-29 12:29:35 +00:00
|
|
|
.to_compile_error())
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::new(
|
|
|
|
meta.span(),
|
|
|
|
"abi must be a string",
|
|
|
|
)
|
2021-10-29 12:29:35 +00:00
|
|
|
.to_compile_error())
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::new(
|
|
|
|
meta.span(),
|
|
|
|
"unrecognized ethcall parameter",
|
|
|
|
)
|
2021-10-29 12:29:35 +00:00
|
|
|
.to_compile_error())
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|