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

undo borrow limit and close factor change and cap liquidation amount #92

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add new liquidation value cap
  • Loading branch information
0xripleys committed Jun 27, 2022
commit 41ceaba1d63cec2d4df856e70ff6379e6ff82b36
79 changes: 78 additions & 1 deletion token-lending/program/src/state/obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ impl Obligation {
let max_liquidation_value = self
.borrowed_value
.try_mul(Rate::from_percent(LIQUIDATION_CLOSE_FACTOR))?
.min(liquidity.market_value);
.min(liquidity.market_value)
.min(Decimal::from(MAX_LIQUIDATABLE_VALUE_AT_ONCE));

let max_liquidation_pct = max_liquidation_value.try_div(liquidity.market_value)?;
liquidity.borrowed_amount_wads.try_mul(max_liquidation_pct)
}
Expand Down Expand Up @@ -661,4 +663,79 @@ mod test {
}
}
}

#[test]
fn max_liquidation_amount_normal() {
let obligation_liquidity = ObligationLiquidity {
borrowed_amount_wads: Decimal::from(50u64),
market_value: Decimal::from(100u64),
..ObligationLiquidity::default()
};

let obligation = Obligation {
deposited_value: Decimal::from(100u64),
borrowed_value: Decimal::from(100u64),
borrows: vec![obligation_liquidity.clone()],
..Obligation::default()
};

let expected_collateral = Decimal::from(50u64)
.try_mul(Decimal::from(LIQUIDATION_CLOSE_FACTOR as u64))
.unwrap()
.try_div(100)
.unwrap();

assert_eq!(
obligation
.max_liquidation_amount(&obligation_liquidity)
.unwrap(),
expected_collateral
);
}

#[test]
fn max_liquidation_amount_low_liquidity() {
let obligation_liquidity = ObligationLiquidity {
borrowed_amount_wads: Decimal::from(100u64),
market_value: Decimal::from(1u64),
..ObligationLiquidity::default()
};

let obligation = Obligation {
deposited_value: Decimal::from(100u64),
borrowed_value: Decimal::from(100u64),
borrows: vec![obligation_liquidity.clone()],
..Obligation::default()
};

assert_eq!(
obligation
.max_liquidation_amount(&obligation_liquidity)
.unwrap(),
Decimal::from(100u64)
);
}

#[test]
fn max_liquidation_amount_big_whale() {
let obligation_liquidity = ObligationLiquidity {
borrowed_amount_wads: Decimal::from(1_000_000_000u64),
market_value: Decimal::from(1_000_000_000u64),
..ObligationLiquidity::default()
};

let obligation = Obligation {
deposited_value: Decimal::from(1_000_000_000u64),
borrowed_value: Decimal::from(1_000_000_000u64),
borrows: vec![obligation_liquidity.clone()],
..Obligation::default()
};

assert_eq!(
obligation
.max_liquidation_amount(&obligation_liquidity)
.unwrap(),
Decimal::from(MAX_LIQUIDATABLE_VALUE_AT_ONCE)
);
}
}
3 changes: 3 additions & 0 deletions token-lending/program/src/state/reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub const LIQUIDATION_CLOSE_FACTOR: u8 = 1;
/// Obligation borrow amount that is small enough to close out
pub const LIQUIDATION_CLOSE_AMOUNT: u64 = 2;

/// Maximum quote currency value that can be liquidated in 1 liquidate_obligation call
pub const MAX_LIQUIDATABLE_VALUE_AT_ONCE: u64 = 500_000;

/// Lending market reserve state
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Reserve {
Expand Down