2022-08-02 18:03:52 +00:00
|
|
|
//! Helper functions for deriving `EthError`
|
|
|
|
|
|
|
|
use crate::{calllike::*, utils, utils::ident};
|
|
|
|
use ethers_core::{
|
|
|
|
abi::{ErrorExt, HumanReadableParser},
|
|
|
|
macros::{ethers_contract_crate, ethers_core_crate},
|
|
|
|
};
|
|
|
|
use proc_macro2::TokenStream;
|
|
|
|
use quote::quote;
|
|
|
|
use syn::{parse::Error, DeriveInput};
|
|
|
|
|
|
|
|
/// Generates the `EthError` trait support
|
2023-02-14 03:54:00 +00:00
|
|
|
pub(crate) fn derive_eth_error_impl(input: DeriveInput) -> Result<TokenStream, Error> {
|
|
|
|
let attributes = parse_calllike_attributes(&input, "etherror")?;
|
2022-08-02 18:03:52 +00:00
|
|
|
|
|
|
|
let error_name = attributes.name.map(|(s, _)| s).unwrap_or_else(|| input.ident.to_string());
|
|
|
|
|
|
|
|
let mut error = if let Some((src, span)) = attributes.abi {
|
|
|
|
let raw_function_sig = src.trim_start_matches("error ").trim_start();
|
|
|
|
// try to parse as solidity error
|
2023-02-14 03:54:00 +00:00
|
|
|
match HumanReadableParser::parse_error(&src) {
|
|
|
|
Ok(solidity_error) => solidity_error,
|
|
|
|
Err(parse_err) => {
|
|
|
|
return match derive_trait_impls_with_abi_type(
|
|
|
|
&input,
|
|
|
|
&error_name,
|
|
|
|
Some(raw_function_sig),
|
|
|
|
) {
|
|
|
|
Ok(derived) => Ok(derived),
|
|
|
|
Err(err) => {
|
|
|
|
Err(Error::new(span, format!("Unable to determine ABI for `{src}`: {err}")))
|
|
|
|
}
|
|
|
|
.map_err(|e| {
|
|
|
|
let mut error = Error::new(span, parse_err);
|
|
|
|
error.combine(Error::new(span, e));
|
|
|
|
error
|
|
|
|
}),
|
2022-08-02 18:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// try to determine the abi by using its fields at runtime
|
2023-02-14 03:54:00 +00:00
|
|
|
return derive_trait_impls_with_abi_type(&input, &error_name, None)
|
2022-08-02 18:03:52 +00:00
|
|
|
};
|
|
|
|
error.name = error_name.clone();
|
2023-02-14 03:54:00 +00:00
|
|
|
|
|
|
|
let sig = error.abi_signature();
|
2022-08-02 18:03:52 +00:00
|
|
|
let selector = utils::selector(error.selector());
|
|
|
|
let decode_impl = derive_decode_impl_from_params(&error.inputs, ident("EthError"));
|
|
|
|
|
2023-02-14 03:54:00 +00:00
|
|
|
derive_trait_impls(&input, &error_name, quote!(#sig.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> {
|
2023-02-14 03:54:00 +00:00
|
|
|
let mut abi_signature = if let Some(sig) = abi_signature {
|
|
|
|
quote!(#sig)
|
2022-08-02 18:03:52 +00:00
|
|
|
} else {
|
2023-02-14 03:54:00 +00:00
|
|
|
utils::abi_signature_with_abi_type(input, function_call_name, "EthError")?
|
2022-08-02 18:03:52 +00:00
|
|
|
};
|
2023-02-14 03:54:00 +00:00
|
|
|
abi_signature.extend(quote!(.into()));
|
2022-08-02 18:03:52 +00:00
|
|
|
let decode_impl = derive_decode_impl_with_abi_type(input, ident("EthError"))?;
|
2023-02-14 03:54:00 +00:00
|
|
|
derive_trait_impls(input, function_call_name, abi_signature, None, decode_impl)
|
2022-08-02 18:03:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates the EthError implementation
|
|
|
|
pub fn derive_trait_impls(
|
|
|
|
input: &DeriveInput,
|
|
|
|
function_call_name: &str,
|
|
|
|
abi_signature: TokenStream,
|
|
|
|
selector: Option<TokenStream>,
|
|
|
|
decode_impl: TokenStream,
|
2023-02-14 03:54:00 +00:00
|
|
|
) -> Result<TokenStream, Error> {
|
2022-08-02 18:03:52 +00:00
|
|
|
// the ethers crates to use
|
2023-02-14 03:54:00 +00:00
|
|
|
let ethers_core = ethers_core_crate();
|
|
|
|
let ethers_contract = ethers_contract_crate();
|
2022-08-02 18:03:52 +00:00
|
|
|
let struct_name = &input.ident;
|
|
|
|
|
|
|
|
let selector = selector.unwrap_or_else(|| {
|
|
|
|
quote! {
|
2023-02-14 03:54:00 +00:00
|
|
|
#ethers_core::utils::id(Self::abi_signature())
|
2022-08-02 18:03:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let etherror_impl = quote! {
|
2023-02-14 03:54:00 +00:00
|
|
|
impl #ethers_contract::EthError for #struct_name {
|
2022-08-02 18:03:52 +00:00
|
|
|
fn error_name() -> ::std::borrow::Cow<'static, str> {
|
|
|
|
#function_call_name.into()
|
|
|
|
}
|
|
|
|
|
2023-02-14 03:54:00 +00:00
|
|
|
fn selector() -> #ethers_core::types::Selector {
|
2022-08-02 18:03:52 +00:00
|
|
|
#selector
|
|
|
|
}
|
|
|
|
|
|
|
|
fn abi_signature() -> ::std::borrow::Cow<'static, str> {
|
|
|
|
#abi_signature
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-02-14 03:54:00 +00:00
|
|
|
let codec_impl = derive_codec_impls(input, decode_impl, ident("EthError"))?;
|
2022-08-02 18:03:52 +00:00
|
|
|
|
2023-02-14 03:54:00 +00:00
|
|
|
Ok(quote! {
|
2022-08-02 18:03:52 +00:00
|
|
|
#etherror_impl
|
|
|
|
#codec_impl
|
2023-02-14 03:54:00 +00:00
|
|
|
})
|
2022-08-02 18:03:52 +00:00
|
|
|
}
|