Skip to content

Commit

Permalink
add basic treasury mod
Browse files Browse the repository at this point in the history
  • Loading branch information
sekisamu committed Jun 19, 2019
1 parent c9cc81f commit f480b40
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 28 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ default_features = false
path = "../srml/staking"
package = 'evo-staking'

[dependencies.treasury]
default_features = false
path = "../srml/treasury"
package = 'evo-treasury'

[dependencies.version]
default_features = false
git = 'https://github.com/paritytech/substrate.git'
Expand Down
8 changes: 7 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl staking::Trait for Runtime {
type Currency = Kton;
type RewardCurrency = Ring;
type CurrencyToVote = CurrencyToVoteHandler;
type OnRewardMinted = ();
type OnRewardMinted = Treasury;
type Event = Event;
type Slash = ();
type Reward = ();
Expand All @@ -264,6 +264,11 @@ impl contract::Trait for Runtime {
type Gas = u64;
}

impl treasury::Trait for Runtime {
type Currency = Ring;
type Event = Event;
}

construct_runtime!(
pub enum Runtime with Log(InternalLog: DigestItem<Hash, AuthorityId, AuthoritySignature>) where
Block = Block,
Expand All @@ -283,6 +288,7 @@ construct_runtime!(
Contract: contract,
Session: session,
Staking: staking,
Treasury: treasury::{Module, Call, Storage, Event<T>},
}
);

Expand Down
23 changes: 23 additions & 0 deletions runtime/wasm/Cargo.lock

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

2 changes: 1 addition & 1 deletion src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn testnet_genesis(initial_authorities: Vec<(AccountId, AccountId, AuthorityId)>
sessions_per_era: 5,
bonding_duration: 12,
offline_slash: Perbill::zero(),
session_reward: Perbill::zero(),
share_bond_reward: Perbill::zero(),
current_session_reward: 0,
offline_slash_grace: 0,
stakers: initial_authorities.iter().map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator)).collect(),
Expand Down
19 changes: 13 additions & 6 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ use primitives::traits::{Convert, Zero, One, As, StaticLookup, CheckedSub, Satur
#[cfg(feature = "std")]
use primitives::{Serialize, Deserialize};
use system::ensure_signed;
use evo_support::traits::{ LockRate, SystemCurrency };
use evo_support::traits::{ LockRate, SystemCurrency, DarwiniaDilution };

mod mock;
mod tests;
Expand Down Expand Up @@ -410,7 +410,7 @@ pub trait Trait: system::Trait + session::Trait {
type CurrencyToVote: Convert<BalanceOf<Self>, u64> + Convert<u128, BalanceOf<Self>>;

/// Some tokens minted.
type OnRewardMinted: OnDilution<RewardBalanceOf<Self>>;
type OnRewardMinted: DarwiniaDilution<RewardBalanceOf<Self>>;

/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
Expand Down Expand Up @@ -525,6 +525,8 @@ decl_storage! {
/// it can be calculated when shifting epoch
pub IdealEraReward get(ideal_era_reward): RewardBalanceOf<T>;

pub TreasuryReward get(treasury_reward): Perbill;


}

Expand Down Expand Up @@ -770,6 +772,13 @@ decl_module! {
fn set_invulnerables(validators: Vec<T::AccountId>) {
<Invulnerables<T>>::put(validators);
}

fn set_multi_reward(bond_reward: u32, holder_reward: u32) {
let treasury_reward = 1_000_000_000 - bond_reward - holder_reward;
<ShareBondReward<T>>::put(Perbill::from_billionths(bond_reward));
<ShareHolderReward<T>>::put(Perbill::from_billionths(holder_reward));
<TreasuryReward<T>>::put(Perbill::from_billionths(treasury_reward));
}
}
}

Expand Down Expand Up @@ -935,10 +944,8 @@ impl<T: Trait> Module<T> {
T::Currency::reward_ktoner(holder_reward);

// TODO: ready for hacking
// deprecated for now
// let total_minted = reward * <BalanceOf<T> as As<usize>>::sa(validators.len());
// let total_rewarded_stake = Self::slot_stake() * <BalanceOf<T> as As<usize>>::sa(validators.len());
// T::OnRewardMinted::on_dilution(total_minted, total_rewarded_stake);
let bill_rest = Self::treasury_reward() * reward;
T::OnRewardMinted::on_dilution(bill_rest);
}

// Increment current era.
Expand Down
5 changes: 3 additions & 2 deletions srml/staking/src/minting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ pub fn compute_next_era_reward<T: Trait>() -> Result<RewardBalanceOf<T>, &'stati
let total_issuance_now = T::RewardCurrency::total_issuance();
if let Some(surplus) = cap.checked_sub(&total_issuance_now) {
// mint 20% of the rest
Ok(surplus / <RewardBalanceOf<T>>::sa(eras_per_epoch.as_()))
Ok(surplus / <RewardBalanceOf<T>>::sa(5 * eras_per_epoch.as_()))
} else {
return Err("too large.");
}

}
}

9 changes: 5 additions & 4 deletions srml/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ fn construct_staking_env() {
Staking::bond(Origin::signed(102), 2, 100, RewardDestination::Stash);
Staking::bond(Origin::signed(103), 3, 100, RewardDestination::Controller);

assert_eq!(Kton::total_issuance(), 300);
assert_eq!(Ring::total_issuance(), 10007734000000000);

}

#[test]
Expand Down Expand Up @@ -128,10 +131,8 @@ fn session_era_epoch_should_work_well() {
Session::check_rotate_session(System::block_number());
assert_eq!(Session::current_index(), 4);
assert_eq!(Staking::current_epoch(), 1);

assert_eq!(Staking::ideal_era_reward(), 0);


// ring total issuance /( 5 * 2)
assert_eq!(Staking::ideal_era_reward(), 998999226600000000);
});
}

Expand Down
6 changes: 5 additions & 1 deletion srml/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ pub trait LockRate {
fn bill_lock_rate() -> Perbill;

fn update_total_lock(amount: u64, is_add: bool) -> Result;
}
}

pub trait DarwiniaDilution<Balance> {
fn on_dilution(treasury_income: Balance);
}
35 changes: 22 additions & 13 deletions srml/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use serde::{Serialize, Deserialize};
use rstd::prelude::*;
use srml_support::{StorageValue, StorageMap, decl_module, decl_storage, decl_event, ensure};
use srml_support::traits::{Currency, ReservableCurrency, OnDilution, OnUnbalanced, Imbalance};
use runtime_primitives::{Permill, traits::{Zero, EnsureOrigin, StaticLookup}};
use primitives::{Permill, traits::{Zero, EnsureOrigin, StaticLookup}};
use parity_codec::{Encode, Decode};
use system::ensure_signed;
use evo_support::traits::DarwiniaDilution;

type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;
type PositiveImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::PositiveImbalance;
Expand All @@ -31,28 +32,36 @@ decl_module! {

decl_storage! {
trait Store for Module<T: Trait> as Treasury {

Pot get(pot): BalanceOf<T>;
}
}

decl_event! {
pub enum Event<T>
where
Balance = BalanceOf<T>,
<T as system::Trait>::AccountId
Balance = BalanceOf<T>
{

IntoPot(Balance),
}
}

impl<T: Trait> OnDilution<BalanceOf<T>> for Module<T> {
fn on_dilution(minted: BalanceOf<T>, portion: BalanceOf<T>) {
// Mint extra funds for the treasury to keep the ratio of portion to total_issuance equal
// pre dilution and post-dilution.
if !minted.is_zero() && !portion.is_zero() {
let total_issuance = T::Currency::total_issuance();
let funding = (total_issuance - portion) / portion * minted;
<Pot<T>>::mutate(|x| *x += funding);
//impl<T: Trait> OnDilution<BalanceOf<T>> for Module<T> {
// fn on_dilution(minted: BalanceOf<T>, portion: BalanceOf<T>) {
// // Mint extra funds for the treasury to keep the ratio of portion to total_issuance equal
// // pre dilution and post-dilution.
// if !minted.is_zero() && !portion.is_zero() {
// let total_issuance = T::Currency::total_issuance();
// let funding = (total_issuance - portion) / portion * minted;
// <Pot<T>>::mutate(|x| *x += funding);
// }
// }
//}

impl<T: Trait> DarwiniaDilution<BalanceOf<T>> for Module<T> {
fn on_dilution(treasury_income: BalanceOf<T>) {

if !treasury_income.is_zero() {
<Pot<T>>::mutate(|x| *x += treasury_income);
}
}
}

0 comments on commit f480b40

Please sign in to comment.