Skip to main content

Destroy an Alias Output

You can destroy an alias output by ID using the Account.destroy_alias(alias_id, options) function. If the alias still owns any outputs when you try to destroy it, you will get an error.

Code Example

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 an alias 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_alias --release
// In this example we will destroy an existing alias output. This is only possible if possible foundry outputs have
// circulating supply of 0. Rename `.env.example` to `.env` first

use std::{env, str::FromStr};

use dotenv::dotenv;
use iota_client::block::output::AliasId;
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 AliasId that is available in the account
let alias_id = AliasId::from_str("0x57f1bafae0ef43190597a0dfe72ef1477b769560203c1854c6fb427c486e6530")?;
let transaction = account.destroy_alias(alias_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(())
}

Before you run the example you should update the alias_id to one available in your account.

Run the example by running the following command:

cargo run --example destroy_alias --release

Expected Output

Balancebeforedestroying: AccountBalance{
base_coin: BaseCoinBalance{
total: 109999491000,
available: 109999491000
},
required_storage_deposit: 3155000,
native_tokens: [
...
],
nfts: [
NftId(0x346cf240e9e56132295b0d648fde7037ad0b2e43a35c46668313e1be2970c08c),
NftId(0x79862038299b19b704c10da655afedd77c930278ae56f7a4cb9bca4bf6dd7313)
],
aliases: [
AliasId(0x0aa4fe362b61da2d4c11909162939c0fe20913e2edf5c12ea15cf92f6b36ef60),
],
[...]
}

Balanceafterdestroying: AccountBalance{
base_coin: BaseCoinBalance{
total: 109999491000,
available: 109999491000
},
required_storage_deposit: 3155000,
native_tokens: [
...
],
nfts: [
NftId(0x346cf240e9e56132295b0d648fde7037ad0b2e43a35c46668313e1be2970c08c),
NftId(0x79862038299b19b704c10da655afedd77c930278ae56f7a4cb9bca4bf6dd7313)
],
aliases: [
],
[...]
}