Broker: sale price runtime api (#3485)

Defines a runtime api for `pallet-broker` for getting the current price
of a core if there is an ongoing sale.

Closes: #3413

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Sergej Sakac
2024-04-06 01:29:35 +02:00
committed by GitHub
parent 05b97068f9
commit 1c85bfe901
8 changed files with 95 additions and 7 deletions
@@ -105,8 +105,8 @@ impl<T: Config> Pallet<T> {
) -> Result<RegionId, DispatchError> {
let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
let mut sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
ensure!(sale.first_core < status.core_count, Error::<T>::Unavailable);
ensure!(sale.cores_sold < sale.cores_offered, Error::<T>::SoldOut);
Self::ensure_cores_for_sale(&status, &sale)?;
let now = frame_system::Pallet::<T>::block_number();
ensure!(now > sale.sale_start, Error::<T>::TooEarly);
let price = Self::sale_price(&sale, now);
@@ -131,8 +131,7 @@ impl<T: Config> Pallet<T> {
let config = Configuration::<T>::get().ok_or(Error::<T>::Uninitialized)?;
let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
let mut sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
ensure!(sale.first_core < status.core_count, Error::<T>::Unavailable);
ensure!(sale.cores_sold < sale.cores_offered, Error::<T>::SoldOut);
Self::ensure_cores_for_sale(&status, &sale)?;
let renewal_id = AllowedRenewalId { core, when: sale.region_begin };
let record = AllowedRenewals::<T>::get(renewal_id).ok_or(Error::<T>::NotAllowed)?;
@@ -456,4 +455,25 @@ impl<T: Config> Pallet<T> {
Ok(())
}
pub(crate) fn ensure_cores_for_sale(
status: &StatusRecord,
sale: &SaleInfoRecordOf<T>,
) -> Result<(), DispatchError> {
ensure!(sale.first_core < status.core_count, Error::<T>::Unavailable);
ensure!(sale.cores_sold < sale.cores_offered, Error::<T>::SoldOut);
Ok(())
}
/// If there is an ongoing sale returns the current price of a core.
pub fn current_price() -> Result<BalanceOf<T>, DispatchError> {
let status = Status::<T>::get().ok_or(Error::<T>::Uninitialized)?;
let sale = SaleInfo::<T>::get().ok_or(Error::<T>::NoSales)?;
Self::ensure_cores_for_sale(&status, &sale)?;
let now = frame_system::Pallet::<T>::block_number();
Ok(Self::sale_price(&sale, now))
}
}