Skip to main content

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:

  1. Create an account manager.
  2. Get Alice's account which was created in the first guide.
  3. Get the account's balance.
  4. Destroy a foundry output by id.
  5. Get the account's balance again to show the difference after step 4.
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 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

Expected Output

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: {
...
}
}