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

For each obligation

Coding

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