From e1663d6235d45beb509ef2cf78a65294e0182f7f Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Thu, 2 May 2024 15:16:16 +0530 Subject: [PATCH] stake at staking period --- pallets/positive-externality/src/lib.rs | 62 +++++++++++------------ pallets/positive-externality/src/tests.rs | 19 ------- 2 files changed, 29 insertions(+), 52 deletions(-) diff --git a/pallets/positive-externality/src/lib.rs b/pallets/positive-externality/src/lib.rs index 0f32a1e..7b6c128 100644 --- a/pallets/positive-externality/src/lib.rs +++ b/pallets/positive-externality/src/lib.rs @@ -122,10 +122,16 @@ pub mod pallet { pub type StakeBalance = StorageMap<_, Twox64Concat, T::AccountId, BalanceOf, ValueQuery>; + + #[pallet::type_value] + pub fn DefaultValidate() -> bool { + true + } + #[pallet::storage] #[pallet::getter(fn validate)] pub type Validate = - StorageMap<_, Twox64Concat, T::AccountId, bool, ValueQuery>; + StorageMap<_, Twox64Concat, T::AccountId, bool, ValueQuery, DefaultValidate>; #[pallet::storage] #[pallet::getter(fn validation_block)] @@ -192,30 +198,8 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(1)] - #[pallet::weight(0)] - pub fn add_positive_externality_stake( - origin: OriginFor, - deposit: BalanceOf, - ) -> DispatchResult { - let who = ensure_signed(origin)?; - // Check user has done kyc - let _ = ::Currency::withdraw( - &who, - deposit, - WithdrawReasons::TRANSFER, - ExistenceRequirement::AllowDeath, - )?; - let stake = StakeBalance::::get(&who); - let total_balance = stake.saturating_add(deposit); - StakeBalance::::insert(&who, total_balance); - - - // emit event - Ok(()) - } - #[pallet::call_index(2)] + #[pallet::call_index(1)] #[pallet::weight(0)] pub fn set_validate_positive_externality( origin: OriginFor, @@ -229,7 +213,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(3)] + #[pallet::call_index(2)] #[pallet::weight(0)] pub fn apply_staking_period( origin: OriginFor, @@ -238,7 +222,19 @@ pub mod pallet { let who = ensure_signed(origin)?; Self::ensure_validation_on_positive_externality(user_to_calculate.clone())?; - Self::ensure_min_stake_positive_externality(user_to_calculate.clone())?; + + let stake = MinimumStake::::get(); + + let _ = ::Currency::withdraw( + &who, + stake, + WithdrawReasons::TRANSFER, + ExistenceRequirement::AllowDeath, + )?; + + StakeBalance::::insert(&user_to_calculate, stake); + + let pe_block_number = >::get(user_to_calculate.clone()); @@ -276,7 +272,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(4)] + #[pallet::call_index(3)] #[pallet::weight(0)] pub fn apply_jurors( origin: OriginFor, @@ -303,7 +299,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(5)] + #[pallet::call_index(4)] #[pallet::weight(0)] pub fn pass_period( origin: OriginFor, @@ -326,7 +322,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(6)] + #[pallet::call_index(5)] #[pallet::weight(0)] pub fn draw_jurors( origin: OriginFor, @@ -352,7 +348,7 @@ pub mod pallet { // Unstaking // Stop drawn juror to unstake ✔️ - #[pallet::call_index(7)] + #[pallet::call_index(6)] #[pallet::weight(0)] pub fn unstaking(origin: OriginFor, user_to_calculate: T::AccountId) -> DispatchResult { let who = ensure_signed(origin)?; @@ -368,7 +364,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(8)] + #[pallet::call_index(7)] #[pallet::weight(0)] pub fn commit_vote( origin: OriginFor, @@ -388,7 +384,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(9)] + #[pallet::call_index(8)] #[pallet::weight(0)] pub fn reveal_vote( origin: OriginFor, @@ -412,7 +408,7 @@ pub mod pallet { Ok(()) } - #[pallet::call_index(10)] + #[pallet::call_index(9)] #[pallet::weight(0)] pub fn get_incentives( origin: OriginFor, diff --git a/pallets/positive-externality/src/tests.rs b/pallets/positive-externality/src/tests.rs index 93a10f8..3abbfb7 100644 --- a/pallets/positive-externality/src/tests.rs +++ b/pallets/positive-externality/src/tests.rs @@ -26,19 +26,6 @@ fn test_positive_externality_post() { }); } -#[test] -fn test_adding_positive_externality_stake() { - new_test_ext().execute_with(|| { - // assert_ok!(TemplateModule::create_positive_externality_post(Origin::signed(1), Content::None)); - // let post = TemplateModule::positive_externality_post_by_id(1); - // let post_compare = Some(PositiveExternalityPost { id: 1, created: WhoAndWhen { account: 1, block: 0, time: 0 }, edited: false, owner: 1, content: Content::None, hidden: false, upvotes_count: 0, downvotes_count: 0 }); - // assert_eq!(post, post_compare); - - assert_ok!(TemplateModule::add_positive_externality_stake(RuntimeOrigin::signed(1), 10000)); - let stake = TemplateModule::user_stake(1); - assert_eq!(stake, 10000); - }); -} #[test] fn test_setting_positive_externality_validation() { @@ -59,7 +46,6 @@ fn test_applying_for_staking_period() { RuntimeOrigin::signed(1), true )); - assert_ok!(TemplateModule::add_positive_externality_stake(RuntimeOrigin::signed(1), 10000)); System::set_block_number(1298000); assert_ok!(TemplateModule::apply_staking_period(RuntimeOrigin::signed(2), 1)); System::set_block_number(1298000 + 1298000); @@ -74,7 +60,6 @@ fn test_appying_jurors() { RuntimeOrigin::signed(1), true )); - assert_ok!(TemplateModule::add_positive_externality_stake(RuntimeOrigin::signed(1), 10000)); System::set_block_number(1298000); assert_ok!(TemplateModule::apply_staking_period(RuntimeOrigin::signed(2), 1)); assert_ok!(TemplateModule::apply_jurors(RuntimeOrigin::signed(4), 1, 1000)); @@ -88,7 +73,6 @@ fn test_change_period() { RuntimeOrigin::signed(1), true )); - assert_ok!(TemplateModule::add_positive_externality_stake(RuntimeOrigin::signed(1), 10000)); System::set_block_number(1298000); assert_ok!(TemplateModule::apply_staking_period(RuntimeOrigin::signed(2), 1)); assert_ok!(TemplateModule::apply_jurors(RuntimeOrigin::signed(4), 1, 1000)); @@ -108,7 +92,6 @@ fn test_draw_jurors_period() { RuntimeOrigin::signed(1), true )); - assert_ok!(TemplateModule::add_positive_externality_stake(RuntimeOrigin::signed(1), 10000)); System::set_block_number(1298000); assert_ok!(TemplateModule::apply_staking_period(RuntimeOrigin::signed(2), 1)); assert_ok!(TemplateModule::apply_jurors(RuntimeOrigin::signed(4), 1, 1000)); @@ -129,7 +112,6 @@ fn test_drawn_jurors() { RuntimeOrigin::signed(1), true )); - assert_ok!(TemplateModule::add_positive_externality_stake(RuntimeOrigin::signed(1), 10000)); System::set_block_number(1298000); assert_ok!(TemplateModule::apply_staking_period(RuntimeOrigin::signed(2), 1)); let balance = Balances::free_balance(4); @@ -157,7 +139,6 @@ fn test_commit_and_incentives_vote() { RuntimeOrigin::signed(1), true )); - assert_ok!(TemplateModule::add_positive_externality_stake(RuntimeOrigin::signed(1), 10000)); System::set_block_number(1298000); assert_ok!(TemplateModule::apply_staking_period(RuntimeOrigin::signed(2), 1)); let balance = Balances::free_balance(4);