Attribute Macro solidity_interface

Source
#[solidity_interface]
Expand description

Derives call enum implementing crate::Callable and crate::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 inheritance in Solidity
  • inline_is - same as is, but ERC165::SupportsInterface will work differently: For is SupportsInterface(A) will return true if A is one of the interfaces the contract is inherited from (e.g. B is created as is(A)). If B is created as inline_is(A) SupportsInterface(A) will internally create a new interface that combines all methods of A and B, so SupportsInterface(A) will return false.

#[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.

Both contract and contract methods may have doccomments, which will end up in a generated solidity interface file, thus you should use solidity syntax for writing documentation in this macro

§Example

struct SuperContract;
struct InlineContract;
struct Contract;

#[derive(ToLog)]
enum ContractEvents {
    Event(#[indexed] uint32),
}

/// @dev This contract provides function to multiply two numbers
#[solidity_interface(name = MyContract, is(SuperContract), inline_is(InlineContract))]
impl Contract {
    /// Multiply two numbers
    /// @param a First number
    /// @param b Second number
    /// @return uint32 Product of two passed numbers
    /// @dev This function returns error in case of overflow
    #[weight(200 + a + b)]
    #[solidity_interface(rename_selector = "mul")]
    fn mul(&mut self, a: uint32, b: uint32) -> Result<uint32> {
        Ok(a.checked_mul(b).ok_or("overflow")?)
    }
}

See documentation for this proc-macro reexported in evm-coder crate