Skip to main content
Sign in
Snippets Groups Projects
Commit a8937e0b authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

perform type conversion

parent 1782a402
Branches
Tags
No related merge requests found
Pipeline #33565 waiting for manual action
...@@ -775,7 +775,11 @@ pub mod pallet { ...@@ -775,7 +775,11 @@ pub mod pallet {
// implement quotas // implement quotas
impl<T: Config, Balance: Zero> IdtyQuotasLinker<T::IdtyIndex, Balance> for Pallet<T> { impl<T: Config, Balance: Zero + core::convert::From<u64>> IdtyQuotasLinker<T::IdtyIndex, Balance>
for Pallet<T>
where
u64: From<Balance>,
{
fn spend_quotas( fn spend_quotas(
idty_id: T::IdtyIndex, idty_id: T::IdtyIndex,
amount: Balance, amount: Balance,
...@@ -784,9 +788,11 @@ impl<T: Config, Balance: Zero> IdtyQuotasLinker<T::IdtyIndex, Balance> for Palle ...@@ -784,9 +788,11 @@ impl<T: Config, Balance: Zero> IdtyQuotasLinker<T::IdtyIndex, Balance> for Palle
if let Some(ref mut idty_val) = idty_val_opt { if let Some(ref mut idty_val) = idty_val_opt {
if idty_val.status == IdtyStatus::Validated { if idty_val.status == IdtyStatus::Validated {
idty_val.data.update_quotas::<T>(); idty_val.data.update_quotas::<T>();
return Ok(idty_val.data.do_spend_quotas(amount)); return Ok(idty_val.data.do_spend_quotas::<Balance>(amount));
} }
// TODO error identity not eligible to refund
} }
// TODO identity not found
Ok(Balance::zero()) Ok(Balance::zero())
}) })
} }
... ...
......
...@@ -102,12 +102,17 @@ pub trait Quotas { ...@@ -102,12 +102,17 @@ pub trait Quotas {
/// update quotas /// update quotas
fn update_quotas<T: frame_system::Config>(&mut self); fn update_quotas<T: frame_system::Config>(&mut self);
/// spend quotas /// spend quotas
fn do_spend_quotas<Balance: Zero>(&mut self, amount: Balance) -> Balance; fn do_spend_quotas<Balance: Zero + core::convert::From<u64>>(
&mut self,
amount: Balance,
) -> Balance
where
u64: From<Balance>;
} }
// dummy impl // dummy impl
impl Quotas for () { impl Quotas for () {
fn update_quotas<T>(&mut self) {} fn update_quotas<T>(&mut self) {}
fn do_spend_quotas<Balance: Zero>(&mut self, amount: Balance) -> Balance { fn do_spend_quotas<Balance: Zero>(&mut self, _amount: Balance) -> Balance {
Balance::zero() Balance::zero()
} }
} }
...@@ -89,29 +89,36 @@ impl pallet_identity::traits::Quotas for IdtyData { ...@@ -89,29 +89,36 @@ impl pallet_identity::traits::Quotas for IdtyData {
use sp_runtime::traits::SaturatedConversion; use sp_runtime::traits::SaturatedConversion;
// TODO make this parameters or runtime constants? // TODO make this parameters or runtime constants?
// 14400 blocks = 24h // 14400 blocks = 24h
const RELOAD_RATE: u64 = 14400; // number of blocks in which MAX_QUOTAS is reloaded const RELOAD_RATE: crate::BlockNumber = 14400; // number of blocks in which MAX_QUOTAS is reloaded
const MAX_QUOTAS: pallet_balances::pallet::Pallet<T>::Balance = 1000.into(); // 10 Ǧ1 const MAX_QUOTAS: crate::Balance = 1000; // 10 Ǧ1
let current_block = frame_system::pallet::Pallet::<T>::block_number(); let current_block = frame_system::pallet::Pallet::<T>::block_number()
.saturated_into::<crate::BlockNumber>();
let (last_use, amount) = self.quotas; let (last_use, amount) = self.quotas;
let new_amount = amount let new_amount = amount
+ MAX_QUOTAS + sp_runtime::Perbill::from_rational(current_block - last_use, RELOAD_RATE)
* sp_runtime::Perbill::from_rational( .mul_floor(MAX_QUOTAS);
(current_block - last_use).saturated_into::<u64>(),
RELOAD_RATE,
);
let new_amount = min(new_amount, MAX_QUOTAS); let new_amount = min(new_amount, MAX_QUOTAS);
self.quotas = (current_block, new_amount); self.quotas = (current_block, new_amount);
} }
/// spend a certain amount of quotas and return what was spent /// spend a certain amount of quotas and return what was spent
fn do_spend_quotas<Balance>(&mut self, amount: Balance) -> Balance { fn do_spend_quotas<Balance: core::convert::From<crate::Balance>>(
&mut self,
amount: Balance,
) -> Balance
where
crate::Balance: From<Balance>,
{
use sp_runtime::SaturatedConversion;
let amount = amount.saturated_into::<crate::Balance>();
let old_amount = self.quotas.1; let old_amount = self.quotas.1;
if amount <= old_amount { let used = if amount <= old_amount {
self.quotas.1 -= amount; self.quotas.1 -= amount;
amount amount
} else { } else {
self.quotas.1 = 0; self.quotas.1 = 0;
amount - old_amount amount - old_amount
} };
used.into()
} }
} }
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment