use candid::CandidType;
use serde::Deserialize;
use crate::common::result::MotokoResult;
use crate::identity::{self_canister_id, to_account_identifier};
use crate::types::NftStorage;
use super::super::types::*;
pub type ExtBearerResult = MotokoResult<ExtAccountIdentifierHex, ExtCommonError>;
#[derive(CandidType, Deserialize, Clone)]
pub struct ExtMintArgs {
pub to: ExtUser, pub metadata: Option<Vec<u8>>,
}
pub trait ExtNonFungible {
fn bearer(&self, token: ExtTokenIdentifier) -> ExtBearerResult;
fn mint_nft(&mut self, args: ExtMintArgs);
}
impl ExtNonFungible for NftStorage {
fn bearer(&self, token: ExtTokenIdentifier) -> ExtBearerResult {
let index = match super::utils::parse_token_index_with_self_canister(&token) {
Ok(index) => index as usize,
Err(e) => return MotokoResult::Err(e),
};
match self.nfts.get(index) {
Some(nft) => MotokoResult::Ok(ExtUser::to_hex(&nft.owner.to_vec())),
None => MotokoResult::Err(ExtCommonError::InvalidToken(token)),
}
}
fn mint_nft(&mut self, args: ExtMintArgs) {
let receiver = args.to.to_account_identity();
let content = args.metadata;
let token_index = self.nfts.len();
self.nfts.push(Nft {
index: token_index, name: super::utils::parse_token_identifier(self_canister_id(), token_index as u32), owner: to_account_identifier(&receiver), approved: None, rarity: String::from(""), content, metadata: vec![], thumbnail: None, ownable: NFTOwnable::None, });
}
}