Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: foundry destroy example #1087

Merged
merged 6 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Wallet::get_or_create_account` convenience method;
- `Output::kind_str()` method;
- `ConflictReason` display implementation with an explanation of the conflict;
- `TokenScheme` methods `is_simple` and `as_simple`;

### Changed

Expand Down
49 changes: 47 additions & 2 deletions sdk/examples/how_tos/native_tokens/destroy_foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
//! cargo run --release --all-features --example destroy_foundry
//! ```

use iota_sdk::{wallet::Result, Wallet};
use iota_sdk::{
types::block::output::TokenId,
wallet::{account::types::NativeTokensBalance, Result},
Wallet,
};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -34,12 +38,51 @@ async fn main() -> Result<()> {
let foundry_count = balance.foundries().len();
println!("Foundries before destroying: {foundry_count}");

let token_id = TokenId::from(*foundry_id);

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

let transaction = account.burn(*foundry_id, None).await?;
// Find the native tokens balance for this foundry if one exists.
let native_tokens: Option<&NativeTokensBalance> = balance
.native_tokens()
.iter()
.find(|native_token| *native_token.token_id() == token_id);
if let Some(native_token) = native_tokens {
let output = account.get_foundry_output(token_id).await?;
// Check if all tokens are melted.
if native_token.available() != output.as_foundry().token_scheme().as_simple().circulating_supply() {
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
// We are not able to melt all tokens, because we dont own them or they are not unlocked.
println!("We dont own all remaining tokens, aborting foundry destruction.");
return Ok(());
}

println!("Melting remaining tokens..");
// Melt all tokens so we can destroy the foundry.
let transaction = account
.melt_native_token(token_id, native_token.available(), None)
.await?;
println!("Transaction sent: {}", transaction.transaction_id);

let block_id = account
.retry_transaction_until_included(&transaction.transaction_id, None, None)
.await?;
println!(
"Block included: {}/block/{}",
std::env::var("EXPLORER_URL").unwrap(),
block_id
);

// Sync to make the foundry output available again, because it was used in the melting transaction.
account.sync(None).await?;
}
println!("Destroying foundry..");

let transaction = account.prepare_burn(*foundry_id, None).await?;

let transaction = account.sign_and_submit_transaction(transaction, None).await?;
println!("Transaction sent: {}", transaction.transaction_id);

let block_id = account
Expand All @@ -51,6 +94,8 @@ async fn main() -> Result<()> {
block_id
);

// Resync to update the foundries list.
let balance = account.sync(None).await?;
let foundry_count = balance.foundries().len();
println!("Foundries after destroying: {foundry_count}");
} else {
Expand Down
12 changes: 12 additions & 0 deletions sdk/src/types/block/output/token_scheme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ impl TokenScheme {
Self::Simple(_) => SimpleTokenScheme::KIND,
}
}

/// Checks whether the token scheme is a [`SimpleTokenScheme`].
pub fn is_simple(&self) -> bool {
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
matches!(self, Self::Simple(_))
}

/// Gets the token scheme as an actual [`SimpleTokenScheme`].
/// PANIC: do not call on a non-simple token scheme.
pub fn as_simple(&self) -> &SimpleTokenScheme {
let Self::Simple(scheme) = self;
scheme
}
}

pub(crate) mod dto {
Expand Down
Loading