Skip to content

Commit

Permalink
Remove getters in ve-minting (#1368)
Browse files Browse the repository at this point in the history
  • Loading branch information
MJLNSN authored Aug 15, 2024
1 parent 06e5903 commit 1d4b994
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 61 deletions.
14 changes: 7 additions & 7 deletions pallets/ve-minting/src/incentive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ impl<T: Config> Pallet<T> {
/// Check if the current block number is within the end time of the reward pool
pub fn last_time_reward_applicable(pool_id: PoolId) -> BlockNumberFor<T> {
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
if current_block_number < Self::incentive_configs(pool_id).period_finish {
if current_block_number < IncentiveConfigs::<T>::get(pool_id).period_finish {
current_block_number
} else {
Self::incentive_configs(pool_id).period_finish
IncentiveConfigs::<T>::get(pool_id).period_finish
}
}

/// Calculate the reward per token for the given pool
pub fn reward_per_token(
pool_id: PoolId,
) -> Result<BTreeMap<CurrencyIdOf<T>, BalanceOf<T>>, DispatchError> {
let mut conf = Self::incentive_configs(pool_id);
let mut conf = IncentiveConfigs::<T>::get(pool_id);
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let total_supply = Self::total_supply(current_block_number)?;
if total_supply == BalanceOf::<T>::zero() {
Expand Down Expand Up @@ -108,7 +108,7 @@ impl<T: Config> Pallet<T> {
) -> Result<BTreeMap<CurrencyIdOf<T>, BalanceOf<T>>, DispatchError> {
let reward_per_token = Self::reward_per_token(pool_id)?;
let vetoken_balance = Self::balance_of_current_block(addr)?;
let mut rewards = if let Some(rewards) = Self::rewards(addr) {
let mut rewards = if let Some(rewards) = Rewards::<T>::get(addr) {
rewards
} else {
BTreeMap::<CurrencyIdOf<T>, BalanceOf<T>>::default()
Expand All @@ -118,7 +118,7 @@ impl<T: Config> Pallet<T> {
.checked_mul(U256::from(
reward
.saturating_sub(
*Self::user_reward_per_token_paid(addr)
*UserRewardPerTokenPaid::<T>::get(addr)
.get(currency)
.unwrap_or(&BalanceOf::<T>::zero()),
)
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<T: Config> Pallet<T> {
if Self::balance_of_current_block(addr)? == BalanceOf::<T>::zero() {
return Ok(());
} // Excit earlier if balance of token is zero
if let Some(rewards) = Self::rewards(addr) {
if let Some(rewards) = Rewards::<T>::get(addr) {
rewards.iter().try_for_each(|(currency, &reward)| -> DispatchResult {
T::MultiCurrency::transfer(
*currency,
Expand Down Expand Up @@ -242,7 +242,7 @@ impl<T: Config> Pallet<T> {
None => return Err(Error::<T>::NoController.into()),
};
Self::update_reward(pool_id, None, None)?;
let mut conf = Self::incentive_configs(pool_id);
let mut conf = IncentiveConfigs::<T>::get(pool_id);
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();

if current_block_number >= conf.period_finish {
Expand Down
63 changes: 23 additions & 40 deletions pallets/ve-minting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,16 @@ pub mod pallet {
}

#[pallet::storage]
#[pallet::getter(fn supply)]
pub type Supply<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn ve_configs)]
pub type VeConfigs<T: Config> =
StorageValue<_, VeConfig<BalanceOf<T>, BlockNumberFor<T>>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn epoch)]
pub type Epoch<T: Config> = StorageValue<_, U256, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn locked)]
pub type Locked<T: Config> = StorageMap<
_,
Blake2_128Concat,
Expand All @@ -227,18 +223,15 @@ pub mod pallet {
>;

#[pallet::storage]
#[pallet::getter(fn user_locked)]
pub type UserLocked<T: Config> =
StorageMap<_, Blake2_128Concat, AccountIdOf<T>, BalanceOf<T>, ValueQuery>;

// Each week has a Point struct stored in PointHistory.
#[pallet::storage]
#[pallet::getter(fn point_history)]
pub type PointHistory<T: Config> =
StorageMap<_, Twox64Concat, U256, Point<BalanceOf<T>, BlockNumberFor<T>>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn user_point_history)]
pub type UserPointHistory<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
Expand All @@ -250,17 +243,14 @@ pub mod pallet {
>;

#[pallet::storage]
#[pallet::getter(fn user_point_epoch)]
pub type UserPointEpoch<T: Config> = StorageMap<_, Blake2_128Concat, u128, U256, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn slope_changes)]
pub type SlopeChanges<T: Config> =
StorageMap<_, Twox64Concat, BlockNumberFor<T>, i128, ValueQuery>;

// Incentive
#[pallet::storage]
#[pallet::getter(fn incentive_configs)]
pub type IncentiveConfigs<T: Config> = StorageMap<
_,
Blake2_128Concat,
Expand All @@ -270,7 +260,6 @@ pub mod pallet {
>;

#[pallet::storage]
#[pallet::getter(fn user_reward_per_token_paid)]
pub type UserRewardPerTokenPaid<T: Config> = StorageMap<
_,
Blake2_128Concat,
Expand All @@ -280,17 +269,14 @@ pub mod pallet {
>;

#[pallet::storage]
#[pallet::getter(fn rewards)]
pub type Rewards<T: Config> =
StorageMap<_, Blake2_128Concat, AccountIdOf<T>, BTreeMap<CurrencyIdOf<T>, BalanceOf<T>>>;

#[pallet::storage]
#[pallet::getter(fn user_markup_infos)]
pub type UserMarkupInfos<T: Config> =
StorageMap<_, Blake2_128Concat, AccountIdOf<T>, UserMarkupInfo>;

#[pallet::storage]
#[pallet::getter(fn locked_tokens)]
pub type LockedTokens<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
Expand All @@ -301,17 +287,14 @@ pub mod pallet {
>;

#[pallet::storage]
#[pallet::getter(fn total_lock)]
pub type TotalLock<T: Config> =
StorageMap<_, Twox64Concat, CurrencyIdOf<T>, BalanceOf<T>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn markup_coefficient)]
pub type MarkupCoefficient<T: Config> =
StorageMap<_, Twox64Concat, CurrencyIdOf<T>, MarkupCoefficientInfo<BlockNumberFor<T>>>;

#[pallet::storage]
#[pallet::getter(fn position)]
pub type Position<T: Config> = StorageValue<_, u128, ValueQuery>;

#[pallet::storage]
Expand Down Expand Up @@ -346,7 +329,7 @@ pub mod pallet {
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;

let mut ve_config = Self::ve_configs();
let mut ve_config = VeConfigs::<T>::get();
if let Some(min_mint) = min_mint {
ve_config.min_mint = min_mint;
};
Expand Down Expand Up @@ -493,7 +476,7 @@ pub mod pallet {
let mut u_old = Point::<BalanceOf<T>, BlockNumberFor<T>>::default();
let mut u_new = Point::<BalanceOf<T>, BlockNumberFor<T>>::default();
let mut new_dslope = 0_i128;
let mut g_epoch: U256 = Self::epoch();
let mut g_epoch: U256 = Epoch::<T>::get();
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();

if old_locked.end > current_block_number && old_locked.amount > BalanceOf::<T>::zero() {
Expand Down Expand Up @@ -526,12 +509,12 @@ pub mod pallet {
)
.ok_or(ArithmeticError::Overflow)?;
}
let mut old_dslope = Self::slope_changes(old_locked.end);
let mut old_dslope = SlopeChanges::<T>::get(old_locked.end);
if new_locked.end != Zero::zero() {
if new_locked.end == old_locked.end {
new_dslope = old_dslope
} else {
new_dslope = Self::slope_changes(new_locked.end)
new_dslope = SlopeChanges::<T>::get(new_locked.end)
}
}

Expand All @@ -542,13 +525,13 @@ pub mod pallet {
amount: Zero::zero(),
};
if g_epoch > U256::zero() {
last_point = Self::point_history(g_epoch);
last_point = PointHistory::<T>::get(g_epoch);
} else {
// last_point.amount = T::MultiCurrency::free_balance(
// T::TokenType::get(),
// &T::VeMintingPalletId::get().into_account_truncating(),
// );
last_point.amount = Self::supply();
last_point.amount = Supply::<T>::get();
}
let mut last_checkpoint = last_point.block;
let mut t_i: BlockNumberFor<T> = last_checkpoint
Expand All @@ -562,7 +545,7 @@ pub mod pallet {
if t_i > current_block_number {
t_i = current_block_number
} else {
d_slope = Self::slope_changes(t_i)
d_slope = SlopeChanges::<T>::get(t_i)
}
last_point.bias = last_point
.bias
Expand Down Expand Up @@ -596,7 +579,7 @@ pub mod pallet {

// Fill for the current block, if applicable
if t_i == current_block_number {
last_point.amount = Self::supply();
last_point.amount = Supply::<T>::get();
// last_point.amount = T::MultiCurrency::free_balance(
// T::TokenType::get(),
// &T::VeMintingPalletId::get().into_account_truncating(),
Expand Down Expand Up @@ -649,12 +632,12 @@ pub mod pallet {
}

// Now handle user history
let user_epoch = Self::user_point_epoch(addr)
let user_epoch = UserPointEpoch::<T>::get(addr)
.checked_add(U256::one())
.ok_or(ArithmeticError::Overflow)?;
UserPointEpoch::<T>::insert(addr, user_epoch);
u_new.block = current_block_number;
// u_new.amount = Self::locked(addr).amount;
// u_new.amount = Locked::<T>::get(addr).amount;
u_new.amount = new_locked.amount;
UserPointHistory::<T>::insert(addr, user_epoch, u_new);

Expand All @@ -670,7 +653,7 @@ pub mod pallet {
) -> DispatchResult {
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let mut _locked = locked_balance;
let supply_before = Self::supply();
let supply_before = Supply::<T>::get();
Supply::<T>::set(supply_before.checked_add(value).ok_or(ArithmeticError::Overflow)?);

let old_locked = _locked.clone();
Expand Down Expand Up @@ -715,12 +698,12 @@ pub mod pallet {
addr: u128,
) -> Result<BalanceOf<T>, DispatchError> {
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let u_epoch = Self::user_point_epoch(addr);
let u_epoch = UserPointEpoch::<T>::get(addr);
if u_epoch == U256::zero() {
return Ok(Zero::zero());
} else {
let mut last_point: Point<BalanceOf<T>, BlockNumberFor<T>> =
Self::user_point_history(addr, u_epoch);
UserPointHistory::<T>::get(addr, u_epoch);

last_point.bias = last_point
.bias
Expand Down Expand Up @@ -756,7 +739,7 @@ pub mod pallet {

// Binary search
let mut _min = U256::zero();
let mut _max = Self::user_point_epoch(addr);
let mut _max = UserPointEpoch::<T>::get(addr);
for _i in 0..128 {
if _min >= _max {
break;
Expand All @@ -769,15 +752,15 @@ pub mod pallet {
.checked_div(U256::from(2_u128))
.ok_or(ArithmeticError::Overflow)?;

if Self::user_point_history(addr, _mid).block <= block {
if UserPointHistory::<T>::get(addr, _mid).block <= block {
_min = _mid
} else {
_max = _mid.checked_sub(U256::one()).ok_or(ArithmeticError::Overflow)?
}
}

let mut upoint: Point<BalanceOf<T>, BlockNumberFor<T>> =
Self::user_point_history(addr, _min);
UserPointHistory::<T>::get(addr, _min);
upoint.bias = upoint
.bias
.checked_sub(
Expand Down Expand Up @@ -883,7 +866,7 @@ pub mod pallet {
let left: FixedU128 = FixedU128::checked_from_integer(locked_token.amount)
.and_then(|x| x.checked_mul(&markup_coefficient.markup_coefficient))
.and_then(|x| {
x.checked_div(&FixedU128::checked_from_integer(Self::total_lock(asset_id))?)
x.checked_div(&FixedU128::checked_from_integer(TotalLock::<T>::get(asset_id))?)
})
.ok_or(ArithmeticError::Overflow)?;

Expand Down Expand Up @@ -916,7 +899,7 @@ pub mod pallet {
UserPositions::<T>::get(&addr).into_iter().try_for_each(
|position| -> DispatchResult {
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
Self::locked(position);
Locked::<T>::get(position);
ensure!(!_locked.amount.is_zero(), Error::<T>::ArgumentsError);
Self::markup_calc(
&addr,
Expand Down Expand Up @@ -961,7 +944,7 @@ pub mod pallet {
UserPositions::<T>::get(&addr).into_iter().try_for_each(
|position| -> DispatchResult {
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
Self::locked(position);
Locked::<T>::get(position);
ensure!(!_locked.amount.is_zero(), Error::<T>::ArgumentsError); // TODO
Self::markup_calc(
&addr,
Expand Down Expand Up @@ -998,7 +981,7 @@ pub mod pallet {
let left: FixedU128 = FixedU128::checked_from_integer(locked_token.amount)
.and_then(|x| x.checked_mul(&markup_coefficient.markup_coefficient))
.and_then(|x| {
x.checked_div(&FixedU128::checked_from_integer(Self::total_lock(
x.checked_div(&FixedU128::checked_from_integer(TotalLock::<T>::get(
asset_id,
))?)
})
Expand Down Expand Up @@ -1035,7 +1018,7 @@ pub mod pallet {
UserPositions::<T>::get(&addr).into_iter().try_for_each(
|position| -> DispatchResult {
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
Self::locked(position);
Locked::<T>::get(position);
ensure!(!_locked.amount.is_zero(), Error::<T>::ArgumentsError); // TODO
Self::markup_calc(
&addr,
Expand Down Expand Up @@ -1080,7 +1063,7 @@ pub mod pallet {
_locked.amount = Zero::zero();
Locked::<T>::insert(position, _locked.clone());

let supply_before = Self::supply();
let supply_before = Supply::<T>::get();
Supply::<T>::set(supply_before.saturating_sub(value));

// BNC should be transferred before checkpoint
Expand Down Expand Up @@ -1132,7 +1115,7 @@ pub mod pallet {

/// This function will check the lock and redeem it regardless of whether it has expired.
pub fn redeem_unlock_inner(who: &AccountIdOf<T>, position: u128) -> DispatchResult {
let mut _locked = Self::locked(position);
let mut _locked = Locked::<T>::get(position);
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
ensure!(_locked.end > current_block_number, Error::<T>::Expired);
let fast = Self::redeem_commission(_locked.end - current_block_number)?;
Expand Down
Loading

0 comments on commit 1d4b994

Please sign in to comment.