-
Notifications
You must be signed in to change notification settings - Fork 198
program: param-update-for-summary-stats-update-pool-balances #2008
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -493,14 +493,14 @@ fn calculate_revenue_pool_transfer( | |
| market: &PerpMarket, | ||
| spot_market: &SpotMarket, | ||
| amm_fee_pool_token_amount_after: u128, | ||
| terminal_state_surplus: i128, | ||
| market_surplus: i128, | ||
| ) -> DriftResult<i128> { | ||
| // Calculates the revenue pool transfer amount for a given market state (positive = send to revenue pool, negative = pull from revenue pool) | ||
| // If the AMM budget is above `FEE_POOL_TO_REVENUE_POOL_THRESHOLD` (in surplus), settle fees collected to the revenue pool depending on the health of the AMM state | ||
| // Otherwise, spull from the revenue pool (up to a constraint amount) | ||
| // Otherwise, pull from the revenue pool (up to a constraint amount) | ||
|
|
||
| let amm_budget_surplus = | ||
| terminal_state_surplus.saturating_sub(FEE_POOL_TO_REVENUE_POOL_THRESHOLD.cast()?); | ||
| market_surplus.saturating_sub(FEE_POOL_TO_REVENUE_POOL_THRESHOLD.cast()?); | ||
|
|
||
| if amm_budget_surplus > 0 { | ||
| let fee_pool_threshold = amm_fee_pool_token_amount_after | ||
|
|
@@ -553,20 +553,22 @@ fn calculate_revenue_pool_transfer( | |
|
|
||
| Ok(revenue_pool_transfer) | ||
| } else if amm_budget_surplus < 0 { | ||
| // auto withdraw only from pool only when revenue pool has some buffer | ||
|
|
||
| let revenue_pool_balance: u128 = get_token_amount( | ||
| spot_market.revenue_pool.scaled_balance, | ||
| spot_market, | ||
| &SpotBalanceType::Deposit, | ||
| )? | ||
| .cast()?; | ||
|
|
||
| let max_revenue_withdraw_allowed = market | ||
| .insurance_claim | ||
| .max_revenue_withdraw_per_period | ||
| .cast::<i64>()? | ||
| .saturating_sub(market.insurance_claim.revenue_withdraw_since_last_settle) | ||
| .cast::<u128>()? | ||
| .min( | ||
| get_token_amount( | ||
| spot_market.revenue_pool.scaled_balance, | ||
| spot_market, | ||
| &SpotBalanceType::Deposit, | ||
| )? | ||
| .cast()?, | ||
| ) | ||
| .min(revenue_pool_balance.saturating_sub(FEE_POOL_TO_REVENUE_POOL_THRESHOLD)) | ||
| .min( | ||
| market | ||
| .insurance_claim | ||
|
|
@@ -665,10 +667,7 @@ pub fn update_pool_balances( | |
| market.amm.fee_pool.balance_type(), | ||
| )?; | ||
|
|
||
| let terminal_state_surplus = market | ||
| .amm | ||
| .total_fee_minus_distributions | ||
| .safe_sub(market.amm.total_fee_withdrawn.cast()?)?; | ||
| let market_surplus = market.amm.total_fee_minus_distributions; | ||
|
|
||
| // market can perform withdraw from revenue pool | ||
| if spot_market.insurance_fund.last_revenue_settle_ts | ||
|
|
@@ -688,7 +687,7 @@ pub fn update_pool_balances( | |
| market, | ||
| spot_market, | ||
| amm_fee_pool_token_amount_after, | ||
| terminal_state_surplus, | ||
| market_surplus, | ||
| )?; | ||
|
|
||
| match revenue_pool_transfer.cmp(&0) { | ||
|
|
@@ -710,6 +709,10 @@ pub fn update_pool_balances( | |
| spot_market, | ||
| &mut market.amm.fee_pool, | ||
| )?; | ||
| market.amm.total_fee_withdrawn = market | ||
| .amm | ||
| .total_fee_withdrawn | ||
| .saturating_sub(revenue_pool_transfer.unsigned_abs()); | ||
|
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. saturating sub or safe sub? should it throw if it wants to take the value negative instead of sending to 0? above there's safe_add as well |
||
| } | ||
| Ordering::Equal => (), | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2579,8 +2579,17 @@ pub fn handle_update_prelaunch_oracle(ctx: Context<UpdatePrelaunchOracle>) -> Re | |
| )] | ||
| pub fn handle_update_perp_bid_ask_twap<'c: 'info, 'info>( | ||
| ctx: Context<'_, '_, 'c, 'info, UpdatePerpBidAskTwap<'info>>, | ||
| update_market_summary_stats: bool, | ||
| ) -> Result<()> { | ||
| let perp_market = &mut load_mut!(ctx.accounts.perp_market)?; | ||
| let spot_market = &mut load_mut!(ctx.accounts.quote_spot_market)?; | ||
|
|
||
| validate!( | ||
| spot_market.market_index == QUOTE_SPOT_MARKET_INDEX, | ||
| ErrorCode::DefaultError, | ||
| "invalid spot market" | ||
| )?; | ||
|
|
||
|
Comment on lines
+2585
to
+2592
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. did you run this through a bankrun test yet? quote market is not marked as mutable in the context struct so i think it would throw. can also through this validate in the context struct as well as a constraint |
||
| let clock = Clock::get()?; | ||
| let now = clock.unix_timestamp; | ||
| let slot = clock.slot; | ||
|
|
@@ -2677,6 +2686,16 @@ pub fn handle_update_perp_bid_ask_twap<'c: 'info, 'info>( | |
| )?; | ||
| } | ||
|
|
||
| if update_market_summary_stats { | ||
| let new_total_fees_minus_distributions = calculate_perp_market_amm_summary_stats( | ||
| perp_market, | ||
| spot_market, | ||
| mm_oracle_price_data.get_price(), | ||
| false, | ||
| ); | ||
| perp_market.amm.total_fee_minus_distributions = new_total_fees_minus_distributions; | ||
| } | ||
|
|
||
| let funding_paused = | ||
| state.funding_paused()? || perp_market.is_operation_paused(PerpOperation::UpdateFunding); | ||
| controller::funding::update_funding_rate( | ||
|
|
@@ -3748,6 +3767,7 @@ pub struct UpdatePerpBidAskTwap<'info> { | |
| pub state: Box<Account<'info, State>>, | ||
| #[account(mut)] | ||
| pub perp_market: AccountLoader<'info, PerpMarket>, | ||
| pub quote_spot_market: AccountLoader<'info, SpotMarket>, | ||
| /// CHECK: checked in `update_funding_rate` ix constraint | ||
| pub oracle: AccountInfo<'info>, | ||
| pub keeper_stats: AccountLoader<'info, UserStats>, | ||
|
|
||
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.
nice one