Skip to content

Commit

Permalink
fix max withdraw (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
nope-finance authored Apr 2, 2022
1 parent 8f72de8 commit 0dc1fc6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 4 additions & 1 deletion token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,10 @@ fn _withdraw_obligation_collateral<'a>(
msg!("Obligation deposited value is zero");
return Err(LendingError::ObligationDepositsZero.into());
} else {
let max_withdraw_value = obligation.max_withdraw_value()?;
let max_withdraw_value = obligation.max_withdraw_value(Rate::from_percent(
withdraw_reserve.config.loan_to_value_ratio,
))?;

if max_withdraw_value == Decimal::zero() {
msg!("Maximum withdraw value is zero");
return Err(LendingError::WithdrawTooLarge.into());
Expand Down
18 changes: 11 additions & 7 deletions token-lending/program/src/state/obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,19 @@ impl Obligation {
}

/// Calculate the maximum collateral value that can be withdrawn
pub fn max_withdraw_value(&self) -> Result<Decimal, ProgramError> {
let required_deposit_value = self
.borrowed_value
.try_mul(self.deposited_value)?
.try_div(self.allowed_borrow_value)?;
if required_deposit_value >= self.deposited_value {
pub fn max_withdraw_value(
&self,
withdraw_collateral_ltv: Rate,
) -> Result<Decimal, ProgramError> {
if self.allowed_borrow_value <= self.borrowed_value {
return Ok(Decimal::zero());
}
self.deposited_value.try_sub(required_deposit_value)
if withdraw_collateral_ltv == Rate::zero() {
return Ok(self.deposited_value);
}
self.allowed_borrow_value
.try_sub(self.borrowed_value)?
.try_div(withdraw_collateral_ltv)
}

/// Calculate the maximum liquidity value that can be borrowed
Expand Down

0 comments on commit 0dc1fc6

Please sign in to comment.