Skip to content

Commit f25b57b

Browse files
committed
stake sets the staking type
1 parent 98d59d3 commit f25b57b

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

pallets/capacity/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ pub mod pallet {
352352
let staker = ensure_signed(origin)?;
353353

354354
let (mut staking_account, actual_amount) =
355-
Self::ensure_can_stake(&staker, target, amount, staking_type)?;
355+
Self::ensure_can_stake(&staker, target, amount, &staking_type)?;
356+
staking_account.staking_type = staking_type;
356357

357358
let capacity = Self::increase_stake_and_issue_capacity(
358359
&staker,
@@ -462,7 +463,7 @@ impl<T: Config> Pallet<T> {
462463
staker: &T::AccountId,
463464
target: MessageSourceId,
464465
amount: BalanceOf<T>,
465-
staking_type: StakingType,
466+
staking_type: &StakingType,
466467
) -> Result<(StakingAccountDetails<T>, BalanceOf<T>), DispatchError> {
467468
ensure!(amount > Zero::zero(), Error::<T>::ZeroAmountNotAllowed);
468469
ensure!(T::TargetValidator::validate(target), Error::<T>::InvalidTarget);
@@ -472,7 +473,7 @@ impl<T: Config> Pallet<T> {
472473
// if total > 0 the account exists already. Prevent the staking type from being changed.
473474
if staking_account.total > Zero::zero() {
474475
ensure!(
475-
staking_account.staking_type == staking_type,
476+
staking_account.staking_type == *staking_type,
476477
Error::<T>::CannotChangeStakingType
477478
);
478479
}

pallets/capacity/src/tests/stake_and_deposit_tests.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use super::{mock::*, testing_utils::*};
22
use crate::{BalanceOf, CapacityDetails, Error, Event, StakingAccountDetails};
33
use common_primitives::{
4-
capacity::{Nontransferable, StakingType, StakingType::MaximumCapacity},
4+
capacity::{
5+
Nontransferable, StakingType,
6+
StakingType::{MaximumCapacity, ProviderBoost},
7+
},
58
msa::MessageSourceId,
69
};
7-
use frame_benchmarking::AnalysisChoice::Max;
810
use frame_support::{assert_noop, assert_ok, traits::WithdrawReasons};
911
use sp_runtime::ArithmeticError;
1012

1113
#[test]
12-
fn stake_works() {
14+
fn stake_max_capacity_works() {
1315
new_test_ext().execute_with(|| {
1416
let account = 200;
1517
let target: MessageSourceId = 1;
@@ -53,6 +55,35 @@ fn stake_works() {
5355
});
5456
}
5557

58+
#[test]
59+
fn stake_rewards_works() {
60+
new_test_ext().execute_with(|| {
61+
let account = 200;
62+
let target: MessageSourceId = 1;
63+
let amount = 50;
64+
let capacity = 5;
65+
register_provider(target, String::from("Foo"));
66+
assert_ok!(Capacity::stake(RuntimeOrigin::signed(account), target, amount, ProviderBoost));
67+
68+
// Check that StakingAccountLedger is updated.
69+
let staking_account: StakingAccountDetails<Test> =
70+
Capacity::get_staking_account_for(account).unwrap();
71+
72+
assert_eq!(staking_account.total, 50);
73+
assert_eq!(staking_account.active, 50);
74+
assert_eq!(staking_account.unlocking.len(), 0);
75+
assert_eq!(staking_account.staking_type, ProviderBoost);
76+
assert_eq!(staking_account.last_rewards_claimed_at, None);
77+
assert_eq!(staking_account.stake_change_unlocking.len(), 0);
78+
79+
let events = staking_events();
80+
assert_eq!(events.first().unwrap(), &Event::Staked { account, target, amount, capacity });
81+
82+
assert_eq!(Balances::locks(&account)[0].amount, amount);
83+
assert_eq!(Balances::locks(&account)[0].reasons, WithdrawReasons::all().into());
84+
});
85+
}
86+
5687
#[test]
5788
fn stake_errors_invalid_target_when_target_is_not_registered_provider() {
5889
new_test_ext().execute_with(|| {
@@ -331,7 +362,7 @@ fn ensure_can_stake_errors_with_zero_amount_not_allowed() {
331362
let target: MessageSourceId = 1;
332363
let amount = 0;
333364
assert_noop!(
334-
Capacity::ensure_can_stake(&account, target, amount, MaximumCapacity),
365+
Capacity::ensure_can_stake(&account, target, amount, &MaximumCapacity),
335366
Error::<Test>::ZeroAmountNotAllowed
336367
);
337368
});
@@ -369,7 +400,7 @@ fn ensure_can_stake_errors_invalid_target() {
369400
let amount = 1;
370401

371402
assert_noop!(
372-
Capacity::ensure_can_stake(&account, target, amount, MaximumCapacity),
403+
Capacity::ensure_can_stake(&account, target, amount, &MaximumCapacity),
373404
Error::<Test>::InvalidTarget
374405
);
375406
});
@@ -384,7 +415,7 @@ fn ensure_can_stake_errors_insufficient_staking_amount() {
384415
register_provider(target, String::from("Foo"));
385416

386417
assert_noop!(
387-
Capacity::ensure_can_stake(&account, target, amount, MaximumCapacity),
418+
Capacity::ensure_can_stake(&account, target, amount, &MaximumCapacity),
388419
Error::<Test>::InsufficientStakingAmount
389420
);
390421
});
@@ -400,7 +431,7 @@ fn ensure_can_stake_is_successful() {
400431

401432
let staking_details = StakingAccountDetails::<Test>::default();
402433
assert_ok!(
403-
Capacity::ensure_can_stake(&account, target, amount, MaximumCapacity),
434+
Capacity::ensure_can_stake(&account, target, amount, &MaximumCapacity),
404435
(staking_details, BalanceOf::<Test>::from(10u64))
405436
);
406437
});

0 commit comments

Comments
 (0)