Skip to main content

Mint NFTs

Online Faucet

You can request test funds from the Shimmer Testnet Faucet.

You can use a node running the stardust update to mint non-fungible tokens (NFTs). To do so, you will only need to add some funds to your account, define the native token's options and call the Account.mint_nfts(nft_options, options) function.

Iota.js

You can also find this guide in the native iota.js library

Code Example

The following example will:

  1. Create an account manager.
  2. Get Alice's which was created in the first guide.
  3. Create the NftOptions.
  4. Mint the NFT by calling the Account.mint_nfts(nft_options, options) function.
Dotenv

This example uses dotenv, which is not safe for use in production environments.

// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example mint_nft --release
// In this example we will mint a native token
// Rename `.env.example` to `.env` first

use std::env;

use dotenv::dotenv;
use iota_wallet::{
account_manager::AccountManager,
iota_client::block::output::{
feature::{IssuerFeature, SenderFeature},
unlock_condition::AddressUnlockCondition,
Feature, NftId, NftOutputBuilder, UnlockCondition,
},
NftOptions, Result,
};

#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production
dotenv().ok();

// Create the account manager
let manager = AccountManager::builder().finish().await?;

// Get the account we generated with `01_create_wallet`
let account = manager.get_account("Alice").await?;

account.sync(None).await?;

// Set the stronghold password
manager
.set_stronghold_password(&env::var("STRONGHOLD_PASSWORD").unwrap())
.await?;

let nft_options = vec![NftOptions {
address: Some("rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu".to_string()),
sender: Some("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy".to_string()),
metadata: Some(b"some NFT metadata".to_vec()),
tag: Some(b"some NFT tag".to_vec()),
issuer: Some("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy".to_string()),
immutable_metadata: Some(b"some NFT immutable metadata".to_vec()),
}];

let transaction = account.mint_nfts(nft_options, None).await?;

println!("Transaction: {}.", transaction.transaction_id,);
println!(
"Block sent: {}/api/core/v2/blocks/{}.",
&env::var("NODE_URL").unwrap(),
transaction.block_id.expect("no block created yet")
);

// Build nft output manually
let sender_address = account.addresses().await?[0].address().clone();
let token_supply = account.client().get_token_supply().await?;
let outputs = vec![
// address of the owner of the NFT
NftOutputBuilder::new_with_amount(1_000_000, NftId::null())?
.add_unlock_condition(UnlockCondition::Address(AddressUnlockCondition::new(
*sender_address.as_ref(),
)))
.add_feature(Feature::Sender(SenderFeature::new(*sender_address.as_ref())))
.add_immutable_feature(Feature::Issuer(IssuerFeature::new(*sender_address.as_ref())))
.finish_output(token_supply)?,
];

let transaction = account.send(outputs, None).await?;

println!(
"Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
transaction.transaction_id,
&env::var("NODE_URL").unwrap(),
transaction.block_id.expect("No block created yet")
);

Ok(())
}

Run the Example

Run the example by running the following command:

cargo run --example mint_nft --release

Expected Output

Transaction: 0x0ba6e5d164f4c5ba0e16d9ba42852fa9fa19989956632444fe3ef0bf9d12737c
Block sent: http://localhost:14265/api/core/v2/blocks/0x7d9e46cd59119c6b00f2affd88fe497276b5f6f97874e2ca42e4dae9651d1be2
Transaction: 0xf99a245889f942084b6d9d216c61f2dcb42b6b14fb595bdc92322e87caaa6bbc
Block sent: http://localhost:14265/api/core/v2/blocks/0xce3b0f453f935288356bbfdc43f217b29e3a0ff1f7f08459d08e091c941da4b4