Skip to content

Commit

Permalink
show-nft
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelaced committed Aug 27, 2024
1 parent 21c3717 commit c109fa3
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ clap = { version = "4", features = ["derive"] }
dirs = { version = "5" }
pallet-nfts = { version = "31" }
colored = "2.1"
spinners = "4.1"

[features]
default = []
Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ pub enum Commands {
#[arg(value_name = "NFT_ID")]
nft_id: u32,
},
ShowNft {
#[arg(value_name = "COLLECTION_ID")]
collection_id: u32,
#[arg(value_name = "NFT_ID")]
nft_id: u32,
},
/// Set the account to use for cli
SetAccount {
#[arg(long, value_name = "MNEMONIC", conflicts_with = "secret_uri")]
Expand Down
69 changes: 59 additions & 10 deletions src/commands/mint.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::error::Result;
use colored::*;
use spinners::{Spinner, Spinners};
use subxt::{
OnlineClient, PolkadotConfig,
utils::{AccountId32, MultiAddress},
};
use crate::commands::statemint;
use pallet_nfts::{CollectionSettings, ItemSettings};
use std::marker::PhantomData;
use std::time::Duration;
use tokio::time::sleep;

// Function to convert CollectionSettings to the required BitFlags1 type
fn to_collection_bitflags(
Expand Down Expand Up @@ -52,24 +55,47 @@ pub async fn mint_collection() -> Result<()> {

let payload = statemint::tx().nfts().create(admin, config);

// Start the spinner for preparation
let mut sp = Spinner::new(Spinners::Dots12, "⏳ Preparing transaction...".yellow().bold().to_string());
sleep(Duration::from_secs(1)).await;
sp.stop_and_persist("🚀", "Sending transaction to the network...".yellow().bold().to_string());

let extrinsic_result = api
.tx()
.sign_and_submit_then_watch_default(&payload, &account_signer)
.await?
.wait_for_finalized_success()
.await?;

// Update the spinner for finalization with periodic status updates
let mut sp = Spinner::new(Spinners::Dots12, "⏳ Finalizing transaction...".yellow().bold().to_string());

for i in 1..=5 {
sleep(Duration::from_secs(2)).await;
sp.stop_and_persist(
"⏳",
format!("Finalizing transaction... ({}s)", i * 2)
.yellow()
.bold()
.to_string(),
);
sp = Spinner::new(Spinners::Dots12, "".into());
}

let extrinsic_result = extrinsic_result.wait_for_finalized_success().await?;

// Stop the spinner with a final message
sp.stop_and_persist("✅", "Collection creation finalized!".green().bold().to_string());

let extrinsic_hash = extrinsic_result.extrinsic_hash();

// Find the `Created` event
let created_event = extrinsic_result.find_first::<statemint::nfts::events::Created>()?;

if let Some(event) = created_event {
if let statemint::nfts::events::Created { collection, .. } = event {
println!("\n{}\n", "🎉 Collection Created Successfully!".blue().bold());
println!(
"{}: {}",
"Collection ID".yellow().bold(),
"📦 Collection ID".cyan().bold(),
collection.to_string().bright_white()
);
}
Expand All @@ -79,7 +105,7 @@ pub async fn mint_collection() -> Result<()> {

println!(
"{}: {}",
"Extrinsic Hash".yellow().bold(),
"🔗 Extrinsic Hash".cyan().bold(),
format!("{:?}", extrinsic_hash).bright_white()
);

Expand All @@ -100,13 +126,36 @@ pub async fn mint_nft(collection_id: u32, nft_id: u32) -> Result<()> {
.nfts()
.mint(collection_id, nft_id, account.clone(), witness);

// Start the spinner for preparation
let mut sp = Spinner::new(Spinners::Dots12, "⏳ Preparing transaction...".yellow().bold().to_string());
sleep(Duration::from_secs(1)).await;
sp.stop_and_persist("🚀", "Sending transaction to the network...".yellow().bold().to_string());

let extrinsic_result = api
.tx()
.sign_and_submit_then_watch_default(&nft_creation_tx, &account_signer)
.await?
.wait_for_finalized_success()
.await?;

// Update the spinner for finalization with periodic status updates
let mut sp = Spinner::new(Spinners::Dots12, "⏳ Finalizing transaction...".yellow().bold().to_string());

for i in 1..=5 {
sleep(Duration::from_secs(2)).await;
sp.stop_and_persist(
"⏳",
format!("Finalizing transaction... ({}s)", i * 2)
.yellow()
.bold()
.to_string(),
);
sp = Spinner::new(Spinners::Dots12, "".into());
}

let extrinsic_result = extrinsic_result.wait_for_finalized_success().await?;

// Stop the spinner with a final message
sp.stop_and_persist("✅", "NFT minting finalized!".green().bold().to_string());

let extrinsic_hash = extrinsic_result.extrinsic_hash();

// Find the `Minted` event or other relevant event
Expand All @@ -117,12 +166,12 @@ pub async fn mint_nft(collection_id: u32, nft_id: u32) -> Result<()> {
println!("\n{}\n", "🎉 NFT Minted Successfully!".blue().bold());
println!(
"{}: {}",
"Collection ID".yellow().bold(),
"📦 Collection ID".cyan().bold(),
collection.to_string().bright_white()
);
println!(
"{}: {}",
"NFT ID".yellow().bold(),
"🎨 NFT ID".cyan().bold(),
item.to_string().bright_white()
);
}
Expand All @@ -132,7 +181,7 @@ pub async fn mint_nft(collection_id: u32, nft_id: u32) -> Result<()> {

println!(
"{}: {}",
"Extrinsic Hash".yellow().bold(),
"🔗 Extrinsic Hash".cyan().bold(),
format!("{:?}", extrinsic_hash).bright_white()
);

Expand Down
4 changes: 3 additions & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use crate::config::set_account;
use crate::error::Result;
#[cfg(feature = "nft")]
use crate::commands::mint::{mint_collection, mint_nft};
use crate::commands::show_nft::show_nft;

#[subxt::subxt(runtime_metadata_path = "metadata.scale")]
pub mod statemint {}

pub mod account;
pub mod balance;
pub mod mint;

pub mod show_nft;


pub async fn run_command(cli: Commands) -> Result<()> {
Expand All @@ -22,6 +23,7 @@ pub async fn run_command(cli: Commands) -> Result<()> {
Commands::MintCollection => mint_collection().await,
#[cfg(feature = "nft")]
Commands::MintNft { collection_id, nft_id } => mint_nft(collection_id, nft_id).await,
Commands::ShowNft { collection_id, nft_id } => show_nft(collection_id, nft_id).await,
Commands::SetAccount { mnemonic, secret_uri } => set_account(mnemonic, secret_uri).await,
Commands::Balance { address } => balance::balance(address).await,
Commands::Account { public_key } => account_info(public_key).await,
Expand Down
47 changes: 47 additions & 0 deletions src/commands/show_nft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::error::Result;
use colored::*;
use spinners::{Spinner, Spinners};
use subxt::{
OnlineClient, PolkadotConfig,
utils::{AccountId32, MultiAddress},
};
use crate::commands::statemint;
use std::time::Duration;
use tokio::time::sleep;

// Function to convert AccountId32 to SS58 format
fn format_account_ss58(account_id: &AccountId32) -> String {
account_id.to_string()
}

#[cfg(feature = "nft")]
pub async fn show_nft(collection_id: u32, nft_id: u32) -> Result<()> {
let api = OnlineClient::<PolkadotConfig>::from_url("wss://asset-hub-paseo-rpc.dwellir.com").await?;
println!("{}", "🔍 Fetching NFT information...".green().bold());

// Start the spinner
let mut sp = Spinner::new(Spinners::Dots12, "⏳ Retrieving NFT data...".yellow().bold().to_string());
sleep(Duration::from_secs(1)).await;

// Use the API to query the NFT data based on `collection_id` and `nft_id`
let storage_query = statemint::storage().nfts().item(collection_id, nft_id);
let nft_info = api.storage().at_latest().await?.fetch(&storage_query).await?;

// Stop the spinner with a final message
sp.stop_and_persist("✅", "NFT data retrieved!".green().bold().to_string());

// Display the NFT information
if let Some(info) = nft_info {
println!("\n{}\n", "🎨 NFT Information".blue().bold());
println!("{}: {}", "Collection ID".cyan().bold(), collection_id.to_string().bright_white());
println!("{}: {}", "NFT ID".cyan().bold(), nft_id.to_string().bright_white());
println!("{}: {:?}", "Owner".cyan().bold(), format_account_ss58(&info.owner));
println!("{}: {:?}", "Approvals".cyan().bold(), info.approvals);
println!("{}: {:?}", "Deposit".cyan().bold(), info.deposit);
} else {
println!("{}", "❌ NFT not found.".red().bold());
}

Ok(())
}

0 comments on commit c109fa3

Please sign in to comment.