Skip to content

Commit

Permalink
Fix bb bnc (#1591)
Browse files Browse the repository at this point in the history
* refactor: 💡 rename fn

* fix: 🐛 set_incentive
  • Loading branch information
yooml authored Jan 7, 2025
1 parent 595fd01 commit a48d519
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 69 deletions.
131 changes: 64 additions & 67 deletions pallets/bb-bnc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,6 @@ pub mod pallet {
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;

if !TotalLock::<T>::contains_key(currency_id) {
TotalLock::<T>::insert(currency_id, BalanceOf::<T>::zero());
}
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
MarkupCoefficient::<T>::insert(
Expand Down Expand Up @@ -654,7 +651,7 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
pub fn _checkpoint(
pub fn checkpoint(
who: &AccountIdOf<T>,
position: PositionId,
old_locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>>,
Expand Down Expand Up @@ -834,7 +831,7 @@ pub mod pallet {
Ok(())
}

pub fn _deposit_for(
pub fn deposit_for_inner(
who: &AccountIdOf<T>,
position: PositionId,
value: BalanceOf<T>,
Expand All @@ -843,22 +840,22 @@ pub mod pallet {
) -> DispatchResult {
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
let mut _locked = locked_balance;
let mut locked = locked_balance;
let supply_before = Supply::<T>::get();
let supply_after = supply_before
.checked_add(value)
.ok_or(ArithmeticError::Overflow)?;
Supply::<T>::set(supply_after);

let old_locked = _locked.clone();
_locked.amount = _locked
let old_locked = locked.clone();
locked.amount = locked
.amount
.checked_add(value)
.ok_or(ArithmeticError::Overflow)?;
if unlock_time != Zero::zero() {
_locked.end = unlock_time
locked.end = unlock_time
}
Locked::<T>::insert(position, _locked.clone());
Locked::<T>::insert(position, locked.clone());

let free_balance = T::MultiCurrency::free_balance(T::TokenType::get(), &who);
if value != BalanceOf::<T>::zero() {
Expand All @@ -876,16 +873,16 @@ pub mod pallet {
who,
position,
old_locked,
_locked.clone(),
locked.clone(),
UserMarkupInfos::<T>::get(who).as_ref(),
)?;

Self::deposit_event(Event::Minted {
who: who.clone(),
position,
value,
total_value: _locked.amount,
end: _locked.end,
total_value: locked.amount,
end: locked.end,
now: current_block_number,
});
Self::deposit_event(Event::Supply {
Expand Down Expand Up @@ -1040,7 +1037,7 @@ pub mod pallet {
.ok_or(ArithmeticError::Overflow)?;
}

Self::_checkpoint(who, position, old_locked.clone(), new_locked.clone())?;
Self::checkpoint(who, position, old_locked.clone(), new_locked.clone())?;
Ok(())
}

Expand Down Expand Up @@ -1111,14 +1108,14 @@ pub mod pallet {
LockedTokens::<T>::insert(&currency_id, &who, locked_token);
UserPositions::<T>::get(&who).into_iter().try_for_each(
|position| -> DispatchResult {
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
let locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
Locked::<T>::get(position);
ensure!(!_locked.amount.is_zero(), Error::<T>::ArgumentsError);
ensure!(!locked.amount.is_zero(), Error::<T>::ArgumentsError);
Self::markup_calc(
&who,
position,
_locked.clone(),
_locked,
locked.clone(),
locked,
Some(&user_markup_info),
)
},
Expand Down Expand Up @@ -1161,14 +1158,14 @@ pub mod pallet {
LockedTokens::<T>::remove(&currency_id, &who);
UserPositions::<T>::get(&who).into_iter().try_for_each(
|position| -> DispatchResult {
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
let locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
Locked::<T>::get(position);
ensure!(!_locked.amount.is_zero(), Error::<T>::ArgumentsError); // TODO
ensure!(!locked.amount.is_zero(), Error::<T>::ArgumentsError); // TODO
Self::markup_calc(
&who,
position,
_locked.clone(),
_locked,
locked.clone(),
locked,
Some(&user_markup_info),
)
},
Expand Down Expand Up @@ -1241,14 +1238,14 @@ pub mod pallet {
LockedTokens::<T>::insert(&currency_id, &who, locked_token);
UserPositions::<T>::get(&who).into_iter().try_for_each(
|position| -> DispatchResult {
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
let locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
Locked::<T>::get(position);
ensure!(!_locked.amount.is_zero(), Error::<T>::ArgumentsError); // TODO
ensure!(!locked.amount.is_zero(), Error::<T>::ArgumentsError); // TODO
Self::markup_calc(
&who,
position,
_locked.clone(),
_locked,
locked.clone(),
locked,
Some(&user_markup_info),
)
},
Expand Down Expand Up @@ -1278,19 +1275,19 @@ pub mod pallet {
pub fn withdraw_no_ensure(
who: &AccountIdOf<T>,
position: PositionId,
mut _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>>,
mut locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>>,
if_fast: Option<FixedU128>,
) -> DispatchResult {
let value = _locked.amount;
let old_locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> = _locked.clone();
_locked.end = Zero::zero();
_locked.amount = Zero::zero();
Locked::<T>::insert(position, _locked.clone());
let value = locked.amount;
let old_locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> = locked.clone();
locked.end = Zero::zero();
locked.amount = Zero::zero();
Locked::<T>::insert(position, locked.clone());

let supply_before = Supply::<T>::get();
let supply_after = supply_before
.checked_sub(value)
.ok_or(ArithmeticError::Overflow)?;
.ok_or(ArithmeticError::Underflow)?;
Supply::<T>::set(supply_after);

// BNC should be transferred before checkpoint
Expand All @@ -1314,7 +1311,7 @@ pub mod pallet {
}
}

Self::_checkpoint(who, position, old_locked, _locked.clone())?;
Self::checkpoint(who, position, old_locked, locked.clone())?;

T::FarmingInfo::refresh_gauge_pool(who)?;
Self::deposit_event(Event::Withdrawn {
Expand Down Expand Up @@ -1349,12 +1346,12 @@ 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: PositionId) -> DispatchResult {
let mut _locked = Locked::<T>::get(position);
let locked = Locked::<T>::get(position);
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
ensure!(_locked.end > current_block_number, Error::<T>::Expired);
let fast = Self::redeem_commission(_locked.end - current_block_number)?;
Self::withdraw_no_ensure(who, position, _locked, Some(fast))
ensure!(locked.end > current_block_number, Error::<T>::Expired);
let fast = Self::redeem_commission(locked.end - current_block_number)?;
Self::withdraw_no_ensure(who, position, locked, Some(fast))
}

fn set_ve_locked(who: &AccountIdOf<T>, new_locked_balance: BalanceOf<T>) -> DispatchResult {
Expand Down Expand Up @@ -1388,20 +1385,19 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
unlock_time: BlockNumberFor<T>,
) -> DispatchResult {
let new_position = Position::<T>::get();
let mut user_positions = UserPositions::<T>::get(who);
user_positions
.try_push(new_position)
.map_err(|_| Error::<T>::ExceedsMaxPositions)?;
UserPositions::<T>::insert(who, user_positions);
UserPositions::<T>::try_mutate(who, |user_positions| {
user_positions
.try_push(new_position)
.map_err(|_| Error::<T>::ExceedsMaxPositions)
})?;
Position::<T>::set(new_position + 1);

let bb_config = BbConfigs::<T>::get();
ensure!(value >= bb_config.min_mint, Error::<T>::BelowMinimumMint);

let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
Locked::<T>::get(new_position);
let locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> = Locked::<T>::get(new_position);
let real_unlock_time: BlockNumberFor<T> = unlock_time
.saturating_add(current_block_number)
.checked_div(&T::Week::get())
Expand All @@ -1423,11 +1419,11 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
.ok_or(ArithmeticError::Overflow)?;
ensure!(real_unlock_time <= max_block, Error::<T>::ArgumentsError);
ensure!(
_locked.amount == BalanceOf::<T>::zero(),
locked.amount == BalanceOf::<T>::zero(),
Error::<T>::LockExist
); // Withdraw old tokens first

Self::_deposit_for(who, new_position, value, real_unlock_time, _locked)?;
Self::deposit_for_inner(who, new_position, value, real_unlock_time, locked)?;
T::FarmingInfo::refresh_gauge_pool(who)?;
Self::deposit_event(Event::LockCreated {
who: who.to_owned(),
Expand Down Expand Up @@ -1474,7 +1470,7 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
Error::<T>::LockNotExist
);

Self::_deposit_for(
Self::deposit_for_inner(
who,
position,
BalanceOf::<T>::zero(),
Expand All @@ -1498,15 +1494,15 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
) -> DispatchResult {
let bb_config = BbConfigs::<T>::get();
ensure!(value >= bb_config.min_mint, Error::<T>::BelowMinimumMint);
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> = Locked::<T>::get(position);
let locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> = Locked::<T>::get(position);
ensure!(
_locked.amount > BalanceOf::<T>::zero(),
locked.amount > BalanceOf::<T>::zero(),
Error::<T>::LockNotExist
); // Need to be executed after create_lock
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
ensure!(_locked.end > current_block_number, Error::<T>::Expired); // Cannot add to expired/non-existent lock
Self::_deposit_for(who, position, value, Zero::zero(), _locked)?;
ensure!(locked.end > current_block_number, Error::<T>::Expired); // Cannot add to expired/non-existent lock
Self::deposit_for_inner(who, position, value, Zero::zero(), locked)?;
T::FarmingInfo::refresh_gauge_pool(who)?;
Self::deposit_event(Event::AmountIncreased {
who: who.to_owned(),
Expand All @@ -1518,17 +1514,17 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl

#[transactional]
fn deposit_for(who: &AccountIdOf<T>, position: u128, value: BalanceOf<T>) -> DispatchResult {
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> = Locked::<T>::get(position);
Self::_deposit_for(who, position, value, Zero::zero(), _locked)
let locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> = Locked::<T>::get(position);
Self::deposit_for_inner(who, position, value, Zero::zero(), locked)
}

#[transactional]
fn withdraw_inner(who: &AccountIdOf<T>, position: u128) -> DispatchResult {
let mut _locked = Locked::<T>::get(position);
let locked = Locked::<T>::get(position);
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
ensure!(current_block_number >= _locked.end, Error::<T>::Expired);
Self::withdraw_no_ensure(who, position, _locked, None)
ensure!(current_block_number >= locked.end, Error::<T>::Expired);
Self::withdraw_no_ensure(who, position, locked, None)
}

fn balance_of(
Expand Down Expand Up @@ -1651,16 +1647,17 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
rewards_duration: Option<BlockNumberFor<T>>,
controller: Option<AccountIdOf<T>>,
) {
let mut incentive_config = IncentiveConfigs::<T>::get(pool_id);

if let Some(rewards_duration) = rewards_duration {
incentive_config.rewards_duration = rewards_duration;
};
if let Some(controller) = controller {
incentive_config.incentive_controller = Some(controller.clone());
}
IncentiveConfigs::<T>::set(pool_id, incentive_config.clone());
Self::deposit_event(Event::IncentiveSet { incentive_config });
IncentiveConfigs::<T>::mutate(pool_id, |incentive_config| {
if let Some(rewards_duration) = rewards_duration {
incentive_config.rewards_duration = rewards_duration;
};
if let Some(controller) = controller {
incentive_config.incentive_controller = Some(controller.clone());
}
Self::deposit_event(Event::IncentiveSet {
incentive_config: incentive_config.clone(),
});
})
}

#[transactional]
Expand Down
4 changes: 2 additions & 2 deletions pallets/bb-bnc/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub trait BbBNCInterface<AccountId, CurrencyId, Balance, BlockNumber> {
who: &AccountId,
value: Balance,
unlock_time: BlockNumber,
) -> DispatchResult; // Deposit `_value` BNC for `who` and lock until `_unlock_time`
fn increase_amount_inner(who: &AccountId, position: u128, value: Balance) -> DispatchResult; // Deposit `_value` additional BNC for `who` without modifying the unlock time
) -> DispatchResult; // Deposit `value` BNC for `who` and lock until `_unlock_time`
fn increase_amount_inner(who: &AccountId, position: u128, value: Balance) -> DispatchResult; // Deposit `value` additional BNC for `who` without modifying the unlock time
fn increase_unlock_time_inner(
who: &AccountId,
position: u128,
Expand Down

0 comments on commit a48d519

Please sign in to comment.