Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0xripleys outflow limits #125

Merged
Merged
Show file tree
Hide file tree
Changes from 14 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
5 changes: 5 additions & 0 deletions token-lending/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use lending_state::SolendState;
use solana_client::rpc_config::RpcSendTransactionConfig;
use solana_sdk::{commitment_config::CommitmentLevel, compute_budget::ComputeBudgetInstruction};
use solend_program::state::{RateLimiterConfig, SLOTS_PER_YEAR};
use solend_sdk::{
instruction::{
liquidate_obligation_and_redeem_reserve_collateral, redeem_reserve_collateral,
Expand Down Expand Up @@ -1675,6 +1676,10 @@ fn command_update_reserve(
&[update_reserve_config(
config.lending_program_id,
reserve.config,
RateLimiterConfig {
window_duration: SLOTS_PER_YEAR / 365,
max_outflow: u64::MAX,
},
reserve_pubkey,
lending_market_pubkey,
lending_market_owner_keypair.pubkey(),
Expand Down
98 changes: 90 additions & 8 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use solana_program::{
Sysvar,
},
};
use solend_sdk::state::{RateLimiter, RateLimiterConfig};
use solend_sdk::{switchboard_v2_devnet, switchboard_v2_mainnet};
use spl_token::state::Mint;
use std::{cmp::min, result::Result};
Expand All @@ -53,9 +54,9 @@ pub fn process_instruction(
msg!("Instruction: Init Lending Market");
process_init_lending_market(program_id, owner, quote_currency, accounts)
}
LendingInstruction::SetLendingMarketOwner { new_owner } => {
LendingInstruction::SetLendingMarketOwnerAndConfig { new_owner, config } => {
msg!("Instruction: Set Lending Market Owner");
process_set_lending_market_owner(program_id, new_owner, accounts)
process_set_lending_market_owner_and_config(program_id, new_owner, config, accounts)
}
LendingInstruction::InitReserve {
liquidity_amount,
Expand Down Expand Up @@ -128,9 +129,12 @@ pub fn process_instruction(
accounts,
)
}
LendingInstruction::UpdateReserveConfig { config } => {
LendingInstruction::UpdateReserveConfig {
config,
rate_limiter_config,
} => {
msg!("Instruction: UpdateReserveConfig");
process_update_reserve_config(program_id, config, accounts)
process_update_reserve_config(program_id, config, rate_limiter_config, accounts)
}
LendingInstruction::LiquidateObligationAndRedeemReserveCollateral { liquidity_amount } => {
msg!("Instruction: Liquidate Obligation and Redeem Reserve Collateral");
Expand Down Expand Up @@ -190,16 +194,18 @@ fn process_init_lending_market(
token_program_id: *token_program_id.key,
oracle_program_id: *oracle_program_id.key,
switchboard_oracle_program_id: *switchboard_oracle_program_id.key,
current_slot: Clock::get()?.slot,
});
LendingMarket::pack(lending_market, &mut lending_market_info.data.borrow_mut())?;

Ok(())
}

#[inline(never)] // avoid stack frame limit
fn process_set_lending_market_owner(
fn process_set_lending_market_owner_and_config(
program_id: &Pubkey,
new_owner: Pubkey,
rate_limiter_config: RateLimiterConfig,
accounts: &[AccountInfo],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
Expand All @@ -221,6 +227,11 @@ fn process_set_lending_market_owner(
}

lending_market.owner = new_owner;

if rate_limiter_config != lending_market.rate_limiter.config {
lending_market.rate_limiter = RateLimiter::new(rate_limiter_config, Clock::get()?.slot);
}

LendingMarket::pack(lending_market, &mut lending_market_info.data.borrow_mut())?;

Ok(())
Expand Down Expand Up @@ -347,6 +358,7 @@ fn process_init_reserve(
supply_pubkey: *reserve_collateral_supply_info.key,
}),
config,
rate_limiter_config: RateLimiterConfig::default(),
});

let collateral_amount = reserve.deposit_liquidity(liquidity_amount)?;
Expand Down Expand Up @@ -659,7 +671,6 @@ fn process_redeem_reserve_collateral(
}
let token_program_id = next_account_info(account_info_iter)?;

_refresh_reserve_interest(program_id, reserve_info, clock)?;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the lending market rate limiter is denominated in usd, we need an up-to-date price so i removed this function call

_redeem_reserve_collateral(
program_id,
collateral_amount,
Expand All @@ -673,6 +684,7 @@ fn process_redeem_reserve_collateral(
user_transfer_authority_info,
clock,
token_program_id,
true,
)?;
let mut reserve = Reserve::unpack(&reserve_info.data.borrow())?;
reserve.last_update.mark_stale();
Expand All @@ -695,8 +707,9 @@ fn _redeem_reserve_collateral<'a>(
user_transfer_authority_info: &AccountInfo<'a>,
clock: &Clock,
token_program_id: &AccountInfo<'a>,
check_rate_limits: bool,
) -> Result<u64, ProgramError> {
let lending_market = LendingMarket::unpack(&lending_market_info.data.borrow())?;
let mut lending_market = LendingMarket::unpack(&lending_market_info.data.borrow())?;
if lending_market_info.owner != program_id {
msg!("Lending market provided is not owned by the lending program");
return Err(LendingError::InvalidAccountOwner.into());
Expand Down Expand Up @@ -750,8 +763,38 @@ fn _redeem_reserve_collateral<'a>(
}

let liquidity_amount = reserve.redeem_collateral(collateral_amount)?;

if check_rate_limits {
let redemption_value = reserve
.liquidity
.market_price
.try_mul(Decimal::from(liquidity_amount))?
.try_div(Decimal::from(
(10u128)
.checked_pow(reserve.liquidity.mint_decimals as u32)
.ok_or(LendingError::MathOverflow)?,
))?;

lending_market
.rate_limiter
.update(clock.slot, redemption_value)
.map_err(|err| {
msg!("Market outflow limit exceeded! Please try again later.");
err
})?;

reserve
.rate_limiter
.update(clock.slot, Decimal::from(liquidity_amount))
.map_err(|err| {
msg!("Reserve outflow limit exceeded! Please try again later.");
err
})?;
}

reserve.last_update.mark_stale();
Reserve::pack(reserve, &mut reserve_info.data.borrow_mut())?;
LendingMarket::pack(lending_market, &mut lending_market_info.data.borrow_mut())?;

spl_token_burn(TokenBurnParams {
mint: reserve_collateral_mint_info.clone(),
Expand Down Expand Up @@ -1359,7 +1402,7 @@ fn process_borrow_obligation_liquidity(
}
let token_program_id = next_account_info(account_info_iter)?;

let lending_market = LendingMarket::unpack(&lending_market_info.data.borrow())?;
let mut lending_market = LendingMarket::unpack(&lending_market_info.data.borrow())?;
if lending_market_info.owner != program_id {
msg!("Lending market provided is not owned by the lending program");
return Err(LendingError::InvalidAccountOwner.into());
Expand Down Expand Up @@ -1477,6 +1520,37 @@ fn process_borrow_obligation_liquidity(

let cumulative_borrow_rate_wads = borrow_reserve.liquidity.cumulative_borrow_rate_wads;

// check outflow rate limits
{
let borrow_value = borrow_reserve
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make borrow_reserve.market_value(liquidity_amount) method

.liquidity
.market_price
.try_mul(borrow_amount)?
.try_div(Decimal::from(
(10u128)
.checked_pow(borrow_reserve.liquidity.mint_decimals as u32)
.ok_or(LendingError::MathOverflow)?,
))?;

lending_market
.rate_limiter
.update(clock.slot, borrow_value)
.map_err(|err| {
msg!("Market outflow limit exceeded! Please try again later.");
err
})?;

borrow_reserve
.rate_limiter
.update(clock.slot, borrow_amount)
.map_err(|err| {
msg!("Reserve outflow limit exceeded! Please try again later");
err
})?;
}

LendingMarket::pack(lending_market, &mut lending_market_info.data.borrow_mut())?;

borrow_reserve.liquidity.borrow(borrow_amount)?;
borrow_reserve.last_update.mark_stale();
Reserve::pack(borrow_reserve, &mut borrow_reserve_info.data.borrow_mut())?;
Expand Down Expand Up @@ -1885,6 +1959,7 @@ fn process_liquidate_obligation_and_redeem_reserve_collateral(
user_transfer_authority_info,
clock,
token_program_id,
false,
)?;
let withdraw_reserve = Reserve::unpack(&withdraw_reserve_info.data.borrow())?;
if &withdraw_reserve.config.fee_receiver != withdraw_reserve_liquidity_fee_receiver_info.key
Expand Down Expand Up @@ -1959,6 +2034,7 @@ fn process_withdraw_obligation_collateral_and_redeem_reserve_liquidity(
user_transfer_authority_info,
clock,
token_program_id,
true,
)?;
Ok(())
}
Expand All @@ -1967,6 +2043,7 @@ fn process_withdraw_obligation_collateral_and_redeem_reserve_liquidity(
fn process_update_reserve_config(
program_id: &Pubkey,
config: ReserveConfig,
rate_limiter_config: RateLimiterConfig,
accounts: &[AccountInfo],
) -> ProgramResult {
validate_reserve_config(config)?;
Expand Down Expand Up @@ -2024,6 +2101,11 @@ fn process_update_reserve_config(
return Err(LendingError::InvalidMarketAuthority.into());
}

// if window duration and max outflow are different, then create a new rate limiter instance.
if rate_limiter_config != reserve.rate_limiter.config {
reserve.rate_limiter = RateLimiter::new(rate_limiter_config, Clock::get()?.slot);
}

if *pyth_price_info.key != reserve.liquidity.pyth_oracle_pubkey {
validate_pyth_keys(&lending_market, pyth_product_info, pyth_price_info)?;
reserve.liquidity.pyth_oracle_pubkey = *pyth_price_info.key;
Expand Down
Loading