Skip to content

Commit

Permalink
Rewards: move rewards api tests to generic testing (#1778)
Browse files Browse the repository at this point in the history
* move runtime api tests to generic

* remove non-generic runtime-api tests
  • Loading branch information
lemunozm authored Mar 27, 2024
1 parent 47fff26 commit e368dd5
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 332 deletions.
4 changes: 1 addition & 3 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ use runtime_common::{
},
permissions::PoolAdminCheck,
remarks::Remark,
rewards::SingleCurrencyMovement,
transfer_filter::PreLpTransfer,
xcm::AccountIdToMultiLocation,
xcm_transactor, AllowanceDeposit, CurrencyED, HoldId,
Expand Down Expand Up @@ -1204,9 +1205,6 @@ impl cumulus_pallet_dmp_queue::Config for Runtime {
// Block Rewards

parameter_types! {
// BlockRewards have exactly one group and currency
#[derive(scale_info::TypeInfo)]
pub const SingleCurrencyMovement: u32 = 1;
#[derive(scale_info::TypeInfo, Debug, PartialEq, Eq, Clone)]
pub const MaxChangesPerEpoch: u32 = 50;
pub const BlockRewardsPalletId: PalletId = cfg_types::ids::BLOCK_REWARDS_PALLET_ID;
Expand Down
4 changes: 1 addition & 3 deletions runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ use runtime_common::{
},
origin::EnsureAccountOrRootOr,
permissions::PoolAdminCheck,
rewards::SingleCurrencyMovement,
transfer_filter::PreLpTransfer,
xcm::AccountIdToMultiLocation,
xcm_transactor, AllowanceDeposit, CurrencyED, HoldId,
Expand Down Expand Up @@ -1220,9 +1221,6 @@ impl pallet_xcm_transactor::Config for Runtime {
// Block Rewards

parameter_types! {
// BlockRewards have exactly one group and currency
#[derive(scale_info::TypeInfo)]
pub const SingleCurrencyMovement: u32 = 1;
#[derive(scale_info::TypeInfo, Debug, PartialEq, Eq, Clone)]
pub const MaxChangesPerEpoch: u32 = 50;
pub const BlockRewardsPalletId: PalletId = cfg_types::ids::BLOCK_REWARDS_PALLET_ID;
Expand Down
7 changes: 7 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,10 @@ pub mod permissions {
}
}
}

pub mod rewards {
frame_support::parameter_types! {
#[derive(scale_info::TypeInfo)]
pub const SingleCurrencyMovement: u32 = 1;
}
}
23 changes: 9 additions & 14 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ use runtime_common::{
Feeder, OracleConverterBridge, OracleRatioProvider, OracleRatioProviderLocalAssetExtension,
},
permissions::PoolAdminCheck,
rewards::SingleCurrencyMovement,
transfer_filter::PreLpTransfer,
xcm::AccountIdToMultiLocation,
xcm_transactor, AllowanceDeposit, CurrencyED, HoldId,
Expand Down Expand Up @@ -1692,7 +1693,7 @@ impl pallet_rewards::mechanism::gap::Config for Runtime {
type Rate = FixedI128;
}

impl pallet_rewards::Config<pallet_rewards::Instance1> for Runtime {
impl pallet_rewards::Config<pallet_rewards::Instance2> for Runtime {
type Currency = Tokens;
type CurrencyId = CurrencyId;
type GroupId = u32;
Expand All @@ -1719,13 +1720,7 @@ impl pallet_liquidity_rewards::Config for Runtime {
type WeightInfo = ();
}

parameter_types! {
// BlockRewards have exactly one group and currency
#[derive(scale_info::TypeInfo)]
pub const SingleCurrencyMovement: u32 = 1;
}

impl pallet_rewards::Config<pallet_rewards::Instance2> for Runtime {
impl pallet_rewards::Config<pallet_rewards::Instance1> for Runtime {
type Currency = Tokens;
type CurrencyId = CurrencyId;
type GroupId = u32;
Expand Down Expand Up @@ -2105,11 +2100,11 @@ construct_runtime!(
// Removed: Nft = 103
Keystore: pallet_keystore::{Pallet, Call, Storage, Event<T>} = 104,
Investments: pallet_investments::{Pallet, Call, Storage, Event<T>} = 105,
LiquidityRewardsBase: pallet_rewards::<Instance1>::{Pallet, Storage, Event<T>, Config<T>} = 106,
LiquidityRewardsBase: pallet_rewards::<Instance2>::{Pallet, Storage, Event<T>, Config<T>} = 106,
LiquidityRewards: pallet_liquidity_rewards::{Pallet, Call, Storage, Event<T>} = 107,
LiquidityPools: pallet_liquidity_pools::{Pallet, Call, Storage, Event<T>} = 108,
PoolRegistry: pallet_pool_registry::{Pallet, Call, Storage, Event<T>} = 109,
BlockRewardsBase: pallet_rewards::<Instance2>::{Pallet, Storage, Event<T>, Config<T>} = 110,
BlockRewardsBase: pallet_rewards::<Instance1>::{Pallet, Storage, Event<T>, Config<T>} = 110,
BlockRewards: pallet_block_rewards::{Pallet, Call, Storage, Event<T>, Config<T>} = 111,
TransferAllowList: pallet_transfer_allowlist::{Pallet, Call, Storage, Event<T>} = 112,
GapRewardMechanism: pallet_rewards::mechanism::gap = 114,
Expand Down Expand Up @@ -2440,15 +2435,15 @@ impl_runtime_apis! {
impl runtime_common::apis::RewardsApi<Block, AccountId, Balance, CurrencyId> for Runtime {
fn list_currencies(domain: runtime_common::apis::RewardDomain, account_id: AccountId) -> Vec<CurrencyId> {
match domain {
runtime_common::apis::RewardDomain::Block => pallet_rewards::Pallet::<Runtime, pallet_rewards::Instance2>::list_currencies(&account_id),
runtime_common::apis::RewardDomain::Liquidity => pallet_rewards::Pallet::<Runtime, pallet_rewards::Instance1>::list_currencies(&account_id),
runtime_common::apis::RewardDomain::Block => pallet_rewards::Pallet::<Runtime, pallet_rewards::Instance1>::list_currencies(&account_id),
runtime_common::apis::RewardDomain::Liquidity => pallet_rewards::Pallet::<Runtime, pallet_rewards::Instance2>::list_currencies(&account_id),
}
}

fn compute_reward(domain: runtime_common::apis::RewardDomain, currency_id: CurrencyId, account_id: AccountId) -> Option<Balance> {
match domain {
runtime_common::apis::RewardDomain::Block => <pallet_rewards::Pallet::<Runtime, pallet_rewards::Instance2> as cfg_traits::rewards::AccountRewards<AccountId>>::compute_reward(currency_id, &account_id).ok(),
runtime_common::apis::RewardDomain::Liquidity => <pallet_rewards::Pallet::<Runtime, pallet_rewards::Instance1> as cfg_traits::rewards::AccountRewards<AccountId>>::compute_reward(currency_id, &account_id).ok(),
runtime_common::apis::RewardDomain::Block => <pallet_rewards::Pallet::<Runtime, pallet_rewards::Instance1> as cfg_traits::rewards::AccountRewards<AccountId>>::compute_reward(currency_id, &account_id).ok(),
runtime_common::apis::RewardDomain::Liquidity => <pallet_rewards::Pallet::<Runtime, pallet_rewards::Instance2> as cfg_traits::rewards::AccountRewards<AccountId>>::compute_reward(currency_id, &account_id).ok(),
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions runtime/integration-tests/src/generic/cases/rewards.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use cfg_primitives::CFG;
use cfg_traits::rewards::{AccountRewards, CurrencyGroupChange, DistributedRewards};
use cfg_types::tokens::CurrencyId;
use frame_support::assert_ok;
use runtime_common::apis::{runtime_decl_for_rewards_api::RewardsApiV1, RewardDomain};
use sp_runtime::traits::Get;

use crate::{
generic::{config::Runtime, env::Env, envs::runtime_env::RuntimeEnv, utils},
utils::accounts::Keyring,
};

type BlockRewards = pallet_rewards::Instance1;

const STAKER: Keyring = Keyring::Alice;

fn block_rewards_api<T: Runtime>() {
RuntimeEnv::<T>::default().parachain_state_mut(|| {
let group_id = 1u32;
let amount = 100 * CFG;

utils::give_balance::<T>(STAKER.id(), T::ExistentialDeposit::get() + amount);

assert_ok!(pallet_rewards::Pallet::<T, BlockRewards>::attach_currency(
CurrencyId::Native,
group_id,
));

assert_ok!(pallet_rewards::Pallet::<T, BlockRewards>::deposit_stake(
CurrencyId::Native,
&STAKER.id(),
amount,
));

assert_ok!(
pallet_rewards::Pallet::<T, BlockRewards>::distribute_reward(200 * CFG, [group_id])
);

assert_eq!(
T::Api::list_currencies(RewardDomain::Block, STAKER.id()),
vec![CurrencyId::Native]
);

assert_eq!(
T::Api::compute_reward(RewardDomain::Block, CurrencyId::Native, STAKER.id()),
Some(200 * CFG)
)
});
}

crate::test_for_runtimes!(all, block_rewards_api);
20 changes: 19 additions & 1 deletion runtime/integration-tests/src/generic/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Debug;

use cfg_primitives::{
AccountId, Address, AuraId, Balance, BlockNumber, CollectionId, CouncilCollective, Header,
Index, ItemId, LoanId, OrderId, PoolId, Signature, TrancheId,
IBalance, Index, ItemId, LoanId, OrderId, PoolId, Signature, TrancheId,
};
use cfg_traits::Millis;
use cfg_types::{
Expand All @@ -29,11 +29,13 @@ use runtime_common::{
fees::{DealWithFees, WeightToFee},
oracle::Feeder,
remarks::Remark,
rewards::SingleCurrencyMovement,
};
use sp_core::H256;
use sp_runtime::{
scale_info::TypeInfo,
traits::{AccountIdLookup, Block, Dispatchable, Get, Member},
FixedI128,
};

/// Kind of runtime to check in runtime time
Expand Down Expand Up @@ -149,6 +151,17 @@ pub trait Runtime:
+ pallet_evm_chain_id::Config
+ pallet_remarks::Config<RuntimeCall = Self::RuntimeCallExt, Remark = Remark>
+ pallet_utility::Config<RuntimeCall = Self::RuntimeCallExt>
+ pallet_rewards::Config<
pallet_rewards::Instance1,
GroupId = u32,
CurrencyId = CurrencyId,
RewardMechanism = pallet_rewards::mechanism::base::Mechanism<
Balance,
IBalance,
FixedI128,
SingleCurrencyMovement,
>,
>
{
/// Just the RuntimeCall type, but redefined with extra bounds.
/// You can add `From` bounds in order to convert pallet calls to
Expand Down Expand Up @@ -270,6 +283,11 @@ pub trait Runtime:
> + apis::runtime_decl_for_account_conversion_api::AccountConversionApiV1<
Self::Block,
AccountId,
> + apis::runtime_decl_for_rewards_api::RewardsApiV1<
Self::Block,
AccountId,
Balance,
CurrencyId,
>;

type MaxTranchesExt: Codec + Get<u32> + Member + PartialOrd + TypeInfo;
Expand Down
1 change: 1 addition & 0 deletions runtime/integration-tests/src/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod cases {
mod oracles;
mod proxy;
mod restricted_transfers;
mod rewards;
}

/// Generate tests for the specified runtimes or all runtimes.
Expand Down
1 change: 0 additions & 1 deletion runtime/integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
mod evm;
mod generic;
mod rewards;
mod runtime_apis;
mod utils;

/// Re-exports the correct runtimes that we run the integration tests with
Expand Down
Loading

0 comments on commit e368dd5

Please sign in to comment.