Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function selectors to psp22 trait #316

Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pop_api::{
v0::fungibles::{
self as api,
events::{Approval, Created, Transfer},
traits::{PSP22Burnable, PSP22Metadata, PSP22Mintable, PSP22},
traits::{Psp22, Psp22Burnable, Psp22Metadata, Psp22Mintable},
PSP22Error,
},
};
Expand Down Expand Up @@ -57,7 +57,7 @@ mod fungibles {
}
}

impl PSP22 for Fungible {
impl Psp22 for Fungible {
/// Returns the total token supply.
#[ink(message)]
fn total_supply(&self) -> Balance {
Expand Down Expand Up @@ -210,7 +210,7 @@ mod fungibles {
}
}

impl PSP22Metadata for Fungible {
impl Psp22Metadata for Fungible {
/// Returns the token name.
#[ink(message)]
fn token_name(&self) -> Option<String> {
Expand All @@ -236,7 +236,7 @@ mod fungibles {
}
}

impl PSP22Mintable for Fungible {
impl Psp22Mintable for Fungible {
/// Creates `value` amount of tokens and assigns them to `account`, increasing the total
/// supply.
///
Expand All @@ -255,7 +255,7 @@ mod fungibles {
}
}

impl PSP22Burnable for Fungible {
impl Psp22Burnable for Fungible {
/// Destroys `value` amount of tokens from `account`, reducing the total supply.
///
/// # Parameters
Expand Down
26 changes: 13 additions & 13 deletions pop-api/examples/fungibles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,18 +782,18 @@ fn deploy(
// A set of helper methods to test the contract calls.

fn total_supply(session: &mut Session<Pop>) -> Balance {
call::<Pop, Balance, PSP22Error>(session, "PSP22::total_supply", vec![], None).unwrap()
call::<Pop, Balance, PSP22Error>(session, "Psp22::total_supply", vec![], None).unwrap()
}

fn balance_of(session: &mut Session<Pop>, owner: AccountId) -> Balance {
call::<Pop, Balance, PSP22Error>(session, "PSP22::balance_of", vec![owner.to_string()], None)
call::<Pop, Balance, PSP22Error>(session, "Psp22::balance_of", vec![owner.to_string()], None)
.unwrap()
}

fn allowance(session: &mut Session<Pop>, owner: AccountId, spender: AccountId) -> Balance {
call::<Pop, Balance, PSP22Error>(
session,
"PSP22::allowance",
"Psp22::allowance",
vec![owner.to_string(), spender.to_string()],
None,
)
Expand All @@ -803,7 +803,7 @@ fn allowance(session: &mut Session<Pop>, owner: AccountId, spender: AccountId) -
fn transfer(session: &mut Session<Pop>, to: AccountId, amount: Balance) -> Result<(), PSP22Error> {
call::<Pop, (), PSP22Error>(
session,
"PSP22::transfer",
"Psp22::transfer",
vec![to.to_string(), amount.to_string(), serde_json::to_string::<[u8; 0]>(&[]).unwrap()],
None,
)
Expand All @@ -817,7 +817,7 @@ fn transfer_from(
) -> Result<(), PSP22Error> {
call::<Pop, (), PSP22Error>(
session,
"PSP22::transfer_from",
"Psp22::transfer_from",
vec![
from.to_string(),
to.to_string(),
Expand All @@ -835,7 +835,7 @@ fn approve(
) -> Result<(), PSP22Error> {
call::<Pop, (), PSP22Error>(
session,
"PSP22::approve",
"Psp22::approve",
vec![spender.to_string(), value.to_string()],
None,
)
Expand All @@ -848,7 +848,7 @@ fn increase_allowance(
) -> Result<(), PSP22Error> {
call::<Pop, (), PSP22Error>(
session,
"PSP22::increase_allowance",
"Psp22::increase_allowance",
vec![spender.to_string(), value.to_string()],
None,
)
Expand All @@ -861,30 +861,30 @@ fn decrease_allowance(
) -> Result<(), PSP22Error> {
call::<Pop, (), PSP22Error>(
session,
"PSP22::decrease_allowance",
"Psp22::decrease_allowance",
vec![spender.to_string(), value.to_string()],
None,
)
}

fn token_name(session: &mut Session<Pop>) -> Option<String> {
call::<Pop, Option<String>, PSP22Error>(session, "PSP22Metadata::token_name", vec![], None)
call::<Pop, Option<String>, PSP22Error>(session, "Psp22Metadata::token_name", vec![], None)
.unwrap()
}

fn token_symbol(session: &mut Session<Pop>) -> Option<String> {
call::<Pop, Option<String>, PSP22Error>(session, "PSP22Metadata::token_symbol", vec![], None)
call::<Pop, Option<String>, PSP22Error>(session, "Psp22Metadata::token_symbol", vec![], None)
.unwrap()
}

fn token_decimals(session: &mut Session<Pop>) -> u8 {
call::<Pop, u8, PSP22Error>(session, "PSP22Metadata::token_decimals", vec![], None).unwrap()
call::<Pop, u8, PSP22Error>(session, "Psp22Metadata::token_decimals", vec![], None).unwrap()
}

fn mint(session: &mut Session<Pop>, account: AccountId, amount: Balance) -> Result<(), PSP22Error> {
call::<Pop, (), PSP22Error>(
session,
"PSP22Mintable::mint",
"Psp22Mintable::mint",
vec![account.to_string(), amount.to_string()],
None,
)
Expand All @@ -893,7 +893,7 @@ fn mint(session: &mut Session<Pop>, account: AccountId, amount: Balance) -> Resu
fn burn(session: &mut Session<Pop>, account: AccountId, amount: Balance) -> Result<(), PSP22Error> {
call::<Pop, (), PSP22Error>(
session,
"PSP22Burnable::burn",
"Psp22Burnable::burn",
vec![account.to_string(), amount.to_string()],
None,
)
Expand Down
47 changes: 29 additions & 18 deletions pop-api/src/v0/fungibles/traits.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
//! Traits that can be used by contracts. Including standard compliant traits.

use super::*;
use core::result::Result;

use ink::prelude::string::String;

use super::*;

/// Function selectors as per the PSP22 standard: https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md.
/// The mint and burn selectors are not defined in the standard, but have been created in the same way.

/// The PSP22 trait.
#[ink::trait_definition]
pub trait PSP22 {
pub trait Psp22 {
/// Returns the total token supply.
#[ink(message)]
#[ink(message, selector = 0x162df8c2)]
fn total_supply(&self) -> Balance;

/// Returns the account balance for the specified `owner`.
///
/// # Parameters
/// - `owner` - The account whose balance is being queried.
#[ink(message)]
#[ink(message, selector = 0x6568382f)]
fn balance_of(&self, owner: AccountId) -> Balance;

/// Returns the allowance for a `spender` approved by an `owner`.
///
/// # Parameters
/// - `owner` - The account that owns the tokens.
/// - `spender` - The account that is allowed to spend the tokens.
#[ink(message)]
#[ink(message, selector = 0x4d47d921)]
fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance;

/// Transfers `value` amount of tokens from the caller's account to account `to`
Expand All @@ -33,7 +38,7 @@ pub trait PSP22 {
/// - `to` - The recipient account.
/// - `value` - The number of tokens to transfer.
/// - `data` - Additional data in unspecified format.
#[ink(message)]
#[ink(message, selector = 0xdb20f9f5)]
fn transfer(&mut self, to: AccountId, value: Balance, data: Vec<u8>) -> Result<(), PSP22Error>;

/// Transfers `value` tokens on behalf of `from` to the account `to`
Expand All @@ -44,7 +49,7 @@ pub trait PSP22 {
/// - `to` - The recipient account.
/// - `value` - The number of tokens to transfer.
/// - `data` - Additional data with unspecified format.
#[ink(message)]
#[ink(message, selector = 0x54b3c76e)]
fn transfer_from(
&mut self,
from: AccountId,
Expand All @@ -60,62 +65,68 @@ pub trait PSP22 {
/// # Parameters
/// - `spender` - The account that is allowed to spend the tokens.
/// - `value` - The number of tokens to approve.
#[ink(message)]
#[ink(message, selector = 0xb20f1bbd)]
fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error>;

/// Increases the allowance of `spender` by `value` amount of tokens.
///
/// # Parameters
/// - `spender` - The account that is allowed to spend the tokens.
/// - `value` - The number of tokens to increase the allowance by.
#[ink(message)]
#[ink(message, selector = 0x96d6b57a)]
fn increase_allowance(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error>;

/// Decreases the allowance of `spender` by `value` amount of tokens.
///
/// # Parameters
/// - `spender` - The account that is allowed to spend the tokens.
/// - `value` - The number of tokens to decrease the allowance by.
#[ink(message)]
#[ink(message, selector = 0xfecb57d5)]
fn decrease_allowance(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error>;
}

/// The PSP22 Metadata trait.
#[ink::trait_definition]
pub trait PSP22Metadata {
pub trait Psp22Metadata {
/// Returns the token name.
#[ink(message)]
#[ink(message, selector = 0x3d261bd4)]
fn token_name(&self) -> Option<String>;

/// Returns the token symbol.
#[ink(message)]
#[ink(message, selector = 0x34205be5)]
fn token_symbol(&self) -> Option<String>;

/// Returns the token decimals.
#[ink(message)]
#[ink(message, selector = 0x7271b782)]
fn token_decimals(&self) -> u8;
}

/// The PSP22 Mintable trait.
#[ink::trait_definition]
pub trait PSP22Mintable {
pub trait Psp22Mintable {
/// Creates `value` amount of tokens and assigns them to `account`, increasing the total supply.
///
/// The selector for this message is `0xfc3c75d4` (first 4 bytes of
/// `blake2b_256("PSP22Mintable::mint")`).
///
/// # Parameters
/// - `account` - The account to be credited with the created tokens.
/// - `value` - The number of tokens to mint.
#[ink(message)]
#[ink(message, selector = 0xfc3c75d4)]
evilrobot-01 marked this conversation as resolved.
Show resolved Hide resolved
fn mint(&mut self, account: AccountId, value: Balance) -> Result<(), PSP22Error>;
}

/// The PSP22 Burnable trait.
#[ink::trait_definition]
pub trait PSP22Burnable {
pub trait Psp22Burnable {
/// Destroys `value` amount of tokens from `account`, reducing the total supply.
///
/// The selector for this message is `0x7a9da510` (first 4 bytes of
/// `blake2b_256("PSP22Burnable::burn")`).
///
/// # Parameters
/// - `account` - The account from which the tokens will be destroyed.
/// - `value` - The number of tokens to destroy.
#[ink(message)]
#[ink(message, selector = 0x7a9da510)]
evilrobot-01 marked this conversation as resolved.
Show resolved Hide resolved
fn burn(&mut self, account: AccountId, value: Balance) -> Result<(), PSP22Error>;
}
Loading