Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nope-finance committed Aug 10, 2021
1 parent 512c129 commit 46b6849
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
9 changes: 3 additions & 6 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,18 +1398,15 @@ fn process_borrow_obligation_liquidity(
return Err(LendingError::BorrowTooSmall.into());
}

// logically i think this should be below but is equiv because how borrow doesn't update rate
let cumulative_borrow_rate_wads = borrow_reserve.liquidity.cumulative_borrow_rate_wads;

borrow_reserve.liquidity.borrow(borrow_amount)?;
borrow_reserve.last_update.mark_stale();
Reserve::pack(borrow_reserve, &mut borrow_reserve_info.data.borrow_mut())?;

let obligation_liquidity =
obligation.find_or_add_liquidity_to_borrows(*borrow_reserve_info.key)?;
if obligation_liquidity.cumulative_borrow_rate_wads == Decimal::zero() {
obligation_liquidity.cumulative_borrow_rate_wads = cumulative_borrow_rate_wads;
}
let obligation_liquidity = obligation
.find_or_add_liquidity_to_borrows(*borrow_reserve_info.key, cumulative_borrow_rate_wads)?;

obligation_liquidity.borrow(borrow_amount)?;
obligation.last_update.mark_stale();
Obligation::pack(obligation, &mut obligation_info.data.borrow_mut())?;
Expand Down
7 changes: 4 additions & 3 deletions token-lending/program/src/state/obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl Obligation {
pub fn find_or_add_liquidity_to_borrows(
&mut self,
borrow_reserve: Pubkey,
cumulative_borrow_rate_wads: Decimal,
) -> Result<&mut ObligationLiquidity, ProgramError> {
if let Some(liquidity_index) = self._find_liquidity_index_in_borrows(borrow_reserve) {
return Ok(&mut self.borrows[liquidity_index]);
Expand All @@ -191,7 +192,7 @@ impl Obligation {
);
return Err(LendingError::ObligationReserveLimit.into());
}
let liquidity = ObligationLiquidity::new(borrow_reserve);
let liquidity = ObligationLiquidity::new(borrow_reserve, cumulative_borrow_rate_wads);
self.borrows.push(liquidity);
Ok(self.borrows.last_mut().unwrap())
}
Expand Down Expand Up @@ -279,10 +280,10 @@ pub struct ObligationLiquidity {

impl ObligationLiquidity {
/// Create new obligation liquidity
pub fn new(borrow_reserve: Pubkey) -> Self {
pub fn new(borrow_reserve: Pubkey, cumulative_borrow_rate_wads: Decimal) -> Self {
Self {
borrow_reserve,
cumulative_borrow_rate_wads: Decimal::zero(),
cumulative_borrow_rate_wads: cumulative_borrow_rate_wads,
borrowed_amount_wads: Decimal::zero(),
market_value: Decimal::zero(),
}
Expand Down
2 changes: 1 addition & 1 deletion token-lending/program/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub fn add_obligation(
.map(|(borrow_reserve, liquidity_amount)| {
let borrowed_amount_wads = Decimal::from(*liquidity_amount);

let mut liquidity = ObligationLiquidity::new(borrow_reserve.pubkey);
let mut liquidity = ObligationLiquidity::new(borrow_reserve.pubkey, Decimal::one());
liquidity.borrowed_amount_wads = borrowed_amount_wads;

(
Expand Down

0 comments on commit 46b6849

Please sign in to comment.