Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1994 Error when stake is more than available stakable balance #2228

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions e2e/capacity/staking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,14 @@ describe('Capacity Staking Tests', function () {
const failStakeObj = ExtrinsicHelper.stake(stakingKeys, providerId, stakingAmount);
await assert.rejects(failStakeObj.signAndSend(), { name: 'BalanceTooLowtoStake' });
});

it('fails to stake when stake is >= than stakable_amount + minimum token balance', async function () {
const stakingKeys = createKeys('stakingKeys');
const providerId = await createMsaAndProvider(fundingSource, stakingKeys, 'stakingKeys', 1n * DOLLARS);
const stakingAmount = 1n * DOLLARS;

const failStakeObj = ExtrinsicHelper.stake(stakingKeys, providerId, stakingAmount);
await assert.rejects(failStakeObj.signAndSend(), { name: 'BalanceTooLowtoStake' });
});
});
});
9 changes: 6 additions & 3 deletions pallets/capacity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,12 @@ impl<T: Config> Pallet<T> {
) -> BalanceOf<T> {
let account_balance =
T::Currency::reducible_balance(&staker, Preservation::Preserve, Fortitude::Polite);
account_balance
.saturating_sub(T::MinimumTokenBalance::get())
.min(proposed_amount)
let stakable_amount = account_balance.saturating_sub(T::MinimumTokenBalance::get());
if stakable_amount >= proposed_amount {
proposed_amount
} else {
Zero::zero()
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the calling end of this function a validation is to check if returned amount > 0 and a error is thrown then

}

pub(crate) fn do_withdraw_unstaked(
Expand Down
12 changes: 8 additions & 4 deletions pallets/capacity/src/tests/stake_and_deposit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,19 @@ fn stake_an_account_can_stake_to_multiple_targets() {
}

#[test]
fn stake_when_staking_amount_is_greater_than_free_balance_it_stakes_maximum() {
fn stake_when_staking_amount_is_greater_than_free_balance_it_stakes_zero() {
new_test_ext().execute_with(|| {
let target: MessageSourceId = 1;
register_provider(target, String::from("Foo"));
let account = 200;
// An amount greater than the free balance
let amount = 230;

assert_ok!(Capacity::stake(RuntimeOrigin::signed(account), target, amount));
assert_noop!(
Capacity::stake(RuntimeOrigin::signed(account), target, amount),
Error::<Test>::BalanceTooLowtoStake
);
assert_ok!(Capacity::stake(RuntimeOrigin::signed(account), target, 189));

// Check that StakingAccountLedger is updated.
assert_eq!(StakingAccountLedger::<Test>::get(account).unwrap().active, 189);
Expand All @@ -272,10 +276,10 @@ fn stake_when_staking_amount_is_greater_than_free_balance_it_stakes_maximum() {
fn get_stakable_amount_for_works() {
new_test_ext().execute_with(|| {
let account = 200;
// An amount greater than the free balance
// An amount greater than the free balance should not be stakable
let amount = 230;
let res: u64 = Capacity::get_stakable_amount_for(&account, amount);
assert_eq!(res, 189);
assert_eq!(res, 0);
})
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/frequency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("frequency"),
impl_name: create_runtime_str!("frequency"),
authoring_version: 1,
spec_version: 133,
spec_version: 134,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand All @@ -418,7 +418,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("frequency-testnet"),
impl_name: create_runtime_str!("frequency"),
authoring_version: 1,
spec_version: 133,
spec_version: 134,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading