Skip to content

Commit

Permalink
guard against self liquidations
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 authored and piobab committed Jul 20, 2023
1 parent d0a8611 commit 5e47c40
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions contracts/red-bank/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub enum ContractError {
#[error("Amount to repay is greater than total debt")]
CannotRepayMoreThanDebt {},

#[error("User cannot issue liquidation of own account")]
CannotLiquidateSelf {},

#[error("User has a positive uncollateralized loan limit and thus cannot be liquidated")]
CannotLiquidateWhenPositiveUncollateralizedLoanLimit {},

Expand Down
6 changes: 6 additions & 0 deletions contracts/red-bank/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,12 @@ pub fn liquidate(
let recipient = User(&recipient_addr);

// 1. Validate liquidation

// User cannot liquidate themselves
if info.sender == user_addr {
return Err(ContractError::CannotLiquidateSelf {});
}

// If user (contract) has a positive uncollateralized limit then the user
// cannot be liquidated
if !user.uncollateralized_loan_limit(deps.storage, &debt_denom)?.is_zero() {
Expand Down
18 changes: 18 additions & 0 deletions contracts/red-bank/tests/test_liquidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,24 @@ fn liquidate_if_no_coins_sent() {
assert_eq!(error_res, PaymentError::NoFunds {}.into());
}

#[test]
fn cannot_self_liquidate() {
let TestSuite {
mut deps,
..
} = setup_test();

let env = mock_env(MockEnvParams::default());
let info = mock_info("liquidator", &[coin(100, "somecoin1")]);
let msg = ExecuteMsg::Liquidate {
user: "liquidator".to_string(),
collateral_denom: "collateral".to_string(),
recipient: None,
};
let error_res = execute(deps.as_mut(), env, info, msg).unwrap_err();
assert_eq!(error_res, ContractError::CannotLiquidateSelf {});
}

#[test]
fn liquidate_if_many_coins_sent() {
let TestSuite {
Expand Down
2 changes: 1 addition & 1 deletion schemas/mars-params/mars-params.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"contract_name": "mars-params",
"contract_version": "1.0.7",
"contract_version": "1.1.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down

0 comments on commit 5e47c40

Please sign in to comment.