2021-10-18 10:28:38 +00:00
|
|
|
//! Helper functions for deriving `EthCall`
|
|
|
|
|
2022-08-02 18:03:52 +00:00
|
|
|
use crate::{calllike::*, utils, utils::ident};
|
2021-11-05 13:00:01 +00:00
|
|
|
use ethers_core::{
|
2022-08-02 18:03:52 +00:00
|
|
|
abi::{FunctionExt, HumanReadableParser},
|
2021-11-05 13:00:01 +00:00
|
|
|
macros::{ethers_contract_crate, ethers_core_crate},
|
|
|
|
};
|
2022-08-02 18:03:52 +00:00
|
|
|
use proc_macro2::TokenStream;
|
2022-06-27 00:26:43 +00:00
|
|
|
use quote::quote;
|
2022-08-02 18:03:52 +00:00
|
|
|
use syn::{parse::Error, DeriveInput};
|
2021-10-18 10:28:38 +00:00
|
|
|
|
|
|
|
/// Generates the `ethcall` trait support
|
|
|
|
pub(crate) fn derive_eth_call_impl(input: DeriveInput) -> TokenStream {
|
2022-08-02 18:03:52 +00:00
|
|
|
let attributes = match parse_calllike_attributes(&input, "ethcall") {
|
2021-10-18 10:28:38 +00:00
|
|
|
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) => {
|
2022-11-07 23:43:11 +00:00
|
|
|
Error::new(span, format!("Unable to determine ABI for `{src}` : {err}"))
|
2022-06-27 00:26:43 +00:00
|
|
|
.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());
|
2022-08-02 18:03:52 +00:00
|
|
|
let decode_impl = derive_decode_impl_from_params(&function.inputs, ident("EthCall"));
|
2021-12-04 04:19:00 +00:00
|
|
|
|
|
|
|
derive_trait_impls(
|
|
|
|
&input,
|
|
|
|
&function_call_name,
|
|
|
|
quote! {#abi.into()},
|
|
|
|
Some(selector),
|
|
|
|
decode_impl,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:03:52 +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,
|
|
|
|
abi_signature: Option<&str>,
|
|
|
|
) -> Result<TokenStream, Error> {
|
|
|
|
let abi_signature = if let Some(abi) = abi_signature {
|
|
|
|
quote! {#abi}
|
|
|
|
} else {
|
|
|
|
utils::derive_abi_signature_with_abi_type(input, function_call_name, "EthCall")?
|
|
|
|
};
|
|
|
|
|
|
|
|
let abi_signature = quote! {
|
|
|
|
#abi_signature.into()
|
|
|
|
};
|
|
|
|
let decode_impl = derive_decode_impl_with_abi_type(input, ident("EthCall"))?;
|
|
|
|
Ok(derive_trait_impls(input, function_call_name, abi_signature, None, decode_impl))
|
|
|
|
}
|
|
|
|
|
2021-12-04 04:19:00 +00:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2022-08-02 18:03:52 +00:00
|
|
|
let codec_impl = derive_codec_impls(input, decode_impl, ident("EthCall"));
|
2021-10-18 10:28:38 +00:00
|
|
|
|
|
|
|
quote! {
|
|
|
|
#ethcall_impl
|
2022-08-02 18:03:52 +00:00
|
|
|
#codec_impl
|
2021-10-18 10:28:38 +00:00
|
|
|
}
|
|
|
|
}
|