@@ -11,10 +11,9 @@ Through the `#[ink::trait_definition]` proc. macro it is now possible to define
1111This allows to define shared smart contract interfaces to different concrete implementations.
1212Note that this ink! trait definition can be defined anywhere, even in another crate!
1313
14- See our [ ` ERC20-Trait example contract ` ] ( https://github.com/use-ink/ink-examples/blob/main/trait-erc20/lib.rs )
15- for an elaborate example which uses trait definitions.
14+ ## Example
1615
17- ### Example
16+ ### Definition
1817
1918Defined in the ` base_erc20.rs ` module.
2019
@@ -31,6 +30,8 @@ pub trait BaseErc20 {
3130}
3231```
3332
33+ ### Implementation
34+
3435An ink! smart contract definition can then implement this trait definition as follows:
3536
3637``` rust
@@ -66,6 +67,8 @@ mod erc20 {
6667}
6768```
6869
70+ ### Usage
71+
6972Calling the above ` Erc20 ` explicitly through its trait implementation can be done just as if it was normal Rust code:
7073
7174``` rust
@@ -84,74 +87,11 @@ use base_erc20::BaseErc20;
8487assert_eq! (erc20 . total_supply (), 1000 );
8588```
8689
90+ See our [ ` ERC20-Trait example contract ` ] ( https://github.com/use-ink/ink-examples/blob/main/trait-erc20/lib.rs )
91+ for an elaborate example which uses trait definitions.
92+
93+ ## Limitations
94+
8795There are still many limitations to ink! trait definitions and trait implementations.
8896For example, it is not possible to define associated constants or types or have default implemented methods.
8997These limitations exist because of technical intricacies, however, please expect that many of those will be tackled in future ink! releases.
90-
91-
92-
93-
94- Marks trait definitions to ink! as special ink! trait definitions.
95-
96- There are some restrictions that apply to ink! trait definitions that
97- this macro checks. Also ink! trait definitions are required to have specialized
98- structure so that the main [ ` #[ink::contract] ` ] ( https://use-ink.github.io/ink/ink/attr.contract.html ) macro can
99- properly generate code for its implementations.
100-
101- # Example: Definition
102-
103- ``` rust
104- type Balance = <ink :: env :: DefaultEnvironment as ink :: env :: Environment >:: Balance ;
105-
106- #[ink:: trait_definition]
107- pub trait Erc20 {
108- /// Returns the total supply of the ERC-20 smart contract.
109- #[ink(message)]
110- fn total_supply (& self ) -> Balance ;
111-
112- // etc.
113- }
114- ```
115-
116- # Example: Implementation
117-
118- Given the above trait definition you can implement it as shown below:
119-
120- ``` rust
121- #[ink:: contract]
122- mod base_erc20 {
123- /// We somehow cannot put the trait in the doc-test crate root due to bugs.
124- #[ink:: trait_definition]
125- pub trait Erc20 {
126- /// Returns the total supply of the ERC-20 smart contract.
127- #[ink(message)]
128- fn total_supply (& self ) -> Balance ;
129- }
130-
131- #[ink(storage)]
132- pub struct BaseErc20 {
133- total_supply : Balance ,
134- // etc ..
135- }
136-
137- impl BaseErc20 {
138- /// Constructs a new ERC-20 compliant smart contract using the initial supply.
139- #[ink(constructor)]
140- fn new (initial_supply : Balance ) -> Self {
141- Self { total_supply : initial_supply }
142- }
143- }
144-
145- impl Erc20 for BaseErc20 {
146- /// Returns the total supply of the ERC-20 smart contract.
147- #[ink(message)]
148- fn total_supply (& self ) -> Balance {
149- self . total_supply
150- }
151-
152- // etc ..
153- }
154- }
155- ```
156-
157-
0 commit comments