Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ pub fn handle_collect_protocol_fees(context: Context<CollectProtocolFees>) -> Re
let reserve = &mut context.accounts.reserve;
// Fees are a claim on liquidity; only what is currently un-borrowed can be paid
// out right now. Any remainder stays owed until borrowers repay.
let amount = reserve.accumulated_protocol_fees.min(reserve.available_liquidity);
let amount = reserve
.accumulated_protocol_fees
.min(reserve.available_liquidity);
require!(amount > 0, LendingError::NothingToCollect);

reserve.accumulated_protocol_fees = reserve
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::constants::{
};
use crate::state::{LendingMarket, PriceFeed, Reserve, ReserveConfig};

pub fn handle_initialize_reserve(context: Context<InitializeReserve>, config: ReserveConfig) -> Result<()> {
pub fn handle_initialize_reserve(
context: Context<InitializeReserve>,
config: ReserveConfig,
) -> Result<()> {
config.validate()?;

let reserve = &mut context.accounts.reserve;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ pub fn handle_liquidate_obligation(
repay_reserve.config.close_factor_bps as u128,
BPS_DENOMINATOR,
)?;
let repay = liquidity_amount.min(u64::try_from(max_repay).map_err(|_| LendingError::MathOverflow)?);
let repay =
liquidity_amount.min(u64::try_from(max_repay).map_err(|_| LendingError::MathOverflow)?);
require!(repay > 0, LendingError::ZeroAmount);

// Collateral to seize: value of the repayment plus the bonus, converted into
Expand Down Expand Up @@ -99,8 +100,8 @@ pub fn handle_liquidate_obligation(
LendingError::LiquidationTooLarge
);

let scaled_removed =
mul_div_floor(repay as u128, FIXED_POINT_SCALE, accumulation_factor)?.min(borrowed_principal);
let scaled_removed = mul_div_floor(repay as u128, FIXED_POINT_SCALE, accumulation_factor)?
.min(borrowed_principal);

// Effects: repay side.
{
Expand Down Expand Up @@ -150,14 +151,25 @@ pub fn handle_liquidate_obligation(
)?;

let bump = [obligation_bump];
let seeds: [&[u8]; 4] = [OBLIGATION_SEED, lending_market.as_ref(), owner.as_ref(), &bump];
let seeds: [&[u8]; 4] = [
OBLIGATION_SEED,
lending_market.as_ref(),
owner.as_ref(),
&bump,
];
transfer_checked(
CpiContext::new_with_signer(
context.accounts.token_program.key(),
TransferChecked {
from: context.accounts.obligation_collateral_vault.to_account_info(),
from: context
.accounts
.obligation_collateral_vault
.to_account_info(),
mint: context.accounts.collateral_share_mint.to_account_info(),
to: context.accounts.liquidator_collateral_dest.to_account_info(),
to: context
.accounts
.liquidator_collateral_dest
.to_account_info(),
authority: context.accounts.obligation.to_account_info(),
},
&[&seeds],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ pub fn handle_redeem_reserve_collateral(
let reserve = &mut context.accounts.reserve;
reserve.require_refreshed()?;

require!(reserve.share_mint_supply > 0, LendingError::InsufficientReserveLiquidity);
require!(
reserve.share_mint_supply > 0,
LendingError::InsufficientReserveLiquidity
);
let liquidity_amount = mul_div_floor(
share_amount as u128,
reserve.total_liquidity()?,
reserve.total_shares()?,
)?;
let liquidity_amount = u64::try_from(liquidity_amount).map_err(|_| LendingError::MathOverflow)?;
let liquidity_amount =
u64::try_from(liquidity_amount).map_err(|_| LendingError::MathOverflow)?;
require!(
liquidity_amount <= reserve.available_liquidity,
LendingError::InsufficientReserveLiquidity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,26 @@ pub fn handle_refresh_obligation(context: Context<RefreshObligation>) -> Result<
let mut unhealthy_borrow_value: u128 = 0;

for collateral in obligation.deposits.iter_mut() {
let (reserve, price_scaled) =
read_pair(accounts, &mut cursor, collateral.reserve, lending_market, slot)?;
let (reserve, price_scaled) = read_pair(
accounts,
&mut cursor,
collateral.reserve,
lending_market,
slot,
)?;

let liquidity = mul_div_floor(
collateral.deposited_shares as u128,
reserve.total_liquidity()?,
reserve.total_shares()?,
)?;
let liquidity = u64::try_from(liquidity).map_err(|_| LendingError::MathOverflow)?;
let value = market_value(liquidity, reserve.liquidity_decimals, price_scaled, Rounding::Down)?;
let value = market_value(
liquidity,
reserve.liquidity_decimals,
price_scaled,
Rounding::Down,
)?;

collateral.market_value = value;
deposited_value = deposited_value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ pub fn handle_repay_obligation_liquidity(
let repay = liquidity_amount.min(debt_now);
require!(repay > 0, LendingError::ZeroAmount);

let scaled_removed = mul_div_floor(repay as u128, FIXED_POINT_SCALE, index)?.min(borrowed_principal);
let scaled_removed =
mul_div_floor(repay as u128, FIXED_POINT_SCALE, index)?.min(borrowed_principal);

{
let reserve = &mut context.accounts.reserve;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ pub fn handle_withdraw_obligation_collateral(
reserve.total_liquidity()?,
reserve.total_shares()?,
)?;
let removed_liquidity = u64::try_from(removed_liquidity).map_err(|_| LendingError::MathOverflow)?;
let removed_liquidity =
u64::try_from(removed_liquidity).map_err(|_| LendingError::MathOverflow)?;
let removed_value = market_value(
removed_liquidity,
reserve.liquidity_decimals,
Expand Down
7 changes: 5 additions & 2 deletions finance/lending/anchor-v1/programs/lending/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use anchor_lang::prelude::*;

pub mod constants;
pub mod errors;
pub mod math;
pub mod instructions;
pub mod math;
pub mod state;

use instructions::*;
Expand All @@ -22,7 +22,10 @@ pub mod lending {
instructions::handle_initialize_lending_market(context, market_id)
}

pub fn initialize_reserve(context: Context<InitializeReserve>, config: ReserveConfig) -> Result<()> {
pub fn initialize_reserve(
context: Context<InitializeReserve>,
config: ReserveConfig,
) -> Result<()> {
instructions::handle_initialize_reserve(context, config)
}

Expand Down
12 changes: 10 additions & 2 deletions finance/lending/anchor-v1/programs/lending/src/state/obligation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ impl Obligation {
/// Index of the collateral entry for `reserve`, creating an empty one if the
/// obligation has room. Used when posting collateral.
pub fn upsert_collateral(&mut self, reserve: Pubkey) -> Result<usize> {
if let Some(index) = self.deposits.iter().position(|entry| entry.reserve == reserve) {
if let Some(index) = self
.deposits
.iter()
.position(|entry| entry.reserve == reserve)
{
return Ok(index);
}
require!(
Expand All @@ -93,7 +97,11 @@ impl Obligation {
/// Index of the borrow entry for `reserve`, creating an empty one if the
/// obligation has room. Used when borrowing.
pub fn upsert_borrow(&mut self, reserve: Pubkey) -> Result<usize> {
if let Some(index) = self.borrows.iter().position(|entry| entry.reserve == reserve) {
if let Some(index) = self
.borrows
.iter()
.position(|entry| entry.reserve == reserve)
{
return Ok(index);
}
require!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ impl PriceFeed {
let age = current_slot
.checked_sub(self.last_updated_slot)
.ok_or(LendingError::MathOverflow)?;
require!(age <= MAX_PRICE_STALENESS_SLOTS, LendingError::StalePriceFeed);
require!(
age <= MAX_PRICE_STALENESS_SLOTS,
LendingError::StalePriceFeed
);

// Restart handling. A cluster halt stops the slot count but not the
// wall clock, so after a restart a feed can look fresh in slots while
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ impl Reserve {
if gross == 0 {
return Ok(0);
}
mul_div_floor(self.current_borrowed_amount()? as u128, BPS_DENOMINATOR, gross)
mul_div_floor(
self.current_borrowed_amount()? as u128,
BPS_DENOMINATOR,
gross,
)
}

/// Per-second borrow rate (FIXED_POINT_SCALE-scaled) from the kinked curve:
Expand Down
Loading
Loading