mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-21 21:31:02 +00:00
08369f3e43
* codegen: Update polkadot.rs polkadot commit-hash: d96d3bea85 polkadot tag: v0.9.16-rc2 Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Reference key storage api Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Regenerate polkadot.rs with reference api Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * tests: Update tests with reference interface Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * cli: Fix polkadot.rs license check Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Update polkadot.rs with copyright Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Revert "codegen: Update polkadot.rs with copyright" This reverts commit 2970d0573dc0b11d01072b270a525ad497992ddf. Revert "cli: Fix polkadot.rs license check" This reverts commit 6fe8818582ae39669c059c1ed0424b6606620295. * codegen: Implement AccountData trait in the expected order Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Store implementation of StorageEntry Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Generate AccountDefaultData wrapper struct Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Allow `Account` references Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Utilize AccountDefaultData instead of Account Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * tests: Update tests to utilize `Account` reference Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Rename AccountDefaultData to AccountOwned Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Add comments for wrapper account Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Obtain vector type parameter for TypePath::Type Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Use slices instead of `& std::vec` in storage API Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Fix documentation Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * tests: Remove extra reference Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Add staking example to exercise storage API Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Update polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * tests: Update storage tests Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Fix cargo clippy Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Simplify vec_type_param Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Rename staking_details.rs to fetch_staking_details.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * tests: Remove dummy variable Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Update polkadot version Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Apply rust-fmt Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * codegen: Regenerate polkadot.rs Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * examples: Remove comment Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
262 lines
6.9 KiB
Rust
262 lines
6.9 KiB
Rust
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
|
|
// This file is part of 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 subxt. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
use crate::{
|
|
node_runtime::{
|
|
runtime_types::pallet_staking::{
|
|
RewardDestination,
|
|
ValidatorPrefs,
|
|
},
|
|
staking,
|
|
DispatchError,
|
|
},
|
|
pair_signer,
|
|
test_context,
|
|
};
|
|
use assert_matches::assert_matches;
|
|
use sp_core::{
|
|
sr25519,
|
|
Pair,
|
|
};
|
|
use sp_keyring::AccountKeyring;
|
|
use subxt::{
|
|
Error,
|
|
Signer,
|
|
};
|
|
|
|
/// 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() {
|
|
let alice = pair_signer(AccountKeyring::Alice.pair());
|
|
let ctx = test_context().await;
|
|
ctx.api
|
|
.tx()
|
|
.staking()
|
|
.validate(default_validator_prefs())
|
|
.sign_and_submit_then_watch(&alice)
|
|
.await
|
|
.unwrap()
|
|
.wait_for_finalized_success()
|
|
.await
|
|
.expect("should be successful");
|
|
}
|
|
|
|
#[async_std::test]
|
|
async fn validate_not_possible_for_stash_account() -> Result<(), Error<DispatchError>> {
|
|
let alice_stash = pair_signer(get_from_seed("Alice//stash"));
|
|
let ctx = test_context().await;
|
|
let announce_validator = ctx
|
|
.api
|
|
.tx()
|
|
.staking()
|
|
.validate(default_validator_prefs())
|
|
.sign_and_submit_then_watch(&alice_stash)
|
|
.await?
|
|
.wait_for_finalized_success()
|
|
.await;
|
|
assert_matches!(announce_validator, Err(Error::Module(err)) => {
|
|
assert_eq!(err.pallet, "Staking");
|
|
assert_eq!(err.error, "NotController");
|
|
});
|
|
Ok(())
|
|
}
|
|
|
|
#[async_std::test]
|
|
async fn nominate_with_controller_account() {
|
|
let alice = pair_signer(AccountKeyring::Alice.pair());
|
|
let bob = pair_signer(AccountKeyring::Bob.pair());
|
|
let ctx = test_context().await;
|
|
|
|
ctx.api
|
|
.tx()
|
|
.staking()
|
|
.nominate(vec![bob.account_id().clone().into()])
|
|
.sign_and_submit_then_watch(&alice)
|
|
.await
|
|
.unwrap()
|
|
.wait_for_finalized_success()
|
|
.await
|
|
.expect("should be successful");
|
|
}
|
|
|
|
#[async_std::test]
|
|
async fn nominate_not_possible_for_stash_account() -> Result<(), Error<DispatchError>> {
|
|
let alice_stash = pair_signer(get_from_seed("Alice//stash"));
|
|
let bob = pair_signer(AccountKeyring::Bob.pair());
|
|
let ctx = test_context().await;
|
|
|
|
let nomination = ctx
|
|
.api
|
|
.tx()
|
|
.staking()
|
|
.nominate(vec![bob.account_id().clone().into()])
|
|
.sign_and_submit_then_watch(&alice_stash)
|
|
.await?
|
|
.wait_for_finalized_success()
|
|
.await;
|
|
|
|
assert_matches!(nomination, Err(Error::Module(err)) => {
|
|
assert_eq!(err.pallet, "Staking");
|
|
assert_eq!(err.error, "NotController");
|
|
});
|
|
Ok(())
|
|
}
|
|
|
|
#[async_std::test]
|
|
async fn chill_works_for_controller_only() -> Result<(), Error<DispatchError>> {
|
|
let alice_stash = pair_signer(get_from_seed("Alice//stash"));
|
|
let bob_stash = pair_signer(get_from_seed("Bob//stash"));
|
|
let alice = pair_signer(AccountKeyring::Alice.pair());
|
|
let ctx = test_context().await;
|
|
|
|
// this will fail the second time, which is why this is one test, not two
|
|
ctx.api
|
|
.tx()
|
|
.staking()
|
|
.nominate(vec![bob_stash.account_id().clone().into()])
|
|
.sign_and_submit_then_watch(&alice)
|
|
.await?
|
|
.wait_for_finalized_success()
|
|
.await?;
|
|
|
|
let ledger = ctx
|
|
.api
|
|
.storage()
|
|
.staking()
|
|
.ledger(alice.account_id(), None)
|
|
.await?
|
|
.unwrap();
|
|
assert_eq!(alice_stash.account_id(), &ledger.stash);
|
|
|
|
let chill = ctx
|
|
.api
|
|
.tx()
|
|
.staking()
|
|
.chill()
|
|
.sign_and_submit_then_watch(&alice_stash)
|
|
.await?
|
|
.wait_for_finalized_success()
|
|
.await;
|
|
|
|
assert_matches!(chill, Err(Error::Module(err)) => {
|
|
assert_eq!(err.pallet, "Staking");
|
|
assert_eq!(err.error, "NotController");
|
|
});
|
|
|
|
let is_chilled = ctx
|
|
.api
|
|
.tx()
|
|
.staking()
|
|
.chill()
|
|
.sign_and_submit_then_watch(&alice)
|
|
.await?
|
|
.wait_for_finalized_success()
|
|
.await?
|
|
.has::<staking::events::Chilled>()?;
|
|
assert!(is_chilled);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[async_std::test]
|
|
async fn tx_bond() -> Result<(), Error<DispatchError>> {
|
|
let alice = pair_signer(AccountKeyring::Alice.pair());
|
|
let ctx = test_context().await;
|
|
|
|
let bond = ctx
|
|
.api
|
|
.tx()
|
|
.staking()
|
|
.bond(
|
|
AccountKeyring::Bob.to_account_id().into(),
|
|
100_000_000_000_000,
|
|
RewardDestination::Stash,
|
|
)
|
|
.sign_and_submit_then_watch(&alice)
|
|
.await?
|
|
.wait_for_finalized_success()
|
|
.await;
|
|
|
|
assert!(bond.is_ok());
|
|
|
|
let bond_again = ctx
|
|
.api
|
|
.tx()
|
|
.staking()
|
|
.bond(
|
|
AccountKeyring::Bob.to_account_id().into(),
|
|
100_000_000_000_000,
|
|
RewardDestination::Stash,
|
|
)
|
|
.sign_and_submit_then_watch(&alice)
|
|
.await?
|
|
.wait_for_finalized_success()
|
|
.await;
|
|
|
|
assert_matches!(bond_again, Err(Error::Module(err)) => {
|
|
assert_eq!(err.pallet, "Staking");
|
|
assert_eq!(err.error, "AlreadyBonded");
|
|
});
|
|
Ok(())
|
|
}
|
|
|
|
#[async_std::test]
|
|
async fn storage_history_depth() -> Result<(), Error<DispatchError>> {
|
|
let ctx = test_context().await;
|
|
let history_depth = ctx.api.storage().staking().history_depth(None).await?;
|
|
assert_eq!(history_depth, 84);
|
|
Ok(())
|
|
}
|
|
|
|
#[async_std::test]
|
|
async fn storage_current_era() -> Result<(), Error<DispatchError>> {
|
|
let ctx = test_context().await;
|
|
let _current_era = ctx
|
|
.api
|
|
.storage()
|
|
.staking()
|
|
.current_era(None)
|
|
.await?
|
|
.expect("current era always exists");
|
|
Ok(())
|
|
}
|
|
|
|
#[async_std::test]
|
|
async fn storage_era_reward_points() -> Result<(), Error<DispatchError>> {
|
|
let cxt = test_context().await;
|
|
let current_era_result = cxt
|
|
.api
|
|
.storage()
|
|
.staking()
|
|
.eras_reward_points(&0, None)
|
|
.await;
|
|
assert!(current_era_result.is_ok());
|
|
|
|
Ok(())
|
|
}
|