From fd61ae04f9de1f79b1e03b23649bbb4f501d903e Mon Sep 17 00:00:00 2001 From: 0xbigz <83473873+0xbigz@users.noreply.github.com> Date: Fri, 9 Aug 2024 14:36:28 -0400 Subject: [PATCH] program: support-more-insurance-revenue-rate-limits --- programs/drift/src/controller/insurance.rs | 25 ++++++++++++++++++- .../drift/src/controller/insurance/tests.rs | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/programs/drift/src/controller/insurance.rs b/programs/drift/src/controller/insurance.rs index 06b8839c6..61bb4d3cc 100644 --- a/programs/drift/src/controller/insurance.rs +++ b/programs/drift/src/controller/insurance.rs @@ -741,7 +741,30 @@ pub fn settle_revenue_to_insurance_fund( .max(1), )?; let capped_token_pct_amount = token_amount.safe_div(10)?; - token_amount = capped_token_pct_amount.min(capped_apr_amount); + + // other per market limits to impose + let other_token_rate_limits = if spot_market.market_index == GOV_SPOT_MARKET_INDEX { + 64_u128.safe_mul(10_u128.pow(spot_market.decimals))? + } else if spot_market.market_index != QUOTE_SPOT_MARKET_INDEX { + // additionally, don't allow token emissions to exceed 100% of borrows annualized + // or 1 token if thats lower + let capped_token_amount_by_risk = spot_market + .get_borrows()? + .safe_div( + ONE_YEAR + .safe_div(spot_market.insurance_fund.revenue_settle_period.cast()?)? + .max(1), + )? + .max(10_u128.pow(spot_market.decimals)); + + capped_token_amount_by_risk + } else { + u128::MAX + }; + + token_amount = capped_token_pct_amount + .min(capped_apr_amount) + .min(other_token_rate_limits) } let insurance_fund_token_amount = get_proportion_u128( diff --git a/programs/drift/src/controller/insurance/tests.rs b/programs/drift/src/controller/insurance/tests.rs index 4301a3d26..646814757 100644 --- a/programs/drift/src/controller/insurance/tests.rs +++ b/programs/drift/src/controller/insurance/tests.rs @@ -221,6 +221,7 @@ pub fn large_num_seeded_stake_if_test() { // all funds in revenue pool let mut spot_market = SpotMarket { deposit_balance: 100 * SPOT_BALANCE_PRECISION, + decimals: 0, cumulative_deposit_interest: 1111 * SPOT_CUMULATIVE_INTEREST_PRECISION / 1000, insurance_fund: InsuranceFund { unstaking_period: 0,