forked from iotaledger/iota.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.rs
106 lines (90 loc) · 4.05 KB
/
transaction.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! cargo run --example transaction --release
use iota_client::{Client, Result, Seed};
extern crate dotenv;
use dotenv::dotenv;
use std::env;
/// In this example we will send 9_000_000 tokens to the following 3 locations, respectively
/// First send 10 Mi from the faucet to atoi1qzt0nhsf38nh6rs4p6zs5knqp6psgha9wsv74uajqgjmwc75ugupx3y7x0r
/// That's the first address of the first seed in the .env.example file
///
/// Address Index 0. Note that we can use the `address` example codes to know the addresses belong to the seed.
/// Outputs we send to the first addresses from the second seed:
/// output 0: 3_000_000 tokens atoi1qzj8s3kpacr6kmh05sxul4zp0xqulzn2vy9rznqj6rrc4nwd304pk6w523x
/// output 1: 3_000_000 tokens atoi1qzu7dnlfld2p0rhld20nr6axdnl0katmwu59fprwcnahglmnvgpwjsc20jg
/// output 2: 3_000_000 tokens atoi1qz0vue67w2e2wjk9jh07s7wfgxmsxgy9ssctn3nntyf9uqd6qs3zsp0k73u
///
///
/// Then we will send 6_000_000 tokens from the second seed to the first one
/// to addresses "atoi1qpnrumvaex24dy0duulp4q07lpa00w20ze6jfd0xly422kdcjxzakzsz5kf" (index 1) and
/// "atoi1qz4sfmp605vnj6fxt0sf0cwclffw5hpxjqkf6fthyd74r9nmmu337m3lwl2" (index 2), and check the ledger
/// inclusion state, which should be "Some(Included)".
const EXPLORER_URL: &str = "https://explorer.iota.org/testnet/message/";
#[tokio::main]
async fn main() -> Result<()> {
// Create a client instance
let iota = Client::builder()
.with_node("https://api.lb-0.testnet.chrysalis2.com")? // Insert your node URL here
.with_node_sync_disabled()
.finish()
.await?;
// This example uses dotenv, which is not safe for use in production
// Configure your own seed in ".env". Since the output amount cannot be zero, the seed must contain non-zero balance
dotenv().ok();
let seed_1 = Seed::from_bytes(&hex::decode(env::var("NONSECURE_USE_OF_DEVELOPMENT_SEED_1").unwrap())?);
let seed_2 = Seed::from_bytes(&hex::decode(env::var("NONSECURE_USE_OF_DEVELOPMENT_SEED_2").unwrap())?);
let message = iota
.message()
.with_seed(&seed_1)
// Insert the output address and amount to spent. The amount cannot be zero.
.with_output(
&iota.get_addresses(&seed_2).with_range(0..1).finish().await?[0],
3_000_000,
)?
.finish()
.await?;
println!("First transaction sent: {}{}", EXPLORER_URL, message.id().0);
let _ = iota.retry_until_included(&message.id().0, None, None).await?;
let message = iota
.message()
.with_seed(&seed_1)
.with_output(
&iota.get_addresses(&seed_2).with_range(1..2).finish().await?[0],
3_000_000,
)?
.finish()
.await?;
println!("Second transaction sent: {}{}", EXPLORER_URL, message.id().0);
let _ = iota.retry_until_included(&message.id().0, None, None).await?;
let message = iota
.message()
.with_seed(&seed_1)
.with_output(
&iota.get_addresses(&seed_2).with_range(2..3).finish().await?[0],
3_000_000,
)?
.finish()
.await?;
println!("Third transaction sent: {}{}", EXPLORER_URL, message.id().0);
let _ = iota.retry_until_included(&message.id().0, None, None).await?;
let message = iota
.message()
.with_seed(&seed_2)
// Note that we can transfer to multiple outputs by using the `SendTransactionBuilder`
.with_output(
&iota.get_addresses(&seed_1).with_range(1..2).finish().await?[0],
3_000_000,
)?
.with_output(
&iota.get_addresses(&seed_1).with_range(2..3).finish().await?[0],
3_000_000,
)?
.finish()
.await?;
println!("Last transaction sent: {}{}", EXPLORER_URL, message.id().0);
let _ = iota.retry_until_included(&message.id().0, None, None).await?;
let message_metadata = iota.get_message().metadata(&message.id().0).await;
println!("Ledger Inclusion State: {:?}", message_metadata?.ledger_inclusion_state);
Ok(())
}