Skip to content

support multi currency rewards #601

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 24 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 16 additions & 4 deletions rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ pub mod module {
type Handler: RewardHandler<Self::AccountId, Self::CurrencyId, Balance = Self::Balance, PoolId = Self::PoolId>;
}

#[pallet::error]
pub enum Error<T> {
/// Pool does not exist
PoolDoesNotExist,
}

/// Stores reward pool info.
#[pallet::storage]
#[pallet::getter(fn pools)]
Expand Down Expand Up @@ -124,11 +130,16 @@ pub mod module {
}

impl<T: Config> Pallet<T> {
pub fn accumulate_reward(pool: &T::PoolId, reward_currency: T::CurrencyId, reward_increment: T::Balance) {
pub fn accumulate_reward(
pool: &T::PoolId,
reward_currency: T::CurrencyId,
reward_increment: T::Balance,
) -> DispatchResult {
if reward_increment.is_zero() {
return;
return Ok(());
}
Pools::<T>::mutate_exists(pool, |maybe_pool_info| {
Pools::<T>::mutate_exists(pool, |maybe_pool_info| -> DispatchResult {
ensure!(maybe_pool_info.is_some(), Error::<T>::PoolDoesNotExist);
Copy link
Member

@xlc xlc Sep 4, 2021

Choose a reason for hiding this comment

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

let pool_info = maybe_pool_info.ok_or(Error::<T>::PoolDoesNotExist)?;

Copy link
Member Author

Choose a reason for hiding this comment

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

let pool_info = maybe_pool_info.ok_or(Error::::PoolDoesNotExist)?;

missed that 👌

if let Some(pool_info) = maybe_pool_info {
if let Some((total_reward, _)) = pool_info.rewards.get_mut(&reward_currency) {
*total_reward = total_reward.saturating_add(reward_increment);
Expand All @@ -138,7 +149,8 @@ impl<T: Config> Pallet<T> {
.insert(reward_currency, (reward_increment, Zero::zero()));
}
}
});
Ok(())
})
}

pub fn add_share(who: &T::AccountId, pool: &T::PoolId, add_amount: T::Share) {
Expand Down
10 changes: 7 additions & 3 deletions rewards/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![cfg(test)]

use super::*;
use frame_support::{assert_noop, assert_ok};
use mock::*;

#[test]
Expand Down Expand Up @@ -434,12 +435,15 @@ fn accumulate_reward_should_work() {
assert_eq!(RewardsModule::pools(DOT_POOL), Default::default());

// should not accumulate if pool doesn't exist
RewardsModule::accumulate_reward(&DOT_POOL, NATIVE_COIN, 100);
assert_noop!(
RewardsModule::accumulate_reward(&DOT_POOL, NATIVE_COIN, 100),
Error::<Runtime>::PoolDoesNotExist
);
assert_eq!(RewardsModule::pools(DOT_POOL), PoolInfo::default());

RewardsModule::add_share(&ALICE, &DOT_POOL, 100);

RewardsModule::accumulate_reward(&DOT_POOL, NATIVE_COIN, 100);
assert_ok!(RewardsModule::accumulate_reward(&DOT_POOL, NATIVE_COIN, 100));
assert_eq!(
RewardsModule::pools(DOT_POOL),
PoolInfo {
Expand All @@ -448,7 +452,7 @@ fn accumulate_reward_should_work() {
}
);

RewardsModule::accumulate_reward(&DOT_POOL, STABLE_COIN, 200);
assert_ok!(RewardsModule::accumulate_reward(&DOT_POOL, STABLE_COIN, 200));
assert_eq!(
RewardsModule::pools(DOT_POOL),
PoolInfo {
Expand Down