-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathget_storage.rs
More file actions
144 lines (122 loc) · 5.78 KB
/
Copy pathget_storage.rs
File metadata and controls
144 lines (122 loc) · 5.78 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Copyright 2019 Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//! Very simple example that shows how to get some storage values.
use codec::Encode;
use frame_system::AccountInfo as GenericAccountInfo;
use pallet_recovery::{ApprovalBitfield, Attempt, FriendGroup};
use rococo_runtime::Address;
use sp_keyring::Sr25519Keyring;
use sp_runtime::traits::ConstU32;
use substrate_api_client::{
Api, GetAccountInformation, GetStorage, SubmitAndWatch, XtStatus,
ac_compose_macros::compose_extrinsic,
ac_primitives::{Config, RococoRuntimeConfig},
rpc::JsonrpseeClient,
};
// To test this example with CI we run it against the Polkadot Rococo node. Remember to switch the Config to match your
// own runtime if it uses different parameter configurations. Several pre-compiled runtimes are available in the ac-primitives crate.
type AccountInfo = GenericAccountInfo<
<RococoRuntimeConfig as Config>::Index,
<RococoRuntimeConfig as Config>::AccountData,
>;
type Balance = <RococoRuntimeConfig as Config>::Balance;
type AccountId = <RococoRuntimeConfig as Config>::AccountId;
type BlockNumber = <RococoRuntimeConfig as Config>::BlockNumber;
type MaxFriendsPerConfig = ConstU32<100>;
#[tokio::main]
async fn main() {
env_logger::init();
// Initialize the api.
let client = JsonrpseeClient::with_default_url().await.unwrap();
let mut api = Api::<RococoRuntimeConfig, _>::new(client).await.unwrap();
// Get some plain storage values.
let (balance, proof) = tokio::try_join!(
api.get_storage::<Balance>("Balances", "TotalIssuance", None),
api.get_storage_value_proof("Balances", "TotalIssuance", None)
)
.unwrap();
println!("[+] TotalIssuance is {:?}", balance.unwrap());
println!("[+] StorageValueProof: {:?}", proof);
// Get the AccountInfo of Alice and the associated StoragePrefix.
let account: sp_core::sr25519::Public = Sr25519Keyring::Alice.public();
let (maybe_account_info, key_prefix) = tokio::try_join!(
api.get_storage_map::<_, AccountInfo>("System", "Account", account, None),
api.get_storage_map_key_prefix("System", "Account")
)
.unwrap();
println!("[+] AccountInfo for Alice is {:?}", maybe_account_info.unwrap());
println!("[+] Key prefix for System Account map is {:?}", key_prefix);
// Get Alice's and Bobs AccountNonce with api.get_nonce(). Alice will be set as the signer for
// the current api, so the nonce retrieval can be simplified:
let signer = Sr25519Keyring::Alice.pair();
api.set_signer(signer.into());
let bob = Sr25519Keyring::Bob.to_account_id();
let (alice_nonce, bob_nonce) =
tokio::try_join!(api.get_nonce(), api.get_account_nonce(&bob)).unwrap();
println!("[+] Alice's Account Nonce is {}", alice_nonce);
println!("[+] Bob's Account Nonce is {}", bob_nonce);
// Get an vector of storage keys, numbering up to the given max keys and that start with the (optionally) given storage key prefix.
let storage_key_prefix = api.get_storage_map_key_prefix("System", "Account").await.unwrap();
let max_keys = 3;
let storage_keys = api
.get_storage_keys_paged(Some(storage_key_prefix), max_keys, None, None)
.await
.unwrap();
assert_eq!(storage_keys.len() as u32, max_keys);
// Get the storage values that belong to the retrieved storage keys.
for storage_key in storage_keys.iter() {
println!("Retrieving value for key {:?}", storage_key);
// We're expecting account info as return value because we fetch a storage value with prefix combination of "System" + "Account".
let storage_data: AccountInfo =
api.get_storage_by_key(storage_key.clone(), None).await.unwrap().unwrap();
println!("Retrieved data {:?}", storage_data);
}
// Create a friend group, so we can fetch an actual Attempt from the chain.
let alice = Sr25519Keyring::Alice.to_account_id();
let bob = Sr25519Keyring::Bob.to_account_id();
let alice_multiaddress: Address = alice.clone().into();
let charlie = Sr25519Keyring::Charlie.to_account_id();
let ferdie = Sr25519Keyring::Ferdie.to_account_id();
let friend_group = FriendGroup {
friends: vec![&bob, &charlie],
friends_needed: 2,
inheritor: ferdie,
inheritance_delay: 0,
inheritance_priority: 0,
cancel_delay: 10,
};
let xt = compose_extrinsic!(&api, "Recovery", "set_friend_groups", vec![friend_group]).unwrap();
let _report = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock).await.unwrap();
// Set Bob as signer, so we can send the attempt initiation extrinsic as Bob.
let signer2 = Sr25519Keyring::Bob.pair();
api.set_signer(signer2.into());
let xt =
compose_extrinsic!(&api, "Recovery", "initiate_attempt", &alice_multiaddress, 0).unwrap();
println!("{:?}", xt.encode());
let _report = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock).await.unwrap();
let storage_double_map_key_prefix = api
.get_storage_double_map_key_prefix("Recovery", "Attempt", &alice)
.await
.unwrap();
let double_map_storage_keys = api
.get_storage_keys_paged(Some(storage_double_map_key_prefix), max_keys, None, None)
.await
.unwrap();
// Get the storage values that belong to the retrieved storage keys.
for storage_key in double_map_storage_keys.iter() {
println!("Retrieving value for key {:?}", storage_key);
let storage_data: Attempt<BlockNumber, ApprovalBitfield<MaxFriendsPerConfig>, AccountId> =
api.get_storage_by_key(storage_key.clone(), None).await.unwrap().unwrap();
println!("Retrieved data {:?}", storage_data);
}
}