Skip to content
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

Rewards pallet-integration tests #428

Merged
merged 64 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
582f234
WIP adapting pool rewards and calling from other parts of the runtime…
gianfra-t Oct 6, 2023
0ca6ba9
pallet pooled rewards configured and connected to pallet nomination
gianfra-t Oct 9, 2023
1a86542
rename CurrencyId type of pooled-reward pallet to avoid conflict with…
gianfra-t Oct 10, 2023
af57fd3
remove tight coupling of pallet pooled rewards to nomination
gianfra-t Oct 10, 2023
9121dfa
remove coupling of vault registry to new pallet pooled rewards
gianfra-t Oct 10, 2023
1927a98
format
gianfra-t Oct 10, 2023
2756421
missing implementation of new pallet pooled-reward in replace mock
gianfra-t Oct 10, 2023
9e95a0b
call reward pool in vault-registry
gianfra-t Oct 10, 2023
112659f
added pooled-rewards to vault-registry mock runtime
gianfra-t Oct 10, 2023
4b4bfd5
implementing only one reward pallet
gianfra-t Oct 11, 2023
9b551ce
set reward staking to be equal to total stake (pallet stake)
gianfra-t Oct 12, 2023
30741b6
remove basecurrency from fee, conversion
gianfra-t Oct 12, 2023
f568071
remove comments
gianfra-t Oct 12, 2023
ab0fa4f
fix incorrect vault_id update
gianfra-t Oct 12, 2023
c126287
comments, fmt
gianfra-t Oct 12, 2023
06f3fa5
merge with branch main
gianfra-t Oct 12, 2023
ef50a31
WIP reward-distribution hook and percentage caculation
gianfra-t Oct 12, 2023
496197f
WIP base implementation
gianfra-t Oct 13, 2023
9283202
rewards distribution api with fee connection
gianfra-t Oct 13, 2023
4d2bf4e
better error handling in on initialize
gianfra-t Oct 13, 2023
4610683
remove temporary price api, added OracleApi from oracle module. Added…
gianfra-t Oct 13, 2023
5950f2f
set up basic tests for distribution
gianfra-t Oct 13, 2023
bd9d6c3
added more pallet tests for reward-distribution logic
gianfra-t Oct 16, 2023
d779185
print cleanups, added mock for security chain
gianfra-t Oct 16, 2023
6e9079c
wip commit
gianfra-t Oct 16, 2023
543ccf3
added collect extrinsic, added dependency of currecies config, remove…
gianfra-t Oct 17, 2023
b5f1391
wip
gianfra-t Oct 17, 2023
000d0c0
modified mock configs due to changes in reward-distribution, removed …
gianfra-t Oct 17, 2023
718345e
added benchmarks for collect extrinsic and on initialize function
gianfra-t Oct 17, 2023
90276fd
removed reward distribution logic from fee pallet
gianfra-t Oct 18, 2023
1cdeaa8
moved withdraw_all_rewards_from_vault to new pallet, added logic to h…
gianfra-t Oct 18, 2023
9c96d54
added new store to hold all reward currencies in stake pallet, modifi…
gianfra-t Oct 18, 2023
60dc317
fix configs for new Staking value
gianfra-t Oct 18, 2023
d789ef3
addressed comments and fixes
gianfra-t Oct 18, 2023
4af8e86
fix broken tests for staking pallet
gianfra-t Oct 18, 2023
5abd900
pallet integration tests for per block reward distribution
gianfra-t Oct 18, 2023
c500e93
fee reward distribution tests
gianfra-t Oct 19, 2023
8f99767
multiple vaults multiple collaterals works
gianfra-t Oct 19, 2023
92f4ca8
fix critical bug in reward collection
gianfra-t Oct 19, 2023
386ba48
multiple vault, collaterals and nominators works
gianfra-t Oct 19, 2023
d63e0d5
Merge branch '404-add-pooled-reward-pallet' into reward-pallet-tests
gianfra-t Oct 20, 2023
3ab1a2b
change currency id back to associated type
gianfra-t Oct 23, 2023
4f3f0ba
get the relevant block numbers in reward-distribution pallet from sec…
gianfra-t Oct 23, 2023
6f248b4
optimization on distribute_rewards loop
gianfra-t Oct 23, 2023
beace26
change wrongly modified extrinsic indexes
gianfra-t Oct 23, 2023
07eea5e
Merge branch '404-add-pooled-reward-pallet' into reward-pallet-tests
gianfra-t Oct 23, 2023
fd2804d
vault-registry integration tests for rewards cleanup
gianfra-t Oct 23, 2023
2cf6582
vault-registry integration tests for rewards cleanup
gianfra-t Oct 23, 2023
d4166cf
Merge branch 'reward-pallet-tests' of github.com:pendulum-chain/space…
gianfra-t Oct 23, 2023
dd4f8ed
added pool staking manager interface
gianfra-t Oct 24, 2023
05b2bed
native currency reward tracking and check
gianfra-t Oct 24, 2023
4a4e874
add native liability check to tests
gianfra-t Oct 24, 2023
9d1e2f1
add native liability check to tests
gianfra-t Oct 24, 2023
1df1504
Merge branch 'reward-pallet-tests' of github.com:pendulum-chain/space…
gianfra-t Oct 24, 2023
cb3cf16
comments and fixes
gianfra-t Oct 25, 2023
70f9a1a
review fixes and changes
gianfra-t Oct 26, 2023
ab136f0
merge with 404 and review changes
gianfra-t Oct 26, 2023
ae080e5
existential deposit check
gianfra-t Oct 27, 2023
cd448b0
existential deposit check in collect extrinsic
gianfra-t Oct 27, 2023
0c03096
merged with 404
gianfra-t Oct 27, 2023
f2cb856
merged with main
gianfra-t Nov 3, 2023
be4b95c
Merge branch 'main' into reward-pallet-tests
ebma Nov 8, 2023
3b316b4
clippy changes
gianfra-t Nov 8, 2023
75bcf69
Merge branch 'reward-pallet-tests' of github.com:pendulum-chain/space…
gianfra-t Nov 8, 2023
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
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.

2 changes: 2 additions & 0 deletions pallets/issue/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-li
[dev-dependencies]
mocktopus = "0.8.0"
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
nomination = {path = "../nomination", default-features = false}

currency = { path = "../currency", features = ['testing-utils', 'testing-constants'] }
reward = { path = "../reward" }
Expand Down Expand Up @@ -88,6 +89,7 @@ std = [
"orml-tokens/std",
"orml-traits/std",
"reward-distribution/std",
"nomination/std"
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
190 changes: 134 additions & 56 deletions pallets/issue/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ use frame_support::{
PalletId,
};
use mocktopus::{macros::mockable, mocking::clear_mocks};
use oracle::{
dia::DiaOracleAdapter,
testing_utils::{
MockConvertMoment, MockConvertPrice, MockDataFeeder, MockDiaOracle, MockOracleKeyConvertor,
},
};
use orml_currencies::BasicCurrencyAdapter;
use orml_traits::parameter_type_with_key;
use primitives::CurrencyId::XCM;
use sp_arithmetic::{FixedI128, FixedPointNumber, FixedU128};
use sp_core::H256;
use sp_runtime::{
Expand All @@ -17,15 +24,10 @@ use sp_runtime::{
pub use currency::{
testing_constants::{
DEFAULT_COLLATERAL_CURRENCY, DEFAULT_NATIVE_CURRENCY, DEFAULT_WRAPPED_CURRENCY,
DEFAULT_WRAPPED_CURRENCY_2, DEFAULT_WRAPPED_CURRENCY_3,
},
Amount,
};
use oracle::{
dia::DiaOracleAdapter,
testing_utils::{
MockConvertMoment, MockConvertPrice, MockDataFeeder, MockDiaOracle, MockOracleKeyConvertor,
},
};
pub use primitives::CurrencyId;
use primitives::{AmountCompatibility, VaultCurrencyPair, VaultId};

Expand Down Expand Up @@ -61,6 +63,7 @@ frame_support::construct_runtime!(
Oracle: oracle::{Pallet, Call, Config, Storage, Event<T>},
Fee: fee::{Pallet, Call, Config<T>, Storage},
Staking: staking::{Pallet, Storage, Event<T>},
Nomination: nomination::{Pallet, Call, Config, Storage, Event<T>},
VaultRegistry: vault_registry::{Pallet, Call, Config<T>, Storage, Event<T>},
}
);
Expand Down Expand Up @@ -263,6 +266,18 @@ impl security::Config for Test {
type RuntimeEvent = TestEvent;
type WeightInfo = ();
}
parameter_types! {
pub const MaxRewardCurrencies: u32= 10;
}

impl pooled_rewards::Config for Test {
type RuntimeEvent = TestEvent;
type SignedFixedPoint = SignedFixedPoint;
type PoolId = CurrencyId;
type PoolRewardsCurrencyId = CurrencyId;
type StakeId = VaultId<AccountId, CurrencyId>;
type MaxRewardCurrencies = MaxRewardCurrencies;
}

impl staking::Config for Test {
type RuntimeEvent = TestEvent;
Expand Down Expand Up @@ -307,16 +322,14 @@ impl fee::Config for Test {
}

parameter_types! {
pub const MaxRewardCurrencies: u32= 10;
pub const MinimumPeriod: Moment = 5;
}

impl pooled_rewards::Config for Test {
type RuntimeEvent = TestEvent;
type SignedFixedPoint = SignedFixedPoint;
type PoolId = CurrencyId;
type PoolRewardsCurrencyId = CurrencyId;
type StakeId = VaultId<AccountId, CurrencyId>;
type MaxRewardCurrencies = MaxRewardCurrencies;
impl pallet_timestamp::Config for Test {
type Moment = Moment;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}

parameter_types! {
Expand All @@ -327,14 +340,26 @@ parameter_types! {
pub struct OracleApiMock {}
impl oracle::OracleApi<Balance, CurrencyId> for OracleApiMock {
fn currency_to_usd(
_amount: &Balance,
amount: &Balance,
currency_id: &CurrencyId,
) -> Result<Balance, DispatchError> {
let native_currency = GetNativeCurrencyId::get();
match currency_id {
id if *id == native_currency => Ok(100),
_ => Ok(500),
}
let _native_currency = GetNativeCurrencyId::get();
let amount_in_usd = match currency_id {
&XCM(0) => amount * 5,
&XCM(1) => amount * 10,
&XCM(2) => amount * 15,
&XCM(3) => amount * 20,
&XCM(4) => amount * 35,
_native_currency => amount * 3,
};
Ok(amount_in_usd)
}
}
pub struct BlockNumberToBalance;

impl Convert<BlockNumber, Balance> for BlockNumberToBalance {
fn convert(a: BlockNumber) -> Balance {
a.into()
}
}

Expand All @@ -352,23 +377,9 @@ impl reward_distribution::Config for Test {
type FeePalletId = FeePalletId;
}

parameter_types! {
pub const MinimumPeriod: Moment = 5;
}

impl pallet_timestamp::Config for Test {
type Moment = Moment;
type OnTimestampSet = ();
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}

pub struct BlockNumberToBalance;

impl Convert<BlockNumber, Balance> for BlockNumberToBalance {
fn convert(a: BlockNumber) -> Balance {
a.into()
}
impl nomination::Config for Test {
type RuntimeEvent = TestEvent;
type WeightInfo = nomination::SubstrateWeight<Test>;
}

impl Config for Test {
Expand All @@ -382,13 +393,53 @@ pub type TestError = Error<Test>;
pub type VaultRegistryError = vault_registry::Error<Test>;

pub const USER: AccountId = 1;
pub const NOMINATOR1: AccountId = 11;
pub const NOMINATOR2: AccountId = 12;
pub const NOMINATOR_INIT_BALANCE: Balance = 10000;
pub const VAULT: VaultId<AccountId, CurrencyId> = VaultId {
account_id: 2,
currencies: VaultCurrencyPair {
collateral: DEFAULT_COLLATERAL_CURRENCY,
wrapped: DEFAULT_WRAPPED_CURRENCY,
},
};
pub const VAULT_2: VaultId<AccountId, CurrencyId> = VaultId {
account_id: 3,
currencies: VaultCurrencyPair {
collateral: DEFAULT_COLLATERAL_CURRENCY,
wrapped: DEFAULT_WRAPPED_CURRENCY_2,
},
};

pub const VAULT_3: VaultId<AccountId, CurrencyId> = VaultId {
account_id: 5,
currencies: VaultCurrencyPair { collateral: XCM(1), wrapped: DEFAULT_WRAPPED_CURRENCY_3 },
};

pub const VAULT_4: VaultId<AccountId, CurrencyId> = VaultId {
account_id: 6,
currencies: VaultCurrencyPair { collateral: XCM(1), wrapped: DEFAULT_WRAPPED_CURRENCY },
};

pub const VAULT_5: VaultId<AccountId, CurrencyId> = VaultId {
account_id: 7,
currencies: VaultCurrencyPair { collateral: XCM(2), wrapped: DEFAULT_WRAPPED_CURRENCY_2 },
};

const PAIR: VaultCurrencyPair<CurrencyId> = VaultCurrencyPair {
collateral: DEFAULT_COLLATERAL_CURRENCY,
wrapped: DEFAULT_WRAPPED_CURRENCY,
};
const PAIR_2: VaultCurrencyPair<CurrencyId> = VaultCurrencyPair {
collateral: DEFAULT_COLLATERAL_CURRENCY,
wrapped: DEFAULT_WRAPPED_CURRENCY_2,
};
const PAIR_3: VaultCurrencyPair<CurrencyId> =
VaultCurrencyPair { collateral: XCM(1), wrapped: DEFAULT_WRAPPED_CURRENCY_3 };
const PAIR_4: VaultCurrencyPair<CurrencyId> =
VaultCurrencyPair { collateral: XCM(1), wrapped: DEFAULT_WRAPPED_CURRENCY };
const PAIR_5: VaultCurrencyPair<CurrencyId> =
VaultCurrencyPair { collateral: XCM(2), wrapped: DEFAULT_WRAPPED_CURRENCY_2 };

pub const ALICE_BALANCE: u128 = 1_000_000;
pub const BOB_BALANCE: u128 = 1_000_000;
Expand All @@ -414,6 +465,12 @@ impl ExtBuilder {
.assimilate_storage(&mut storage)
.unwrap();

frame_support::traits::GenesisBuild::<Test>::assimilate_storage(
&nomination::GenesisConfig { is_nomination_enabled: true },
&mut storage,
)
.unwrap();

fee::GenesisConfig::<Test> {
issue_fee: UnsignedFixedPoint::checked_from_rational(5, 1000).unwrap(), // 0.5%
issue_griefing_collateral: UnsignedFixedPoint::checked_from_rational(5, 1_000_000_000)
Expand All @@ -434,26 +491,41 @@ impl ExtBuilder {
.assimilate_storage(&mut storage)
.unwrap();

const PAIR: VaultCurrencyPair<CurrencyId> = VaultCurrencyPair {
collateral: DEFAULT_COLLATERAL_CURRENCY,
wrapped: DEFAULT_WRAPPED_CURRENCY,
};
vault_registry::GenesisConfig::<Test> {
minimum_collateral_vault: vec![(DEFAULT_COLLATERAL_CURRENCY, 0)],
minimum_collateral_vault: vec![
(DEFAULT_COLLATERAL_CURRENCY, 0),
(XCM(1), 0),
(XCM(2), 0),
],
punishment_delay: 8,
system_collateral_ceiling: vec![(PAIR, 1_000_000_000_000)],
secure_collateral_threshold: vec![(
PAIR,
UnsignedFixedPoint::checked_from_rational(200, 100).unwrap(),
)],
premium_redeem_threshold: vec![(
PAIR,
UnsignedFixedPoint::checked_from_rational(120, 100).unwrap(),
)],
liquidation_collateral_threshold: vec![(
PAIR,
UnsignedFixedPoint::checked_from_rational(110, 100).unwrap(),
)],
system_collateral_ceiling: vec![
(PAIR, 1_000_000_000_000),
(PAIR_2, 1_000_000_000_000),
(PAIR_3, 1_000_000_000_000),
(PAIR_4, 1_000_000_000_000),
(PAIR_5, 1_000_000_000_000),
],
secure_collateral_threshold: vec![
(PAIR, UnsignedFixedPoint::checked_from_rational(200, 100).unwrap()),
(PAIR_2, UnsignedFixedPoint::checked_from_rational(200, 100).unwrap()),
(PAIR_3, UnsignedFixedPoint::checked_from_rational(200, 100).unwrap()),
(PAIR_4, UnsignedFixedPoint::checked_from_rational(200, 100).unwrap()),
(PAIR_5, UnsignedFixedPoint::checked_from_rational(200, 100).unwrap()),
],
premium_redeem_threshold: vec![
(PAIR, UnsignedFixedPoint::checked_from_rational(120, 100).unwrap()),
(PAIR_2, UnsignedFixedPoint::checked_from_rational(120, 100).unwrap()),
(PAIR_3, UnsignedFixedPoint::checked_from_rational(120, 100).unwrap()),
(PAIR_4, UnsignedFixedPoint::checked_from_rational(120, 100).unwrap()),
(PAIR_5, UnsignedFixedPoint::checked_from_rational(120, 100).unwrap()),
],
liquidation_collateral_threshold: vec![
(PAIR, UnsignedFixedPoint::checked_from_rational(110, 100).unwrap()),
(PAIR_2, UnsignedFixedPoint::checked_from_rational(110, 100).unwrap()),
(PAIR_3, UnsignedFixedPoint::checked_from_rational(110, 100).unwrap()),
(PAIR_4, UnsignedFixedPoint::checked_from_rational(110, 100).unwrap()),
(PAIR_5, UnsignedFixedPoint::checked_from_rational(110, 100).unwrap()),
],
}
.assimilate_storage(&mut storage)
.unwrap();
Expand All @@ -463,12 +535,18 @@ impl ExtBuilder {

pub fn build() -> sp_io::TestExternalities {
ExtBuilder::build_with(orml_tokens::GenesisConfig::<Test> {
balances: vec![DEFAULT_COLLATERAL_CURRENCY, DEFAULT_NATIVE_CURRENCY]
balances: vec![DEFAULT_COLLATERAL_CURRENCY, DEFAULT_NATIVE_CURRENCY, XCM(1), XCM(2)]
.into_iter()
.flat_map(|currency_id| {
vec![
(USER, currency_id, ALICE_BALANCE),
(VAULT.account_id, currency_id, BOB_BALANCE),
(VAULT_2.account_id, currency_id, BOB_BALANCE),
(VAULT_3.account_id, currency_id, BOB_BALANCE),
(VAULT_4.account_id, currency_id, BOB_BALANCE),
(VAULT_5.account_id, currency_id, BOB_BALANCE),
(NOMINATOR1, currency_id, NOMINATOR_INIT_BALANCE),
(NOMINATOR2, currency_id, NOMINATOR_INIT_BALANCE),
]
})
.collect(),
Expand Down
Loading
Loading