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

Update current block #1552

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions pallets/bb-bnc/src/incentive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ where
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();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
if current_block_number < IncentiveConfigs::<T>::get(pool_id).period_finish {
current_block_number
} else {
Expand All @@ -76,7 +77,8 @@ impl<T: Config> Pallet<T> {
pool_id: PoolId,
) -> Result<BTreeMap<CurrencyIdOf<T>, BalanceOf<T>>, DispatchError> {
let mut conf = IncentiveConfigs::<T>::get(pool_id);
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
let total_supply = Self::total_supply(current_block_number)?;
if total_supply == BalanceOf::<T>::zero() {
return Ok(conf.reward_per_token_stored);
Expand Down Expand Up @@ -262,7 +264,8 @@ impl<T: Config> Pallet<T> {
};
Self::update_reward(pool_id, None, None)?;
let mut conf = IncentiveConfigs::<T>::get(pool_id);
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();

if current_block_number >= conf.period_finish {
Self::add_reward(&account, &mut conf, &rewards, Zero::zero())?;
Expand Down
46 changes: 31 additions & 15 deletions pallets/bb-bnc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use frame_support::{
pallet_prelude::*,
sp_runtime::{
traits::{
AccountIdConversion, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Convert,
Saturating, UniqueSaturatedInto, Zero,
AccountIdConversion, BlockNumberProvider, CheckedAdd, CheckedDiv, CheckedMul,
CheckedSub, Convert, Saturating, UniqueSaturatedInto, Zero,
},
ArithmeticError, DispatchError, FixedPointNumber, FixedU128, SaturatedConversion,
},
Expand Down Expand Up @@ -151,6 +151,9 @@ pub mod pallet {

#[pallet::constant]
type FourYears: Get<BlockNumberFor<Self>>;

/// The current block number provider.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
}

#[pallet::event]
Expand Down Expand Up @@ -396,7 +399,8 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
let n: BlockNumberFor<T> = T::BlockNumberProvider::current_block_number();
let conf = IncentiveConfigs::<T>::get(BB_BNC_SYSTEM_POOL_ID);
if n == conf.period_finish {
if let Some(e) = Self::notify_reward_amount(
Expand Down Expand Up @@ -590,7 +594,8 @@ pub mod pallet {
if !TotalLock::<T>::contains_key(currency_id) {
TotalLock::<T>::insert(currency_id, BalanceOf::<T>::zero());
}
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
MarkupCoefficient::<T>::insert(
currency_id,
MarkupCoefficientInfo {
Expand Down Expand Up @@ -661,7 +666,8 @@ pub mod pallet {
let mut u_new = Point::<BalanceOf<T>, BlockNumberFor<T>>::default();
let mut new_dslope = 0_i128;
let mut g_epoch: U256 = Epoch::<T>::get();
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();

if old_locked.end > current_block_number && old_locked.amount > BalanceOf::<T>::zero() {
u_old.slope = U256::from(old_locked.amount.saturated_into::<u128>())
Expand Down Expand Up @@ -835,7 +841,8 @@ pub mod pallet {
unlock_time: BlockNumberFor<T>,
locked_balance: LockedBalance<BalanceOf<T>, BlockNumberFor<T>>,
) -> DispatchResult {
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
let mut _locked = locked_balance;
let supply_before = Supply::<T>::get();
let supply_after = supply_before
Expand Down Expand Up @@ -892,7 +899,8 @@ pub mod pallet {
pub(crate) fn balance_of_position_current_block(
position: PositionId,
) -> Result<BalanceOf<T>, DispatchError> {
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
let u_epoch = UserPointEpoch::<T>::get(position);
if u_epoch == U256::zero() {
return Ok(Zero::zero());
Expand Down Expand Up @@ -929,7 +937,8 @@ pub mod pallet {
position: PositionId,
block: BlockNumberFor<T>,
) -> Result<BalanceOf<T>, DispatchError> {
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
ensure!(block <= current_block_number, Error::<T>::Expired);

// Binary search
Expand Down Expand Up @@ -1051,7 +1060,8 @@ pub mod pallet {
Ok(())
})?;

let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();

let mut user_markup_info = UserMarkupInfos::<T>::get(&who).unwrap_or_default();
let mut locked_token =
Expand Down Expand Up @@ -1174,7 +1184,8 @@ pub mod pallet {
pub fn refresh_inner(currency_id: CurrencyIdOf<T>) -> DispatchResult {
let markup_coefficient =
MarkupCoefficient::<T>::get(currency_id).ok_or(Error::<T>::ArgumentsError)?;
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
let limit = T::MarkupRefreshLimit::get();
let mut all_refreshed = true;
let mut refresh_count = 0;
Expand Down Expand Up @@ -1339,7 +1350,8 @@ 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 current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
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))
Expand Down Expand Up @@ -1386,7 +1398,8 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
let bb_config = BbConfigs::<T>::get();
ensure!(value >= bb_config.min_mint, Error::<T>::BelowMinimumMint);

let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
let _locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> =
Locked::<T>::get(new_position);
let real_unlock_time: BlockNumberFor<T> = unlock_time
Expand Down Expand Up @@ -1432,7 +1445,8 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
) -> DispatchResult {
let bb_config = BbConfigs::<T>::get();
let locked: LockedBalance<BalanceOf<T>, BlockNumberFor<T>> = Locked::<T>::get(position);
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
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
let real_unlock_time: BlockNumberFor<T> = unlock_time
Expand Down Expand Up @@ -1487,7 +1501,8 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
_locked.amount > BalanceOf::<T>::zero(),
Error::<T>::LockNotExist
); // Need to be executed after create_lock
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
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)?;
Self::deposit_event(Event::AmountIncreased {
Expand All @@ -1507,7 +1522,8 @@ impl<T: Config> BbBNCInterface<AccountIdOf<T>, CurrencyIdOf<T>, BalanceOf<T>, Bl
#[transactional]
fn withdraw_inner(who: &AccountIdOf<T>, position: u128) -> DispatchResult {
let mut _locked = Locked::<T>::get(position);
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
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)
}
Expand Down
1 change: 1 addition & 0 deletions pallets/bb-bnc/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ impl bb_bnc::Config for Runtime {
type FarmingInfo = ();
type FourYears = FourYears;
type OneYear = OneYear;
type BlockNumberProvider = System;
}

pub struct ParachainId;
Expand Down
10 changes: 7 additions & 3 deletions pallets/buy-back/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use cumulus_primitives_core::ParaId;
use frame_support::{
pallet_prelude::*,
sp_runtime::{
traits::{AccountIdConversion, One, Zero},
traits::{AccountIdConversion, BlockNumberProvider, One, Zero},
Permill, SaturatedConversion, Saturating,
},
transactional, PalletId,
Expand Down Expand Up @@ -95,6 +95,9 @@ pub mod pallet {
BalanceOf<Self>,
BlockNumberFor<Self>,
>;

/// The current block number provider.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
}

#[pallet::event]
Expand Down Expand Up @@ -196,7 +199,8 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
let n: BlockNumberFor<T> = T::BlockNumberProvider::current_block_number();
let buyback_address = T::BuyBackAccount::get().into_account_truncating();
let liquidity_address = T::LiquidityAccount::get().into_account_truncating();
for (currency_id, mut info) in Infos::<T>::iter() {
Expand Down Expand Up @@ -357,7 +361,7 @@ pub mod pallet {
Error::<T>::ZeroDuration
);

let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();

let info = Info {
min_swap_value,
Expand Down
2 changes: 2 additions & 0 deletions pallets/buy-back/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ impl bifrost_buy_back::Config for Runtime {
type ParachainId = ParaInfo;
type CurrencyIdRegister = AssetIdMaps<Runtime>;
type BbBNC = BbBNC;
type BlockNumberProvider = System;
}

pub struct ParaInfo;
Expand Down Expand Up @@ -490,6 +491,7 @@ impl bb_bnc::Config for Runtime {
type FarmingInfo = ();
type FourYears = MaxBlock;
type OneYear = OneYear;
type BlockNumberProvider = System;
}

pub struct ExtBuilder {
Expand Down
10 changes: 8 additions & 2 deletions pallets/buy-back/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,11 @@ fn on_initialize_no_burn_should_work() {
Some(buyback_account.clone()),
);
assert_ok!(BuyBack::charge(RuntimeOrigin::signed(ALICE), VKSM, 1000));
System::set_block_number(1);
BuyBack::on_initialize(1);
System::set_block_number(2);
BuyBack::on_initialize(2);
System::set_block_number(3);
BuyBack::on_initialize(3);
assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000);
assert_eq!(
Expand Down Expand Up @@ -301,10 +304,10 @@ fn on_initialize_with_burn_should_work() {
Some(buyback_account.clone()),
);
assert_ok!(BuyBack::charge(RuntimeOrigin::signed(ALICE), VKSM, 1000));
BuyBack::on_initialize(<frame_system::Pallet<Runtime>>::block_number() + 1);
System::set_block_number(System::block_number() + 1);
BuyBack::on_initialize(<frame_system::Pallet<Runtime>>::block_number() + 1);
BuyBack::on_initialize(0);
System::set_block_number(System::block_number() + 1);
BuyBack::on_initialize(0);
assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000);
assert_eq!(
Currencies::free_balance(VKSM, &zenlink_pair_account_id),
Expand Down Expand Up @@ -360,6 +363,7 @@ fn on_initialize_with_bias_should_work() {
Some(buyback_account.clone()),
);
assert_ok!(BuyBack::charge(RuntimeOrigin::signed(ALICE), VKSM, 1000));
System::set_block_number(1);
BuyBack::on_initialize(1);
let path = vec![
AssetId::try_convert_from(VKSM, PARAID).unwrap(),
Expand All @@ -374,7 +378,9 @@ fn on_initialize_with_bias_should_work() {
<frame_system::Pallet<Runtime>>::block_number()
+ BlockNumberFor::<Runtime>::from(1u32)
));
System::set_block_number(2);
BuyBack::on_initialize(2);
System::set_block_number(3);
BuyBack::on_initialize(3);
assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000);
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions pallets/clouds-convert/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ impl bb_bnc::Config for Runtime {
type FarmingInfo = ();
type FourYears = MaxBlock;
type OneYear = OneYear;
type BlockNumberProvider = System;
}

pub struct ExtBuilder {
Expand Down
8 changes: 5 additions & 3 deletions pallets/farming/src/boost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ impl<T: Config> Pallet<T> {
);
}

let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
boost_pool_info.start_round = current_block_number;
boost_pool_info.end_round = current_block_number.saturating_add(round_length);
boost_pool_info.total_votes = Zero::zero();
Expand Down Expand Up @@ -156,7 +157,8 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::RoundStartError { info: e });
})
.ok();
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
boost_pool_info.start_round = current_block_number;
boost_pool_info.end_round =
current_block_number.saturating_add(boost_pool_info.round_length);
Expand Down Expand Up @@ -211,7 +213,7 @@ impl<T: Config> Pallet<T> {
who: &AccountIdOf<T>,
vote_list: Vec<(PoolId, Percent)>,
) -> DispatchResult {
let current_block_number = frame_system::Pallet::<T>::block_number();
let current_block_number = T::BlockNumberProvider::current_block_number();
let mut boost_pool_info = BoostPoolInfos::<T>::get();

if let Some(user_boost_info) = UserBoostInfos::<T>::get(who) {
Expand Down
2 changes: 1 addition & 1 deletion pallets/farming/src/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ where
None => (),
Some(gid) => {
let current_block_number: BlockNumberFor<T> =
frame_system::Pallet::<T>::block_number();
T::BlockNumberProvider::current_block_number();
let gauge_pool_info =
GaugePoolInfos::<T>::get(gid).ok_or(Error::<T>::GaugePoolNotExist)?;
let gauge_info =
Expand Down
15 changes: 10 additions & 5 deletions pallets/farming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use frame_support::{
pallet_prelude::*,
sp_runtime::{
traits::{
AccountIdConversion, AtLeast32BitUnsigned, CheckedAdd, CheckedDiv, CheckedMul,
CheckedSub, Convert, Saturating, Zero,
AccountIdConversion, AtLeast32BitUnsigned, BlockNumberProvider, CheckedAdd, CheckedDiv,
CheckedMul, CheckedSub, Convert, Saturating, Zero,
},
ArithmeticError, Perbill, Percent,
},
Expand Down Expand Up @@ -126,6 +126,9 @@ pub mod pallet {

#[pallet::constant]
type GaugeRewardIssuer: Get<PalletId>;

/// The current block number provider.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
}

#[pallet::event]
Expand Down Expand Up @@ -410,7 +413,8 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
fn on_initialize(_n: BlockNumberFor<T>) -> Weight {
let n: BlockNumberFor<T> = T::BlockNumberProvider::current_block_number();
PoolInfos::<T>::iter().for_each(|(pid, mut pool_info)| match pool_info.state {
PoolState::Ongoing => {
pool_info.basic_rewards.clone().iter_mut().for_each(
Expand Down Expand Up @@ -622,7 +626,7 @@ pub mod pallet {

if let PoolState::Charged = pool_info.state {
let current_block_number: BlockNumberFor<T> =
frame_system::Pallet::<T>::block_number();
T::BlockNumberProvider::current_block_number();
ensure!(
current_block_number >= pool_info.after_block_to_start,
Error::<T>::CanNotDeposit
Expand Down Expand Up @@ -758,7 +762,8 @@ pub mod pallet {
Error::<T>::InvalidPoolState
);

let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
let share_info = SharesAndWithdrawnRewards::<T>::get(&pid, &exchanger)
.ok_or(Error::<T>::ShareInfoNotExists)?;
ensure!(
Expand Down
2 changes: 2 additions & 0 deletions pallets/farming/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ impl bifrost_farming::Config for Runtime {
type BlockNumberToBalance = ConvertInto;
type WhitelistMaximumLimit = WhitelistMaximumLimit;
type GaugeRewardIssuer = FarmingGaugeRewardIssuerPalletId;
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down Expand Up @@ -182,6 +183,7 @@ impl bb_bnc::Config for Runtime {
type FarmingInfo = Farming;
type FourYears = MaxBlock;
type OneYear = OneYear;
type BlockNumberProvider = System;
}

ord_parameter_types! {
Expand Down
Loading