Obligation Health
Shows if you have the risk to be liquidated.

For each obligation

Coding

let deposit_reserve_info = next_account_info(account_info_iter)?;
if deposit_reserve_info.owner != program_id {
msg!(
"Deposit reserve provided for collateral {} is not owned by the lending program",
index
);
return Err(LendingError::InvalidAccountOwner.into());
}
if collateral.deposit_reserve != *deposit_reserve_info.key {
msg!(
"Deposit reserve of collateral {} does not match the deposit reserve provided",
index
);
return Err(LendingError::InvalidAccountInput.into());
}
let deposit_reserve = Reserve::unpack(&deposit_reserve_info.data.borrow())?;
if deposit_reserve.last_update.is_stale(clock.slot)? {
msg!( "Deposit reserve provided for collateral {} is stale and must be refreshed in the current slot",
index);
return Err(LendingError::ReserveStale.into());}
// @TODO: add lookup table https://git.io/JOCYq
let decimals = 10u64
.checked_pow(deposit_reserve.liquidity.mint_decimals as u32)
.ok_or(LendingError::MathOverflow)?;
let market_value = deposit_reserve
.collateral_exchange_rate()?
.decimal_collateral_to_liquidity(collateral.deposited_amount.into())?
.try_mul(deposit_reserve.liquidity.market_price)?
.try_div(decimals)?;
collateral.market_value = market_value;
for (index, liquidity) in obligation.borrows.iter_mut().enumerate() {
let borrow_reserve_info = next_account_info(account_info_iter)?;
if borrow_reserve_info.owner != program_id {
msg!("Borrow reserve provided for liquidity {} is not owned by the lending program",
index
);
return Err(LendingError::InvalidAccountOwner.into());
}
if liquidity.borrow_reserve != *borrow_reserve_info.key {
msg!(
"Borrow reserve of liquidity {} does not match the borrow reserve provided",
index
);
return Err(LendingError::InvalidAccountInput.into());
}
let borrow_reserve = Reserve::unpack(&borrow_reserve_info.data.borrow())?;
if borrow_reserve.last_update.is_stale(clock.slot)? {
msg!(
"Borrow reserve provided for liquidity {} is stale and must be refreshed in the current slot",
index
);
return Err(LendingError::ReserveStale.into());
}
liquidity.accrue_interest(borrow_reserve.liquidity.cumulative_borrow_rate_wads)?;
// @TODO: add lookup table https://git.io/JOCYq
let decimals = 10u64
.checked_pow(borrow_reserve.liquidity.mint_decimals as u32)
.ok_or(LendingError::MathOverflow)?;
let market_value = liquidity
.borrowed_amount_wads
.try_mul(borrow_reserve.liquidity.market_price)?
.try_div(decimals)?;
liquidity.market_value = market_value;
borrowed_value = borrowed_value.try_add(market_value)?;
}
Last modified 7mo ago
Copy link
On this page