Skip to content
Snippets Groups Projects

upgrade substrate to monthly-2022-01

Merged Éloïs requested to merge elois-upgrade-substrate into master
6 files
+ 79
14
Compare changes
  • Side-by-side
  • Inline
Files
6
@@ -27,11 +27,12 @@ mod tests;
@@ -27,11 +27,12 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
mod benchmarking;
use frame_support::traits::Currency;
use frame_support::traits::{tokens::ExistenceRequirement, Currency};
use sp_arithmetic::{
use sp_arithmetic::{
per_things::Permill,
per_things::Permill,
traits::{One, Zero},
traits::{CheckedSub, One, Saturating, Zero},
};
};
 
use sp_runtime::traits::StaticLookup;
use sp_std::prelude::*;
use sp_std::prelude::*;
const OFFCHAIN_PREFIX_UD_HISTORY: &[u8] = b"ud::history::";
const OFFCHAIN_PREFIX_UD_HISTORY: &[u8] = b"ud::history::";
@@ -48,17 +49,6 @@ pub mod pallet {
@@ -48,17 +49,6 @@ pub mod pallet {
#[pallet::config]
#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config {
#[pallet::constant]
/// Universal dividend creation period
type UdCreationPeriod: Get<Self::BlockNumber>;
#[pallet::constant]
/// Universal dividend reevaluation period (in number of creation period)
type UdReevalPeriod: Get<BalanceOf<Self>>;
#[pallet::constant]
/// Universal dividend reevaluation period in number of blocks
/// Must be equal to UdReevalPeriod * UdCreationPeriod
type UdReevalPeriodInBlocks: Get<Self::BlockNumber>;
// The currency
// The currency
type Currency: Currency<Self::AccountId>;
type Currency: Currency<Self::AccountId>;
/// Because this pallet emits events, it depends on the runtime's definition of an event.
/// Because this pallet emits events, it depends on the runtime's definition of an event.
@@ -70,6 +60,25 @@ pub mod pallet {
@@ -70,6 +60,25 @@ pub mod pallet {
#[pallet::constant]
#[pallet::constant]
/// Square of the money growth rate per ud reevaluation period
/// Square of the money growth rate per ud reevaluation period
type SquareMoneyGrowthRate: Get<Permill>;
type SquareMoneyGrowthRate: Get<Permill>;
 
#[pallet::constant]
 
/// Universal dividend creation period
 
type UdCreationPeriod: Get<Self::BlockNumber>;
 
#[pallet::constant]
 
/// Universal dividend first reevaluation (in block number)
 
/// Must be leess than UdReevalPeriodInBlocks
 
type UdFirstReeval: Get<Self::BlockNumber>;
 
#[pallet::constant]
 
/// Universal dividend reevaluation period (in number of creation period)
 
type UdReevalPeriod: Get<BalanceOf<Self>>;
 
#[pallet::constant]
 
/// Universal dividend reevaluation period in number of blocks
 
/// Must be equal to UdReevalPeriod * UdCreationPeriod
 
type UdReevalPeriodInBlocks: Get<Self::BlockNumber>;
 
#[pallet::constant]
 
/// The number of units to divide the amounts expressed in number of UDs
 
/// Example: If you wish to express the UD amounts with a maximum precision of the order
 
/// of the milliUD, choose 1000
 
type UnitsPerUd: Get<BalanceOf<Self>>;
}
}
// STORAGE //
// STORAGE //
@@ -158,7 +167,9 @@ pub mod pallet {
@@ -158,7 +167,9 @@ pub mod pallet {
fn on_initialize(n: T::BlockNumber) -> Weight {
fn on_initialize(n: T::BlockNumber) -> Weight {
if (n % T::UdCreationPeriod::get()).is_zero() {
if (n % T::UdCreationPeriod::get()).is_zero() {
let current_members_count = T::MembersCount::get();
let current_members_count = T::MembersCount::get();
if (n % T::UdReevalPeriodInBlocks::get()).is_zero() {
if (n % T::UdReevalPeriodInBlocks::get()).checked_sub(&T::UdFirstReeval::get())
 
== Some(Zero::zero())
 
{
Self::reeval_ud(current_members_count)
Self::reeval_ud(current_members_count)
+ Self::create_ud(current_members_count, n)
+ Self::create_ud(current_members_count, n)
} else {
} else {
@@ -204,6 +215,24 @@ pub mod pallet {
@@ -204,6 +215,24 @@ pub mod pallet {
total_weight
total_weight
}
}
 
fn do_transfer_ud(
 
origin: OriginFor<T>,
 
dest: <T::Lookup as StaticLookup>::Source,
 
value: BalanceOf<T>,
 
existence_requirement: ExistenceRequirement,
 
) -> DispatchResultWithPostInfo {
 
let who = ensure_signed(origin)?;
 
let dest = T::Lookup::lookup(dest)?;
 
let ud_amount = <CurrentUdStorage<T>>::try_get()
 
.map_err(|_| DispatchError::Other("corrupted storage"))?;
 
T::Currency::transfer(
 
&who,
 
&dest,
 
value.saturating_mul(ud_amount) / T::UnitsPerUd::get(),
 
existence_requirement,
 
)?;
 
Ok(().into())
 
}
fn reeval_ud(members_count: BalanceOf<T>) -> Weight {
fn reeval_ud(members_count: BalanceOf<T>) -> Weight {
let total_weight: Weight = 0;
let total_weight: Weight = 0;
@@ -252,4 +281,29 @@ pub mod pallet {
@@ -252,4 +281,29 @@ pub mod pallet {
sp_io::offchain_index::set(key.as_ref(), ud_amount.encode().as_ref());
sp_io::offchain_index::set(key.as_ref(), ud_amount.encode().as_ref());
}
}
}
}
 
 
// CALLS //
 
 
#[pallet::call]
 
impl<T: Config> Pallet<T> {
 
/// Transfer some liquid free balance to another account, in milliUD.
 
#[pallet::weight(0)]
 
pub fn transfer_ud(
 
origin: OriginFor<T>,
 
dest: <T::Lookup as StaticLookup>::Source,
 
#[pallet::compact] value: BalanceOf<T>,
 
) -> DispatchResultWithPostInfo {
 
Self::do_transfer_ud(origin, dest, value, ExistenceRequirement::AllowDeath)
 
}
 
 
/// Transfer some liquid free balance to another account, in milliUD.
 
#[pallet::weight(0)]
 
pub fn transfer_ud_keep_alive(
 
origin: OriginFor<T>,
 
dest: <T::Lookup as StaticLookup>::Source,
 
#[pallet::compact] value: BalanceOf<T>,
 
) -> DispatchResultWithPostInfo {
 
Self::do_transfer_ud(origin, dest, value, ExistenceRequirement::KeepAlive)
 
}
 
}
}
}
Loading