-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate_account_and_send_near.rs
54 lines (45 loc) · 1.61 KB
/
create_account_and_send_near.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
use near_api::*;
use near_token::NearToken;
use signer::generate_secret_key;
#[tokio::main]
async fn main() {
let network = near_workspaces::sandbox().await.unwrap();
let account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);
let balance = Tokens::account(account.id().clone())
.near_balance()
.fetch_from(&network)
.await
.unwrap();
println!("Balance: {}", balance.total);
let new_account: AccountId = format!("{}.{}", "bob", account.id()).parse().unwrap();
let signer = Signer::new(Signer::from_workspace(&account)).unwrap();
Account::create_account(new_account.clone())
.fund_myself(account.id().clone(), NearToken::from_near(1))
.public_key(generate_secret_key().unwrap().public_key())
.unwrap()
.with_signer(signer.clone())
.send_to(&network)
.await
.unwrap();
Tokens::account(account.id().clone())
.send_to(new_account.clone())
.near(NearToken::from_near(1))
.with_signer(signer)
.send_to(&network)
.await
.unwrap();
let new_account_balance = Tokens::account(account.id().clone())
.near_balance()
.fetch_from(&network)
.await
.unwrap();
let bob_balance = Tokens::account(new_account)
.near_balance()
.fetch_from(&network)
.await
.unwrap();
println!("Balance: {}", new_account_balance.total);
// Expect to see 2 NEAR in Bob's account. 1 NEAR from create_account and 1 NEAR from send_near
println!("Bob balance: {}", bob_balance.total);
}