Skip to content

Commit

Permalink
das-api: Include mint-extensions in the response
Browse files Browse the repository at this point in the history
  • Loading branch information
niks3089 committed Feb 19, 2024
1 parent 1e04239 commit 93427ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions digital_asset_types/src/dapi/common/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use log::warn;
use mime_guess::Mime;

use sea_orm::DbErr;
use serde_json::Map;
use serde_json::Value;
use std::cmp::Ordering;
use std::collections::HashMap;
Expand Down Expand Up @@ -47,6 +48,30 @@ pub fn file_from_str(str: String) -> File {
}
}

fn filter_non_null_fields(value: Option<&Value>) -> Option<Value> {
match value {
Some(Value::Null) => None,
Some(Value::Object(map)) => {
if map.values().all(|v| matches!(v, Value::Null)) {
None
} else {
let filtered_map: Map<String, Value> = map
.into_iter()
.filter(|(_k, v)| !matches!(v, Value::Null))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();

if filtered_map.is_empty() {
None
} else {
Some(Value::Object(filtered_map))
}
}
}
_ => value.cloned(),
}
}

pub fn build_asset_response(
assets: Vec<FullAsset>,
limit: u64,
Expand Down Expand Up @@ -368,6 +393,8 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
.unwrap_or(false);
let edition_nonce =
safe_select(chain_data_selector, "$.edition_nonce").and_then(|v| v.as_u64());

let mint_ext = filter_non_null_fields(asset.mint_extensions.as_ref());
Ok(RpcAsset {
interface: interface.clone(),
id: bs58::encode(asset.id).into_string(),
Expand Down Expand Up @@ -435,6 +462,7 @@ pub fn asset_to_rpc(asset: FullAsset, options: &Options) -> Result<RpcAsset, DbE
remaining: u.get("remaining").and_then(|t| t.as_u64()).unwrap_or(0),
}),
burnt: asset.burnt,
mint_extensions: mint_ext,
})
}

Expand Down
3 changes: 3 additions & 0 deletions digital_asset_types/src/rpc/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::BTreeMap;

use crate::dao::sea_orm_active_enums::ChainMutability;
use schemars::JsonSchema;
use serde_json::Value;
use {
serde::{Deserialize, Serialize},
std::collections::HashMap,
Expand Down Expand Up @@ -364,4 +365,6 @@ pub struct Asset {
pub supply: Option<Supply>,
pub mutable: bool,
pub burnt: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub mint_extensions: Option<Value>,
}

0 comments on commit 93427ff

Please sign in to comment.