Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add metadata to PSP22 contracts
  • Loading branch information
obrok committed Aug 11, 2022
commit 58e51ca04660c503c3594d2f819da9a4fd5b910b
24 changes: 19 additions & 5 deletions contracts/game_token/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@ pub mod game_token {
codegen::{EmitEvent, Env},
reflect::ContractEventBase,
};
use ink_prelude::format;
use ink_prelude::{format, string::String};
use ink_storage::traits::SpreadAllocate;
use openbrush::{contracts::psp22::*, traits::Storage};
use openbrush::{
contracts::psp22::{extensions::metadata::*, Internal},
traits::Storage,
};

pub const BALANCE_OF_SELECTOR: [u8; 4] = [0x65, 0x68, 0x38, 0x2f];
pub const TRANSFER_SELECTOR: [u8; 4] = [0xdb, 0x20, 0xf9, 0xf5];
pub const TOKEN_DECIMALS: u8 = 18;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not need to be a const, just hardcode in constructor


#[ink(storage)]
#[derive(Default, SpreadAllocate, Storage)]
pub struct GameToken {
#[storage_field]
psp22: psp22::Data,
#[storage_field]
metadata: metadata::Data,
/// access control contract
access_control: AccountId,
}

impl PSP22 for GameToken {}

impl PSP22Metadata for GameToken {}

// emit events
// https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md
impl Internal for GameToken {
Expand Down Expand Up @@ -93,11 +101,14 @@ pub mod game_token {
}

impl GameToken {
/// Creates a new contract with the specified initial supply.
/// Creates a new game token with the specified initial supply.
///
/// The token will have its name and symbol set in metadata to the specified values.
/// Decimals are fixed at 18.
///
/// Will revert if called from an account without a proper role
/// Will revert if called from an account without a proper role
#[ink(constructor)]
pub fn new(total_supply: Balance) -> Self {
pub fn new(name: String, symbol: String, total_supply: Balance) -> Self {
let caller = Self::env().caller();
let code_hash = Self::env()
.own_code_hash()
Expand All @@ -116,6 +127,9 @@ pub mod game_token {

match role_check {
Ok(_) => ink_lang::codegen::initialize_contract(|instance: &mut GameToken| {
instance.metadata.name = Some(name);
instance.metadata.symbol = Some(symbol);
instance.metadata.decimals = TOKEN_DECIMALS;
instance
._mint(instance.env().caller(), total_supply)
.expect("Should mint");
Expand Down
10 changes: 6 additions & 4 deletions contracts/scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ function instrument_game_token {

local __resultvar=$1
local contract_name=$2
local salt=$3
local token_name=\"$3\"
local token_symbol=\"$4\"
local salt=$5

# --- CREATE AN INSTANCE OF THE TOKEN CONTRACT

cd "$CONTRACTS_PATH"/$contract_name

local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt)
local contract_address=$(cargo contract instantiate --url $NODE --constructor new --args $token_name $token_symbol $TOTAL_BALANCE --suri "$AUTHORITY_SEED" --salt $salt)
local contract_address=$(echo "$contract_address" | grep Contract | tail -1 | cut -c 15-)

echo $contract_name "token contract instance address: " $contract_address
Expand Down Expand Up @@ -157,7 +159,7 @@ cargo contract call --url $NODE --contract $ACCESS_CONTROL --message grant_role

start=`date +%s.%N`

instrument_game_token EARLY_BIRD_SPECIAL_TOKEN game_token 0x4561726C79426972645370656369616C
instrument_game_token EARLY_BIRD_SPECIAL_TOKEN game_token Ubik UBI 0x4561726C79426972645370656369616C

# --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME CONTRACT

Expand All @@ -169,7 +171,7 @@ deploy_and_instrument_game EARLY_BIRD_SPECIAL early_bird_special $EARLY_BIRD_SPE

# --- CREATE AN INSTANCE OF THE TOKEN CONTRACT FOR THE BACK_TO_THE_FUTURE GAME

instrument_game_token BACK_TO_THE_FUTURE_TOKEN game_token 0x4261636B546F546865467574757265
instrument_game_token BACK_TO_THE_FUTURE_TOKEN game_token Cyberiad CYB 0x4261636B546F546865467574757265

# --- UPLOAD CODE AND CREATE AN INSTANCE OF THE EARLY_BIRD_SPECIAL GAME CONTRACT

Expand Down