Skip to content

Commit

Permalink
fix for withdraw case (solana-labs#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xripleys authored Apr 11, 2023
1 parent 153bc00 commit f1f05d6
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions token-lending/sdk/src/state/obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,16 @@ impl Obligation {
collateral: &ObligationCollateral,
withdraw_reserve: &Reserve,
) -> Result<u64, ProgramError> {
if self.borrows.is_empty() {
return Ok(collateral.deposited_amount);
}

if self.allowed_borrow_value <= self.borrowed_value_upper_bound {
return Ok(0);
}

let loan_to_value_ratio = withdraw_reserve.loan_to_value_ratio();

if self.borrows.is_empty() || loan_to_value_ratio == Rate::zero() {
if loan_to_value_ratio == Rate::zero() {
return Ok(collateral.deposited_amount);
}

Expand Down Expand Up @@ -815,12 +818,22 @@ mod test {
deposited_amount: 20 * LAMPORTS_PER_SOL,
..ObligationCollateral::default()
}],
borrows: vec![ObligationLiquidity {
borrowed_amount_wads: Decimal::from(10u64),
..ObligationLiquidity::default()
}],
deposited_value: Decimal::from(100u64),
borrowed_value_upper_bound: Decimal::from(50u64),
allowed_borrow_value: Decimal::from(50u64),
..Obligation::default()
},
reserve: Reserve::default(),
reserve: Reserve {
config: ReserveConfig {
loan_to_value_ratio: 50,
..ReserveConfig::default()
},
..Reserve::default()
},
expected_max_withdraw_amount: 0,
}),
// regular case
Expand Down Expand Up @@ -930,7 +943,7 @@ mod test {
},
expected_max_withdraw_amount: 100 * LAMPORTS_PER_SOL,
}),
// ltv is 0 so we can withdraw everything
// ltv is 0 and the obligation is healthy so we can withdraw everything
Just(MaxWithdrawAmountTestCase {
obligation: Obligation {
deposits: vec![ObligationCollateral {
Expand All @@ -943,12 +956,33 @@ mod test {
}],

allowed_borrow_value: Decimal::from(100u64),
borrowed_value_upper_bound: Decimal::from(50u64),
..Obligation::default()
},

reserve: Reserve::default(),
expected_max_withdraw_amount: 100 * LAMPORTS_PER_SOL,
}),
// ltv is 0 but the obligation is unhealthy so we can't withdraw anything
Just(MaxWithdrawAmountTestCase {
obligation: Obligation {
deposits: vec![ObligationCollateral {
deposited_amount: 100 * LAMPORTS_PER_SOL,
..ObligationCollateral::default()
}],
borrows: vec![ObligationLiquidity {
borrowed_amount_wads: Decimal::from(10u64),
..ObligationLiquidity::default()
}],

allowed_borrow_value: Decimal::from(100u64),
borrowed_value_upper_bound: Decimal::from(100u64),
..Obligation::default()
},

reserve: Reserve::default(),
expected_max_withdraw_amount: 0,
}),
]
}

Expand Down

0 comments on commit f1f05d6

Please sign in to comment.