Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 7d2048b

Browse files
authored
token-lending: Fix maximum withdrawal value calculation (#3138)
1 parent a1c827d commit 7d2048b

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

token-lending/program/src/processor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,9 @@ fn process_withdraw_obligation_collateral(
10021002
msg!("Obligation deposited value is zero");
10031003
return Err(LendingError::ObligationDepositsZero.into());
10041004
} else {
1005-
let max_withdraw_value = obligation.max_withdraw_value()?;
1005+
let max_withdraw_value = obligation.max_withdraw_value(Rate::from_percent(
1006+
withdraw_reserve.config.loan_to_value_ratio,
1007+
))?;
10061008
if max_withdraw_value == Decimal::zero() {
10071009
msg!("Maximum withdraw value is zero");
10081010
return Err(LendingError::WithdrawTooLarge.into());

token-lending/program/src/state/obligation.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,19 @@ impl Obligation {
9191
}
9292

9393
/// Calculate the maximum collateral value that can be withdrawn
94-
pub fn max_withdraw_value(&self) -> Result<Decimal, ProgramError> {
95-
let required_deposit_value = self
96-
.borrowed_value
97-
.try_mul(self.deposited_value)?
98-
.try_div(self.allowed_borrow_value)?;
99-
if required_deposit_value >= self.deposited_value {
94+
pub fn max_withdraw_value(
95+
&self,
96+
withdraw_collateral_ltv: Rate,
97+
) -> Result<Decimal, ProgramError> {
98+
if self.allowed_borrow_value <= self.borrowed_value {
10099
return Ok(Decimal::zero());
101100
}
102-
self.deposited_value.try_sub(required_deposit_value)
101+
if withdraw_collateral_ltv == Rate::zero() {
102+
return Ok(self.deposited_value);
103+
}
104+
self.allowed_borrow_value
105+
.try_sub(self.borrowed_value)?
106+
.try_div(withdraw_collateral_ltv)
103107
}
104108

105109
/// Calculate the maximum liquidity value that can be borrowed

0 commit comments

Comments
 (0)