Skip to main content

PSP1155

This example shows how you can reuse the implementation of PSP1155 token. Also, this example shows how you can customize the logic, for example, to track the number of token types with unique_ids, adding a new token type with the add_type function.

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 PSP1155 via brush feature.

brush = { tag = "v1.6.1", git = "https://github.com/Supercolony-net/openbrush-contracts", default-features = false, features = ["psp1155"] }

Step 2: Add imports and enable unstable feature#

Use brush::contract macro instead of ink::contract. Import everything from brush::contracts::psp1155.

#![cfg_attr(not(feature = "std"), no_std)]#![feature(min_specialization)]
#[brush::contract]pub mod my_psp1155 {    use brush::contracts::psp1155::*;    use ink_prelude::{        string::String,        vec,    };    use ink_storage::{        traits::SpreadAllocate,        Mapping,    };...

Step 3: Define storage#

Declare storage struct and declare the field related to the PSP1155Storage trait. Then you need to derive the PSP1155Storage trait and mark the corresponding field with the #[PSP1155StorageField] attribute. Deriving this trait allows you to reuse the default implementation of PSP1155.

#[derive(Default, SpreadAllocate, PSP1155Storage)]#[ink(storage)]pub struct MyPSP1155 {    #[PSP1155StorageField]    psp1155: PSP1155Data,}

Step 4: Inherit logic#

Inherit implementations of the PSP1155 trait. You can customize (override) methods in this impl block.

impl PSP1155 for MyPSP1155 {}

Step 5: Define constructor#

Define constructor. Your basic version of the PSP1155 contract is ready!

impl MyPSP1155 {    #[ink(constructor)]    pub fn new() -> Self {        ink_lang::codegen::initialize_contract(|_instance: &mut Self| {})    }}

Step 6: Customize your contract#

Customize it by adding logic for denying of minting some tokens. We can deny minting of token with id by deny function. Id will be added to denied_ids map. If someone tries to mint token with denied id, we will reject transaction.

#[derive(Default, SpreadAllocate, PSP1155Storage)]#[ink(storage)]pub struct MyPSP1155 {    #[PSP1155StorageField]    psp1155: PSP1155Data,    denied_ids: Mapping<Id, ()>,}
impl PSP1155 for MyPSP1155 {}
impl MyPSP1155 {    #[ink(constructor)]    pub fn new() -> Self {        ink_lang::codegen::initialize_contract(|_instance: &mut Self| {})    }
    #[ink(message)]    pub fn deny(&mut self, id: Id) {        self.denied_ids.insert(&id, &());    }
    #[ink(message)]    pub fn mint_tokens(&mut self, id: Id, amount: Balance) -> Result<(), PSP1155Error> {        if self.denied_ids.get(&id).is_some() {            return Err(PSP1155Error::Custom(String::from("Id is denied")))        }        self._mint_to(Self::env().caller(), vec![(id, amount)])    }}

You can check an example of the usage of PSP1155.

Also you can use extensions for PSP1155 token:

PSP1155Metadata: metadata for PSP1155.

PSP1155Mintable: creation of new tokens.

PSP1155Burnable: destruction of contract's tokens.