difftreelog
style fix formatting
in: master
2 files changed
crates/evm-coder-macros/src/lib.rsdiffbeforeafterboth1#![allow(dead_code)]23use darling::FromMeta;4use inflector::cases;5use proc_macro::TokenStream;6use quote::quote;7use sha3::{Digest, Keccak256};8use syn::{9 AttributeArgs, DeriveInput, GenericArgument, Ident, ItemImpl, Pat, Path, PathArguments,10 PathSegment, Type, parse_macro_input, spanned::Spanned,11};1213mod solidity_interface;14mod to_log;1516fn fn_selector_str(input: &str) -> u32 {17 let mut hasher = Keccak256::new();18 hasher.update(input.as_bytes());19 let result = hasher.finalize();2021 let mut selector_bytes = [0; 4];22 selector_bytes.copy_from_slice(&result[0..4]);2324 u32::from_be_bytes(selector_bytes)25}2627/// Returns solidity function selector (first 4 bytes of hash) by its28/// textual representation29///30/// ```ignore31/// use evm_coder_macros::fn_selector;32///33/// assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);34/// ```35#[proc_macro]36pub fn fn_selector(input: TokenStream) -> TokenStream {37 let input = input.to_string().replace(' ', "");38 let selector = fn_selector_str(&input);3940 (quote! {41 #selector42 })43 .into()44}4546fn event_selector_str(input: &str) -> [u8; 32] {47 let mut hasher = Keccak256::new();48 hasher.update(input.as_bytes());49 let result = hasher.finalize();5051 let mut selector_bytes = [0; 32];52 selector_bytes.copy_from_slice(&result[0..32]);53 selector_bytes54}5556/// Returns solidity topic (hash) by its textual representation57///58/// ```ignore59/// use evm_coder_macros::event_topic;60///61/// assert_eq!(62/// format!("{:x}", event_topic!(Transfer(address, address, uint256))),63/// "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",64/// );65/// ```66#[proc_macro]67pub fn event_topic(stream: TokenStream) -> TokenStream {68 let input = stream.to_string().replace(' ', "");69 let selector_bytes = event_selector_str(&input);7071 (quote! {72 ::primitive_types::H256([#(73 #selector_bytes,74 )*])75 })76 .into()77}7879fn parse_path(ty: &Type) -> syn::Result<&Path> {80 match &ty {81 syn::Type::Path(pat) => {82 if let Some(qself) = &pat.qself {83 return Err(syn::Error::new(qself.ty.span(), "no receiver expected"));84 }85 Ok(&pat.path)86 }87 _ => Err(syn::Error::new(ty.span(), "expected ty to be path")),88 }89}9091fn parse_path_segment(path: &Path) -> syn::Result<&PathSegment> {92 if path.segments.len() != 1 {93 return Err(syn::Error::new(94 path.span(),95 "expected path to have only segment",96 ));97 }98 let last_segment = &path.segments.last().unwrap();99 Ok(last_segment)100}101102fn parse_ident_from_pat(pat: &Pat) -> syn::Result<&Ident> {103 match pat {104 Pat::Ident(i) => Ok(&i.ident),105 _ => Err(syn::Error::new(pat.span(), "expected pat ident")),106 }107}108109fn parse_ident_from_segment(segment: &PathSegment, allow_generics: bool) -> syn::Result<&Ident> {110 if segment.arguments != PathArguments::None && !allow_generics {111 return Err(syn::Error::new(112 segment.arguments.span(),113 "unexpected generic type",114 ));115 }116 Ok(&segment.ident)117}118119fn parse_ident_from_path(path: &Path, allow_generics: bool) -> syn::Result<&Ident> {120 let segment = parse_path_segment(path)?;121 parse_ident_from_segment(segment, allow_generics)122}123124fn parse_ident_from_type(ty: &Type, allow_generics: bool) -> syn::Result<&Ident> {125 let path = parse_path(ty)?;126 parse_ident_from_path(path, allow_generics)127}128129// Gets T out of Result<T>130fn parse_result_ok(ty: &Type) -> syn::Result<&Type> {131 let path = parse_path(ty)?;132 let segment = parse_path_segment(path)?;133134 if segment.ident != "Result" {135 return Err(syn::Error::new(136 ty.span(),137 "expected Result as return type (no renamed aliases allowed)",138 ));139 }140 let args = match &segment.arguments {141 PathArguments::AngleBracketed(e) => e,142 _ => {143 return Err(syn::Error::new(144 segment.arguments.span(),145 "missing Result generics",146 ))147 }148 };149150 let args = &args.args;151 let arg = args.first().unwrap();152153 let ty = match arg {154 GenericArgument::Type(ty) => ty,155 _ => {156 return Err(syn::Error::new(157 arg.span(),158 "expected first generic to be type",159 ))160 }161 };162163 Ok(ty)164}165166fn pascal_ident_to_call(ident: &Ident) -> Ident {167 let name = format!("{}Call", ident);168 Ident::new(&name, ident.span())169}170fn snake_ident_to_pascal(ident: &Ident) -> Ident {171 let name = ident.to_string();172 let name = cases::pascalcase::to_pascal_case(&name);173 Ident::new(&name, ident.span())174}175fn snake_ident_to_screaming(ident: &Ident) -> Ident {176 let name = ident.to_string();177 let name = cases::screamingsnakecase::to_screaming_snake_case(&name);178 Ident::new(&name, ident.span())179}180fn pascal_ident_to_snake_call(ident: &Ident) -> Ident {181 let name = ident.to_string();182 let name = cases::snakecase::to_snake_case(&name);183 let name = format!("call_{}", name);184 Ident::new(&name, ident.span())185}186187/// Derives call enum implementing [`evm_coder::Callable`], [`evm_coder::Weighted`]188/// and [`evm_coder::Call`] from impl block189/// 190/// ## Macro syntax191/// 192/// `#[solidity_interface(name, is, inline_is, events)]`193/// - *name*: used in generated code, and for Call enum name194/// - *is*: used to provide call inheritance, not found methods will be delegated to all contracts195/// specified in is/inline_is 196/// - *inline_is*: same as is, but selectors for passed contracts will be used by derived ERC165197/// implementation198/// 199/// `#[weight(value)]`200/// Can be added to every method of impl block, used for deriving [`evm_coder::Weighted`], which201/// is used by substrate bridge202/// - *value*: expression, which evaluates to weight required to call this method.203/// This expression can use call arguments to calculate non-constant execution time.204/// This expression should evaluate faster than actual execution does, and may provide worser case205/// than one is called206/// 207/// `#[solidity_interface(rename_selector)]`208/// - *rename_selector*: by default, selector name will be generated by transforming method name209/// from snake_case to camelCase. Use this option, if other naming convention is required.210/// I.e: method `token_uri` will be automatically renamed to `tokenUri` in selector, but name211/// required by ERC721 standard is `tokenURI`, thus we need to specify `rename_selector = "tokenURI"`212/// explicitly213/// 214/// Also, any contract method may have doc comments, which will be automatically added to generated215/// solidity interface definitions216/// 217/// ## Example218/// 219/// ```ignore220/// struct SuperContract;221/// struct InlineContract;222/// struct Contract;223/// 224/// #[derive(ToLog)]225/// enum ContractEvents {226/// Event(#[indexed] uint32), 227/// }228/// 229/// #[solidity_interface(name = "MyContract", is(SuperContract), inline_is(InlineContract))]230/// impl Contract {231/// /// Multiply two numbers232/// #[weight(200 + a + b)]233/// #[solidity_interface(rename_selector = "mul")]234/// fn mul(&mut self, a: uint32, b: uint32) -> Result<uint32> {235/// Ok(a.checked_mul(b).ok_or("overflow")?) 236/// }237/// }238/// ```239#[proc_macro_attribute]240pub fn solidity_interface(args: TokenStream, stream: TokenStream) -> TokenStream {241 let args = parse_macro_input!(args as AttributeArgs);242 let args = solidity_interface::InterfaceInfo::from_list(&args).unwrap();243244 let input: ItemImpl = match syn::parse(stream) {245 Ok(t) => t,246 Err(e) => return e.to_compile_error().into(),247 };248249 let expanded = match solidity_interface::SolidityInterface::try_from(args, &input) {250 Ok(v) => v.expand(),251 Err(e) => e.to_compile_error(),252 };253254 (quote! {255 #input256257 #expanded258 })259 .into()260}261262#[proc_macro_attribute]263pub fn solidity(_args: TokenStream, stream: TokenStream) -> TokenStream {264 stream265}266#[proc_macro_attribute]267pub fn weight(_args: TokenStream, stream: TokenStream) -> TokenStream {268 stream269}270271/// ## Syntax272/// 273/// `#[indexed]`274/// Marks this field as indexed, so it will appear in [`ethereum::Log`] topics instead of data275#[proc_macro_derive(ToLog, attributes(indexed))]276pub fn to_log(value: TokenStream) -> TokenStream {277 let input = parse_macro_input!(value as DeriveInput);278279 match to_log::Events::try_from(&input) {280 Ok(e) => e.expand(),281 Err(e) => e.to_compile_error(),282 }283 .into()284}pallets/unique/src/mock.rsdiffbeforeafterboth--- a/pallets/unique/src/mock.rs
+++ b/pallets/unique/src/mock.rs
@@ -162,7 +162,8 @@
_source: H160,
_tx: pallet_ethereum::Transaction,
_logs: Vec<pallet_ethereum::Log>,
- ) {}
+ ) {
+ }
}
impl pallet_evm_coder_substrate::Config for Test {