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 interfaces & error for psp22 #297

Merged
merged 22 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
154 changes: 103 additions & 51 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
@@ -1,108 +1,160 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use ink::prelude::vec::Vec;
use ink::prelude::{string::String, vec::Vec};
use pop_api::{
fungibles::{self as api},
primitives::TokenId,
v0::fungibles::{
self as api,
events::Created,
interfaces::{Psp22, Psp22Burnable, Psp22Metadata, Psp22Mintable},
PSP22Error, PSP22Result,
},
StatusCode,
};

pub type Result<T> = core::result::Result<T, StatusCode>;
#[cfg(test)]
mod tests;

#[ink::contract]
mod fungibles {
use super::*;

#[ink(storage)]
#[derive(Default)]
pub struct Fungibles;
pub struct Fungibles {
id: TokenId,
}

impl Fungibles {
/// Instantiate the contract and wrap around an existing token.
///
/// # Parameters
/// * - `token` - The token.
#[ink(constructor, payable)]
pub fn new_existing(id: TokenId) -> PSP22Result<Self> {
// Make sure token exists.
if !api::token_exists(id).unwrap_or_default() {
return Err(PSP22Error::Custom(String::from("Unknown")));
}
let contract = Self { id };
Ok(contract)
}

/// Instantiate the contract and create a new token. The token identifier will be stored
/// in contract's storage.
///
/// # Parameters
/// * - `id` - The identifier of the token.
/// * - `admin` - The account that will administer the token.
/// * - `min_balance` - The minimum balance required for accounts holding this token.
#[ink(constructor, payable)]
pub fn new() -> Self {
Default::default()
pub fn new(id: TokenId, admin: AccountId, min_balance: Balance) -> PSP22Result<Self> {
// TODO: should be nicer conversion possible.
api::create(id, admin, min_balance).map_err(|e| PSP22Error::from(e))?;
let contract = Self { id };
contract
.env()
.emit_event(Created { id, creator: contract.env().account_id(), admin });
Ok(contract)
}

#[ink(message)]
pub fn total_supply(&self, token: TokenId) -> Result<Balance> {
api::total_supply(token)
pub fn set_metadata(
&self,
name: Vec<u8>,
symbol: Vec<u8>,
decimals: u8,
) -> PSP22Result<()> {
api::set_metadata(self.id, name, symbol, decimals).map_err(|e| PSP22Error::from(e))
}

#[ink(message)]
pub fn balance_of(&self, token: TokenId, owner: AccountId) -> Result<Balance> {
api::balance_of(token, owner)
pub fn token_exists(&self) -> PSP22Result<bool> {
api::token_exists(self.id).map_err(|e| PSP22Error::from(e))
}
}

impl Psp22 for Fungibles {
#[ink(message)]
pub fn allowance(
&self,
token: TokenId,
owner: AccountId,
spender: AccountId,
) -> Result<Balance> {
api::allowance(token, owner, spender)
fn total_supply(&self) -> Balance {
api::total_supply(self.id).unwrap_or_default()
}

#[ink(message)]
pub fn transfer(&mut self, token: TokenId, to: AccountId, value: Balance) -> Result<()> {
api::transfer(token, to, value)
fn balance_of(&self, owner: AccountId) -> Balance {
api::balance_of(self.id, owner).unwrap_or_default()
}

#[ink(message)]
pub fn transfer_from(
fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance {
api::allowance(self.id, owner, spender).unwrap_or_default()
}

#[ink(message)]
fn transfer(&mut self, to: AccountId, value: Balance, _data: Vec<u8>) -> PSP22Result<()> {
api::transfer(self.id, to, value).map_err(|e| PSP22Error::from(e))
}

#[ink(message)]
fn transfer_from(
&mut self,
token: TokenId,
from: AccountId,
to: AccountId,
value: Balance,
_data: Vec<u8>,
) -> Result<()> {
api::transfer_from(token, from, to, value)
) -> PSP22Result<()> {
api::transfer_from(self.id, from, to, value).map_err(|e| PSP22Error::from(e))
}

#[ink(message)]
pub fn approve(
&mut self,
token: TokenId,
spender: AccountId,
value: Balance,
) -> Result<()> {
api::approve(token, spender, value)
fn approve(&mut self, spender: AccountId, value: Balance) -> PSP22Result<()> {
api::approve(self.id, spender, value).map_err(|e| PSP22Error::from(e))
}

#[ink(message)]
pub fn increase_allowance(
&mut self,
token: TokenId,
spender: AccountId,
value: Balance,
) -> Result<()> {
api::increase_allowance(token, spender, value)
fn increase_allowance(&mut self, spender: AccountId, value: Balance) -> PSP22Result<()> {
api::increase_allowance(self.id, spender, value).map_err(|e| PSP22Error::from(e))
}

#[ink(message)]
pub fn decrease_allowance(
&mut self,
token: TokenId,
spender: AccountId,
value: Balance,
) -> Result<()> {
api::decrease_allowance(token, spender, value)
fn decrease_allowance(&mut self, spender: AccountId, value: Balance) -> PSP22Result<()> {
api::decrease_allowance(self.id, spender, value).map_err(|e| PSP22Error::from(e))
}
}

impl Psp22Metadata for Fungibles {
#[ink(message)]
fn token_name(&self) -> Option<Vec<u8>> {
if let Ok(value) = api::token_name(self.id) {
return Some(value);
}
None
}

#[ink(message)]
pub fn token_name(&self, token: TokenId) -> Result<Vec<u8>> {
api::token_name(token)
fn token_symbol(&self) -> Option<Vec<u8>> {
if let Ok(value) = api::token_symbol(self.id) {
return Some(value);
}
None
}

#[ink(message)]
pub fn token_symbol(&self, token: TokenId) -> Result<Vec<u8>> {
api::token_symbol(token)
fn token_decimals(&self) -> u8 {
api::token_decimals(self.id).unwrap_or_default()
}
}

impl Psp22Mintable for Fungibles {
#[ink(message)]
fn mint(&mut self, account: AccountId, amount: Balance) -> PSP22Result<()> {
api::mint(self.id, account, amount).map_err(|e| PSP22Error::from(e))
}
}

impl Psp22Burnable for Fungibles {
#[ink(message)]
pub fn token_decimals(&self, token: TokenId) -> Result<u8> {
api::token_decimals(token)
fn burn(&mut self, account: AccountId, amount: Balance) -> PSP22Result<()> {
api::burn(self.id, account, amount).map_err(|e| PSP22Error::from(e))
}
}
}
Loading
Loading