From e9b62cbbe62040e14612af0ddb96e3edf614180e Mon Sep 17 00:00:00 2001 From: librelois <c@elo.tf> Date: Mon, 19 Jul 2021 22:47:39 +0200 Subject: [PATCH] mod(pallet-ud): change ud formula: apply current M/N instead of previous --- pallets/universal-dividend/src/lib.rs | 38 +++++++------------------ pallets/universal-dividend/src/tests.rs | 24 ++++++++-------- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/pallets/universal-dividend/src/lib.rs b/pallets/universal-dividend/src/lib.rs index e6e55b9bb..6652dce56 100644 --- a/pallets/universal-dividend/src/lib.rs +++ b/pallets/universal-dividend/src/lib.rs @@ -109,8 +109,8 @@ pub mod pallet { /// Last reevaluation #[pallet::storage] - #[pallet::getter(fn last_reeval)] - pub type LastReevalStorage<T: Config> = StorageValue<_, LastReeval<T>, ValueQuery>; + #[pallet::getter(fn current_ud)] + pub type CurrentUdStorage<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>; /// Total quantity of money created by universal dividend (does not take into account the possible destruction of money) #[pallet::storage] @@ -142,11 +142,7 @@ pub mod pallet { assert!(self.initial_monetary_mass >= T::Currency::total_issuance()); <StorageVersion<T>>::put(Releases::V1_0_0); - <LastReevalStorage<T>>::put(LastReeval { - monetary_mass: T::Currency::total_issuance(), - members_count: T::MembersCount::get(), - ud_amount: self.first_ud, - }); + <CurrentUdStorage<T>>::put(self.first_ud); <MonetaryMassStorage<T>>::put(self.initial_monetary_mass); } } @@ -188,11 +184,10 @@ pub mod pallet { // INTERNAL FUNCTIONS // impl<T: Config> Pallet<T> { - fn create_ud(current_members_count: BalanceOf<T>, n: T::BlockNumber) -> Weight { + fn create_ud(members_count: BalanceOf<T>, n: T::BlockNumber) -> Weight { let total_weight: Weight = 0; - let LastReeval { ud_amount, .. } = - <LastReevalStorage<T>>::try_get().expect("corrupted storage"); + let ud_amount = <CurrentUdStorage<T>>::try_get().expect("corrupted storage"); let mut monetary_mass = <MonetaryMassStorage<T>>::try_get().expect("corrupted storage"); for account_id in T::MembersIds::get() { @@ -202,22 +197,16 @@ pub mod pallet { } <MonetaryMassStorage<T>>::put(monetary_mass); - Self::deposit_event(Event::NewUdCreated(ud_amount, current_members_count)); + Self::deposit_event(Event::NewUdCreated(ud_amount, members_count)); total_weight } - fn reeval_ud(current_members_count: BalanceOf<T>) -> Weight { + fn reeval_ud(members_count: BalanceOf<T>) -> Weight { let total_weight: Weight = 0; - let LastReeval { - members_count, - mut monetary_mass, - ud_amount, - } = <LastReevalStorage<T>>::try_get().expect("corrupted storage"); + let ud_amount = <CurrentUdStorage<T>>::try_get().expect("corrupted storage"); - if monetary_mass.is_zero() { - monetary_mass = ud_amount * members_count; - } + let monetary_mass = <MonetaryMassStorage<T>>::try_get().expect("corrupted storage"); let new_ud_amount = Self::reeval_ud_formula( ud_amount, @@ -227,19 +216,14 @@ pub mod pallet { T::UdReevalPeriod::get(), ); + <CurrentUdStorage<T>>::put(new_ud_amount); + Self::deposit_event(Event::UdReevalued( new_ud_amount, monetary_mass, members_count, )); - let monetary_mass = <MonetaryMassStorage<T>>::try_get().expect("corrupted storage"); - <LastReevalStorage<T>>::put(LastReeval { - members_count: current_members_count, - monetary_mass, - ud_amount: new_ud_amount, - }); - total_weight } fn reeval_ud_formula( diff --git a/pallets/universal-dividend/src/tests.rs b/pallets/universal-dividend/src/tests.rs index b073f4707..8af0844e7 100644 --- a/pallets/universal-dividend/src/tests.rs +++ b/pallets/universal-dividend/src/tests.rs @@ -82,26 +82,26 @@ fn test_ud_creation() { // Block #8 should cause a re-evaluation of UD run_to_block(8); - assert_eq!(Balances::free_balance(1), 4_025); - assert_eq!(Balances::free_balance(2), 4_025); - assert_eq!(Balances::free_balance(3), 4_025); + assert_eq!(Balances::free_balance(1), 4_075); + assert_eq!(Balances::free_balance(2), 4_075); + assert_eq!(Balances::free_balance(3), 4_075); assert_eq!(Balances::free_balance(4), 0); - assert_eq!(UniversalDividend::total_money_created(), 12_075); + assert_eq!(UniversalDividend::total_money_created(), 12_225); // Block #10 #12 and #14should creates the reevalued UD run_to_block(14); - assert_eq!(Balances::free_balance(1), 7_100); - assert_eq!(Balances::free_balance(2), 7_100); - assert_eq!(Balances::free_balance(3), 7_100); + assert_eq!(Balances::free_balance(1), 7_300); + assert_eq!(Balances::free_balance(2), 7_300); + assert_eq!(Balances::free_balance(3), 7_300); assert_eq!(Balances::free_balance(4), 0); - assert_eq!(UniversalDividend::total_money_created(), 21_300); + assert_eq!(UniversalDividend::total_money_created(), 21_900); // Block #16 should cause a second re-evaluation of UD run_to_block(16); - assert_eq!(Balances::free_balance(1), 8_200); - assert_eq!(Balances::free_balance(2), 8_200); - assert_eq!(Balances::free_balance(3), 8_200); + assert_eq!(Balances::free_balance(1), 8_557); + assert_eq!(Balances::free_balance(2), 8_557); + assert_eq!(Balances::free_balance(3), 8_557); assert_eq!(Balances::free_balance(4), 0); - assert_eq!(UniversalDividend::total_money_created(), 24_600); + assert_eq!(UniversalDividend::total_money_created(), 25_671); }); } -- GitLab