1

I'm owning an alias output and I know its id. How can I access and modify the state of that alias output with my alias id?

Question asked by Genzi God on Discord

1 Answer 1

1

Here the example code that shows how to access and modify the state of an alias

// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! cargo run --example update_alias_output --release
// In this example we will update the state metadata of an alias output
// Rename `.env.example` to `.env` first

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

use iota_sdk::{
    types::block::output::{AliasId, AliasOutputBuilder, Output},
    wallet::{account::FilterOptions, Result, Wallet},
};

#[tokio::main]
async fn main() -> Result<()> {
    //  This example uses secrets in environment variables for simplicity which should not be done in production.
    dotenvy::dotenv().ok();

    // Create the wallet
    let wallet = Wallet::builder().finish().await?;

    // Get the account we generated with `01_create_wallet`
    let account = wallet.get_account("Alice").await?;

    account.sync(None).await?;

    // Set the stronghold password
    wallet
        .set_stronghold_password(&env::var("STRONGHOLD_PASSWORD").unwrap())
        .await?;

    // Replace with an AliasId
    let alias_id = AliasId::from_str("0xc94fc4d280d63c7de09c8cc49ecefba6192e104d200ab7472db9e943e0feef7c")?;

    // Get the alias output by it's alias id
    let alias_output = account
        .unspent_outputs(Some(FilterOptions {
            output_types: Some(vec![4]),
            ..Default::default()
        }))
        .await?
        .into_iter()
        .find_map(|output_data| match &output_data.output {
            Output::Alias(alias_output) => {
                let output_alias_id = alias_output.alias_id_non_null(&output_data.output_id);
                if output_alias_id == alias_id {
                    Some(output_data.clone())
                } else {
                    None
                }
            }
            _ => None,
        })
        .expect("output is not in the unspent outputs");

    let token_supply = account.client().get_token_supply().await?;
    let rent_structure = account.client().get_rent_structure().await?;

    let updated_alias_output = AliasOutputBuilder::from(alias_output.output.as_alias())
        // Minimum required storage deposit will change if the new metadata has a different size, so we will update the
        // amount
        .with_minimum_storage_deposit(rent_structure)
        .with_state_metadata("updated state metadata".as_bytes().to_vec())
        .finish_output(token_supply)?;

    // Send the updated output
    let transaction = account.send(vec![updated_alias_output], None).await?;
    println!(
        "Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
        transaction.transaction_id,
        &env::var("NODE_URL").unwrap(),
        transaction.block_id.expect("no block created yet")
    );

    Ok(())
}

Source

This is a Rust program that updates the state metadata of an output. The output is identified by its alias ID, which is obtained from the environment variables. The program connects to a node, and uses the provided alias ID to locate the output. It then updates the state metadata of the output with a new value, and sends the updated output back to the node.

Here is a brief overview of what the code does:

  • Imports the required libraries and modules
  • Defines the main function of the program, which is annotated with #[tokio::main] to indicate that it will be executed in the context of a Tokio runtime
  • Loads environment variables from a file using the dotenvy library
  • Creates an instance of the wallet using the Wallet builder
  • Obtains the account associated with the alias
  • Synchronizes the account with the node
  • Sets the stronghold password for the wallet
  • Retrieves the alias output by its ID from the unspent outputs of the account
  • Updates the state metadata of the alias output with a new value
  • Sends the updated output back to the node and prints the transaction and block IDs

Note that the program is an example, and should not be used in production without modifications.

Credit goes to Thoralf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.