forked from paritytech/subxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaking.rs
More file actions
287 lines (267 loc) · 8.49 KB
/
staking.rs
File metadata and controls
287 lines (267 loc) · 8.49 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of substrate-subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
use codec::{
Decode,
Encode,
};
use crate::{
node_runtime::{
runtime_types::pallet_staking::{
ActiveEraInfo,
Exposure,
Nominations,
RewardDestination,
StakingLedger,
ValidatorPrefs,
},
staking,
},
test_context,
TestRuntime,
};
use assert_matches::assert_matches;
use sp_core::{
sr25519,
Pair,
};
use sp_keyring::AccountKeyring;
use std::{
collections::BTreeMap,
fmt::Debug,
marker::PhantomData,
};
use subxt::{
extrinsic::{
PairSigner,
Signer,
},
Error,
ExtrinsicSuccess,
RuntimeError,
};
/// Helper function to generate a crypto pair from seed
fn get_from_seed(seed: &str) -> sr25519::Pair {
sr25519::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
}
fn default_validator_prefs() -> ValidatorPrefs {
ValidatorPrefs {
commission: sp_runtime::Perbill::default(),
blocked: false,
}
}
#[async_std::test]
async fn validate_with_controller_account() -> Result<(), Error> {
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
let cxt = test_context().await;
let announce_validator = cxt
.api
.tx()
.staking()
.validate(default_validator_prefs())
.sign_and_submit_then_watch(&alice)
.await;
assert_matches!(announce_validator, Ok(ExtrinsicSuccess {block: _, extrinsic: _, events}) => {
// TOOD: this is unsatisfying – can we do better?
assert_eq!(events.len(), 2);
});
Ok(())
}
#[async_std::test]
async fn validate_not_possible_for_stash_account() -> Result<(), Error> {
let alice_stash = PairSigner::<TestRuntime, _>::new(get_from_seed("Alice//stash"));
let cxt = test_context().await;
let announce_validator = cxt
.api
.tx()
.staking()
.validate(default_validator_prefs())
.sign_and_submit_then_watch(&alice_stash)
.await;
assert_matches!(announce_validator, Err(Error::Runtime(RuntimeError::Module(module_err))) => {
assert_eq!(module_err.pallet, "Staking");
assert_eq!(module_err.error, "NotController");
});
Ok(())
}
#[async_std::test]
async fn nominate_with_controller_account() -> Result<(), Error> {
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
let cxt = test_context().await;
let nomination = cxt
.api
.tx()
.staking()
.nominate(vec![bob.account_id().clone().into()])
.sign_and_submit_then_watch(&alice)
.await;
assert_matches!(nomination, Ok(ExtrinsicSuccess {block: _, extrinsic: _, events}) => {
// TOOD: this is unsatisfying – can we do better?
assert_eq!(events.len(), 2);
});
Ok(())
}
#[async_std::test]
async fn nominate_not_possible_for_stash_account() -> Result<(), Error> {
let alice_stash =
PairSigner::<TestRuntime, sr25519::Pair>::new(get_from_seed("Alice//stash"));
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
let cxt = test_context().await;
let nomination = cxt
.api
.tx()
.staking()
.nominate(vec![bob.account_id().clone().into()])
.sign_and_submit_then_watch(&alice_stash)
.await;
assert_matches!(nomination, Err(Error::Runtime(RuntimeError::Module(module_err))) => {
assert_eq!(module_err.pallet, "Staking");
assert_eq!(module_err.error, "NotController");
});
Ok(())
}
#[async_std::test]
async fn chill_works_for_controller_only() -> Result<(), Error> {
let alice_stash =
PairSigner::<TestRuntime, sr25519::Pair>::new(get_from_seed("Alice//stash"));
let bob_stash =
PairSigner::<TestRuntime, sr25519::Pair>::new(get_from_seed("Bob//stash"));
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
let cxt = test_context().await;
// this will fail the second time, which is why this is one test, not two
cxt.api
.tx()
.staking()
.nominate(vec![bob_stash.account_id().clone().into()])
.sign_and_submit_then_watch(&alice)
.await;
let ledger = cxt
.api
.storage()
.staking()
.ledger(alice.account_id().clone(), None)
.await?
.unwrap();
assert_eq!(alice_stash.account_id(), &ledger.stash);
let chill = cxt
.api
.tx()
.staking()
.chill()
.sign_and_submit_then_watch(&alice_stash)
.await;
assert_matches!(chill, Err(Error::Runtime(RuntimeError::Module(module_err))) => {
assert_eq!(module_err.pallet, "Staking");
assert_eq!(module_err.error, "NotController");
});
let result = cxt
.api
.tx()
.staking()
.chill()
.sign_and_submit_then_watch(&alice)
.await?;
let chill = result.find_event::<staking::events::Chilled>()?;
assert!(chill.is_some());
Ok(())
}
// #[async_std::test]
// async fn test_bond() -> Result<(), Error> {
// env_logger::try_init().ok();
// let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
// let test_node_proc = test_node_process().await;
// let client = test_node_proc.client();
//
// let bond = client
// .bond_and_watch(
// &alice,
// &AccountKeyring::Bob.to_account_id().into(),
// 100_000_000_000_000,
// RewardDestination::Stash,
// )
// .await;
//
// assert_matches!(bond, Ok(ExtrinsicSuccess {block: _, extrinsic: _, events}) => {
// // TOOD: this is unsatisfying – can we do better?
// assert_eq!(events.len(), 3);
// });
//
// let bond_again = client
// .bond_and_watch(
// &alice,
// &AccountKeyring::Bob.to_account_id().into(),
// 100_000_000_000,
// RewardDestination::Stash,
// )
// .await;
//
// assert_matches!(bond_again, Err(Error::Runtime(RuntimeError::Module(module_err))) => {
// assert_eq!(module_err.module, "Staking");
// assert_eq!(module_err.error, "AlreadyBonded");
// });
//
// Ok(())
// }
//
// #[async_std::test]
// async fn test_total_issuance_is_okay() -> Result<(), Error> {
// env_logger::try_init().ok();
// let test_node_proc = test_node_process().await;
// let client = test_node_proc.client();
// let total_issuance = client.total_issuance(None).await?;
// assert!(total_issuance > 1u128 << 32);
// Ok(())
// }
//
// #[async_std::test]
// async fn test_history_depth_is_okay() -> Result<(), Error> {
// env_logger::try_init().ok();
// let test_node_proc = test_node_process().await;
// let client = test_node_proc.client();
// let history_depth = client.history_depth(None).await?;
// assert_eq!(history_depth, 84);
// Ok(())
// }
//
// #[async_std::test]
// async fn test_current_era_is_okay() -> Result<(), Error> {
// env_logger::try_init().ok();
// let test_node_proc = test_node_process().await;
// let client = test_node_proc.client();
// let _current_era = client
// .current_era(None)
// .await?
// .expect("current era always exists");
// Ok(())
// }
//
// #[async_std::test]
// async fn test_era_reward_points_is_okay() -> Result<(), Error> {
// env_logger::try_init().ok();
// let test_node_proc = test_node_process().await;
// let client = test_node_proc.client();
// let store = ErasRewardPointsStore {
// _phantom: PhantomData,
// index: 0,
// };
//
// let current_era_result = client.fetch(&store, None).await?;
//
// assert_matches!(current_era_result, Some(_));
//
// Ok(())
// }