This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
account_key_pooling.rs
74 lines (63 loc) · 2.13 KB
/
account_key_pooling.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
/// You can use account key pooling to use different keys for consecutive transactions
/// to avoid nonce-related issues.
///
/// This is an example of how to use account key pooling to send multiple transactions
/// using different keys.
use near::{signer::Signer, Account, NetworkConfig, Tokens};
use near_token::NearToken;
use std::sync::Arc;
#[tokio::main]
async fn main() {
let network = near_workspaces::sandbox().await.unwrap();
let account = network.dev_create_account().await.unwrap();
let second_account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);
let signer = Signer::new(Signer::from_workspace(&account)).unwrap();
println!(
"Initial public key: {}",
signer.get_public_key().await.unwrap()
);
let (key, tx) = Account(account.id().clone())
.add_key(near_primitives::account::AccessKeyPermission::FullAccess)
.new_keypair()
.generate_secret_key()
.unwrap();
println!("New public key: {}", key.public_key());
tx.with_signer(Arc::clone(&signer))
.send_to(&network)
.await
.unwrap()
.assert_success();
signer
.add_signer_to_pool(Signer::secret_key(key))
.await
.unwrap();
let txs = (0..2).map(|_| {
Tokens::of(account.id().clone())
.send_to(second_account.id().clone())
.near(NearToken::from_near(1))
.with_signer(Arc::clone(&signer))
.send_to(&network)
});
let results = futures::future::join_all(txs)
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(results.len(), 2);
results.iter().for_each(|e| e.assert_success());
println!("All transactions are successful");
println!(
"Transaction one public key: {}",
results[0].transaction.public_key
);
println!(
"Transaction two public key: {}",
results[1].transaction.public_key
);
assert_ne!(
results[0].transaction.public_key,
results[1].transaction.public_key
);
println!("All transactions are successful");
}