Skip to main content
Version: 2.2.0

PSP37 Metadata

This example shows how you can reuse the implementation of PSP37 token with PSP37Metadata extension.

First, you should implement basic version of PSP37.

Step 1: Add imports and enable unstable feature

Import everything from openbrush::contracts::psp37::extensions::metadata.

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]

#[openbrush::contract]
pub mod my_psp37 {
use openbrush::contracts::psp37::extensions::metadata::*;
...

Step 2: Define storage

Declare storage struct and declare the field related to the metadata module. Then you need to derive the Storage trait and mark the corresponding field with the #[storage_field] attribute. Deriving this trait allows you to reuse the PSP37Metadata extension in your PSP37 implementation.

#[derive(Default, SpreadAllocate, Storage)]
#[ink(storage)]
pub struct Contract {
...
#[storage_field]
metadata: metadata::Data,
}

Step 3: Inherit logic

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

impl PSP37Metadata for Contract {}

Step 4: Define constructor

Define constructor. Your PSP37Metadata contract is ready!

impl Contract {
#[ink(constructor)]
pub fn new(id: Id, key: Vec<u8>, attribute: Vec<u8>) -> Self {
ink_lang::codegen::initialize_contract(|instance: &mut Self| {
instance._set_attribute(&id, &key, &data)
})
}
}

Final code

#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]

#[openbrush::contract]
pub mod my_psp37 {
use ink_prelude::vec::Vec;
use ink_storage::traits::SpreadAllocate;
use openbrush::{
contracts::psp37::extensions::metadata::*,
traits::Storage,
};

#[derive(Default, SpreadAllocate, Storage)]
#[ink(storage)]
pub struct Contract {
#[storage_field]
psp37: psp37::Data,
#[storage_field]
metadata: metadata::Data,
}

impl PSP37 for Contract {}

impl PSP37Metadata for Contract {}

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

#[ink(message)]
pub fn set_attribute(&mut self, id: Id, key: Vec<u8>, data: Vec<u8>) -> Result<(), PSP37Error> {
self._set_attribute(&id, &key, &data)
}
}
}

You can check an example of the usage of PSP37 Metadata.

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