Burn NFTs
You may want to burn some of your circulating NFTs. To do so, you will need to call the
Account.burn_nft(native_token, options)
function.
Code Example
Replace the native token ID
Before you run the code example, make sure to update the token ID with one which is available in your account. If you haven't done so already, you can follow the how to mint a native token guide. If you don't know the token ID you can check your accounts balance to retrieve the available native tokens in your account.
The following example will:
- Create an account manager.
- Get Alice's account which was created in the first guide.
- Get the account's balance.
- Burn an NFT of the supplied ID.
- Rust
- Nodejs
- Python
- Java
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 burn_nft --release
// In this example we will burn an existing nft output
// Rename `.env.example` to `.env` first
use std::{env, str::FromStr};
use dotenv::dotenv;
use iota_client::block::output::NftId;
use iota_wallet::{account_manager::AccountManager, 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?;
let balance = account.balance().await?;
println!("Balance before burning:\n{balance:?}",);
// Set the stronghold password
manager
.set_stronghold_password(&env::var("STRONGHOLD_PASSWORD").unwrap())
.await?;
// Replace with an NftId that is available in the account
let nft_id = NftId::from_str("0xe192461b30098a5da889ef6abc9e8130bf3b2d980450fa9201e5df404121b932")?;
let transaction = account.burn_nft(nft_id, None).await?;
account
.retry_transaction_until_included(&transaction.transaction_id, None, None)
.await?;
let balance = account.sync(None).await?;
println!("Balance after burning:\n{balance:?}",);
Ok(())
}
Run the example by running the following command:
cargo run --example burn_nft --release
/**
* This example will burn an NFT
*/
const getUnlockedManager = require('./account-manager');
async function run() {
try {
const { initLogger } = require('@iota/wallet');
initLogger({
name: './wallet.log',
levelFilter: 'debug',
targetExclusions: ["h2", "hyper", "rustls"]
});
const manager = await getUnlockedManager();
const account = await manager.getAccount('0');
await account.sync();
//TODO: Replace with an NFT id from your account, you can mint one with `25-mint-nft.js`.
const response = await account.burnNft('0xeb7a7f6b4b8f932ed0d60d5a6018cb51dfa53af1173f9ca8944d1ab49772dd2b');
console.log(response);
console.log(
`Check your block on ${process.env.NODE_URL}/api/core/v2/blocks/${response.blockId}`,
);
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}
run();
You can run the example by running the following command from the wallet/bindings/nodejs/examples/
folder:
node 27-burn-nft.js
from iota_wallet import IotaWallet
# In this example we will burn an NFT
wallet = IotaWallet('./alice-database')
account = wallet.get_account('Alice')
# Sync account with the node
response = account.sync()
print(f'Synced: {response}')
wallet.set_stronghold_password("some_hopefully_secure_password")
# TODO: replace with your own values.
nftId = "0xf95f4d5344217a2ba19a6c19a47f97d267edf8c4d76a7b8c08072ad35acbebbe"
# Send transaction.
t = account.burn_nft(token_id)
# Print transaction.
print(t)
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import org.iota.Wallet;
import org.iota.types.*;
import org.iota.types.account_methods.SyncAccount;
import org.iota.types.exceptions.InitializeWalletException;
import org.iota.types.exceptions.WalletException;
import org.iota.types.ids.NftId;
import org.iota.types.ids.account.AccountAlias;
import org.iota.types.secret.StrongholdSecretManager;
public class BurnNft {
public static void main(String[] args) throws WalletException, InterruptedException, InitializeWalletException {
// This example assumes that a wallet has already been created using the ´SetupWallet.java´ example.
// If you haven't run the ´SetupWallet.java´ example yet, you must run it first to be able to load the wallet as shown below:
Wallet wallet = new Wallet(new WalletConfig()
.withClientOptions(new ClientConfig().withNodes(Env.NODE))
.withSecretManager(new StrongholdSecretManager(Env.STRONGHOLD_PASSWORD, null, Env.STRONGHOLD_VAULT_PATH))
.withCoinType(CoinType.Shimmer)
.withStoragePath(Env.STORAGE_PATH)
);
// Get account and sync it with the registered node to ensure that its balances are up-to-date.
AccountHandle a = wallet.getAccount(new AccountAlias(Env.ACCOUNT_NAME));
a.syncAccount(new SyncAccount().withOptions(new SyncOptions()));
// TODO: replace with your own values.
NftId nftId = new NftId("0xf95f4d5344217a2ba19a6c19a47f97d267edf8c4d76a7b8c08072ad35acbebbe");
// Send transaction.
Transaction t = a.burnNft(new org.iota.types.account_methods.BurnNft()
.withNftId(nftId)
);
// Print transaction.
System.out.println(t);
// In case you are done and don't need the wallet instance anymore you can destroy the instance to clean up memory.
// For this, check out the ´DestroyWallet.java´ example.
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
Balance before burning:
AccountBalance{
base_coin: BaseCoinBalance{
total: 209996955000,
available: 209996955000
},
required_storage_deposit: 902500,
native_tokens: [
],
nfts: [
NftId(0xe314d76664f0ffe8d5fbe670527f108328c14bc8dad89b234dde3ff8deb58ec9)
],
aliases: [
],
foundries: [
],
potentially_locked_outputs: {
}
}
Balance after burning:
AccountBalance{
base_coin: BaseCoinBalance{
total: 209996955000,
available: 209996955000
},
required_storage_deposit: 852000,
native_tokens: [
],
nfts: [
],
aliases: [
],
foundries: [
],
potentially_locked_outputs: {
}
}
{
payload: {
type: 6,
essence: {
type: 1,
networkId: '1856588631910923207',
inputs: [Array],
inputsCommitment: '0x8ed6de2d3570269b4cfe2eb7ad29ee324c1b6ff78ad1f608db2b4e40c03db366',
outputs: [Array]
},
unlocks: [ [Object] ]
},
blockId: '0x2ea37ddeeaad7e6546e22cb92821ada3a213760b6a799c49b68d57a287d68a3b',
inclusionState: 'Pending',
timestamp: '1663000414059',
transactionId: '0x8e178dddc6c65be31ebe9fc980add3a62573d972a15deee03ac2434fc91812c5',
networkId: '1856588631910923207',
incoming: false,
note: null
}
Check your block on http://localhost:14265/api/core/v2/blocks/0x2ea37ddeeaad7e6546e22cb92821ada3a213760b6a799c49b68d57a287d68a3b
{
"payload":{
"type":6,
"essence":{
"type":1,
"networkId":"1856588631910923207",
"inputs":[
{
"type":0,
"transactionId":"0xf5e022f199d90f2020ab941de947368e9d02a4f05139fcb44f66feade216f150",
"transactionOutputIndex":0
}
],
"inputsCommitment":"0xd64acb6fec68315c200484458fb0a2c1e9b7d06106aecdcb9c86403d955ad69a",
"outputs":[
{
"type":3,
"amount":"52000",
"unlockConditions":[
{
"type":0,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
}
]
},
"unlocks":[
{
"type":0,
"signature":{
"type":0,
"publicKey":"0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature":"0xd602074065070b6dada7ce8f5a3c0181a2662443e6e71d7599dcf1464ac04938bdb9b2a39e1f36152cd3226840ab8d63c77d4035a30407ab029b607a7ff4d108"
}
}
]
},
"blockId":"0x6bc73ef16d51924154fecf4cc2da1b14297b1aa75ff5261800ab88411185d7c2",
"inclusionState":"Pending",
"timestamp":"1664876190299",
"transactionId":"0x9c26110d611ea78fc0542dca45834abb35497c5ec6045448713f5a588e7f4292",
"networkId":"1856588631910923207",
"incoming":false
}
{
"payload":{
"type":6,
"essence":{
"type":1,
"networkId":"1856588631910923207",
"inputs":[
{
"type":0,
"transactionId":"0xf5e022f199d90f2020ab941de947368e9d02a4f05139fcb44f66feade216f150",
"transactionOutputIndex":0
}
],
"inputsCommitment":"0xd64acb6fec68315c200484458fb0a2c1e9b7d06106aecdcb9c86403d955ad69a",
"outputs":[
{
"type":3,
"amount":"52000",
"unlockConditions":[
{
"type":0,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
}
]
},
"unlocks":[
{
"type":0,
"signature":{
"type":0,
"publicKey":"0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature":"0xd602074065070b6dada7ce8f5a3c0181a2662443e6e71d7599dcf1464ac04938bdb9b2a39e1f36152cd3226840ab8d63c77d4035a30407ab029b607a7ff4d108"
}
}
]
},
"blockId":"0x6bc73ef16d51924154fecf4cc2da1b14297b1aa75ff5261800ab88411185d7c2",
"inclusionState":"Pending",
"timestamp":"1664876190299",
"transactionId":"0x9c26110d611ea78fc0542dca45834abb35497c5ec6045448713f5a588e7f4292",
"networkId":"1856588631910923207",
"incoming":false
}