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: token name & symbol return Option #312

Merged
merged 4 commits into from
Oct 2, 2024
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
22 changes: 12 additions & 10 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ pub mod pallet {
BalanceOf(BalanceOf<T>),
/// Allowance for a spender approved by an owner, for a specified token.
Allowance(BalanceOf<T>),
/// Name of the specified token.
TokenName(Vec<u8>),
/// Symbol for the specified token.
TokenSymbol(Vec<u8>),
/// Name of the specified token, if available.
TokenName(Option<Vec<u8>>),
/// Symbol for the specified token, if available.
TokenSymbol(Option<Vec<u8>>),
/// Decimals for the specified token.
TokenDecimals(u8),
/// Whether the specified token exists.
Expand Down Expand Up @@ -514,12 +514,14 @@ pub mod pallet {
ReadResult::BalanceOf(AssetsOf::<T>::balance(token, owner)),
Allowance { token, owner, spender } =>
ReadResult::Allowance(AssetsOf::<T>::allowance(token, &owner, &spender)),
TokenName(token) => ReadResult::TokenName(<AssetsOf<T> as MetadataInspect<
AccountIdOf<T>,
>>::name(token)),
TokenSymbol(token) => ReadResult::TokenSymbol(<AssetsOf<T> as MetadataInspect<
AccountIdOf<T>,
>>::symbol(token)),
TokenName(token) => ReadResult::TokenName(
Some(<AssetsOf<T> as MetadataInspect<AccountIdOf<T>>>::name(token))
.filter(|v| !v.is_empty()),
),
TokenSymbol(token) => ReadResult::TokenSymbol(
Some(<AssetsOf<T> as MetadataInspect<AccountIdOf<T>>>::symbol(token))
.filter(|v| !v.is_empty()),
),
TokenDecimals(token) => ReadResult::TokenDecimals(
<AssetsOf<T> as MetadataInspect<AccountIdOf<T>>>::decimals(token),
),
Expand Down
35 changes: 18 additions & 17 deletions pallets/api/src/fungibles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ mod encoding_read_result {

#[test]
fn token_name() {
let name = vec![42, 42, 42, 42, 42];
let mut name = Some("some name".as_bytes().to_vec());
assert_eq!(ReadResult::TokenName::<Test>(name.clone()).encode(), name.encode());
name = None;
assert_eq!(ReadResult::TokenName::<Test>(name.clone()).encode(), name.encode());
}

#[test]
fn token_symbol() {
let symbol = vec![42, 42, 42, 42, 42];
let mut symbol = Some("some symbol".as_bytes().to_vec());
assert_eq!(ReadResult::TokenSymbol::<Test>(symbol.clone()).encode(), symbol.encode());
symbol = None;
assert_eq!(ReadResult::TokenSymbol::<Test>(symbol.clone()).encode(), symbol.encode());
}

Expand Down Expand Up @@ -494,21 +498,18 @@ fn token_metadata_works() {
let name: Vec<u8> = vec![11, 12, 13];
let symbol: Vec<u8> = vec![21, 22, 23];
let decimals: u8 = 69;
assert_eq!(Fungibles::read(TokenName(TOKEN)), ReadResult::TokenName(Default::default()));
assert_eq!(
Fungibles::read(TokenSymbol(TOKEN)),
ReadResult::TokenSymbol(Default::default())
);
assert_eq!(
Fungibles::read(TokenDecimals(TOKEN)),
ReadResult::TokenDecimals(Default::default())
);
assert_eq!(Fungibles::read(TokenName(TOKEN)), ReadResult::TokenName(None));
assert_eq!(Fungibles::read(TokenSymbol(TOKEN)), ReadResult::TokenSymbol(None));
assert_eq!(Fungibles::read(TokenDecimals(TOKEN)), ReadResult::TokenDecimals(0));
assets::create_and_set_metadata(ALICE, TOKEN, name.clone(), symbol.clone(), decimals);
assert_eq!(Fungibles::read(TokenName(TOKEN)), ReadResult::TokenName(name));
assert_eq!(Fungibles::read(TokenSymbol(TOKEN)), ReadResult::TokenSymbol(symbol));
assert_eq!(Fungibles::read(TokenName(TOKEN)), ReadResult::TokenName(Some(name)));
assert_eq!(Fungibles::read(TokenSymbol(TOKEN)), ReadResult::TokenSymbol(Some(symbol)));
assert_eq!(Fungibles::read(TokenDecimals(TOKEN)), ReadResult::TokenDecimals(decimals));
assert_eq!(Fungibles::read(TokenName(TOKEN)).encode(), Assets::name(TOKEN).encode());
assert_eq!(Fungibles::read(TokenSymbol(TOKEN)).encode(), Assets::symbol(TOKEN).encode());
assert_eq!(Fungibles::read(TokenName(TOKEN)).encode(), Some(Assets::name(TOKEN)).encode());
assert_eq!(
Fungibles::read(TokenSymbol(TOKEN)).encode(),
Some(Assets::symbol(TOKEN)).encode()
);
assert_eq!(
Fungibles::read(TokenDecimals(TOKEN)).encode(),
Assets::decimals(TOKEN).encode(),
Expand Down Expand Up @@ -677,8 +678,8 @@ mod read_weights {
}

mod ensure_codec_indexes {
use super::{Encode, RuntimeCall, *};
use crate::{fungibles, fungibles::Call::*, mock::RuntimeCall::Fungibles};
use super::{Encode, *};
use crate::{fungibles, mock::RuntimeCall::Fungibles};

#[test]
fn ensure_read_variant_indexes() {
Expand Down
4 changes: 2 additions & 2 deletions pop-api/integration-tests/contracts/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ mod fungibles {
/// - token_decimals

#[ink(message)]
pub fn token_name(&self, token: TokenId) -> Result<Vec<u8>> {
pub fn token_name(&self, token: TokenId) -> Result<Option<Vec<u8>>> {
api::token_name(token)
}

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

Expand Down
15 changes: 6 additions & 9 deletions pop-api/integration-tests/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,15 @@ fn token_metadata_works() {
let decimals: u8 = 69;

// Token does not exist.
assert_eq!(token_name(&addr, TOKEN_ID), Ok(Assets::name(TOKEN_ID)));
assert_eq!(token_name(&addr, TOKEN_ID), Ok(Vec::<u8>::new()));
assert_eq!(token_symbol(&addr, TOKEN_ID), Ok(Assets::symbol(TOKEN_ID)));
assert_eq!(token_symbol(&addr, TOKEN_ID), Ok(Vec::<u8>::new()));
assert_eq!(token_decimals(&addr, TOKEN_ID), Ok(Assets::decimals(TOKEN_ID)));
assert_eq!(token_name(&addr, TOKEN_ID), Ok(None));
assert_eq!(token_symbol(&addr, TOKEN_ID), Ok(None));
assert_eq!(token_decimals(&addr, TOKEN_ID), Ok(0));
// Create Token.
assets::create_and_set_metadata(&addr, TOKEN_ID, name.clone(), symbol.clone(), decimals);
assert_eq!(token_name(&addr, TOKEN_ID), Ok(Assets::name(TOKEN_ID)));
assert_eq!(token_name(&addr, TOKEN_ID), Ok(name));
assert_eq!(token_symbol(&addr, TOKEN_ID), Ok(Assets::symbol(TOKEN_ID)));
assert_eq!(token_symbol(&addr, TOKEN_ID), Ok(symbol));
assert_eq!(token_name(&addr, TOKEN_ID), Ok(Some(Assets::name(TOKEN_ID))));
assert_eq!(token_name(&addr, TOKEN_ID), Ok(Some(name)));
assert_eq!(token_symbol(&addr, TOKEN_ID), Ok(Some(Assets::symbol(TOKEN_ID))));
assert_eq!(token_symbol(&addr, TOKEN_ID), Ok(Some(symbol)));
assert_eq!(token_decimals(&addr, TOKEN_ID), Ok(Assets::decimals(TOKEN_ID)));
assert_eq!(token_decimals(&addr, TOKEN_ID), Ok(decimals));
});
Expand Down
11 changes: 7 additions & 4 deletions pop-api/integration-tests/src/fungibles/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ pub(super) fn allowance(
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

pub(super) fn token_name(addr: &AccountId32, token_id: TokenId) -> Result<Vec<u8>, Error> {
pub(super) fn token_name(addr: &AccountId32, token_id: TokenId) -> Result<Option<Vec<u8>>, Error> {
let result = do_bare_call("token_name", addr, token_id.encode());
decoded::<Result<Vec<u8>, Error>>(result.clone())
decoded::<Result<Option<Vec<u8>>, Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

pub(super) fn token_symbol(addr: &AccountId32, token_id: TokenId) -> Result<Vec<u8>, Error> {
pub(super) fn token_symbol(
addr: &AccountId32,
token_id: TokenId,
) -> Result<Option<Vec<u8>>, Error> {
let result = do_bare_call("token_symbol", addr, token_id.encode());
decoded::<Result<Vec<u8>, Error>>(result.clone())
decoded::<Result<Option<Vec<u8>>, Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

Expand Down
23 changes: 12 additions & 11 deletions pop-api/src/v0/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
//! 3. Management
//! 4. PSP-22 Mintable & Burnable

use crate::{
constants::{ASSETS, BALANCES, FUNGIBLES},
primitives::{AccountId, Balance, TokenId},
ChainExtensionMethodApi, Result, StatusCode,
};
use constants::*;
pub use errors::*;
pub use events::*;
Expand All @@ -19,6 +14,12 @@ pub use management::*;
pub use metadata::*;
pub use traits::*;

use crate::{
constants::{ASSETS, BALANCES, FUNGIBLES},
primitives::{AccountId, Balance, TokenId},
ChainExtensionMethodApi, Result, StatusCode,
};

pub mod errors;
pub mod events;
pub mod traits;
Expand Down Expand Up @@ -177,28 +178,28 @@ pub fn burn(token: TokenId, account: AccountId, value: Balance) -> Result<()> {
pub mod metadata {
use super::*;

/// Returns the name of the specified token.
/// Returns the name of the specified token, if available.
///
/// # Parameters
/// - `token` - The token.
#[inline]
pub fn token_name(token: TokenId) -> Result<Vec<u8>> {
pub fn token_name(token: TokenId) -> Result<Option<Vec<u8>>> {
build_read_state(TOKEN_NAME)
.input::<TokenId>()
.output::<Result<Vec<u8>>, true>()
.output::<Result<Option<Vec<u8>>>, true>()
.handle_error_code::<StatusCode>()
.call(&(token))
}

/// Returns the symbol for the specified token.
/// Returns the symbol for the specified token, if available.
///
/// # Parameters
/// - `token` - The token.
#[inline]
pub fn token_symbol(token: TokenId) -> Result<Vec<u8>> {
pub fn token_symbol(token: TokenId) -> Result<Option<Vec<u8>>> {
build_read_state(TOKEN_SYMBOL)
.input::<TokenId>()
.output::<Result<Vec<u8>>, true>()
.output::<Result<Option<Vec<u8>>>, true>()
.handle_error_code::<StatusCode>()
.call(&(token))
}
Expand Down
Loading