Melt Native Tokens
You may want to melt some of your circulating native tokens. To do so, you will need to call the
Account.melt_native_token(native_token, options)
function.
If you melt your native tokens, you can still destroy the foundry that created them.
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.
- Melt 10 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 decrease_native_token_supply --release
// In this example we will melt an existing native token with its foundry
// 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 melting:\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, the foundry output which minted it, also needs to be
// available.
let token_id = TokenId::from_str("0x08847bd287c912fadedb6bf38900bda9f2d377b75b2a0bece8738699f56ebca4130100000000")?;
// Melt some of the circulating supply
let melt_amount = U256::from(10);
let transaction = account
.decrease_native_token_supply(token_id, melt_amount, None)
.await?;
account
.retry_transaction_until_included(&transaction.transaction_id, None, None)
.await?;
let balance = account.sync(None).await?;
println!("Balance after melting:\n{balance:?}",);
Ok(())
}
Run the example by running the following command:
cargo run --example melt_native_token --release
/**
* This example will melt 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 =
'0x08e6210d29881310db2afde095e594f6f006fcdbd06e7a83b74bd2bdf3b5190d0e0200000000';
// `100` hex encoded
let meltAmount = "0xc8"
const response = await account.decreaseNativeTokenSupply(tokenId, meltAmount);
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 30-decrease-native-token-supply.js
from iota_wallet import IotaWallet
# In this example we will decrease the native token supply
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 = "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0500000000"
melt_amount = "0x20"
# Send transaction.
t = account.decrease_native_token_supply(token_id, melt_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.DecreaseNativeTokenSupply;
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 MeltNativeToken {
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("0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0500000000");
String meltAmount = "0x20";
// Send transaction.
Transaction t = a.meltNativeToken(new DecreaseNativeTokenSupply()
.withTokenId(tokenId)
.withMeltAmount(meltAmount)
);
// 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
{
payload: {
type: 6,
essence: {
type: 1,
networkId: '1856588631910923207',
inputs: [Array],
inputsCommitment: '0x2d6b40731bd6222d0f7670ea5a1b143e499703cf71fd51a821d32d91a77a5041',
outputs: [Array]
},
unlocks: [ [Object], [Object], [Object] ]
},
blockId: '0x350a89dad8a6d96dc5cd5e7abcafa334c287e14ee4187c0260d7b24209b4ff19',
inclusionState: 'Pending',
timestamp: '1671182041013',
transactionId: '0xd7b94f8487a817bc70febb80d83f0df93f1d7035851affaf7114cd7348cb610d',
networkId: '1856588631910923207',
incoming: false,
note: null
}
Check your block on http://localhost:14265/api/core/v2/blocks/0x350a89dad8a6d96dc5cd5e7abcafa334c287e14ee4187c0260d7b24209b4ff19
{
"payload":{
"type":6,
"essence":{
"type":1,
"networkId":"1856588631910923207",
"inputs":[
{
"type":0,
"transactionId":"0xbd9dc768e5676e9d141ab96622143127328c10361f319f919365c480972dbe83",
"transactionOutputIndex":0
},
{
"type":0,
"transactionId":"0xbd9dc768e5676e9d141ab96622143127328c10361f319f919365c480972dbe83",
"transactionOutputIndex":1
},
{
"type":0,
"transactionId":"0xbd9dc768e5676e9d141ab96622143127328c10361f319f919365c480972dbe83",
"transactionOutputIndex":2
}
],
"inputsCommitment":"0xaea8c0e6b4af1afb241b9d4b850b54438dff3e5dfe36a54f1bbaccdc81d1b245",
"outputs":[
{
"type":4,
"amount":"50300",
"aliasId":"0x429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef",
"stateIndex":6,
"stateMetadata":"0x",
"foundriesCounter":0,
"unlockConditions":[
{
"type":4,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
},
{
"type":5,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
},
{
"type":5,
"amount":"52800",
"serialNumber":3,
"tokenScheme":{
"type":0,
"mintedTokens":"0x17",
"meltedTokens":"0x2",
"maximumSupply":"0x64"
},
"unlockConditions":[
{
"type":6,
"address":{
"type":8,
"aliasId":"0x429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef"
}
}
]
},
{
"type":3,
"amount":"57600",
"nativeTokens":[
{
"id":"0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000",
"amount":"0x15"
}
],
"unlockConditions":[
{
"type":0,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
}
]
},
"unlocks":[
{
"type":0,
"signature":{
"type":0,
"publicKey":"0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature":"0xc9c5369388d5fce9297c816612af93eea35b34d1fd4575de9b5ba5f477186e84f2affe6a2ba230beb1d47e3e5ee31f20d484a857e9c3d18fe0fe5e334d6f1202"
}
},
{
"type":2,
"reference":0
},
{
"type":1,
"reference":0
}
]
},
"blockId":"0x063c35701e459cb6515cd0be7d0ed691fd622f5a2dc3bb126213a4f06910ab54",
"inclusionState":"Pending",
"timestamp":"1664885556280",
"transactionId":"0xcbcfbbd9b36bd9a3d6830311ed428036d13fc37050bcd1cf82fb4e4b695570fd",
"networkId":"1856588631910923207",
"incoming":false
}
{
"payload":{
"type":6,
"essence":{
"type":1,
"networkId":"1856588631910923207",
"inputs":[
{
"type":0,
"transactionId":"0xbd9dc768e5676e9d141ab96622143127328c10361f319f919365c480972dbe83",
"transactionOutputIndex":0
},
{
"type":0,
"transactionId":"0xbd9dc768e5676e9d141ab96622143127328c10361f319f919365c480972dbe83",
"transactionOutputIndex":1
},
{
"type":0,
"transactionId":"0xbd9dc768e5676e9d141ab96622143127328c10361f319f919365c480972dbe83",
"transactionOutputIndex":2
}
],
"inputsCommitment":"0xaea8c0e6b4af1afb241b9d4b850b54438dff3e5dfe36a54f1bbaccdc81d1b245",
"outputs":[
{
"type":4,
"amount":"50300",
"aliasId":"0x429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef",
"stateIndex":6,
"stateMetadata":"0x",
"foundriesCounter":0,
"unlockConditions":[
{
"type":4,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
},
{
"type":5,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
},
{
"type":5,
"amount":"52800",
"serialNumber":3,
"tokenScheme":{
"type":0,
"mintedTokens":"0x17",
"meltedTokens":"0x2",
"maximumSupply":"0x64"
},
"unlockConditions":[
{
"type":6,
"address":{
"type":8,
"aliasId":"0x429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef"
}
}
]
},
{
"type":3,
"amount":"57600",
"nativeTokens":[
{
"id":"0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000",
"amount":"0x15"
}
],
"unlockConditions":[
{
"type":0,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
}
]
},
"unlocks":[
{
"type":0,
"signature":{
"type":0,
"publicKey":"0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature":"0xc9c5369388d5fce9297c816612af93eea35b34d1fd4575de9b5ba5f477186e84f2affe6a2ba230beb1d47e3e5ee31f20d484a857e9c3d18fe0fe5e334d6f1202"
}
},
{
"type":2,
"reference":0
},
{
"type":1,
"reference":0
}
]
},
"blockId":"0x063c35701e459cb6515cd0be7d0ed691fd622f5a2dc3bb126213a4f06910ab54",
"inclusionState":"Pending",
"timestamp":"1664885556280",
"transactionId":"0xcbcfbbd9b36bd9a3d6830311ed428036d13fc37050bcd1cf82fb4e4b695570fd",
"networkId":"1856588631910923207",
"incoming":false
}