git.delta.rocks / unique-network / refs/commits / 50852e23a743

difftreelog

minimal deposit for staking increased to 100 , added impl for `payout_stakers`.

PraetorP2022-08-31parent: #e85b045.patch.diff
in: master

3 files changed

modifiedpallets/app-promotion/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/app-promotion/src/benchmarking.rs
+++ b/pallets/app-promotion/src/benchmarking.rs
@@ -36,7 +36,8 @@
 benchmarks! {
 	where_clause{
 		where T:  Config + pallet_unique::Config + pallet_evm_migration::Config ,
-		T::BlockNumber: From<u32>
+		T::BlockNumber: From<u32> + Into<u32>,
+		<<T as Config>::Currency as Currency<T::AccountId>>::Balance: Sum + From<u128>
 	}
 	start_app_promotion {
 
@@ -73,16 +74,16 @@
 		let _ = <T as Config>::Currency::make_free_balance_be(&caller,  Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value());
 		let _ = PromototionPallet::<T>::stake(RawOrigin::Signed(caller.clone()).into(), share * <T as Config>::Currency::total_balance(&caller))?;
 
-	} : {PromototionPallet::<T>::unstake(RawOrigin::Signed(caller.clone()).into(), share * <T as Config>::Currency::total_balance(&caller))?}
+	} : {PromototionPallet::<T>::unstake(RawOrigin::Signed(caller.clone()).into())?}
 
-	recalculate_stake {
-		let caller = account::<T::AccountId>("caller", 0, SEED);
-		let share = Perbill::from_rational(1u32, 10);
-		let _ = <T as Config>::Currency::make_free_balance_be(&caller,  Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value());
-		let _ = PromototionPallet::<T>::stake(RawOrigin::Signed(caller.clone()).into(), share * <T as Config>::Currency::total_balance(&caller))?;
-		let block = <T::RelayBlockNumberProvider as BlockNumberProvider>::current_block_number();
-		let mut acc = <BalanceOf<T>>::default();
-	} : {PromototionPallet::<T>::recalculate_stake(&caller, block, share * <T as Config>::Currency::total_balance(&caller), &mut acc)}
+	// recalculate_and_insert_stake{
+	// 	let caller = account::<T::AccountId>("caller", 0, SEED);
+	// 	let share = Perbill::from_rational(1u32, 10);
+	// 	let _ = <T as Config>::Currency::make_free_balance_be(&caller,  Perbill::from_rational(1u32, 2) * BalanceOf::<T>::max_value());
+	// 	let _ = PromototionPallet::<T>::stake(RawOrigin::Signed(caller.clone()).into(), share * <T as Config>::Currency::total_balance(&caller))?;
+	// 	let block = <T::RelayBlockNumberProvider as BlockNumberProvider>::current_block_number();
+	// 	let mut acc = <BalanceOf<T>>::default();
+	// } : {PromototionPallet::<T>::recalculate_and_insert_stake(&caller, block, share * <T as Config>::Currency::total_balance(&caller), &mut acc)}
 
 	sponsor_collection {
 		let pallet_admin = account::<T::AccountId>("admin", 0, SEED);
modifiedpallets/app-promotion/src/lib.rsdiffbeforeafterboth
191 pub type NextInterestBlock<T: Config> =191 pub type NextInterestBlock<T: Config> =
192 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;192 StorageValue<Value = T::BlockNumber, QueryKind = ValueQuery>;
193193
194 /// Stores the address of the staker for which the last revenue recalculation was performed.194 /// Stores hash a record for which the last revenue recalculation was performed.
195 /// If `None`, then recalculation has not yet been performed or calculations have been completed for all stakers.195 /// If `None`, then recalculation has not yet been performed or calculations have been completed for all stakers.
196 #[pallet::storage]196 #[pallet::storage]
197 #[pallet::getter(fn get_last_calculated_staker)]197 #[pallet::getter(fn get_next_calculated_record)]
198 pub type LastCalcucaltedStaker<T: Config> =198 pub type NextCalculatedRecord<T: Config> =
199 StorageValue<Value = T::AccountId, QueryKind = OptionQuery>;199 StorageValue<Value = (T::AccountId, T::BlockNumber), QueryKind = OptionQuery>;
200200
201 #[pallet::hooks]201 #[pallet::hooks]
202 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {202 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
259 #[pallet::call]259 #[pallet::call]
260 impl<T: Config> Pallet<T>260 impl<T: Config> Pallet<T>
261 where261 where
262 T::BlockNumber: From<u32>,262 T::BlockNumber: From<u32> + Into<u32>,
263 <<T as Config>::Currency as Currency<T::AccountId>>::Balance: Sum + From<u128>263 <<T as Config>::Currency as Currency<T::AccountId>>::Balance: Sum + From<u128>,
264 {264 {
265 #[pallet::weight(T::WeightInfo::set_admin_address())]265 #[pallet::weight(T::WeightInfo::set_admin_address())]
266 pub fn set_admin_address(origin: OriginFor<T>, admin: T::CrossAccountId) -> DispatchResult {266 pub fn set_admin_address(origin: OriginFor<T>, admin: T::CrossAccountId) -> DispatchResult {
375 .ok_or(ArithmeticError::Overflow)?,377 .ok_or(ArithmeticError::Overflow)?,
376 );378 );
377 379
378 TotalStaked::<T>::set(TotalStaked::<T>::get().checked_sub(&total_staked).ok_or(ArithmeticError::Underflow)?); // when error we should recover stake state for the staker380 TotalStaked::<T>::set(
381 TotalStaked::<T>::get()
382 .checked_sub(&total_staked)
383 .ok_or(ArithmeticError::Underflow)?,
384 ); // when error we should recover initial stake state for the staker
379385
380 Ok(None.into())386 Ok(None.into())
381387
512 Error::<T>::NoPermission518 Error::<T>::NoPermission
513 );519 );
514 520
515 let raw_key = Staked::<T>::hashed_key_for((admin_id, T::BlockNumber::default()));521 let current_recalc_block =
516 522 Self::get_current_recalc_block(T::RelayBlockNumberProvider::current_block_number());
523 let next_recalc_block = current_recalc_block + T::RecalculationInterval::get();
524
517 let key_iterator = Staked::<T>::iter_keys_from(raw_key).skip(1).into_iter();525 let mut storage_iterator = Self::get_next_calculated_key()
518 526 .map_or(Staked::<T>::iter().skip(0), |key| {
527 Staked::<T>::iter_from(key).skip(1)
528 });
529
530 NextCalculatedRecord::<T>::set(None);
531
532 {
533 let mut stakers_number = stakers_number.unwrap_or(20);
534 let mut current_id = admin_id;
535 let mut income_acc = BalanceOf::<T>::default();
536
537 while let Some(((id, staked_block), (amount, next_recalc_block_for_stake))) =
538 storage_iterator.next()
539 {
540 if current_id != id {
541 if income_acc != BalanceOf::<T>::default() {
542 <T::Currency as Currency<T::AccountId>>::transfer(
543 &T::TreasuryAccountId::get(),
544 &current_id,
545 income_acc,
546 ExistenceRequirement::KeepAlive,
547 )
548 .and_then(|_| Self::add_lock_balance(&current_id, income_acc))?;
549
519 match Self::get_last_calculated_staker() {550 Self::deposit_event(Event::StakingRecalculation(
551 current_id, amount, income_acc,
552 ));
553 }
554
555 stakers_number -= 1;
556 if stakers_number == 0 {
520 Some(last_staker) => {},557 NextCalculatedRecord::<T>::set(Some((id, staked_block)));
521 None => {}558 break;
522 };559 }
560 income_acc = BalanceOf::<T>::default();
561 current_id = id;
562 };
563 if next_recalc_block_for_stake >= current_recalc_block {
564 Self::recalculate_and_insert_stake(
565 &current_id,
566 staked_block,
567 next_recalc_block,
568 amount,
569 ((next_recalc_block_for_stake - current_recalc_block)
570 / T::RecalculationInterval::get())
571 .into() + 1,
572 &mut income_acc,
573 );
574 }
575 }
576 }
523577
524 Ok(())578 Ok(())
525 }579 }
609 Self::total_staked_by_id_per_block(staker.as_sub()).unwrap_or_default()663 Self::total_staked_by_id_per_block(staker.as_sub()).unwrap_or_default()
610 }664 }
611665
612 fn recalculate_stake(666 fn recalculate_and_insert_stake(
613 staker: &T::AccountId,667 staker: &T::AccountId,
614 block: T::BlockNumber,668 staked_block: T::BlockNumber,
669 next_recalc_block: T::BlockNumber,
615 base: BalanceOf<T>,670 base: BalanceOf<T>,
671 iters: u32,
616 income_acc: &mut BalanceOf<T>,672 income_acc: &mut BalanceOf<T>,
617 ) {673 ) {
618 let income = Self::calculate_income(base);674 let income = Self::calculate_income(base, iters);
619 // base.checked_add(&income).map(|res| {675
620 // <Staked<T>>::insert((staker, block), res);676 base.checked_add(&income).map(|res| {
621 // *income_acc += income;677 <Staked<T>>::insert((staker, staked_block), (res, next_recalc_block));
622 // <T::Currency as Currency<T::AccountId>>::transfer(678 *income_acc += income;
623 // &T::TreasuryAccountId::get(),679 });
624 // staker,
625 // income,
626 // ExistenceRequirement::KeepAlive,
627 // )
628 // .and_then(|_| Self::add_lock_balance(staker, income));
629 // });
630 }680 }
631681
632 fn calculate_income<I>(base: I) -> I682 fn calculate_income<I>(base: I, iters: u32) -> I
633 where683 where
634 I: EncodeLike<BalanceOf<T>> + Balance,684 I: EncodeLike<BalanceOf<T>> + Balance,
635 {685 {
686 let mut income = base;
687
636 T::IntervalIncome::get() * base688 (0..iters).for_each(|_| income += T::IntervalIncome::get() * income);
689
690 income - base
637 }691 }
692
693 fn get_current_recalc_block(current_relay_block: T::BlockNumber) -> T::BlockNumber {
694 (current_relay_block / T::RecalculationInterval::get()) * T::RecalculationInterval::get()
695 }
696
697 // fn get_next_recalc_block(current_relay_block: T::BlockNumber) -> T::BlockNumber {
698 // Self::get_current_recalc_block(current_relay_block) + T::RecalculationInterval::get()
699 // }
700
701 fn get_next_calculated_key() -> Option<Vec<u8>> {
702 Self::get_next_calculated_record().map(|key| Staked::<T>::hashed_key_for(key))
703 }
638}704}
639705
640impl<T: Config> Pallet<T>706impl<T: Config> Pallet<T>
modifiedpallets/app-promotion/src/weights.rsdiffbeforeafterboth
--- a/pallets/app-promotion/src/weights.rs
+++ b/pallets/app-promotion/src/weights.rs
@@ -3,7 +3,7 @@
 //! Autogenerated weights for pallet_app_promotion
 //!
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
-//! DATE: 2022-08-30, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! DATE: 2022-08-31, STEPS: `50`, REPEAT: 80, LOW RANGE: `[]`, HIGH RANGE: `[]`
 //! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024
 
 // Executed Command:
@@ -26,6 +26,7 @@
 #![cfg_attr(rustfmt, rustfmt_skip)]
 #![allow(unused_parens)]
 #![allow(unused_imports)]
+#![allow(missing_docs)]
 #![allow(clippy::unnecessary_cast)]
 
 use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
@@ -39,7 +40,6 @@
 	fn payout_stakers() -> Weight;
 	fn stake() -> Weight;
 	fn unstake() -> Weight;
-	fn recalculate_stake() -> Weight;
 	fn sponsor_collection() -> Weight;
 	fn stop_sponsoring_collection() -> Weight;
 	fn sponsor_contract() -> Weight;
@@ -53,71 +53,75 @@
 	// Storage: ParachainSystem ValidationData (r:1 w:0)
 	// Storage: Promotion NextInterestBlock (r:0 w:1)
 	fn start_app_promotion() -> Weight {
-		(2_299_000 as Weight)
+		(3_995_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Promotion StartBlock (r:1 w:1)
 	// Storage: Promotion NextInterestBlock (r:0 w:1)
 	fn stop_app_promotion() -> Weight {
-		(1_733_000 as Weight)
+		(3_623_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Promotion Admin (r:0 w:1)
 	fn set_admin_address() -> Weight {
-		(553_000 as Weight)
+		(1_203_000 as Weight)
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
+	// Storage: ParachainSystem ValidationData (r:1 w:0)
+	// Storage: Promotion NextCalculatedRecord (r:1 w:1)
+	// Storage: Promotion Staked (r:2 w:0)
 	fn payout_stakers() -> Weight {
-		(1_398_000 as Weight)
-			.saturating_add(T::DbWeight::get().reads(1 as Weight))
+		(10_859_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(5 as Weight))
+			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: System Account (r:1 w:1)
 	// Storage: Balances Locks (r:1 w:1)
 	// Storage: ParachainSystem ValidationData (r:1 w:0)
 	// Storage: Promotion Staked (r:1 w:1)
+	// Storage: Promotion TotalStaked (r:1 w:1)
 	fn stake() -> Weight {
-		(9_506_000 as Weight)
-			.saturating_add(T::DbWeight::get().reads(4 as Weight))
-			.saturating_add(T::DbWeight::get().writes(3 as Weight))
+		(14_789_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(5 as Weight))
+			.saturating_add(T::DbWeight::get().writes(4 as Weight))
 	}
-	// Storage: System Account (r:1 w:0)
+	// Storage: Promotion Staked (r:2 w:1)
+	// Storage: ParachainSystem ValidationData (r:1 w:0)
+	// Storage: Promotion PendingUnstake (r:1 w:1)
+	// Storage: Promotion TotalStaked (r:1 w:1)
 	fn unstake() -> Weight {
-		(2_529_000 as Weight)
-			.saturating_add(T::DbWeight::get().reads(1 as Weight))
-	}
-	// Storage: System Account (r:1 w:0)
-	fn recalculate_stake() -> Weight {
-		(2_203_000 as Weight)
-			.saturating_add(T::DbWeight::get().reads(1 as Weight))
+		(16_889_000 as Weight)
+			.saturating_add(T::DbWeight::get().reads(5 as Weight))
+			.saturating_add(T::DbWeight::get().writes(3 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
 	// Storage: Common CollectionById (r:1 w:1)
 	fn sponsor_collection() -> Weight {
-		(10_882_000 as Weight)
+		(18_377_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
 	// Storage: Common CollectionById (r:1 w:1)
 	fn stop_sponsoring_collection() -> Weight {
-		(10_544_000 as Weight)
+		(13_989_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
 	// Storage: EvmContractHelpers Sponsoring (r:0 w:1)
 	fn sponsor_contract() -> Weight {
-		(2_163_000 as Weight)
+		(4_162_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
 	// Storage: EvmContractHelpers Sponsoring (r:1 w:1)
 	fn stop_sponsoring_contract() -> Weight {
-		(3_511_000 as Weight)
+		(5_457_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
@@ -129,71 +133,75 @@
 	// Storage: ParachainSystem ValidationData (r:1 w:0)
 	// Storage: Promotion NextInterestBlock (r:0 w:1)
 	fn start_app_promotion() -> Weight {
-		(2_299_000 as Weight)
+		(3_995_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Promotion StartBlock (r:1 w:1)
 	// Storage: Promotion NextInterestBlock (r:0 w:1)
 	fn stop_app_promotion() -> Weight {
-		(1_733_000 as Weight)
+		(3_623_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Promotion Admin (r:0 w:1)
 	fn set_admin_address() -> Weight {
-		(553_000 as Weight)
+		(1_203_000 as Weight)
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
+	// Storage: ParachainSystem ValidationData (r:1 w:0)
+	// Storage: Promotion NextCalculatedRecord (r:1 w:1)
+	// Storage: Promotion Staked (r:2 w:0)
 	fn payout_stakers() -> Weight {
-		(1_398_000 as Weight)
-			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
+		(10_859_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(5 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: System Account (r:1 w:1)
 	// Storage: Balances Locks (r:1 w:1)
 	// Storage: ParachainSystem ValidationData (r:1 w:0)
 	// Storage: Promotion Staked (r:1 w:1)
+	// Storage: Promotion TotalStaked (r:1 w:1)
 	fn stake() -> Weight {
-		(9_506_000 as Weight)
-			.saturating_add(RocksDbWeight::get().reads(4 as Weight))
-			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
+		(14_789_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(5 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(4 as Weight))
 	}
-	// Storage: System Account (r:1 w:0)
+	// Storage: Promotion Staked (r:2 w:1)
+	// Storage: ParachainSystem ValidationData (r:1 w:0)
+	// Storage: Promotion PendingUnstake (r:1 w:1)
+	// Storage: Promotion TotalStaked (r:1 w:1)
 	fn unstake() -> Weight {
-		(2_529_000 as Weight)
-			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
-	}
-	// Storage: System Account (r:1 w:0)
-	fn recalculate_stake() -> Weight {
-		(2_203_000 as Weight)
-			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
+		(16_889_000 as Weight)
+			.saturating_add(RocksDbWeight::get().reads(5 as Weight))
+			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
 	// Storage: Common CollectionById (r:1 w:1)
 	fn sponsor_collection() -> Weight {
-		(10_882_000 as Weight)
+		(18_377_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
 	// Storage: Common CollectionById (r:1 w:1)
 	fn stop_sponsoring_collection() -> Weight {
-		(10_544_000 as Weight)
+		(13_989_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
 	// Storage: EvmContractHelpers Sponsoring (r:0 w:1)
 	fn sponsor_contract() -> Weight {
-		(2_163_000 as Weight)
+		(4_162_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Promotion Admin (r:1 w:0)
 	// Storage: EvmContractHelpers Sponsoring (r:1 w:1)
 	fn stop_sponsoring_contract() -> Weight {
-		(3_511_000 as Weight)
+		(5_457_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}