[][src]Attribute Macro ink_lang_macro::trait_definition

#[trait_definition]

Marks trait definitions to ink! as special ink! trait definitions.

There are some restrictions that apply to ink! trait definitions that this macro checks. Also ink! trait definitions are required to have specialized structure so that the main #[ink::contract] macro can properly generate code for its implementations.

Example: Definition

use ink_lang as ink;

#[ink::trait_definition]
pub trait Erc20 {
    /// Constructs a new ERC-20 compliant smart contract using the initial supply.
    #[ink(constructor)]
    fn new(initial_supply: Balance) -> Self;

    /// Returns the total supply of the ERC-20 smart contract.
    #[ink(message)]
    fn total_supply(&self) -> Balance;

    // etc.
}

Example: Implementation

Given the above trait definition you can implement it as shown below:

#[ink::contract]
mod base_erc20 {
    #[ink(storage)]
    pub struct BaseErc20 {
        total_supply: Balance,
        // etc ..
    }

    impl Erc20 for BaseErc20 {
        #[ink(constructor)]
        fn new(initial_supply: Balance) -> Self {
            Self { total_supply: initial_supply }
        }

        /// Returns the total supply of the ERC-20 smart contract.
        #[ink(message)]
        fn total_supply(&self) -> Balance {
            self.total_supply
        }

        // etc ..
    }
}