diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4bd11a4f5..3cb25e2f2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,10 +37,10 @@ jobs: cargo build --release -p test-no-std --features compose-macros, cargo build --release -p test-no-std --features node-api, - cargo build --release --examples, + cargo build --release --examples --features staking-xt, cargo clippy --all --exclude test-no-std -- -D warnings, - cargo clippy --examples -- -D warnings, + cargo clippy --features staking-xt --examples -- -D warnings, cargo test --all --exclude test-no-std, @@ -61,7 +61,7 @@ jobs: - name: Upload examples uses: actions/upload-artifact@v3 - if: matrix.check == 'cargo build --release --examples' + if: matrix.check == 'cargo build --release --examples --features staking-xt' with: name: examples path: | diff --git a/examples/example_batch_payout.rs b/examples/example_batch_payout.rs index 33417cddd..e196e8ee9 100644 --- a/examples/example_batch_payout.rs +++ b/examples/example_batch_payout.rs @@ -3,16 +3,14 @@ use clap::{load_yaml, App}; #[cfg(feature = "staking-xt")] use codec::{Decode, Encode}; #[cfg(feature = "staking-xt")] -use serde_json::Value; +use pallet_staking::{ActiveEraInfo, Exposure}; #[cfg(feature = "staking-xt")] -use sp_core::{sr25519, Pair}; +use serde_json::Value; #[cfg(feature = "staking-xt")] use sp_keyring::AccountKeyring; #[cfg(feature = "staking-xt")] use sp_runtime::{app_crypto::Ss58Codec, AccountId32}; #[cfg(feature = "staking-xt")] -use staking::{ActiveEraInfo, Exposure}; -#[cfg(feature = "staking-xt")] use substrate_api_client::{ rpc::WsRpcClient, Api, BaseExtrinsicParams, PlainTip, PlainTipExtrinsicParams, XtStatus, }; @@ -29,12 +27,13 @@ fn main() { .unwrap(); let grace_period: GracePeriod = GracePeriod { enabled: false, eras: 0 }; let mut results: Vec = Vec::new(); - let account: AccountId32; + // Give a valid validator account address, given one is westend chain validator account - match AccountId32::from_ss58check("5DJcEbkNxsnNwHGrseg7cgbfUG8eiKzpuZqgSph5HqHrjgf6") { - Ok(address) => account = address, - Err(e) => panic!("Invalid Account id : {:?}", e), - } + let account = + match AccountId32::from_ss58check("5DJcEbkNxsnNwHGrseg7cgbfUG8eiKzpuZqgSph5HqHrjgf6") { + Ok(address) => address, + Err(e) => panic!("Invalid Account id : {:?}", e), + }; let active_era: ActiveEraInfo = api.get_storage_value("Staking", "ActiveEra", None).unwrap().unwrap(); @@ -53,29 +52,27 @@ fn main() { } let mut i = start; - while i <= tx_limit + start - 1 { + while i < tx_limit + start { let idx = last_reward + i; let is_grace_period_satisfied = !grace_period.enabled || (current_active_era - idx > grace_period.eras); let mut exposure: Exposure = Exposure { total: 0, own: 0, others: vec![] }; - match api + if let Some(exp) = api .get_storage_double_map("Staking", "ErasStakers", idx, &account, None) .unwrap() { - Some(exp) => exposure = exp, - None => (), + exposure = exp } if exposure.total.to_be_bytes() > 0_u128.to_be_bytes() && is_grace_period_satisfied { let some = api.payout_stakers(idx, account.clone()); payout_calls.push(some.function); } i += 1; - last_reward = last_reward + 1; + last_reward += 1; } - let mut current_tx_done = false; - let mut payout_calls_len = payout_calls.len(); + let payout_calls_len = payout_calls.len(); if payout_calls_len > 0 { let batching = api.batch(payout_calls); let results_hash = @@ -84,13 +81,12 @@ fn main() { let result = serde_json::to_value(results_hash).unwrap(); results.push(result); - } else { - current_tx_done = true; } num_of_unclaimed_payout -= tx_limit; start += tx_limit; } println!("{:?}", results); + println!("Unclaimed payouts: {:?}", num_of_claimed_payouts); } #[cfg(feature = "staking-xt")] @@ -115,40 +111,37 @@ pub fn get_last_reward( >, ) -> u32 { let api = api; - let mut account: AccountId32; - match AccountId32::from_ss58check(&validator_address) { - Ok(address) => account = address, + let account = match AccountId32::from_ss58check(validator_address) { + Ok(address) => address, Err(e) => panic!("Invalid Account id : {:?}", e), - } - + }; let active_era: ActiveEraInfo = api.get_storage_value("Staking", "ActiveEra", None).unwrap().unwrap(); let storagekey = api.metadata.storage_map_key("Staking", "Ledger", &account).unwrap(); let mut res = StakingLedger { - stash: account.clone(), + stash: account, total: 0, active: 0, unlocking: Vec::new(), claimed_rewards: Vec::new(), }; - match api.get_storage_by_key_hash(storagekey, None) { - Ok(Some(ledger)) => res = ledger, - _ => (), + if let Ok(Some(ledger)) = api.get_storage_by_key_hash(storagekey, None) { + res = ledger } - let mut last_reward = 0_u32; let is_history_checked_force: bool = false; - if is_history_checked_force || res.claimed_rewards.len() == 0 { - last_reward = api.get_constant("Staking", "HistoryDepth").unwrap(); - last_reward = active_era.index - last_reward; + let last_reward = if is_history_checked_force || res.claimed_rewards.is_empty() { + let history_depth: u32 = api.get_constant("Staking", "HistoryDepth").unwrap(); + active_era.index - history_depth } else { - last_reward = res.claimed_rewards.pop().unwrap(); - } + res.claimed_rewards.pop().unwrap() + }; println!("{}", last_reward); last_reward } + #[cfg(feature = "staking-xt")] pub struct GracePeriod { pub enabled: bool, diff --git a/examples/example_staking_payout.rs b/examples/example_staking_payout.rs index c98702b3a..4c615d5f0 100644 --- a/examples/example_staking_payout.rs +++ b/examples/example_staking_payout.rs @@ -1,15 +1,13 @@ #[cfg(feature = "staking-xt")] use clap::{load_yaml, App}; #[cfg(feature = "staking-xt")] -use sp_core::{sr25519, Pair}; +use pallet_staking::{ActiveEraInfo, Exposure}; #[cfg(feature = "staking-xt")] use sp_keyring::AccountKeyring; #[cfg(feature = "staking-xt")] use sp_runtime::{app_crypto::Ss58Codec, AccountId32}; #[cfg(feature = "staking-xt")] -use staking::{ActiveEraInfo, Exposure}; -#[cfg(feature = "staking-xt")] -use substrate_api_client::{rpc::WsRpcClient, AccountId, Api, PlainTipExtrinsicParams, XtStatus}; +use substrate_api_client::{rpc::WsRpcClient, Api, PlainTipExtrinsicParams, XtStatus}; #[cfg(feature = "staking-xt")] fn main() { @@ -21,23 +19,21 @@ fn main() { .map(|api| api.set_signer(from)) .unwrap(); let mut exposure: Exposure = Exposure { total: 0, own: 0, others: vec![] }; - let account: AccountId; - match AccountId32::from_ss58check("5DJcEbkNxsnNwHGrseg7cgbfUG8eiKzpuZqgSph5HqHrjgf6") { - Ok(address) => account = address, - Err(e) => panic!("Invalid Account id : {:?}", e), - } + let account = + match AccountId32::from_ss58check("5DJcEbkNxsnNwHGrseg7cgbfUG8eiKzpuZqgSph5HqHrjgf6") { + Ok(address) => address, + Err(e) => panic!("Invalid Account id : {:?}", e), + }; let active_era: ActiveEraInfo = api.get_storage_value("Staking", "ActiveEra", None).unwrap().unwrap(); println!("{:?}", active_era); let idx = active_era.index - 1; - match api.get_storage_double_map("Staking", "ErasStakers", idx, &account, None) { - Ok(Some(exp)) => { - exposure = exp; - }, - _ => (), + if let Ok(Some(exp)) = api.get_storage_double_map("Staking", "ErasStakers", idx, &account, None) + { + exposure = exp; } if exposure.total > 0_u128 { - let call = api.payout_stakers(idx, account.clone()); + let call = api.payout_stakers(idx, account); let result = api.send_extrinsic(call.hex_encode(), XtStatus::InBlock).unwrap(); println!("{:?}", result); } diff --git a/src/extrinsic/staking.rs b/src/extrinsic/staking.rs index 416b02612..8eb4b1376 100644 --- a/src/extrinsic/staking.rs +++ b/src/extrinsic/staking.rs @@ -25,7 +25,7 @@ use codec::Compact; use sp_core::Pair; use sp_runtime::{AccountId32, MultiSignature, MultiSigner}; -pub use staking::RewardDestination; +pub use pallet_staking::RewardDestination; const STAKING_MODULE: &str = "Staking"; const STAKING_BOND: &str = "bond";