Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 0 additions & 16 deletions rewards/src/default_weight.rs

This file was deleted.

49 changes: 15 additions & 34 deletions rewards/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(clippy::unused_unit)]
#![cfg_attr(not(feature = "std"), no_std)]

mod default_weight;
mod mock;
mod tests;

Expand Down Expand Up @@ -37,10 +36,6 @@ pub use module::*;
pub mod module {
use super::*;

pub trait WeightInfo {
fn on_initialize(c: u32) -> Weight;
}

#[pallet::config]
pub trait Config: frame_system::Config {
/// The share type of pool.
Expand All @@ -64,19 +59,10 @@ pub mod module {
+ FixedPointOperand;

/// The reward pool ID type.
type PoolId: Parameter + Member + Copy + FullCodec;
type PoolId: Parameter + Member + Clone + FullCodec;

/// The `RewardHandler`
type Handler: RewardHandler<
Self::AccountId,
Self::BlockNumber,
Share = Self::Share,
Balance = Self::Balance,
PoolId = Self::PoolId,
>;

/// Weight information for extrinsics in this module.
type WeightInfo: WeightInfo;
type Handler: RewardHandler<Self::AccountId, Balance = Self::Balance, PoolId = Self::PoolId>;
}

/// Stores reward pool info.
Expand All @@ -95,27 +81,22 @@ pub mod module {
pub struct Pallet<T>(PhantomData<T>);

#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
fn on_initialize(now: T::BlockNumber) -> Weight {
let mut count = 0;
T::Handler::accumulate_reward(now, |pool, reward_to_accumulate| {
if !reward_to_accumulate.is_zero() {
count += 1;
Pools::<T>::mutate(pool, |pool_info| {
pool_info.total_rewards = pool_info.total_rewards.saturating_add(reward_to_accumulate)
});
}
});
T::WeightInfo::on_initialize(count)
}
}
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {}
}

impl<T: Config> Pallet<T> {
pub fn add_share(who: &T::AccountId, pool: T::PoolId, add_amount: T::Share) {
pub fn accumulate_reward(pool: &T::PoolId, reward_increment: T::Balance) {
if !reward_increment.is_zero() {
Pools::<T>::mutate(pool, |pool_info| {
pool_info.total_rewards = pool_info.total_rewards.saturating_add(reward_increment)
});
}
}

pub fn add_share(who: &T::AccountId, pool: &T::PoolId, add_amount: T::Share) {
if add_amount.is_zero() {
return;
}
Expand All @@ -135,7 +116,7 @@ impl<T: Config> Pallet<T> {
});
}

pub fn remove_share(who: &T::AccountId, pool: T::PoolId, remove_amount: T::Share) {
pub fn remove_share(who: &T::AccountId, pool: &T::PoolId, remove_amount: T::Share) {
if remove_amount.is_zero() {
return;
}
Expand Down Expand Up @@ -167,7 +148,7 @@ impl<T: Config> Pallet<T> {
});
}

pub fn set_share(who: &T::AccountId, pool: T::PoolId, new_share: T::Share) {
pub fn set_share(who: &T::AccountId, pool: &T::PoolId, new_share: T::Share) {
let (share, _) = Self::share_and_withdrawn_reward(pool, who);

if new_share > share {
Expand All @@ -177,7 +158,7 @@ impl<T: Config> Pallet<T> {
}
}

pub fn claim_rewards(who: &T::AccountId, pool: T::PoolId) {
pub fn claim_rewards(who: &T::AccountId, pool: &T::PoolId) {
ShareAndWithdrawnReward::<T>::mutate(pool, who, |(share, withdrawn_rewards)| {
if share.is_zero() {
return;
Expand Down
36 changes: 4 additions & 32 deletions rewards/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ pub type Balance = u64;
pub type Share = u64;
pub type PoolId = u32;
pub type BlockNumber = u64;
pub type CurrencyId = u32;

pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const CAROL: AccountId = 3;
pub const DOT_POOL: PoolId = 1;
pub const BTC_POOL: PoolId = 2;
pub const XBTC_POOL: PoolId = 3;

parameter_types! {
pub const BlockHashCount: u64 = 250;
Expand Down Expand Up @@ -59,41 +56,17 @@ thread_local! {
}

pub struct Handler;
impl RewardHandler<AccountId, BlockNumber> for Handler {
type Share = Share;
impl RewardHandler<AccountId> for Handler {
type Balance = Balance;
type PoolId = PoolId;
type CurrencyId = CurrencyId;

fn accumulate_reward(
now: BlockNumber,
mut callback: impl FnMut(Self::PoolId, Self::Balance),
) -> Vec<(Self::CurrencyId, Self::Balance)> {
if now % 2 == 0 {
let mut total_accumulated_rewards = 0;
let valid_pool_ids = vec![DOT_POOL, BTC_POOL];

for (pool, _) in Pools::<Runtime>::iter() {
if valid_pool_ids.contains(&pool) {
let rewards: Balance = 100;
callback(pool, rewards);
total_accumulated_rewards += rewards;
}
}

vec![(1, total_accumulated_rewards)]
} else {
vec![]
}
}

fn payout(who: &AccountId, pool: Self::PoolId, amount: Self::Balance) {
fn payout(who: &AccountId, pool: &Self::PoolId, amount: Self::Balance) {
RECEIVED_PAYOUT.with(|v| {
let mut old_map = v.borrow().clone();
if let Some(before) = old_map.get_mut(&(pool, *who)) {
if let Some(before) = old_map.get_mut(&(*pool, *who)) {
*before += amount;
} else {
old_map.insert((pool, *who), amount);
old_map.insert((*pool, *who), amount);
};

*v.borrow_mut() = old_map;
Expand All @@ -106,7 +79,6 @@ impl Config for Runtime {
type Balance = Balance;
type PoolId = PoolId;
type Handler = Handler;
type WeightInfo = ();
}

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
Expand Down
Loading