From b84b3feef736f7bc9ef425642431df0f4086f790 Mon Sep 17 00:00:00 2001 From: ndkazu Date: Mon, 9 Dec 2024 01:50:25 +0900 Subject: [PATCH] First commit --- pop-api/examples/fungibles/lib.rs | 11 ++--- pop-api/examples/fungibles/traits.rs | 64 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 pop-api/examples/fungibles/traits.rs diff --git a/pop-api/examples/fungibles/lib.rs b/pop-api/examples/fungibles/lib.rs index e77ee956..2e1bd666 100644 --- a/pop-api/examples/fungibles/lib.rs +++ b/pop-api/examples/fungibles/lib.rs @@ -1,5 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] - +mod traits; +pub use traits::{Api, ApiImpl}; use ink::prelude::{string::String, vec::Vec}; use pop_api::{ primitives::TokenId, @@ -206,17 +207,13 @@ mod fungibles { /// Returns the token name. #[ink(message)] fn token_name(&self) -> Option { - api::token_name(self.id) - .unwrap_or_default() - .and_then(|v| String::from_utf8(v).ok()) + ApiImpl.token_name(self.id) } /// Returns the token symbol. #[ink(message)] fn token_symbol(&self) -> Option { - api::token_symbol(self.id) - .unwrap_or_default() - .and_then(|v| String::from_utf8(v).ok()) + ApiImpl::token_symbol(self.id) } /// Returns the token decimals. diff --git a/pop-api/examples/fungibles/traits.rs b/pop-api/examples/fungibles/traits.rs new file mode 100644 index 00000000..e3997d33 --- /dev/null +++ b/pop-api/examples/fungibles/traits.rs @@ -0,0 +1,64 @@ + +use ink::prelude::string::String; +use pop_api::{ + primitives::TokenId , + v0::fungibles::{ + self as api, + }, +}; + +pub trait Api{ + fn token_name(&self, id: TokenId) -> Option; + fn token_symbol(id: TokenId) -> Option; + fn token_decimals(&self, id: TokenId) -> u8; + /*fn mint(&self, id: TokenId, account: AccountId, value: u128) -> Result<(), Psp22Error>; + fn burn(&self, id: TokenId, account: AccountId, value: u128) -> Result<(), Psp22Error>;*/ +} + +pub struct ApiImpl; + +impl Api for ApiImpl{ + + fn token_name(&self, id: TokenId) -> Option { + api::token_name(id) + .unwrap_or_default() + .and_then(|v| String::from_utf8(v).ok()) + } + + + fn token_symbol(id: TokenId) -> Option { + api::token_symbol(id) + .unwrap_or_default() + .and_then(|v| String::from_utf8(v).ok()) + } + + + fn token_decimals(&self, id: TokenId) -> u8 { + api::token_decimals(id).unwrap_or_default() + } + + /* + fn mint(&self, id: TokenId, account: AccountId, value: u128) -> Result<(), Psp22Error> { + self.ensure_owner()?; + // No-op if `value` is zero. + if value == 0 { + return Ok(()); + } + api::mint(id, account, value).map_err(Psp22Error::from)?; + self.env().emit_event(Transfer { from: None, to: Some(account), value }); + Ok(()) + } + + + fn burn(&self, id: TokenId, account: AccountId, value: u128) -> Result<(), Psp22Error>{ + self.ensure_owner()?; + // No-op if `value` is zero. + if value == 0 { + return Ok(()); + } + api::burn(id, account, value).map_err(Psp22Error::from)?; + self.env().emit_event(Transfer { from: Some(account), to: None, value }); + Ok(()) + }*/ + +} \ No newline at end of file