-
Notifications
You must be signed in to change notification settings - Fork 47
refactor: separate runtime api from custom rpc #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 39 commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
d723499
wip: poc
wischli 82da1d9
refactor: extrinsics and inherents
wischli 03cd34d
refactor: remove storage redundancy
wischli 766c777
tests: fix existing ones for staking
wischli 791ef9c
tests: 80%
wischli 38e92dd
feat: rm delegate_another_candidate and more
wischli d8bb955
docs: remove deprecated weight
wischli 24dc4bf
Apply suggestions from code review
wischli 7789cda
tests: 100%
wischli 870b534
fix: benchmarks
wischli 8926e4c
feat: remove multiple delegations (#391)
wischli 3dd9c6d
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli 05f520e
Merge branch 'develop' into wf/1930-staking-refactor
wischli f8c1419
Apply suggestions from code review
wischli ad13650
fix: suggestions from @ntn-x2 review
wischli adbcd46
fmt
wischli ddebe3a
fix: typos
wischli 28e47ef
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli 7b9fda3
fix: docker file
wischli 3e21d0c
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli e70c044
feat: add staking rewards runtime api
wischli b1de42a
docs: add new extrinsics to staking header
wischli 6f73296
feat: add staking rates api
wischli 20fd523
tests: add api
wischli 0f8160b
fix: unify staking runtime api
wischli 420170d
refactor: stricter claiming
wischli 25f1878
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli a3b2631
fix: easy suggestions from code review
wischli 15424e0
refactor: separate runtime api from custom rpc
wischli e703493
tests: comp api claim with claiming
wischli a00364b
refactor: move runtime api calls to mod
wischli 02eb407
stlye: fmt
wischli 379c736
refactor: split api into separate crates
wischli dfbd9e3
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli bc0890f
Merge remote-tracking branch 'origin/develop' into wf/1930-staking-re…
wischli c5548db
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli a9f58bc
refactor: simplify staking api name
wischli 01d09bb
refactor: normalize public creds runtime api
wischli 4f30a8a
refactor: machete cleanup
wischli 36e4abd
tests: add safety check to API rewards
wischli 56a5539
style: fmt
wischli 98eab08
Merge branch 'develop' into wf/1930-staking-refactor
wischli 30044dd
feat: write delegator state migration
wischli 55620d2
refactor: remove unneeded no_std flags
wischli 30be731
refactor: Delegator interface functions
wischli 55c6ac3
refactor: rm Option for Delegator.owner
wischli cd9684e
style: apply turbofish to staking
wischli f459050
fix: replace RewardCount with two counters
wischli 39e3716
refactor: rm col input for del stake adjustment
wischli bf304e5
Merge remote-tracking branch 'origin/wf/1930-staking-refactor' into w…
wischli f87f01e
style: fmt
wischli f6f73aa
Merge remote-tracking branch 'origin/develop' into wf-1930-staking-re…
wischli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] | ||
|
|
||
| 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, | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.rsfiles?