Skip to content
Snippets Groups Projects
Commit 8c366fca authored by Éloïs's avatar Éloïs
Browse files

update pallet ud interface

parent 0d3d98b3
No related branches found
No related tags found
1 merge request!83feat(runtime): create UDs manually with new call universalDividend.claim_uds
This commit is part of merge request !83. Comments created here will be created in the context of that merge request.
...@@ -34,9 +34,6 @@ use sp_arithmetic::{ ...@@ -34,9 +34,6 @@ use sp_arithmetic::{
traits::{One, Saturating, Zero}, traits::{One, Saturating, Zero},
}; };
use sp_runtime::traits::StaticLookup; use sp_runtime::traits::StaticLookup;
use sp_std::prelude::*;
const OFFCHAIN_PREFIX_UD_HISTORY: &[u8] = b"ud::history::";
#[frame_support::pallet] #[frame_support::pallet]
pub mod pallet { pub mod pallet {
...@@ -69,7 +66,7 @@ pub mod pallet { ...@@ -69,7 +66,7 @@ pub mod pallet {
/// Somethings that must provide the number of accounts allowed to create the universal dividend /// Somethings that must provide the number of accounts allowed to create the universal dividend
type MembersCount: Get<BalanceOf<Self>>; type MembersCount: Get<BalanceOf<Self>>;
/// Somethings that must provide the list of accounts ids allowed to create the universal dividend /// Somethings that must provide the list of accounts ids allowed to create the universal dividend
type MembersIds: Get<Vec<<Self as frame_system::Config>::AccountId>>; type MembersStorage: frame_support::traits::StoredMap<Self::AccountId, FirstEligibleUd>;
#[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<Perbill>; type SquareMoneyGrowthRate: Get<Perbill>;
...@@ -149,11 +146,11 @@ pub mod pallet { ...@@ -149,11 +146,11 @@ pub mod pallet {
if n >= next_reeval { if n >= next_reeval {
NextReeval::<T>::put(next_reeval.saturating_add(T::UdReevalPeriod::get())); NextReeval::<T>::put(next_reeval.saturating_add(T::UdReevalPeriod::get()));
total_weight += Self::reeval_ud(current_members_count) total_weight += Self::reeval_ud(current_members_count)
+ Self::create_ud(current_members_count, n) + Self::create_ud(current_members_count)
+ T::DbWeight::get().reads_writes(2, 1); + T::DbWeight::get().reads_writes(2, 1);
} else { } else {
total_weight += total_weight +=
Self::create_ud(current_members_count, n) + T::DbWeight::get().reads(2); Self::create_ud(current_members_count) + T::DbWeight::get().reads(2);
} }
} }
total_weight total_weight
...@@ -185,16 +182,13 @@ pub mod pallet { ...@@ -185,16 +182,13 @@ pub mod pallet {
// INTERNAL FUNCTIONS // // INTERNAL FUNCTIONS //
impl<T: Config> Pallet<T> { impl<T: Config> Pallet<T> {
fn create_ud(members_count: BalanceOf<T>, n: T::BlockNumber) -> Weight { fn create_ud(members_count: BalanceOf<T>) -> Weight {
let total_weight: Weight = 0; let total_weight: Weight = 0;
let ud_amount = <CurrentUd<T>>::try_get().expect("corrupted storage"); let ud_amount = <CurrentUd<T>>::try_get().expect("corrupted storage");
let monetary_mass = <MonetaryMass<T>>::try_get().expect("corrupted storage"); let monetary_mass = <MonetaryMass<T>>::try_get().expect("corrupted storage");
for account_id in T::MembersIds::get() { // TODO inc ud index
T::Currency::deposit_creating(&account_id, ud_amount);
Self::write_ud_history(n, account_id, ud_amount);
}
let new_monetary_mass = let new_monetary_mass =
monetary_mass.saturating_add(ud_amount.saturating_mul(members_count)); monetary_mass.saturating_add(ud_amount.saturating_mul(members_count));
...@@ -267,13 +261,6 @@ pub mod pallet { ...@@ -267,13 +261,6 @@ pub mod pallet {
// UD(t+1) = UD(t) + c² (M(t+1) / N(t+1)) / (dt/udFrequency) // UD(t+1) = UD(t) + c² (M(t+1) / N(t+1)) / (dt/udFrequency)
ud_t + (c_square * monetary_mass) / (members_count * count_uds_beetween_two_reevals) ud_t + (c_square * monetary_mass) / (members_count * count_uds_beetween_two_reevals)
} }
fn write_ud_history(n: T::BlockNumber, account_id: T::AccountId, ud_amount: BalanceOf<T>) {
let mut key = Vec::with_capacity(57);
key.extend_from_slice(OFFCHAIN_PREFIX_UD_HISTORY);
account_id.encode_to(&mut key);
n.encode_to(&mut key);
sp_io::offchain_index::set(key.as_ref(), ud_amount.encode().as_ref());
}
} }
// CALLS // // CALLS //
......
...@@ -18,7 +18,7 @@ use super::*; ...@@ -18,7 +18,7 @@ use super::*;
use crate::{self as pallet_universal_dividend}; use crate::{self as pallet_universal_dividend};
use frame_support::{ use frame_support::{
parameter_types, parameter_types,
traits::{Everything, Get, OnFinalize, OnInitialize}, traits::{Everything, OnFinalize, OnInitialize},
}; };
use frame_system as system; use frame_system as system;
use sp_core::H256; use sp_core::H256;
...@@ -103,9 +103,21 @@ parameter_types! { ...@@ -103,9 +103,21 @@ parameter_types! {
} }
pub struct FakeWot; pub struct FakeWot;
impl Get<Vec<u64>> for FakeWot { impl frame_support::traits::StoredMap<u64, FirstEligibleUd> for FakeWot {
fn get() -> Vec<u64> { fn get(key: &u64) -> FirstEligibleUd {
vec![1, 2, 3] match key {
1 | 2 | 3 => FirstEligibleUd::min(),
_ => FirstEligibleUd(None),
}
}
fn try_mutate_exists<R, E: From<sp_runtime::DispatchError>>(
key: &u64,
f: impl FnOnce(&mut Option<FirstEligibleUd>) -> Result<R, E>,
) -> Result<R, E> {
match key {
1 | 2 | 3 => f(&mut Some(FirstEligibleUd::min())),
_ => f(&mut None),
}
} }
} }
...@@ -114,7 +126,7 @@ impl pallet_universal_dividend::Config for Test { ...@@ -114,7 +126,7 @@ impl pallet_universal_dividend::Config for Test {
type Currency = pallet_balances::Pallet<Test>; type Currency = pallet_balances::Pallet<Test>;
type Event = Event; type Event = Event;
type MembersCount = MembersCount; type MembersCount = MembersCount;
type MembersIds = FakeWot; type MembersStorage = FakeWot;
type SquareMoneyGrowthRate = SquareMoneyGrowthRate; type SquareMoneyGrowthRate = SquareMoneyGrowthRate;
type UdCreationPeriod = UdCreationPeriod; type UdCreationPeriod = UdCreationPeriod;
type UdReevalPeriod = UdReevalPeriod; type UdReevalPeriod = UdReevalPeriod;
......
...@@ -386,12 +386,19 @@ macro_rules! pallets_config { ...@@ -386,12 +386,19 @@ macro_rules! pallets_config {
// UNIVERSAL DIVIDEND // // UNIVERSAL DIVIDEND //
pub struct MembersCount;
impl frame_support::pallet_prelude::Get<Balance> for MembersCount {
fn get() -> Balance {
<Membership as sp_membership::traits::MembersCount>::members_count() as Balance
}
}
impl pallet_universal_dividend::Config for Runtime { impl pallet_universal_dividend::Config for Runtime {
type BlockNumberIntoBalance = sp_runtime::traits::ConvertInto; type BlockNumberIntoBalance = sp_runtime::traits::ConvertInto;
type Currency = pallet_balances::Pallet<Runtime>; type Currency = pallet_balances::Pallet<Runtime>;
type Event = Event; type Event = Event;
type MembersCount = common_runtime::providers::UdAccountsProvider<Runtime>; type MembersCount = MembersCount;
type MembersIds = common_runtime::providers::UdAccountsProvider<Runtime>; type MembersStorage = Identity;
type SquareMoneyGrowthRate = SquareMoneyGrowthRate; type SquareMoneyGrowthRate = SquareMoneyGrowthRate;
type UdCreationPeriod = UdCreationPeriod; type UdCreationPeriod = UdCreationPeriod;
type UdReevalPeriod = UdReevalPeriod; type UdReevalPeriod = UdReevalPeriod;
......
...@@ -210,7 +210,7 @@ impl ExtBuilder { ...@@ -210,7 +210,7 @@ impl ExtBuilder {
owner_key: owner_key.clone(), owner_key: owner_key.clone(),
removable_on: 0, removable_on: 0,
status: IdtyStatus::Validated, status: IdtyStatus::Validated,
data: (), data: IdtyData::min(),
}, },
}) })
.collect(), .collect(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment