Skip to content

Commit

Permalink
nmap
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszaaa committed Sep 21, 2023
1 parent 0fb361b commit 197152c
Show file tree
Hide file tree
Showing 2 changed files with 301 additions and 41 deletions.
148 changes: 107 additions & 41 deletions pallets/proof-of-stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub use weights::WeightInfo;
#[derive(Eq, PartialEq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum ScheduleActivationKind {
ActivateKind(Option<ActivateKind>),
ActivatedLiquidity(TokenId)
ActivatedLiquidity(TokenId),
LiquidityMining,
}

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
Expand Down Expand Up @@ -104,8 +105,11 @@ use super::*;
type LiquidityMiningIssuanceVault: Get<Self::AccountId>;
#[pallet::constant]
type RewardsDistributionPeriod: Get<u32>;
/// The maximum number of schedules that can be active at one moment
type RewardsSchedulesLimit: Get<u32>;
/// The minimum number of rewards per session for 3rdparty rewards
type MinRewardsPerSession: Get<u128>;
/// The maximum number of reward tokens per pool
type MaxRewardTokensPerPool: Get<u32>;
type WeightInfo: WeightInfo;
type ValuationApi: Valuate<Balance = mangata_types::Balance, CurrencyId = mangata_types::TokenId>;
Expand Down Expand Up @@ -180,8 +184,8 @@ use super::*;
#[pallet::storage]
pub type PromotedPoolRewards3rdParty<T: Config> = StorageValue<_, BTreeMap<(TokenId, TokenId), U256>, ValueQuery>;

// pub type PromotedPoolRewards3rdParty<T: Config> = StorageValue<_, BTreeMap<(TokenId), U256>, ValueQuery>;

/// Stores information about set of active schedules
#[pallet::storage]
#[pallet::getter(fn schedules)]
pub type RewardsSchedules<T: Config> =
Expand Down Expand Up @@ -223,6 +227,32 @@ use super::*;
pub type TotalActivatedLiquidity3rdParty<T: Config> =
StorageMap<_, Twox64Concat, TokenId, BTreeMap<TokenId, u128>, ValueQuery>;


#[pallet::storage]
pub type ActivatedScheduleLiquidity<T> = StorageNMap<
_,
(
NMapKey<Twox64Concat, AccountIdOf<T>>,
NMapKey<Twox64Concat, TokenId>,
NMapKey<Twox64Concat, TokenId>
),
u128,
OptionQuery,
>;

#[pallet::storage]
pub type MaxActivatedLiquidity<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
AccountIdOf<T>,
Twox64Concat,
TokenId,
u128,
ValueQuery,
>;



#[pallet::call]
impl<T: Config> Pallet<T> {
/// Claims liquidity mining rewards
Expand Down Expand Up @@ -463,31 +493,45 @@ impl<T: Config> Pallet<T> {
),
Error::<T>::NotEnoughAssets
);
MaxActivatedLiquidity::<T>::mutate(user.clone(), liquidity_asset_id, |val| *val += amount);
},
ScheduleActivationKind::ActivatedLiquidity(token_id) => {
let already_activated_amount =
UserRewards3rdPartyInfo::<T>::get(user.clone(), (liquidity_asset_id, reward_token)).activated_amount;
let available_amount =
UserRewards3rdPartyInfo::<T>::get(user.clone(), (liquidity_asset_id, token_id)).activated_amount;
println!("already_activated_amount : {already_activated_amount:?}");
println!("available_amount : {available_amount:?}");

ensure!(
already_activated_amount + amount <= available_amount ,
Error::<T>::NotEnoughAssets
);
}
ScheduleActivationKind::LiquidityMining => {
let already_activated_amount =
UserRewards3rdPartyInfo::<T>::get(user.clone(), (liquidity_asset_id, reward_token)).activated_amount;
let available_amount =
RewardsInfo::<T>::get(user.clone(), liquidity_asset_id).activated_amount;
ensure!(
already_activated_amount + amount <= available_amount ,
Error::<T>::NotEnoughAssets
);
},
}

Self::set_liquidity_minting_checkpoint_3rdparty(user.clone(), liquidity_asset_id, amount, reward_token)?;

if let ScheduleActivationKind::ActivateKind(use_balance_from) = use_balance_from {
<T as Config>::ActivationReservesProvider::activate(
liquidity_asset_id,
&user,
amount,
use_balance_from,
)?;
match use_balance_from {
ScheduleActivationKind::ActivateKind(use_balance_from) => {
<T as Config>::ActivationReservesProvider::activate(
liquidity_asset_id,
&user,
amount,
use_balance_from,
)?;
},
ScheduleActivationKind::LiquidityMining => {
},
_ => {}

}

Pallet::<T>::deposit_event(Event::LiquidityActivated(user, liquidity_asset_id, amount));
Expand Down Expand Up @@ -516,7 +560,6 @@ impl<T: Config> Pallet<T> {

Self::set_liquidity_minting_checkpoint(user.clone(), liquidity_asset_id, amount)?;

// This must not fail due storage edits above
<T as Config>::ActivationReservesProvider::activate(
liquidity_asset_id,
&user,
Expand Down Expand Up @@ -575,27 +618,7 @@ impl<T: Config> Pallet<T> {
) -> Result<Balance, DispatchError> {
Self::ensure_is_promoted_pool(liquidity_asset_id)?;

// let mut result: sp_std::vec::vec<_> = default::default();
//
// // todo: get rid of collect
// let reward_tokens = rewardtokensperpool::<t>::get(liquidity_asset_id)
// .iter()
// .filter_map(|(token_id,_)|
// userrewards3rdpartyinfo::<t>::try_get(user.clone(), (liquidity_asset_id, token_id))
// )
// .map(|info| (info.last_checkpoint, info.activated_amount)
// .sort()
// .first
// .collect::<vec<_>>();
//
//


// let rewards_info = UserRewards3rdPartyInfo::<T>::try_get(user.clone(), (liquidity_asset_id, rewards_asset_id));
//
println!("hello world 0000");
if let Ok(info) = UserRewards3rdPartyInfo::<T>::try_get(user.clone(), (liquidity_asset_id, rewards_asset_id)){
println!("hello world");
let current_rewards = match info.activated_amount {
0 => 0u128,
_ => {
Expand Down Expand Up @@ -687,7 +710,6 @@ impl<T: Config> Pallet<T> {
let rewards_info = calc
.activate_more(liquidity_assets_added)
.map_err(|err| Into::<Error<T>>::into(err))?;
println!("liq minting rewards_info : {rewards_info:?}");

RewardsInfo::<T>::insert(user.clone(), liquidity_asset_id, rewards_info);
}
Expand Down Expand Up @@ -724,10 +746,26 @@ impl<T: Config> Pallet<T> {
let rewards_info = calc
.activate_more(liquidity_assets_added)
.map_err(|err| Into::<Error<T>>::into(err))?;

UserRewards3rdPartyInfo::<T>::insert(user.clone(), (liquidity_asset_id, liquidity_assets_reward), rewards_info);
}

ActivatedScheduleLiquidity::<T>::try_mutate_exists(
(user.clone(), liquidity_asset_id, liquidity_assets_reward ),
|v|
{
match v {
Some(x) => {
v.as_mut().map(|a| *a += liquidity_assets_added);
},
None => {
*v = Some(liquidity_assets_added);
},
};
Ok::<(),Error<T>>(())
}
)?;

let x = ActivatedScheduleLiquidity::<T>::get( (user.clone(), liquidity_asset_id, liquidity_assets_reward));

TotalActivatedLiquidity3rdParty::<T>::mutate(liquidity_asset_id, |activations| {
activations
Expand Down Expand Up @@ -813,11 +851,40 @@ impl<T: Config> Pallet<T> {
.and_modify(|val| *val -= liquidity_assets_burned);
});

<T as Config>::ActivationReservesProvider::deactivate(
liquidity_asset_id,
&user,
liquidity_assets_burned,
);
ActivatedScheduleLiquidity::<T>::try_mutate_exists(
(user.clone(), liquidity_asset_id, reward_token),
|v|
{
v.and_then(|a| a.checked_sub(liquidity_assets_burned)
.and_then(|val| {
if val > 0 {
*v = Some(val);
}else{
*v = None;
}
Some(val)
})
).ok_or(Error::<T>::MathOverflow)
}
)?;



if let None = ActivatedScheduleLiquidity::<T>::iter_prefix_values( (user.clone(), liquidity_asset_id),
).next(){

let amount = MaxActivatedLiquidity::<T>::mutate(user.clone(), liquidity_asset_id, |val| {
let prev = *val;
*val = 0;
prev
});

<T as Config>::ActivationReservesProvider::deactivate(
liquidity_asset_id,
&user,
amount,
);
}

Ok(())
}
Expand Down Expand Up @@ -985,7 +1052,6 @@ impl<T: Config> LiquidityMiningApi for Pallet<T> {
}
}

println!("POOLS : {:?}", pools);
PromotedPoolRewards3rdParty::<T>::put(pools);


Expand Down
Loading

0 comments on commit 197152c

Please sign in to comment.