--- a/crates/evm-coder-macros/src/lib.rs +++ b/crates/evm-coder-macros/src/lib.rs @@ -27,7 +27,7 @@ /// Returns solidity function selector (first 4 bytes of hash) by its /// textual representation /// -/// ```rs +/// ```ignore /// use evm_coder_macros::fn_selector; /// /// assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb); @@ -55,7 +55,7 @@ /// Returns solidity topic (hash) by its textual representation /// -/// ```rs +/// ```ignore /// use evm_coder_macros::event_topic; /// /// assert_eq!( @@ -184,6 +184,58 @@ Ident::new(&name, ident.span()) } +/// Derives call enum implementing [`evm_coder::Callable`], [`evm_coder::Weighted`] +/// and [`evm_coder::Call`] from impl block +/// +/// ## Macro syntax +/// +/// `#[solidity_interface(name, is, inline_is, events)]` +/// - *name*: used in generated code, and for Call enum name +/// - *is*: used to provide call inheritance, not found methods will be delegated to all contracts +/// specified in is/inline_is +/// - *inline_is*: same as is, but selectors for passed contracts will be used by derived ERC165 +/// implementation +/// +/// `#[weight(value)]` +/// Can be added to every method of impl block, used for deriving [`evm_coder::Weighted`], which +/// is used by substrate bridge +/// - *value*: expression, which evaluates to weight required to call this method. +/// This expression can use call arguments to calculate non-constant execution time. +/// This expression should evaluate faster than actual execution does, and may provide worser case +/// than one is called +/// +/// `#[solidity_interface(rename_selector)]` +/// - *rename_selector*: by default, selector name will be generated by transforming method name +/// from snake_case to camelCase. Use this option, if other naming convention is required. +/// I.e: method `token_uri` will be automatically renamed to `tokenUri` in selector, but name +/// required by ERC721 standard is `tokenURI`, thus we need to specify `rename_selector = "tokenURI"` +/// explicitly +/// +/// Also, any contract method may have doc comments, which will be automatically added to generated +/// solidity interface definitions +/// +/// ## Example +/// +/// ```ignore +/// struct SuperContract; +/// struct InlineContract; +/// struct Contract; +/// +/// #[derive(ToLog)] +/// enum ContractEvents { +/// Event(#[indexed] uint32), +/// } +/// +/// #[solidity_interface(name = "MyContract", is(SuperContract), inline_is(InlineContract))] +/// impl Contract { +/// /// Multiply two numbers +/// #[weight(200 + a + b)] +/// #[solidity_interface(rename_selector = "mul")] +/// fn mul(&mut self, a: uint32, b: uint32) -> Result { +/// Ok(a.checked_mul(b).ok_or("overflow")?) +/// } +/// } +/// ``` #[proc_macro_attribute] pub fn solidity_interface(args: TokenStream, stream: TokenStream) -> TokenStream { let args = parse_macro_input!(args as AttributeArgs); @@ -216,6 +268,10 @@ stream } +/// ## Syntax +/// +/// `#[indexed]` +/// Marks this field as indexed, so it will appear in [`ethereum::Log`] topics instead of data #[proc_macro_derive(ToLog, attributes(indexed))] pub fn to_log(value: TokenStream) -> TokenStream { let input = parse_macro_input!(value as DeriveInput);