-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_payment.rs
More file actions
73 lines (63 loc) · 2.41 KB
/
send_payment.rs
File metadata and controls
73 lines (63 loc) · 2.41 KB
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
//! Send a payment transaction on the Mina network.
//!
//! Run with: cargo run --example send_payment
//!
//! Requires a running Mina daemon with the sender account unlocked.
//!
//! ## Providing keys
//!
//! This example reads two env vars — both are base58 Mina public keys like
//! `B62qjVQLxt9nYMWGn45mkgwYfcz8e8jvjNCBo11VKJb7vxDNwv5QLPS`:
//!
//! - `MINA_TEST_SENDER_KEY` — the sender account. Must be imported **and
//! unlocked** in the daemon's wallet (via `importAccount` + `unlockAccount`
//! GraphQL mutations, or `mina accounts import` + `mina accounts unlock`).
//! You also need its secret key file on disk for the daemon to sign with.
//! - `MINA_TEST_RECEIVER_KEY` — any destination public key; no wallet import
//! needed.
//!
//! Generate a fresh keypair locally with:
//!
//! mina advanced generate-keypair --privkey-path <file>
//!
//! Or, on a lightnet node, acquire a pre-funded unlocked account:
//!
//! curl 'http://127.0.0.1:8181/acquire-account?unlockAccount=true'
//!
//! Without both env vars set, the example short-circuits so it stays safe to
//! run as a smoke test.
use mina_sdk::{Currency, MinaClient, Payment};
#[tokio::main]
async fn main() -> mina_sdk::Result<()> {
let client = MinaClient::default();
let (Ok(sender), Ok(receiver)) = (
std::env::var("MINA_TEST_SENDER_KEY"),
std::env::var("MINA_TEST_RECEIVER_KEY"),
) else {
println!("Set MINA_TEST_SENDER_KEY and MINA_TEST_RECEIVER_KEY to run");
return Ok(());
};
let fee = Currency::from_mina("0.01")?;
// Build the payment, then submit it.
let payment = Payment::sender(&sender)
.to(&receiver)
.amount(Currency::from_mina("1.5")?)
.fee(fee)
.memo("coffee payment");
let result = client.send_payment(payment).await?;
println!("Payment submitted!");
println!(" Hash: {}", result.hash);
println!(" ID: {}", result.id);
println!(" Nonce: {}", result.nonce);
// Same pattern with an explicit nonce — useful for submitting a second
// transaction before the first has been included in a block.
let next_payment = Payment::sender(&sender)
.to(&receiver)
.amount(Currency::from_mina("2.0")?)
.fee(fee)
.memo("second payment")
.nonce(result.nonce + 1);
let result2 = client.send_payment(next_payment).await?;
println!("Second payment: {}", result2.hash);
Ok(())
}