Burn Native Tokens
You may want to burn some of your circulating native tokens. To do so, you will need to call the
Account.burn_native_token(native_token, options)
function.
If you burn your native tokens, you will not be able to destroy the foundry that created them. If you want to destroy the foundry in the future, you should consider melting your native tokens
Code Example
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 1 native tokens of the supplied ID.
- Rust
- Nodejs
- Python
- Java
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_native_token --release
// In this example we will burn an existing native token, this will not increase the melted supply in the foundry,
// therefore the foundry output is also not required. But this will also make it impossible to destroy the foundry
// output that minted it.
// Rename `.env.example` to `.env` first
use std::{env, str::FromStr};
use dotenv::dotenv;
use iota_client::block::output::TokenId;
use iota_wallet::{account_manager::AccountManager, Result, U256};
#[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 a TokenId that is available in the account
let token_id = TokenId::from_str("0x08847bd287c912fadedb6bf38900bda9f2d377b75b2a0bece8738699f56ebca4130100000000")?;
// Burn a native token
let burn_amount = U256::from(1);
let transaction = account.burn_native_token(token_id, burn_amount, 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_native_token --release
/**
* This example will burn native tokens
*/
const getUnlockedManager = require('./account-manager');
async function run() {
try {
const manager = await getUnlockedManager();
const account = await manager.getAccount('0');
await account.sync();
// Get a tokenId from your account balance after running example
// 22-mint-native-tokens.js
let tokenId =
'0x08e81324605a946192ad414cf10da992ba5b97001ed4242de084e72cf19a924f7b0100000000';
// `100` hex encoded
let burnAmount = "0x64"
const response = await account.burnNativeToken(tokenId, burnAmount);
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 31-burn-native-token.js
from iota_wallet import IotaWallet
# In this example we will burn native tokens
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.
token_id = "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000"
burn_amount = "0x5"
# Send transaction.
t = account.burn_native_token(token_id, burn_amount)
# 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.TokenId;
import org.iota.types.ids.account.AccountAlias;
import org.iota.types.secret.StrongholdSecretManager;
public class BurnNativeToken {
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.
TokenId tokenId = new TokenId("0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000");
String burnAmount = "0x5";
// Send transaction.
Transaction t = a.burnNativeToken(new org.iota.types.account_methods.BurnNativeToken()
.withTokenId(tokenId)
.withBurnAmount(burnAmount)
);
// 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
Balancebeforeburning:
AccountBalance{
base_coin: BaseCoinBalance{
total: 209995515000,
available: 209995515000
},
required_storage_deposit: 1787500,
native_tokens: [
NativeTokensBalance{
token_id: TokenId(0x08adbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde0300000000),
total: 100,
available: 100
}
],
nfts: [
],
aliases: [
AliasId(0xadbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde)
],
foundries: [
FoundryId(0x08adbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde0200000000),
FoundryId(0x08adbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde0100000000),
FoundryId(0x08adbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde0300000000)
],
potentially_locked_outputs: {
OutputId(0x452a212cbe8512ea7844759402f1679bc1c1e6a088f1f3faaed6aeaa0ac246ee0000): false,
OutputId(0xb8572c3386616ae4be668a46756fcc46bf9cdf178bd50122cf70c81dbec362200000): false,
OutputId(0x041a90dc4d9b23a3ef91c64b7aa69da27e90d3306f644fc7b1647016925d5ff30000): false,
OutputId(0xcb30fa5cbb34b7bb10c0408b4e2bbe323b73fcb9b30b0a9446c1866210cf82d80000): false,
OutputId(0x2c0a9874a309ac3ffce684a8d5f38be54f12760d40ef8fd72f043a11fd4b76110000): false
}
}
Balanceafterburning:
AccountBalance{
base_coin: BaseCoinBalance{
total: 209995515000,
available: 209995515000
},
required_storage_deposit: 1787500,
native_tokens: [
NativeTokensBalance{
token_id: TokenId(0x08adbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde0300000000),
total: 99,
available: 99
}
],
nfts: [
],
aliases: [
AliasId(0xadbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde)
],
foundries: [
FoundryId(0x08adbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde0200000000),
FoundryId(0x08adbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde0100000000),
FoundryId(0x08adbd5f0844fdc9bf4eafe2df929ac42cc90336e68da284cce96f22523bd87cde0300000000)
],
potentially_locked_outputs: {
OutputId(0x2c0a9874a309ac3ffce684a8d5f38be54f12760d40ef8fd72f043a11fd4b76110000): false,
OutputId(0x452a212cbe8512ea7844759402f1679bc1c1e6a088f1f3faaed6aeaa0ac246ee0000): false,
OutputId(0x041a90dc4d9b23a3ef91c64b7aa69da27e90d3306f644fc7b1647016925d5ff30000): false,
OutputId(0xb8572c3386616ae4be668a46756fcc46bf9cdf178bd50122cf70c81dbec362200000): false,
OutputId(0xcb30fa5cbb34b7bb10c0408b4e2bbe323b73fcb9b30b0a9446c1866210cf82d80000): false
}
}
{
payload: {
type: 6,
essence: {
type: 1,
networkId: '1856588631910923207',
inputs: [Array],
inputsCommitment: '0xbbeedfa08b1136421e6c0ad599ada29ccadd326535262de9edadbfe9475b29cf',
outputs: [Array]
},
unlocks: [ [Object] ]
},
blockId: '0xf6bba8d67f6f35bc63658f12df5649f71cbc28512e4f3aa65fda727fa2ae4e5b',
inclusionState: 'Pending',
timestamp: '1671182074342',
transactionId: '0x13df212d5f8ae281226543d7888491f866fbe669b160a9d2893674ff1a00a81a',
networkId: '1856588631910923207',
incoming: false,
note: null
}
Check your block on http://localhost:14265/api/core/v2/blocks/0xf6bba8d67f6f35bc63658f12df5649f71cbc28512e4f3aa65fda727fa2ae4e5b
{
"payload":{
"type":6,
"essence":{
"type":1,
"networkId":"1856588631910923207",
"inputs":[
{
"type":0,
"transactionId":"0xcbcfbbd9b36bd9a3d6830311ed428036d13fc37050bcd1cf82fb4e4b695570fd",
"transactionOutputIndex":2
}
],
"inputsCommitment":"0x3f44d0e0f31bd57f028d80d8c9b07b0e12406e6cd5b2e8b6ed0abd178a1e1b1b",
"outputs":[
{
"type":3,
"amount":"57600",
"nativeTokens":[
{
"id":"0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000",
"amount":"0x10"
}
],
"unlockConditions":[
{
"type":0,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
}
]
},
"unlocks":[
{
"type":0,
"signature":{
"type":0,
"publicKey":"0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature":"0xfd9964cdcfa6bda25419968c3d74d29e35611bbc2a9ddad2be0eadc816da270fcf739c2e7ecc4be4e46f33b32aa37370534e0ee322c23755ecb608de9618ae0a"
}
}
]
},
"blockId":"0xb53140f385372ca247d7d66ee14755a2e18d8ca750fd85122a6b5b6b0c1abf7c",
"inclusionState":"Pending",
"timestamp":"1664885731096",
"transactionId":"0xbb7f884e81ed32dbfe4f9c880b0f6577c9c90744b7ab33d84950706d2924c18c",
"networkId":"1856588631910923207",
"incoming":false
}
{
"payload":{
"type":6,
"essence":{
"type":1,
"networkId":"1856588631910923207",
"inputs":[
{
"type":0,
"transactionId":"0xcbcfbbd9b36bd9a3d6830311ed428036d13fc37050bcd1cf82fb4e4b695570fd",
"transactionOutputIndex":2
}
],
"inputsCommitment":"0x3f44d0e0f31bd57f028d80d8c9b07b0e12406e6cd5b2e8b6ed0abd178a1e1b1b",
"outputs":[
{
"type":3,
"amount":"57600",
"nativeTokens":[
{
"id":"0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000",
"amount":"0x10"
}
],
"unlockConditions":[
{
"type":0,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
}
]
},
"unlocks":[
{
"type":0,
"signature":{
"type":0,
"publicKey":"0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature":"0xfd9964cdcfa6bda25419968c3d74d29e35611bbc2a9ddad2be0eadc816da270fcf739c2e7ecc4be4e46f33b32aa37370534e0ee322c23755ecb608de9618ae0a"
}
}
]
},
"blockId":"0xb53140f385372ca247d7d66ee14755a2e18d8ca750fd85122a6b5b6b0c1abf7c",
"inclusionState":"Pending",
"timestamp":"1664885731096",
"transactionId":"0xbb7f884e81ed32dbfe4f9c880b0f6577c9c90744b7ab33d84950706d2924c18c",
"networkId":"1856588631910923207",
"incoming":false
}