diff --git a/pallets/universal-dividend/Cargo.toml b/pallets/universal-dividend/Cargo.toml index 7f74838eb66aae0a0a04a04dd7e918219c0fa595..046a9dc66e5065ab4b5bbe83d8e940293b28f21e 100644 --- a/pallets/universal-dividend/Cargo.toml +++ b/pallets/universal-dividend/Cargo.toml @@ -17,6 +17,7 @@ std = [ 'frame-system/std', 'frame-benchmarking/std', "sp-arithmetic/std", + "sp-io/std", "sp-std/std", ] try-runtime = ['frame-support/try-runtime'] @@ -48,6 +49,11 @@ default-features = false git = 'https://github.com/paritytech/substrate.git' tag = 'monthly-2021-07' +[dependencies.sp-io] +default-features = false +git = 'https://github.com/paritytech/substrate.git' +tag = 'monthly-2021-07' + [dependencies.sp-std] default-features = false git = 'https://github.com/paritytech/substrate.git' diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs index e6bf3495280a7a5795374013e9d5cd685ed7d86f..a5e2eb470edab7b91012d7f464d1e549c3f8f7f0 100644 --- a/pallets/universal-dividend/src/lib.rs +++ b/pallets/universal-dividend/src/lib.rs @@ -31,6 +31,8 @@ use frame_support::traits::Currency; use sp_arithmetic::{per_things::Permill, traits::Zero}; use sp_std::prelude::*; +const OFFCHAIN_PREFIX_UD_HISTORY: &[u8] = b"ud::history::"; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -154,9 +156,10 @@ pub mod pallet { if (n % T::UD_CREATION_PERIOD).is_zero() { let current_members_count = T::MembersCount::get(); if (n % T::UD_REEVAL_PERIOD_IN_BLOCKS).is_zero() { - Self::reeval_ud(current_members_count) + Self::create_ud(current_members_count) + Self::reeval_ud(current_members_count) + + Self::create_ud(current_members_count, n) } else { - Self::create_ud(current_members_count) + Self::create_ud(current_members_count, n) } } else { 0 @@ -182,7 +185,7 @@ pub mod pallet { // INTERNAL FUNCTIONS // impl<T: Config> Pallet<T> { - fn create_ud(current_members_count: BalanceOf<T>) -> Weight { + fn create_ud(current_members_count: BalanceOf<T>, n: T::BlockNumber) -> Weight { let total_weight: Weight = 0; let LastReeval { ud_amount, .. } = @@ -192,6 +195,7 @@ pub mod pallet { for account_id in T::MembersIds::get() { T::Currency::deposit_creating(&account_id, ud_amount); monetary_mass += ud_amount; + Self::write_ud_history(n, account_id, ud_amount); } <MonetaryMassStorage<T>>::put(monetary_mass); @@ -245,5 +249,12 @@ pub mod pallet { // UD(t+1) = UD(t) + c² (M(t) / N(t)) / (dt/udFrequency) 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()); + } } }