Destroy a Foundry
You can destroy a foundry by calling the Account.destroy_foundry(foundry_id, options)
function. The function will
destroy a foundry output as long as its circulating
native token supply is zero. Any native tokens
which were minted by other foundries will be sent to the controlling
alias.
Code Example
Before you run the example you should update the foundry_id
to one available in your account that has no available
native tokens. If you have no available
foundries, you can create one by minting a native token. If you've already minted your
tokens but need to empty the foundry, you can decrease your native token supply by melting them.
The following example will:
- Create an account manager.
- Get Alice's account which was created in the first guide.
- Get the account's balance.
- Destroy a foundry output by id.
- Get the account's balance again to show the difference after step 4.
- 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 destroy_foundry --release
// In this example we will destroy an existing foundry output. This is only possible if its circulating supply is 0.
// Rename `.env.example` to `.env` first
use std::{env, str::FromStr};
use dotenv::dotenv;
use iota_client::block::output::FoundryId;
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 destroying:\n{balance:?}",);
// Set the stronghold password
manager
.set_stronghold_password(&env::var("STRONGHOLD_PASSWORD").unwrap())
.await?;
// Replace with an FoundryId that is available in the account
let foundry_id =
FoundryId::from_str("0x0857f1bafae0ef43190597a0dfe72ef1477b769560203c1854c6fb427c486e65300100000000")?;
let transaction = account.destroy_foundry(foundry_id, None).await?;
account
.retry_transaction_until_included(&transaction.transaction_id, None, None)
.await?;
let balance = account.sync(None).await?;
println!("Balance after destroying:\n{balance:?}",);
Ok(())
}
Run the example by running the following command:
cargo run --example destroy_foundry --release
/**
* This example will destroy a foundry
*/
const getUnlockedManager = require('./account-manager');
async function run() {
try {
const manager = await getUnlockedManager();
const account = await manager.getAccount('0');
await account.sync();
// Get a foundry id from your account balance after running example
// 22-mint-native-tokens.js
let foundryId =
'0x08e6210d29881310db2afde095e594f6f006fcdbd06e7a83b74bd2bdf3b5190d0e0200000000';
const response = await account.destroyFoundry(foundryId);
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 32-destroy-foundry.js
from iota_wallet import IotaWallet
# In this example we will destroy a foundry
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.
foundry_id = "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0500000000"
# Send transaction.
t = account.destroy_foundry(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.FoundryId;
import org.iota.types.ids.account.AccountAlias;
import org.iota.types.secret.StrongholdSecretManager;
public class DestroyFoundry {
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.
FoundryId foundryId = new FoundryId("0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0500000000");
// Send transaction.
Transaction t = a.destroyFoundry(new org.iota.types.account_methods.DestroyFoundry().withFoundryId(
foundryId
));
// 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
Balancebeforedestroying: AccountBalance{
base_coin: BaseCoinBalance{
total: 109999491000,
available: 109999491000
},
required_storage_deposit: 3046500,
native_tokens: [
...
],
nfts: [
...
]
aliases: [
...
],
foundries: [
FoundryId(0x086d7755dc84a6757ea28285990ddac2eb53f558f8345507ac76d0c33caacaf8970100000000),
],
potentially_locked_outputs: {
...
}
}Balanceafterdestroying: AccountBalance{
base_coin: BaseCoinBalance{
total: 109999491000,
available: 109999491000
},
required_storage_deposit: 3046500,
native_tokens: [
...
],
nfts: [
...
]
aliases: [
...
],
foundries: [
],
potentially_locked_outputs: {
...
}
}
{
payload: {
type: 6,
essence: {
type: 1,
networkId: '1856588631910923207',
inputs: [Array],
inputsCommitment: '0x3179afe33825e2c9557079291324968a159b6f8b89d0c7478ae3ec38b3b00548',
outputs: [Array]
},
unlocks: [ [Object], [Object] ]
},
blockId: '0xeff7d964dea61e303d92e1a4930d42268e8e7d0a19f53f7a929518cb9e57a50f',
inclusionState: 'Pending',
timestamp: '1671182397354',
transactionId: '0x6de7c4366f0419a6cbc2a39f8d67f0b6e5737b8caa8c029f59cee486b36ab8e9',
networkId: '1856588631910923207',
incoming: false,
note: null
}
Check your block on http://localhost:14265/api/core/v2/blocks/0xeff7d964dea61e303d92e1a4930d42268e8e7d0a19f53f7a929518cb9e57a50f
from iota_wallet import IotaWallet
# In this example we will destroy a foundry
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.
foundry_id = "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0500000000"
# Send transaction.
t = account.destroy_foundry(token_id)
# Print transaction.
print(t)
{
"payload":{
"type":6,
"essence":{
"type":1,
"networkId":"1856588631910923207",
"inputs":[
{
"type":0,
"transactionId":"0x27df04eac1408b4ec758f6aa222d616b21d9b237789e601d9d0b81aa6466fa97",
"transactionOutputIndex":0
},
{
"type":0,
"transactionId":"0x27df04eac1408b4ec758f6aa222d616b21d9b237789e601d9d0b81aa6466fa97",
"transactionOutputIndex":1
}
],
"inputsCommitment":"0x06e8bdc8ddf5dac739e620f815aade7f73d06a33a911e1e6956d1f8d087ad6e3",
"outputs":[
{
"type":4,
"amount":"103100",
"aliasId":"0x429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef",
"stateIndex":9,
"stateMetadata":"0x",
"foundriesCounter":0,
"unlockConditions":[
{
"type":4,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
},
{
"type":5,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
}
]
},
"unlocks":[
{
"type":0,
"signature":{
"type":0,
"publicKey":"0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature":"0xb0a0473eb7dc09231707b11a04ad3a2fee9d67dd83f00e27ddbdc11f4473080ff8ac9a78d5dfb83da8bf1e8e77dc5c6cd867a28dc7520dc0f01dd49ce7815a02"
}
},
{
"type":2,
"reference":0
}
]
},
"blockId":"0xf76fc85a74e1ac5381ff25777fef8a1c9ffb98bdad83157151bc8804bf496861",
"inclusionState":"Pending",
"timestamp":"1664886716685",
"transactionId":"0xce5504e02bf0796e48143d5daa1753500e798dbab673e14bb671512750f7d03a",
"networkId":"1856588631910923207",
"incoming":false
}