3-Polymorphism with #[ink::trait_definition]

Prerequisites

Cargo contract installed: https://polkaverse.com/9768/technical-guide-install-cargo-contract-37738

At least on the following node is ready :

This tutorial uses Ubuntu 22.04, cargo 1.68, cargo contract 2.0 and swanky node

#[ink::trait_definition] and File Structure

In Rust, a trait defines functionality a particular type has and can share with other types. We can use traits to define shared behavior in an abstract way.

And through the #[ink::trait_definition] proc. macro, it is now possible to define your very own trait definitions that are then implementable by ink! smart contracts.

In our example we are going to implement a smart contract named “calculator“ which delegates the calculation of operations to other smart contracts.

It means we will have :

  • a library that contains the trait “Operator” in which we defines the method “compute“.
  • several smart contracts “addition“/”subtraction”/… that implement this trait.
  • the smart contract “calculator“ which calls the smart contracts “addition“/”subtraction”

The structure will be:

├─ contracts
│ ├── addition
│ │ ├── Cargo.toml
│ │ └── lib.rs

│ ├── calculator
│ │ ├── Cargo.toml
│ │ └── lib.rs

│ ├── subtraction
│ │ ├── Cargo.toml
│ │ └── lib.rs
│ ├── Cargo.toml
│ └── addition

├─ logics
│ └── traits
│ ├── mod.rs
│ └── operator.rs
├── Cargo.toml
└── lib.rs

You can find more information about the File Structure here: https://docs.astar.network/docs/build/wasm/from-zero-to-ink-hero/dex/Structure/file-structure/#file-structure-of-the-dex-project

Create the library shared by the smart contracts

1) Create the project

You can use the command “cargo contract new” to create a new contract based on the template “flipper“.

cargo contract new logics

2) Add the trait

Create a directory “traits“ to group all the traits of your contract.

In this directory, create the file “operator.rs“. This file defines the trait “Operator” with the method “compute“

3) Modules into different files

In rust, we can manage the modules in differents files via the files “mod.rs” and the file “lib.rs” (the first reading file).

In our exemple, the file “lib.rs”

and the file “mod.rs”

More information about separating modules into different files here:

https://doc.rust-lang.org/book/ch07-05-separating-modules-into-different-files.html

Implementation - the smart contract Addition

1) Create the project

You can use the command “cargo contract new” to create the new contract.

cargo contract new addition

2) Add the library in the cargo.toml

To use the trait “Operator“ in this contract, we have to add the library as dependency.

And

3) Implement the contract

Next, we will update the contract to implement the trait Operator like this

Add the unit tests

4) Add your contract in the workspace

Add your contract in the cargo workspace file (Cargo.toml file in the root directory)

5) Build the project

In the directory “addition“, use the command “cargo contract build” to compile the project.

cargo contract build

6) Run the tests

In the directory “addition“, use the command “cargo test -- --nocapture” to run the unit test(s) and display the output messages.

cargo test -- --nocapture

7) Deploy the contract

You can deploy your contract in local node or swanky node or a testnet.

Here we will start a swanky node and use Contracts UI to interact with the contract.

The result in Contracts UI:

Implementation - the smart contract Subtraction

1) Create the project

You can use the command “cargo contract new” to create the new contract.

cargo contract new subtraction

2) Add the library in the cargo.toml

To use the trait “Operator“ in this contract, we have to add the library as dependency.

And

3) Implement the contract

Next, we will update the contract to implement the trait Operator like this

Add the unit tests

4) Add your contract in the workspace

Add your contract in the cargo workspace file (Cargo.toml file in the root directory)

5) Build the project

In the directory “subtraction“, use the command “cargo contract build” to compile the project.

cargo contract build

6) Run the tests

In the directory “subtraction“, use the command “cargo test -- --nocapture” to run the unit test(s) and display the output messages.

cargo test -- --nocapture

7) Deploy the contract

You can deploy your contract in local node or swanky node or a testnet.

Here we will start a swanky node and use Contracts UI to interact with the contract.

The result in Contracts UI:

Conclusion

Here we built two contracts implementing the same trait.

The traits are important in Rust and are used to build complex code and sophisticated applications based on polymorphism. We will see in the next part how can we use it in the cross contract calls .

Git repository

https://github.com/GuiGou12358/astar-tutorials/tree/main/tuto3

Source

https://use.ink/

https://docs.astar.network/docs/build/wasm/from-zero-to-ink-hero/

0
GuiGouPost author

Crypto-enthusiast, Defi & NFT believer, Dotsam Fam Astar Tech Amb & Phala Amb Web2 builder gradually migrating to Web3

Tutorials to write Smart Contracts in Rust and Ink!

0 comments

Tutorials to write Smart Contracts in Rust and Ink!