-
Notifications
You must be signed in to change notification settings - Fork 39
/
request_funds.rs
65 lines (51 loc) · 2.08 KB
/
request_funds.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! In this example we use an alias as wallet.
//! Rename `.env.example` to `.env` first.
//!
//! `cargo run --release --all-features --example alias_wallet_request_funds`
use iota_sdk::{
client::request_funds_from_faucet,
types::block::address::{AliasAddress, ToBech32Ext},
wallet::{
account::{AliasSyncOptions, SyncOptions},
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();
for var in ["FAUCET_URL", "WALLET_DB_PATH"] {
std::env::var(var).expect(&format!(".env variable '{var}' is undefined, see .env.example"));
}
let faucet_url = std::env::var("FAUCET_URL").unwrap();
// Create the wallet
let wallet = Wallet::builder()
.with_storage_path(&std::env::var("WALLET_DB_PATH").unwrap())
.finish()
.await?;
// Get the account
let account = wallet.get_account("Alice").await?;
let balance = account.sync(None).await?;
let total_base_token_balance = balance.base_coin().total();
println!("Balance before requesting funds on alias address: {total_base_token_balance:#?}");
let alias_id = balance.aliases().first().unwrap();
println!("Alias Id: {alias_id}");
// Get alias address
let alias_address = AliasAddress::new(*alias_id).to_bech32(account.client().get_bech32_hrp().await.unwrap());
let faucet_response = request_funds_from_faucet(&faucet_url, &alias_address).await?;
println!("{faucet_response}");
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
let sync_options = SyncOptions {
alias: AliasSyncOptions {
basic_outputs: true,
..Default::default()
},
..Default::default()
};
let total_base_token_balance = account.sync(Some(sync_options)).await?.base_coin().total();
println!("Balance after requesting funds on alias address: {total_base_token_balance:#?}");
Ok(())
}