From 8c366fcaca7c881776a3046b3f04af80b0e0e241 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Sat, 2 Jul 2022 19:55:29 +0200
Subject: [PATCH]  update pallet ud interface

---
 pallets/universal-dividend/src/lib.rs  | 23 +++++------------------
 pallets/universal-dividend/src/mock.rs | 22 +++++++++++++++++-----
 runtime/common/src/pallets_config.rs   | 11 +++++++++--
 runtime/gdev/tests/common/mod.rs       |  2 +-
 4 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs
index 0d19ab291..4271a9acd 100644
--- a/pallets/universal-dividend/src/lib.rs
+++ b/pallets/universal-dividend/src/lib.rs
@@ -34,9 +34,6 @@ use sp_arithmetic::{
     traits::{One, Saturating, Zero},
 };
 use sp_runtime::traits::StaticLookup;
-use sp_std::prelude::*;
-
-const OFFCHAIN_PREFIX_UD_HISTORY: &[u8] = b"ud::history::";
 
 #[frame_support::pallet]
 pub mod pallet {
@@ -69,7 +66,7 @@ pub mod pallet {
         /// Somethings that must provide the number of accounts allowed to create the universal dividend
         type MembersCount: Get<BalanceOf<Self>>;
         /// 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]
         /// Square of the money growth rate per ud reevaluation period
         type SquareMoneyGrowthRate: Get<Perbill>;
@@ -149,11 +146,11 @@ pub mod pallet {
                 if n >= next_reeval {
                     NextReeval::<T>::put(next_reeval.saturating_add(T::UdReevalPeriod::get()));
                     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);
                 } else {
                     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
@@ -185,16 +182,13 @@ pub mod pallet {
 
     // INTERNAL FUNCTIONS //
     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 ud_amount = <CurrentUd<T>>::try_get().expect("corrupted storage");
             let monetary_mass = <MonetaryMass<T>>::try_get().expect("corrupted storage");
 
-            for account_id in T::MembersIds::get() {
-                T::Currency::deposit_creating(&account_id, ud_amount);
-                Self::write_ud_history(n, account_id, ud_amount);
-            }
+            // TODO inc ud index
 
             let new_monetary_mass =
                 monetary_mass.saturating_add(ud_amount.saturating_mul(members_count));
@@ -267,13 +261,6 @@ pub mod pallet {
             // 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)
         }
-        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 //
diff --git a/pallets/universal-dividend/src/mock.rs b/pallets/universal-dividend/src/mock.rs
index 668069164..33aa44bcb 100644
--- a/pallets/universal-dividend/src/mock.rs
+++ b/pallets/universal-dividend/src/mock.rs
@@ -18,7 +18,7 @@ use super::*;
 use crate::{self as pallet_universal_dividend};
 use frame_support::{
     parameter_types,
-    traits::{Everything, Get, OnFinalize, OnInitialize},
+    traits::{Everything, OnFinalize, OnInitialize},
 };
 use frame_system as system;
 use sp_core::H256;
@@ -103,9 +103,21 @@ parameter_types! {
 }
 
 pub struct FakeWot;
-impl Get<Vec<u64>> for FakeWot {
-    fn get() -> Vec<u64> {
-        vec![1, 2, 3]
+impl frame_support::traits::StoredMap<u64, FirstEligibleUd> for FakeWot {
+    fn get(key: &u64) -> FirstEligibleUd {
+        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 {
     type Currency = pallet_balances::Pallet<Test>;
     type Event = Event;
     type MembersCount = MembersCount;
-    type MembersIds = FakeWot;
+    type MembersStorage = FakeWot;
     type SquareMoneyGrowthRate = SquareMoneyGrowthRate;
     type UdCreationPeriod = UdCreationPeriod;
     type UdReevalPeriod = UdReevalPeriod;
diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs
index fbc1cf7d9..15467dbcb 100644
--- a/runtime/common/src/pallets_config.rs
+++ b/runtime/common/src/pallets_config.rs
@@ -386,12 +386,19 @@ macro_rules! pallets_config {
 
         // 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 {
             type BlockNumberIntoBalance = sp_runtime::traits::ConvertInto;
             type Currency = pallet_balances::Pallet<Runtime>;
             type Event = Event;
-            type MembersCount = common_runtime::providers::UdAccountsProvider<Runtime>;
-            type MembersIds = common_runtime::providers::UdAccountsProvider<Runtime>;
+            type MembersCount = MembersCount;
+            type MembersStorage = Identity;
             type SquareMoneyGrowthRate = SquareMoneyGrowthRate;
             type UdCreationPeriod = UdCreationPeriod;
             type UdReevalPeriod = UdReevalPeriod;
diff --git a/runtime/gdev/tests/common/mod.rs b/runtime/gdev/tests/common/mod.rs
index 26e533c48..03b9fefac 100644
--- a/runtime/gdev/tests/common/mod.rs
+++ b/runtime/gdev/tests/common/mod.rs
@@ -210,7 +210,7 @@ impl ExtBuilder {
                         owner_key: owner_key.clone(),
                         removable_on: 0,
                         status: IdtyStatus::Validated,
-                        data: (),
+                        data: IdtyData::min(),
                     },
                 })
                 .collect(),
-- 
GitLab