Skip to content

Commit

Permalink
Added features for staking and payout (#294)
Browse files Browse the repository at this point in the history
* edited in statking

* Added none condition in error match

* Added payout stakers function call

* added library

* added batch call

* imported library

* removed ord trait

* force batch added

* update batch function

* updated force batch

* Seperate modules

* Added stakinf features

* utilitity call test

* testing call

* Added batch call

* Added staking example

* Edit README

* changed name

* Added reward payout example

* Removed unwanted structs

* Bug fixed for examples

* Fix formatting

* fix clippy unused import warnings

* fix formatting

* Bux Fixed
  • Loading branch information
shanithkk authored Nov 18, 2022
1 parent e35ad7a commit c75038a
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ The following examples can be found in the [examples](/examples) folder:
* [example_print_metadata](/examples/example_print_metadata.rs): Print the metadata of the node in a readable way.
* [example_sudo](/examples/example_sudo.rs): Create and send a sudo wrapped call.
* [example_transfer_using_seed](/examples/example_transfer_using_seed.rs): Transfer tokens by using a wrapper of compose_extrinsic with an account generated with a seed.
* [example_staking_payout](/src/examples/example_staking_payout.rs): Westend staking reward payout for validator.
* [example_batch_payout](/src/examples/example_staking_payout.rs): Batch reward payout for validator.

## Alternatives

Expand Down
170 changes: 170 additions & 0 deletions examples/example_batch_payout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#[cfg(feature = "staking-xt")]
use clap::{load_yaml, App};
#[cfg(feature = "staking-xt")]
use codec::{Decode, Encode};
#[cfg(feature = "staking-xt")]
use serde_json::Value;
#[cfg(feature = "staking-xt")]
use sp_core::{sr25519, Pair};
#[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,
};

#[cfg(feature = "staking-xt")]
fn main() {
env_logger::init();

let url = get_node_url_from_cli();
let from = AccountKeyring::Alice.pair();
let client = WsRpcClient::new(&url);
let api = Api::<_, _, PlainTipExtrinsicParams>::new(client)
.map(|api| api.set_signer(from))
.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 active_era: ActiveEraInfo =
api.get_storage_value("Staking", "ActiveEra", None).unwrap().unwrap();
let mut last_reward = get_last_reward("5DJcEbkNxsnNwHGrseg7cgbfUG8eiKzpuZqgSph5HqHrjgf6", &api);
let max_batched_transactions = 9;
let current_active_era = active_era.index;
let mut num_of_unclaimed_payout = current_active_era - last_reward - 1;
let mut start = 1;
let mut num_of_claimed_payouts = 0;

while num_of_unclaimed_payout > 0 {
let mut payout_calls = vec![];
let mut tx_limit = num_of_unclaimed_payout;
if num_of_unclaimed_payout > max_batched_transactions {
tx_limit = max_batched_transactions;
}

let mut i = start;
while i <= tx_limit + start - 1 {
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
.get_storage_double_map("Staking", "ErasStakers", idx, &account, None)
.unwrap()
{
Some(exp) => exposure = exp,
None => (),
}
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;
}
let mut current_tx_done = false;
let mut payout_calls_len = payout_calls.len();
if payout_calls_len > 0 {
let batching = api.batch(payout_calls);
let results_hash =
api.send_extrinsic(batching.hex_encode(), XtStatus::InBlock).unwrap();
num_of_claimed_payouts += payout_calls_len;

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);
}

#[cfg(feature = "staking-xt")]
pub fn get_node_url_from_cli() -> String {
let yml = load_yaml!("cli.yml");
let matches = App::from_yaml(yml).get_matches();

let node_ip = matches.value_of("node-server").unwrap_or("ws://127.0.0.1");
let node_port = matches.value_of("node-port").unwrap_or("9944");
let url = format!("{}:{}", node_ip, node_port);
println!("Interacting with node on {}\n", url);
url
}

#[cfg(feature = "staking-xt")]
pub fn get_last_reward(
validator_address: &str,
api: &substrate_api_client::Api<
sp_core::sr25519::Pair,
WsRpcClient,
BaseExtrinsicParams<PlainTip>,
>,
) -> u32 {
let api = api;
let mut account: AccountId32;
match AccountId32::from_ss58check(&validator_address) {
Ok(address) => account = 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(),
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,
_ => (),
}

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;
} else {
last_reward = res.claimed_rewards.pop().unwrap();
}
println!("{}", last_reward);
last_reward
}
#[cfg(feature = "staking-xt")]
pub struct GracePeriod {
pub enabled: bool,
pub eras: u32,
}
#[cfg(feature = "staking-xt")]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug)]
pub struct StakingLedger {
pub stash: AccountId32,
#[codec(compact)]
pub total: u128,
#[codec(compact)]
pub active: u128,
pub unlocking: Vec<u32>,
pub claimed_rewards: Vec<u32>,
}

#[cfg(not(feature = "staking-xt"))]
fn main() {}
59 changes: 59 additions & 0 deletions examples/example_staking_payout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#[cfg(feature = "staking-xt")]
use clap::{load_yaml, App};
#[cfg(feature = "staking-xt")]
use sp_core::{sr25519, Pair};
#[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};

#[cfg(feature = "staking-xt")]
fn main() {
env_logger::init();
let url = get_node_url_from_cli();
let from = AccountKeyring::Alice.pair();
let client = WsRpcClient::new(&url);
let api = Api::<_, _, PlainTipExtrinsicParams>::new(client)
.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 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 exposure.total > 0_u128 {
let call = api.payout_stakers(idx, account.clone());
let result = api.send_extrinsic(call.hex_encode(), XtStatus::InBlock).unwrap();
println!("{:?}", result);
}
}

#[cfg(feature = "staking-xt")]
pub fn get_node_url_from_cli() -> String {
let yml = load_yaml!("cli.yml");
let matches = App::from_yaml(yml).get_matches();

let node_ip = matches.value_of("node-server").unwrap_or("ws://127.0.0.1");
let node_port = matches.value_of("node-port").unwrap_or("9944");
let url = format!("{}:{}", node_ip, node_port);
println!("Interacting with node on {}\n", url);
url
}

#[cfg(not(feature = "staking-xt"))]
fn main() {}
33 changes: 33 additions & 0 deletions src/extrinsic/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
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.
*/

// Common types.

use codec::{Decode, Encode};
use sp_runtime::AccountId32;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, Debug)]
pub struct PayoutStakers {
pub validator_stash: AccountId32,
pub era: u32,
}
#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug)]
pub struct ForceEra {}

#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug)]
pub struct Batch<Call> {
pub calls: Vec<Call>,
}
2 changes: 2 additions & 0 deletions src/extrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
//! Offers some predefined extrinsics for common runtime modules.
pub mod balances;
pub mod common;
pub mod contract;
#[cfg(feature = "staking-xt")]
pub mod staking;
pub mod utility;
Loading

0 comments on commit c75038a

Please sign in to comment.