Skip to content

Commit

Permalink
Merge pull request #1702 from mintlayer/fix/api-server-nft-responses
Browse files Browse the repository at this point in the history
fix nft issuance in API Server endpoints
  • Loading branch information
TheQuantumPhysicist authored Mar 29, 2024
2 parents 2c0e8bb + 46c39cd commit e8fc324
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
17 changes: 2 additions & 15 deletions api-server/stack-test-suite/tests/v2/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
48 changes: 41 additions & 7 deletions api-server/web-server/src/api/json_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
})
Expand All @@ -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) => {
Expand Down
21 changes: 3 additions & 18 deletions api-server/web-server/src/api/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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";

Expand Down Expand Up @@ -1107,20 +1107,5 @@ pub async fn nft<T: ApiServerStorage>(
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)))
}

0 comments on commit e8fc324

Please sign in to comment.