Skip to content

Commit

Permalink
add: PoS::reward_pool extrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszaaa committed Sep 13, 2023
1 parent 82db76a commit 76bb3d5
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ orml-tokens = { git = "https://github.com/mangata-finance//open-runtime-module-l
# patch generated by ./scripts/dev_manifest.sh
[patch."https://github.com/mangata-finance/moonbeam"]
# parachain-staking = { git = "https://github.com/mangata-finance//moonbeam", branch = "feature/update-staking-benchmarks" }
parachain-staking = { git = "https://github.com/mangata-finance//moonbeam", branch = "mangata-dev" }
# parachain-staking = { path = "../moonbeam/pallets/parachain-staking" }
# parachain-staking = { git = "https://github.com/mangata-finance//moonbeam", branch = "mangata-dev" }
parachain-staking = { path = "../moonbeam/pallets/parachain-staking" }

[patch."https://github.com/mangata-finance/crowdloan-rewards"]
pallet-crowdloan-rewards = { git = "https://github.com/mangata-finance//crowdloan-rewards", branch = "mangata-dev" }
Expand Down
1 change: 1 addition & 0 deletions pallets/proof-of-stake/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ lazy_static = "1.1.1"
env_logger = "0.9.0"
serial_test = { version = "0.6.0", default-features = false }
test-case = "2.0.2"
mockall = "0.11.0"

[features]
default = ['std']
Expand Down
118 changes: 115 additions & 3 deletions pallets/proof-of-stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
use frame_benchmarking::Zero;
use frame_support::{
dispatch::{DispatchError, DispatchResult},
ensure,
ensure
};
use frame_system::ensure_signed;
use frame_support::storage::bounded_btree_map::BoundedBTreeMap;
use sp_core::U256;
use sp_runtime::traits::AccountIdConversion;
use mangata_support::traits::Valuate;

use frame_support::{
pallet_prelude::*,
Expand Down Expand Up @@ -54,9 +57,14 @@ pub use weights::WeightInfo;

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

const PALLET_ID: frame_support::PalletId = frame_support::PalletId(*b"rewards!");

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::traits::Currency;
use mangata_support::traits::PoolCreateApi;

use super::*;

#[pallet::pallet]
#[pallet::without_storage_info]
Expand All @@ -66,7 +74,7 @@ pub mod pallet {
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}

#[cfg(feature = "runtime-benchmarks")]
pub trait PoSBenchmarkingConfig: pallet_issuance::Config {}
pub trait PoSBenchmarkingConrfig: pallet_issuance::Config {}
#[cfg(feature = "runtime-benchmarks")]
impl<T: pallet_issuance::Config> PoSBenchmarkingConfig for T {}

Expand All @@ -90,7 +98,10 @@ pub mod pallet {
type LiquidityMiningIssuanceVault: Get<Self::AccountId>;
#[pallet::constant]
type RewardsDistributionPeriod: Get<u32>;
type RewardsSchedulesLimit: Get<u32>;
type MinRewardsPerSession: Get<u128>;
type WeightInfo: WeightInfo;
type ValuationApi: Valuate<Balance = mangata_types::Balance, CurrencyId = mangata_types::TokenId>;
}

#[pallet::error]
Expand All @@ -112,6 +123,9 @@ pub mod pallet {
CalculateRewardsAllMathError,
MissingRewardsInfoError,
DeprecatedExtrinsic,
CannotScheduleRewardsInPast,
PoolDoesNotExist,
TooManySchedules
}

#[pallet::event]
Expand All @@ -135,6 +149,17 @@ pub mod pallet {
ValueQuery,
>;

// #[pallet::storage]
// pub type UserRewards3rdPartyInfo<T: Config> = StorageDoubleMap<
// _,
// Twox64Concat,
// AccountIdOf<T>,
// Twox64Concat,
// (TokenId, TokenId),
// RewardInfo,
// ValueQuery,
// >;

#[pallet::storage]
/// Stores information about pool weight and accumulated rewards. The accumulated
/// rewards amount is the number of rewards that can be claimed per liquidity
Expand All @@ -143,6 +168,23 @@ pub mod pallet {
pub type PromotedPoolRewards<T: Config> =
StorageValue<_, BTreeMap<TokenId, PromotedPools>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn schedules)]
pub type RewardsSchedules<T: Config> =
StorageValue<_, BoundedBTreeMap<(T::BlockNumber, TokenId, Balance, u64), (), T::RewardsSchedulesLimit>, ValueQuery>;

#[pallet::storage]
pub type ScheduleId<T: Config> = StorageValue<_, u64, ValueQuery>;


// #[pallet::storage]
// pub type Rewards3rdParty<T: Config> =
// StorageValue<_, BTreeMap<TokenId, (TokenId, Balance)>, ValueQuery>;
//
// #[pallet::storage]
// pub type Rewards3rdParty<T: Config> =
// StorageValue<_, BTreeMap<TokenId, (TokenId, Balance)>, ValueQuery>;

#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq, Eq, TypeInfo)]
/// Information about single token rewards
pub struct PromotedPools {
Expand Down Expand Up @@ -242,10 +284,80 @@ pub mod pallet {
amount,
)
}

/// Schedules rewards for selected liquidity token
/// - tokens - pair of tokens
/// - amount - amount of the token
/// - schedule_end - till when the rewards should be distributed, rewards are
/// distributed starting from the *next* session till
#[transactional]
#[pallet::call_index(4)]
#[pallet::weight(<<T as Config>::WeightInfo>::claim_rewards_all())]
pub fn reward_pool(
origin: OriginFor<T>,
pool: (TokenId, TokenId),
reward_token_id: TokenId,
amount: Balance,
schedule_end: T::BlockNumber,
) -> DispatchResult {
let sender = ensure_signed(origin)?;

ensure!(
<T as Config>::ValuationApi::get_liquidity_asset(pool.0, pool.1).is_ok(), // pool_exists(tokens.0, tokens.1),
Error::<T>::PoolDoesNotExist
);

ensure!(
schedule_end.saturated_into::<u32>() > Self::session_index(),
Error::<T>::CannotScheduleRewardsInPast
);

T::Currency::transfer(
reward_token_id.into(),
&sender,
&Self::pallet_account(),
amount.into(),
ExistenceRequirement::KeepAlive,
)?;

let current_session = Self::session_index();
let schedule_id = ScheduleId::<T>::get();

RewardsSchedules::<T>::try_mutate(|map| {

let key: Option<(_,_,_,_)> = map
.first_key_value()
.map(|(x,y)| x.clone());


if let Some(val) = key {
if current_session > val.0.saturated_into::<u32>() {
map.remove_entry(&val);
}
}

map.try_insert((schedule_end, reward_token_id, amount, schedule_id), ())
}).or(Err(Error::<T>::TooManySchedules))?;

ScheduleId::<T>::mutate(|v| *v += 1);

Ok(())
}
}
}

impl<T: Config> Pallet<T> {
fn pallet_account() -> T::AccountId {
PALLET_ID.into_account_truncating()
}

pub fn session_index() -> u32 {
frame_system::Pallet::<T>::block_number()
.saturated_into::<u32>()
.checked_div(T::RewardsDistributionPeriod::get())
.unwrap_or(0)
}

pub fn rewards_period() -> u32 {
<T as Config>::RewardsDistributionPeriod::get()
}
Expand Down
45 changes: 44 additions & 1 deletion pallets/proof-of-stake/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sp_runtime::{
use crate as pos;
use frame_support::{
construct_runtime, parameter_types,
traits::{tokens::currency::MultiTokenCurrency, ConstU32, Contains, Everything},
traits::{tokens::currency::MultiTokenCurrency, ConstU32, ConstU128, Contains, Everything},
PalletId,
};

Expand Down Expand Up @@ -202,14 +202,56 @@ lazy_static::lazy_static! {
};
}

mockall::mock! {
pub ValuationApi {}

impl Valuate for ValuationApi {
type Balance = Balance;
type CurrencyId = TokenId;

fn get_liquidity_asset(
first_asset_id: TokenId,
second_asset_id: TokenId,
) -> Result<TokenId, DispatchError>;

fn get_liquidity_token_mga_pool(
liquidity_token_id: TokenId,
) -> Result<(TokenId, TokenId), DispatchError>;

fn valuate_liquidity_token(
liquidity_token_id: TokenId,
liquidity_token_amount: Balance,
) -> Balance;

fn scale_liquidity_by_mga_valuation(
mga_valuation: Balance,
liquidity_token_amount: Balance,
mga_token_amount: Balance,
) -> Balance;

fn get_pool_state(liquidity_token_id: TokenId) -> Option<(Balance, Balance)>;

fn get_reserves(
first_asset_id: TokenId,
second_asset_id: TokenId,
) -> Result<(Balance, Balance), DispatchError>;



}
}

impl pos::Config for Test {
type RuntimeEvent = RuntimeEvent;
type ActivationReservesProvider = TokensActivationPassthrough<Test>;
type NativeCurrencyId = NativeCurrencyId;
type Currency = MultiTokenCurrencyAdapter<Test>;
type LiquidityMiningIssuanceVault = FakeLiquidityMiningIssuanceVault;
type RewardsDistributionPeriod = ConstU32<10>;
type RewardsSchedulesLimit = ConstU32<10>;
type MinRewardsPerSession = ConstU128<1_000_000>;
type WeightInfo = ();
type ValuationApi = MockValuationApi;
}

pub struct TokensActivationPassthrough<T: Config>(PhantomData<T>);
Expand Down Expand Up @@ -327,3 +369,4 @@ macro_rules! assert_event_emitted {
}
};
}

Loading

0 comments on commit 76bb3d5

Please sign in to comment.