Skip to content
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
d723499
wip: poc
wischli Jul 20, 2022
82da1d9
refactor: extrinsics and inherents
wischli Jul 28, 2022
03cd34d
refactor: remove storage redundancy
wischli Jul 29, 2022
766c777
tests: fix existing ones for staking
wischli Jul 29, 2022
791ef9c
tests: 80%
wischli Aug 1, 2022
38e92dd
feat: rm delegate_another_candidate and more
wischli Aug 1, 2022
d8bb955
docs: remove deprecated weight
wischli Aug 1, 2022
24dc4bf
Apply suggestions from code review
wischli Aug 2, 2022
7789cda
tests: 100%
wischli Aug 2, 2022
870b534
fix: benchmarks
wischli Aug 3, 2022
8926e4c
feat: remove multiple delegations (#391)
wischli Aug 8, 2022
3dd9c6d
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli Aug 8, 2022
05f520e
Merge branch 'develop' into wf/1930-staking-refactor
wischli Aug 8, 2022
f8c1419
Apply suggestions from code review
wischli Aug 9, 2022
ad13650
fix: suggestions from @ntn-x2 review
wischli Aug 9, 2022
adbcd46
fmt
wischli Aug 9, 2022
ddebe3a
fix: typos
wischli Aug 9, 2022
28e47ef
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Aug 10, 2022
7b9fda3
fix: docker file
wischli Aug 11, 2022
3e21d0c
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Sep 7, 2022
e70c044
feat: add staking rewards runtime api
wischli Sep 8, 2022
b1de42a
docs: add new extrinsics to staking header
wischli Sep 8, 2022
6f73296
feat: add staking rates api
wischli Sep 8, 2022
20fd523
tests: add api
wischli Sep 8, 2022
0f8160b
fix: unify staking runtime api
wischli Sep 9, 2022
420170d
refactor: stricter claiming
wischli Sep 15, 2022
25f1878
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Sep 15, 2022
a3b2631
fix: easy suggestions from code review
wischli Sep 16, 2022
15424e0
refactor: separate runtime api from custom rpc
wischli Sep 16, 2022
e703493
tests: comp api claim with claiming
wischli Sep 19, 2022
a00364b
refactor: move runtime api calls to mod
wischli Sep 19, 2022
02eb407
stlye: fmt
wischli Sep 19, 2022
379c736
refactor: split api into separate crates
wischli Sep 19, 2022
dfbd9e3
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Oct 27, 2022
bc0890f
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli Oct 28, 2022
c5548db
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli Oct 28, 2022
a9f58bc
refactor: simplify staking api name
wischli Oct 28, 2022
01d09bb
refactor: normalize public creds runtime api
wischli Oct 28, 2022
4f30a8a
refactor: machete cleanup
wischli Oct 28, 2022
36e4abd
tests: add safety check to API rewards
wischli Oct 31, 2022
56a5539
style: fmt
wischli Oct 31, 2022
98eab08
Merge branch 'develop' into wf/1930-staking-refactor
wischli Oct 31, 2022
30044dd
feat: write delegator state migration
wischli Sep 16, 2022
55620d2
refactor: remove unneeded no_std flags
wischli Oct 31, 2022
30be731
refactor: Delegator interface functions
wischli Nov 1, 2022
55c6ac3
refactor: rm Option for Delegator.owner
wischli Nov 1, 2022
cd9684e
style: apply turbofish to staking
wischli Nov 1, 2022
f459050
fix: replace RewardCount with two counters
wischli Nov 2, 2022
39e3716
refactor: rm col input for del stake adjustment
wischli Nov 2, 2022
bf304e5
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli Nov 2, 2022
f87f01e
style: fmt
wischli Nov 3, 2022
f6f73aa
Merge remote-tracking branch 'origin/develop' into wf-1930-staking-re…
wischli Nov 3, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ panic = "unwind"
members = [
"nodes/*",
"pallets/*",
"rpc/did/runtime-api",
"rpc/public-credentials/runtime-api",
"runtime-api/*",
"runtimes/*",
"support",
"crates/*",
Expand Down
3 changes: 3 additions & 0 deletions pallets/parachain-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ sp-core = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v
sp-io = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.29"}

[dependencies]
kilt-runtime-api-staking = {path = "../../runtime-api/staking", default-features = false}

log = "0.4.17"
parity-scale-codec = {version = "3.1.5", default-features = false, features = ["derive"]}
scale-info = {version = "2.1.1", default-features = false, features = ["derive", "serde"]}
Expand Down Expand Up @@ -40,6 +42,7 @@ runtime-benchmarks = [
std = [
"frame-support/std",
"frame-system/std",
"kilt-runtime-api-staking/std",
"log/std",
"pallet-authorship/std",
"pallet-balances/std",
Expand Down
87 changes: 87 additions & 0 deletions pallets/parachain-staking/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// KILT Blockchain – https://botlabs.org
// Copyright (C) 2019-2022 BOTLabs GmbH

// The KILT Blockchain 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.

// The KILT Blockchain 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 this program. If not, see <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at [email protected]

#![cfg_attr(not(feature = "std"), no_std)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these? Aren't they only used in lib.rs files?


use crate::{
types::BalanceOf, CandidatePool, Config, DelegatorState, InflationConfig, Pallet, RewardCount, Rewards,
TotalCollatorStake,
};
use frame_support::traits::Currency;
use sp_runtime::{
traits::{Saturating, Zero},
Perquintill,
};

impl<T: Config> Pallet<T> {
/// Calculates the staking rewards for a given account address.
///
/// At least used in Runtime API.
pub fn get_unclaimed_staking_rewards(acc: &T::AccountId) -> BalanceOf<T> {
let mut reward_count = RewardCount::<T>::get(acc);
let rewards = Rewards::<T>::get(acc);

// delegators and collators need to be handled differently
if let Some(delegator_state) = DelegatorState::<T>::get(acc) {
// delegator reward counts do not automatically increment in order to be
// scalable, see [increment_delegator_rewards] for details
// therefore, we need to query the counter of the collator
// (`delegator_stare.owner`)
reward_count =
reward_count.saturating_add(delegator_state.owner.map(RewardCount::<T>::get).unwrap_or(0u32));
let stake = delegator_state.amount;
// rewards += stake * (self_count + collator_count) * delegator_reward_rate
rewards.saturating_add(Self::calc_block_rewards_delegator(stake, reward_count.into()))
} else if Self::is_active_candidate(acc).is_some() {
let stake = CandidatePool::<T>::get(acc)
.map(|state| state.stake)
.unwrap_or_else(BalanceOf::<T>::zero);
// rewards += stake * self_count * collator_reward_rate
rewards.saturating_add(Self::calc_block_rewards_collator(stake, reward_count.into()))
} else {
BalanceOf::<T>::zero()
}
}

/// Calculates the current staking and reward rates for collators and
/// delegators.
///
/// At least used in Runtime API.
pub fn get_staking_rates() -> kilt_runtime_api_staking::StakingRates {
let total_issuance = T::Currency::total_issuance();
let total_stake = TotalCollatorStake::<T>::get();
let inflation_config = InflationConfig::<T>::get();
let collator_staking_rate = Perquintill::from_rational(total_stake.collators, total_issuance);
let delegator_staking_rate = Perquintill::from_rational(total_stake.delegators, total_issuance);
let collator_reward_rate = Perquintill::from_rational(
inflation_config.collator.max_rate.deconstruct(),
collator_staking_rate.deconstruct(),
) * inflation_config.collator.reward_rate.annual;
let delegator_reward_rate = Perquintill::from_rational(
inflation_config.delegator.max_rate.deconstruct(),
delegator_staking_rate.deconstruct(),
) * inflation_config.delegator.reward_rate.annual;

kilt_runtime_api_staking::StakingRates {
collator_staking_rate,
collator_reward_rate,
delegator_staking_rate,
delegator_reward_rate,
}
}
}
Loading