forked from solana-labs/solana-program-library
-
Notifications
You must be signed in to change notification settings - Fork 71
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
0xripleys
merged 17 commits into
solendprotocol:v2_upcoming
from
0xripleys:0xripleys_outflow_limits
Feb 24, 2023
+985
−109
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7a93837
sliding window rate limiter
0xripleys 99cec4d
making RateLimiter pack friendly
0xripleys e7adf0d
adding reserve rate limiter to instructions and updating tests
0xripleys 3fe3564
fmt
0xripleys 03843b2
add rate limiter to lending market
0xripleys 07551e0
fix update_reserve_config
0xripleys 2fc35b1
make argument ordering more consistent
0xripleys 5432602
correct lending market limiter
0xripleys 2255e67
use RateLimiterConfig everywhere
0xripleys 6a7eac9
more space efficient rate limiter
0xripleys 732b09b
fmt
0xripleys 4d3a37c
outflow rate limits bpf test
0xripleys 5e8bffc
fmt
0xripleys 0c7b65c
convert assert to an err
0xripleys 1af333a
refactor into helper function
0xripleys 275e509
slightly safer prev weight calculation
0xripleys ebec41a
config => rate limiter config
0xripleys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -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, | ||
|
@@ -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"); | ||
|
@@ -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(); | ||
|
@@ -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(()) | ||
|
@@ -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)?; | ||
|
@@ -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)?; | ||
_redeem_reserve_collateral( | ||
program_id, | ||
collateral_amount, | ||
|
@@ -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(); | ||
|
@@ -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()); | ||
|
@@ -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(), | ||
|
@@ -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()); | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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())?; | ||
|
@@ -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 | ||
|
@@ -1959,6 +2034,7 @@ fn process_withdraw_obligation_collateral_and_redeem_reserve_liquidity( | |
user_transfer_authority_info, | ||
clock, | ||
token_program_id, | ||
true, | ||
)?; | ||
Ok(()) | ||
} | ||
|
@@ -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)?; | ||
|
@@ -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; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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