Skip to main content

PSP22 Wrapper

This example shows how you can reuse the implementation of PSP22 token with PSP22 Wrapper extension, which allows you to wrap your PSP22 token in a PSP22Wrapper token which can be used for example for governance.

Step 1: Include dependencies#

Include brush as dependency in the cargo file or you can use default Cargo.toml template. After you need to enable default implementation of PSP22 via brush features.

brush = { tag = "v1.6.1", git = "", default-features = false, features = ["psp22"] }

Step 2: Add imports and enable unstable feature#

Use brush::contract macro instead of ink::contract. Import everything from brush::contracts::psp22::extensions::wrapper.

#![cfg_attr(not(feature = "std"), no_std)]#![feature(min_specialization)]
#[brush::contract]pub mod my_psp22_wrapper {    use brush::contracts::psp22::extensions::wrapper::*;    use ink_storage::traits::SpreadAllocate;...

Step 3: Define storage#

Declare storage struct and declare the fields related to PSP22Storage and PSP22WrapperStorage traits. Then you need to derive PSP22Storage and PSP22WrapperStorage traits and mark corresponding fields with #[PSP22StorageField] and #[PSP22WrapperStorageField] attributes. Deriving these traits allows you to reuse the default implementation of PSP22 and PSP22Wrapper.

#[ink(storage)]#[derive(Default, SpreadAllocate, PSP22WrapperStorage, PSP22Storage)]pub struct MyPSP22Wrapper {    #[PSP22StorageField]    psp22: PSP22Data,    #[PSP22WrapperStorageField]    wrapper: PSP22WrapperData,}

Step 4: Inherit logic#

Inherit implementations of PSP22 and PSP22Wrapper traits. You can customize (override) methods in this impl block.

impl PSP22 for MyPSP22Wrapper {}
impl PSP22Wrapper for MyPSP22Wrapper {}

Step 5: Define constructor#

Define constructor. Your implementation of PSP22Wrapper contract is ready!

impl MyPSP22 {   #[ink(constructor)]   pub fn new(token_address: AccountId) -> Self {        ink_lang::codegen::initialize_contract(|instance: &mut Self| {            instance._init(token_address);        })    }}

You can check an example of the usage of PSP22 Wrapper.

You can also check the documentation for the basic implementation of PSP22.