From 46c39cd0f349d7a46ed44a56695cf735e2d12746 Mon Sep 17 00:00:00 2001 From: Boris Oncev Date: Mon, 25 Mar 2024 21:14:01 +0100 Subject: [PATCH] fix nft issuance in API Server endpoints --- api-server/stack-test-suite/tests/v2/nft.rs | 17 +------ api-server/web-server/src/api/json_helpers.rs | 48 ++++++++++++++++--- api-server/web-server/src/api/v2.rs | 21 ++------ 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/api-server/stack-test-suite/tests/v2/nft.rs b/api-server/stack-test-suite/tests/v2/nft.rs index b7720c979d..ef15c54ef5 100644 --- a/api-server/stack-test-suite/tests/v2/nft.rs +++ b/api-server/stack-test-suite/tests/v2/nft.rs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use api_web_server::api::json_helpers::to_json_string; +use api_web_server::api::json_helpers::nft_issuance_data_to_json; use common::{ chain::tokens::{make_token_id, NftIssuance, NftIssuanceV0, TokenId}, primitives::H256, @@ -126,20 +126,7 @@ async fn ok(#[case] seed: Seed) { _ = tx.send([( token_id, - json!({ - "authority": nft.metadata.creator - .map(|creator| Address::new(&chain_config, Destination::PublicKey(creator.public_key)) - .expect("no error in encoding") - .as_str().to_owned() - ), - "name": nft.metadata.name, - "description": nft.metadata.description, - "ticker": to_json_string(&nft.metadata.ticker), - "icon_uri": nft.metadata.icon_uri.as_ref().as_ref().map(|b| to_json_string(b)), - "additional_metadata_uri": nft.metadata.additional_metadata_uri.as_ref().as_ref().map(|b| to_json_string(b)), - "media_uri": nft.metadata.media_uri.as_ref().as_ref().map(|b| to_json_string(b)), - "media_hash": to_json_string(&nft.metadata.media_hash), - }), + nft_issuance_data_to_json(&NftIssuance::V0(nft), &chain_config), )]); chainstate_block_ids diff --git a/api-server/web-server/src/api/json_helpers.rs b/api-server/web-server/src/api/json_helpers.rs index 813d32ea37..be69e9c002 100644 --- a/api-server/web-server/src/api/json_helpers.rs +++ b/api-server/web-server/src/api/json_helpers.rs @@ -23,9 +23,9 @@ use common::{ chain::{ block::ConsensusData, output_value::OutputValue, - tokens::{IsTokenUnfreezable, TokenId}, - AccountCommand, AccountSpending, Block, ChainConfig, OutPointSourceId, Transaction, - TxInput, TxOutput, UtxoOutPoint, + tokens::{IsTokenUnfreezable, NftIssuance, TokenId, TokenTotalSupply}, + AccountCommand, AccountSpending, Block, ChainConfig, Destination, OutPointSourceId, + Transaction, TxInput, TxOutput, UtxoOutPoint, }, primitives::{Amount, BlockHeight, Idable}, Uint256, @@ -147,17 +147,24 @@ pub fn txoutput_to_json( "type": "IssueNft", "token_id": Address::new(chain_config, *token_id).expect("no error").as_str(), "destination": Address::new(chain_config, dest.clone()).expect("no error").as_str(), - "data": data, + "data": nft_issuance_data_to_json(data, chain_config), }) } TxOutput::IssueFungibleToken(data) => match data.as_ref() { common::chain::tokens::TokenIssuance::V1(data) => { + let supply = match data.total_supply { + TokenTotalSupply::Lockable => json!({"type": "Lockable"}), + TokenTotalSupply::Unlimited => json!({"type": "Unlimited"}), + TokenTotalSupply::Fixed(amount) => { + json!({"type": "Fixed", "amount": amount_to_json(amount, data.number_of_decimals)}) + } + }; json!({ "type": "IssueFungibleToken", - "token_ticker": data.token_ticker, + "token_ticker": to_json_string(&data.token_ticker), "number_of_decimals": data.number_of_decimals, - "metadata_uri": data.metadata_uri, - "total_supply": data.total_supply, + "metadata_uri": to_json_string(&data.metadata_uri), + "total_supply": supply, "authority": Address::new(chain_config, data.authority.clone()).expect("no error").as_str(), "is_freezable": data.is_freezable, }) @@ -179,6 +186,33 @@ pub fn txoutput_to_json( } } +pub fn nft_issuance_data_to_json( + data: &NftIssuance, + chain_config: &ChainConfig, +) -> serde_json::Value { + match data { + NftIssuance::V0(data) => { + json!({ + "creator": data.metadata.creator.as_ref().map(|addr| { + Address::new( + chain_config, + Destination::PublicKey(addr.public_key.clone()), + ) + .expect("no error") + .into_string() + }), + "name": to_json_string(&data.metadata.name), + "description": to_json_string(&data.metadata.description), + "ticker": to_json_string(&data.metadata.ticker), + "icon_uri": data.metadata.icon_uri.as_opt_slice().map(to_json_string), + "additional_metadata_uri": data.metadata.additional_metadata_uri.as_opt_slice().map(to_json_string), + "media_uri": data.metadata.media_uri.as_opt_slice().map(to_json_string), + "media_hash": to_json_string(&data.metadata.media_hash), + }) + } + } +} + pub fn utxo_outpoint_to_json(utxo: &UtxoOutPoint) -> serde_json::Value { match utxo.source_id() { OutPointSourceId::Transaction(tx_id) => { diff --git a/api-server/web-server/src/api/v2.rs b/api-server/web-server/src/api/v2.rs index f5733f0c57..85ff564e10 100644 --- a/api-server/web-server/src/api/v2.rs +++ b/api-server/web-server/src/api/v2.rs @@ -38,7 +38,7 @@ use common::{ address::Address, chain::{ block::timestamp::BlockTimestamp, - tokens::{IsTokenFreezable, IsTokenFrozen, IsTokenUnfreezable, NftIssuance}, + tokens::{IsTokenFreezable, IsTokenFrozen, IsTokenUnfreezable}, Block, Destination, SignedTransaction, Transaction, }, primitives::{Amount, BlockHeight, CoinOrTokenId, Id, Idable, H256}, @@ -52,7 +52,7 @@ use utils::ensure; use crate::ApiServerWebServerState; -use super::json_helpers::to_json_string; +use super::json_helpers::{nft_issuance_data_to_json, to_json_string}; pub const API_VERSION: &str = "2.0.0"; @@ -1107,20 +1107,5 @@ pub async fn nft( ApiServerWebServerNotFoundError::NftNotFound, ))?; - match nft { - NftIssuance::V0(nft) => Ok(Json(json!({ - "authority": nft.metadata.creator - .map(|creator| Address::new(&state.chain_config, Destination::PublicKey(creator.public_key)) - .expect("no error in encoding") - .as_str().to_owned() - ), - "name": nft.metadata.name, - "description": nft.metadata.description, - "ticker": to_json_string(&nft.metadata.ticker), - "icon_uri": nft.metadata.icon_uri.as_ref().as_ref().map(|b| to_json_string(b)), - "additional_metadata_uri": nft.metadata.additional_metadata_uri.as_ref().as_ref().map(|b| to_json_string(b)), - "media_uri": nft.metadata.media_uri.as_ref().as_ref().map(|b| to_json_string(b)), - "media_hash": to_json_string(&nft.metadata.media_hash), - }))), - } + Ok(Json(nft_issuance_data_to_json(&nft, &state.chain_config))) }