#[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
§Trait definition:
#[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.
}
§Trait 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!()
}
}
}
§Header Arguments
The #[ink::trait_definition]
macro can be provided with some additional
comma-separated header arguments:
-
namespace: String
The namespace configuration parameter is used to influence the generated selectors of the ink! trait messages. This is useful to disambiguate ink! trait definitions with equal names.
Usage Example:
#[ink::trait_definition(namespace = "foo")] pub trait TraitDefinition { #[ink(message)] fn message1(&self); #[ink(message, selector = 42)] fn message2(&self); }
Default value: Empty.
-
keep_attr: String
Tells the ink! code generator which attributes should be passed to call builders. Call builders are used to doing cross-contract calls and are automatically generated for contracts.
Usage Example:
#[ink::trait_definition(keep_attr = "foo, bar")] pub trait Storage { #[ink(message)] // #[foo] fn message1(&self); #[ink(message)] // #[bar] fn message2(&self); }
Allowed attributes by default:
cfg
,cfg_attr
,allow
,warn
,deny
,forbid
,deprecated
,must_use
,doc
,rustfmt
.