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

mod(pallet-ud): change ud formula: apply current M/N instead of previous

parent ecf2bb16
No related branches found
No related tags found
No related merge requests found
...@@ -109,8 +109,8 @@ pub mod pallet { ...@@ -109,8 +109,8 @@ pub mod pallet {
/// Last reevaluation /// Last reevaluation
#[pallet::storage] #[pallet::storage]
#[pallet::getter(fn last_reeval)] #[pallet::getter(fn current_ud)]
pub type LastReevalStorage<T: Config> = StorageValue<_, LastReeval<T>, ValueQuery>; 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) /// Total quantity of money created by universal dividend (does not take into account the possible destruction of money)
#[pallet::storage] #[pallet::storage]
...@@ -142,11 +142,7 @@ pub mod pallet { ...@@ -142,11 +142,7 @@ pub mod pallet {
assert!(self.initial_monetary_mass >= T::Currency::total_issuance()); assert!(self.initial_monetary_mass >= T::Currency::total_issuance());
<StorageVersion<T>>::put(Releases::V1_0_0); <StorageVersion<T>>::put(Releases::V1_0_0);
<LastReevalStorage<T>>::put(LastReeval { <CurrentUdStorage<T>>::put(self.first_ud);
monetary_mass: T::Currency::total_issuance(),
members_count: T::MembersCount::get(),
ud_amount: self.first_ud,
});
<MonetaryMassStorage<T>>::put(self.initial_monetary_mass); <MonetaryMassStorage<T>>::put(self.initial_monetary_mass);
} }
} }
...@@ -188,11 +184,10 @@ pub mod pallet { ...@@ -188,11 +184,10 @@ pub mod pallet {
// INTERNAL FUNCTIONS // // INTERNAL FUNCTIONS //
impl<T: Config> Pallet<T> { 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 total_weight: Weight = 0;
let LastReeval { ud_amount, .. } = let ud_amount = <CurrentUdStorage<T>>::try_get().expect("corrupted storage");
<LastReevalStorage<T>>::try_get().expect("corrupted storage");
let mut monetary_mass = <MonetaryMassStorage<T>>::try_get().expect("corrupted storage"); let mut monetary_mass = <MonetaryMassStorage<T>>::try_get().expect("corrupted storage");
for account_id in T::MembersIds::get() { for account_id in T::MembersIds::get() {
...@@ -202,22 +197,16 @@ pub mod pallet { ...@@ -202,22 +197,16 @@ pub mod pallet {
} }
<MonetaryMassStorage<T>>::put(monetary_mass); <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 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 total_weight: Weight = 0;
let LastReeval { let ud_amount = <CurrentUdStorage<T>>::try_get().expect("corrupted storage");
members_count,
mut monetary_mass,
ud_amount,
} = <LastReevalStorage<T>>::try_get().expect("corrupted storage");
if monetary_mass.is_zero() { let monetary_mass = <MonetaryMassStorage<T>>::try_get().expect("corrupted storage");
monetary_mass = ud_amount * members_count;
}
let new_ud_amount = Self::reeval_ud_formula( let new_ud_amount = Self::reeval_ud_formula(
ud_amount, ud_amount,
...@@ -227,19 +216,14 @@ pub mod pallet { ...@@ -227,19 +216,14 @@ pub mod pallet {
T::UdReevalPeriod::get(), T::UdReevalPeriod::get(),
); );
<CurrentUdStorage<T>>::put(new_ud_amount);
Self::deposit_event(Event::UdReevalued( Self::deposit_event(Event::UdReevalued(
new_ud_amount, new_ud_amount,
monetary_mass, monetary_mass,
members_count, 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 total_weight
} }
fn reeval_ud_formula( fn reeval_ud_formula(
......
...@@ -82,26 +82,26 @@ fn test_ud_creation() { ...@@ -82,26 +82,26 @@ fn test_ud_creation() {
// Block #8 should cause a re-evaluation of UD // Block #8 should cause a re-evaluation of UD
run_to_block(8); run_to_block(8);
assert_eq!(Balances::free_balance(1), 4_025); assert_eq!(Balances::free_balance(1), 4_075);
assert_eq!(Balances::free_balance(2), 4_025); assert_eq!(Balances::free_balance(2), 4_075);
assert_eq!(Balances::free_balance(3), 4_025); assert_eq!(Balances::free_balance(3), 4_075);
assert_eq!(Balances::free_balance(4), 0); 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 // Block #10 #12 and #14should creates the reevalued UD
run_to_block(14); run_to_block(14);
assert_eq!(Balances::free_balance(1), 7_100); assert_eq!(Balances::free_balance(1), 7_300);
assert_eq!(Balances::free_balance(2), 7_100); assert_eq!(Balances::free_balance(2), 7_300);
assert_eq!(Balances::free_balance(3), 7_100); assert_eq!(Balances::free_balance(3), 7_300);
assert_eq!(Balances::free_balance(4), 0); 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 // Block #16 should cause a second re-evaluation of UD
run_to_block(16); run_to_block(16);
assert_eq!(Balances::free_balance(1), 8_200); assert_eq!(Balances::free_balance(1), 8_557);
assert_eq!(Balances::free_balance(2), 8_200); assert_eq!(Balances::free_balance(2), 8_557);
assert_eq!(Balances::free_balance(3), 8_200); assert_eq!(Balances::free_balance(3), 8_557);
assert_eq!(Balances::free_balance(4), 0); assert_eq!(Balances::free_balance(4), 0);
assert_eq!(UniversalDividend::total_money_created(), 24_600); assert_eq!(UniversalDividend::total_money_created(), 25_671);
}); });
} }
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