Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Dec 3, 2019
1 parent a7ad724 commit 82925e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
32 changes: 20 additions & 12 deletions srml/staking/src/inflation.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use sr_primitives::Perbill;

use sr_primitives::traits::{IntegerSquareRoot, SaturatedConversion};

/// utility in staking
use crate::{KtonBalanceOf, RingBalanceOf, Trait};
use rstd::convert::TryInto;
use sr_primitives::{
traits::{IntegerSquareRoot, SaturatedConversion},
Perbill,
};
use substrate_primitives::U256;

// 1 - (99 /100)^sqrt(year)
use super::{KtonBalanceOf, RingBalanceOf, TimeStamp, Trait};

// 1 - (99 / 100) ^ sqrt(year)
// <T: Trait + 'static>() -> RingBalanceOf<T>
pub fn compute_total_payout<T: Trait>(
era_duration: u64,
living_time: u64,
era_duration: TimeStamp,
living_time: TimeStamp,
total_left: u128,
) -> (RingBalanceOf<T>, RingBalanceOf<T>) {
// Milliseconds per year for the Julian year (365.25 days).
const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100;
const MILLISECONDS_PER_YEAR: TimeStamp = 1000 * 3600 * 24 * 36525 / 100;

let year: u32 = (living_time / MILLISECONDS_PER_YEAR + 1).saturated_into::<u32>();

let portion = Perbill::from_rational_approximation(era_duration as u64, MILLISECONDS_PER_YEAR);
let portion = Perbill::from_rational_approximation(era_duration, MILLISECONDS_PER_YEAR);

let maximum = portion * total_left;

// let maximum = maximum - maximum * 99_u128.pow(year.integer_sqrt()) / 100_u128.pow(year.integer_sqrt());
let maximum = maximum - maximum * 99_u128.pow(year.integer_sqrt()) / 100_u128.pow(year.integer_sqrt());

let payout = maximum * 1; // TODO: add treasury ratio: Perbill::from_rational_approximation(npos_token_staked, total_tokens);

Expand Down Expand Up @@ -59,3 +59,11 @@ pub fn compute_kton_return<T: Trait>(value: RingBalanceOf<T>, months: u32) -> Kt
/ U256::from(1_970_000);
res.as_u128().try_into().unwrap_or_default()
}

#[cfg(any(feature = "std", test))]
mod test {
// use super::*;

#[test]
fn _test() {}
}
16 changes: 6 additions & 10 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,11 @@ use darwinia_support::{
};
use phragmen::{build_support_map, elect, equalize, ExtendedBalance, PhragmenStakedAssignment};

#[allow(unused)]
#[cfg(any(feature = "bench", test))]
#[cfg(test)]
mod mock;
//
#[cfg(test)]
mod tests;

//#[cfg(all(feature = "bench", test))]
//mod benches;

const DEFAULT_MINIMUM_VALIDATOR_COUNT: u32 = 4;
const MAX_NOMINATIONS: usize = 16;
const MAX_UNSTAKE_THRESHOLD: u32 = 10;
Expand Down Expand Up @@ -999,8 +994,9 @@ impl<T: Trait> Module<T> {
// from the nearest expire time
if !value_left.is_zero() {
// sorted by expire_time from far to near
deposit_items
.sort_unstable_by_key(|item| u64::max_value() - item.expire_time.saturated_into::<u64>());
deposit_items.sort_unstable_by_key(|item| {
TimeStamp::max_value() - item.expire_time.saturated_into::<TimeStamp>()
});
deposit_items.drain_filter(|item| {
if value_left.is_zero() {
return false;
Expand Down Expand Up @@ -1084,8 +1080,8 @@ impl<T: Trait> Module<T> {

let total_left: u128 = (T::Cap::get() - T::Ring::total_issuance()).saturated_into::<u128>();
let (total_payout, max_payout) = inflation::compute_total_payout::<T>(
era_duration.saturated_into::<u64>(),
(T::GenesisTime::get() - T::Time::now()).saturated_into::<u64>(),
era_duration.saturated_into::<TimeStamp>(),
(T::Time::now() - T::GenesisTime::get()).saturated_into::<TimeStamp>(),
total_left,
);

Expand Down

0 comments on commit 82925e4

Please sign in to comment.