Skip to content

Commit

Permalink
stake at staking period
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed May 2, 2024
1 parent 74c468d commit e1663d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 52 deletions.
62 changes: 29 additions & 33 deletions pallets/positive-externality/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,16 @@ pub mod pallet {
pub type StakeBalance<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, BalanceOf<T>, ValueQuery>;


#[pallet::type_value]
pub fn DefaultValidate<T: Config>() -> bool {
true
}

#[pallet::storage]
#[pallet::getter(fn validate)]
pub type Validate<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, bool, ValueQuery>;
StorageMap<_, Twox64Concat, T::AccountId, bool, ValueQuery, DefaultValidate<T>>;

#[pallet::storage]
#[pallet::getter(fn validation_block)]
Expand Down Expand Up @@ -192,30 +198,8 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(0)]
pub fn add_positive_externality_stake(
origin: OriginFor<T>,
deposit: BalanceOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
// Check user has done kyc
let _ = <T as pallet::Config>::Currency::withdraw(
&who,
deposit,
WithdrawReasons::TRANSFER,
ExistenceRequirement::AllowDeath,
)?;
let stake = StakeBalance::<T>::get(&who);
let total_balance = stake.saturating_add(deposit);
StakeBalance::<T>::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<T>,
Expand All @@ -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<T>,
Expand All @@ -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::<T>::get();

let _ = <T as pallet::Config>::Currency::withdraw(
&who,
stake,
WithdrawReasons::TRANSFER,
ExistenceRequirement::AllowDeath,
)?;

StakeBalance::<T>::insert(&user_to_calculate, stake);



let pe_block_number =
<ValidationBlock<T>>::get(user_to_calculate.clone());
Expand Down Expand Up @@ -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<T>,
Expand All @@ -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<T>,
Expand All @@ -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<T>,
Expand All @@ -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<T>, user_to_calculate: T::AccountId) -> DispatchResult {
let who = ensure_signed(origin)?;
Expand All @@ -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<T>,
Expand All @@ -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<T>,
Expand All @@ -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<T>,
Expand Down
19 changes: 0 additions & 19 deletions pallets/positive-externality/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e1663d6

Please sign in to comment.