Skip to content

Commit

Permalink
Fix error type when user tries to deactivate more token than he owns
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszaaa committed Nov 23, 2023
1 parent 9e9594f commit 53a6194
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pallets/proof-of-stake/src/reward_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,15 @@ where
#[derive(Debug)]
pub enum RewardsCalcError {
CheckpointMathError,
NotEnoughAssets,
}

impl<T: Config> Into<Error<T>> for RewardsCalcError {
fn into(self) -> Error<T> {
Error::<T>::LiquidityCheckpointMathError
match self {
RewardsCalcError::CheckpointMathError => Error::<T>::LiquidityCheckpointMathError,
RewardsCalcError::NotEnoughAssets => Error::<T>::NotEnoughAssets,
}
}
}

Expand Down Expand Up @@ -326,7 +330,7 @@ where
.rewards_info
.activated_amount
.checked_sub(&liquidity_assets_removed)
.ok_or(RewardsCalcError::CheckpointMathError)?;
.ok_or(RewardsCalcError::NotEnoughAssets)?;

let missing_at_checkpoint_new =
T::calculate_curve_position(&self.rewards_context, &self.rewards_info)
Expand Down
40 changes: 40 additions & 0 deletions pallets/proof-of-stake/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4269,3 +4269,43 @@ fn user_can_withdraw_liquidity_from_withdrown_rewards_when_its_not_used_for_liqu
);
});
}

#[test]
#[serial]
fn test_NotEnoughAssets_is_triggered_when_user_wants_to_deactive_more_tokens_that_he_owns() {
ExtBuilder::new()
.issue(ALICE, REWARD_TOKEN, REWARD_AMOUNT)
.issue(BOB, LIQUIDITY_TOKEN, 100)
.execute_with_default_mocks(|| {
System::set_block_number(1);

ProofOfStake::reward_pool(
RuntimeOrigin::signed(ALICE),
REWARDED_PAIR,
REWARD_TOKEN,
REWARD_AMOUNT,
10u32.into(),
)
.unwrap();

roll_to_session::<Test>(1);
ProofOfStake::activate_liquidity_for_3rdparty_rewards(
RuntimeOrigin::signed(BOB),
LIQUIDITY_TOKEN,
100,
REWARD_TOKEN,
None,
)
.unwrap();

assert_err_ignore_postinfo!(
ProofOfStake::deactivate_liquidity_for_3rdparty_rewards(
RuntimeOrigin::signed(CHARLIE),
LIQUIDITY_TOKEN,
101,
REWARD_TOKEN,
),
Error::<Test>::NotEnoughAssets
);
});
}

0 comments on commit 53a6194

Please sign in to comment.