Skip to content

Commit

Permalink
Add staking feature to CI and fix clippy warnings (#330)
Browse files Browse the repository at this point in the history
* fix clippy warnings

* fix CI

* fix pallet staking import

* rename staking to pallet_staking
  • Loading branch information
haerdib authored Nov 21, 2022
1 parent bd8abf8 commit de8bc5e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 52 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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: |
Expand Down
59 changes: 26 additions & 33 deletions examples/example_batch_payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -29,12 +27,13 @@ fn main() {
.unwrap();
let grace_period: GracePeriod = GracePeriod { enabled: false, eras: 0 };
let mut results: Vec<Value> = 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();
Expand All @@ -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<AccountId32, u128> =
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 =
Expand All @@ -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")]
Expand All @@ -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,
Expand Down
26 changes: 11 additions & 15 deletions examples/example_staking_payout.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -21,23 +19,21 @@ fn main() {
.map(|api| api.set_signer(from))
.unwrap();
let mut exposure: Exposure<AccountId32, u128> = 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/extrinsic/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit de8bc5e

Please sign in to comment.