#[trait_definition]
Expand description

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 {
    /// Returns the total supply of the ERC-20 smart contract.
    #[ink(message)]
    fn total_supply(&self) -> Balance;

    /// Transfers balance from the caller to the given address.
    #[ink(message)]
    fn transfer(&mut self, amount: Balance, to: AccountId) -> bool;

    // 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,
    }

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

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

        #[ink(message)]
        fn transfer(&mut self, amount: Balance, to: AccountId) -> bool {
            unimplemented!()
        }
    }
}