-
Notifications
You must be signed in to change notification settings - Fork 200
Funding includes vamm #1971
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
Open
moosecat2
wants to merge
7
commits into
amm-availability-update
Choose a base branch
from
funding-includes-vamm
base: amm-availability-update
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Funding includes vamm #1971
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
06b80a8
add amm levels
moosecat2 800fcc5
Merge branch 'amm-availability-update' into funding-includes-vamm
moosecat2 fd4d920
tests pass
moosecat2 6ecb34f
open liquidity change
moosecat2 0783078
sipmler logic
moosecat2 b8e1214
remove bba from update_mark_twap_crank
moosecat2 49dad0d
include state being paused in twap crank criteria
moosecat2 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| use crate::controller::amm::SwapDirection; | ||
| use crate::math::amm::{calculate_quote_asset_amount_swapped, calculate_swap_output}; | ||
| use crate::math::amm_spread::get_spread_reserves; | ||
| use crate::math::constants::PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO; | ||
| use crate::math::orders::{standardize_base_asset_amount, standardize_price, Level}; | ||
| use crate::state::fill_mode::FillMode; | ||
| use crate::state::pyth_lazer_oracle::PythLazerOracle; | ||
| use crate::state::user::{MarketType, Order}; | ||
|
|
@@ -1700,6 +1705,110 @@ impl AMM { | |
| self.mm_oracle_slot = mm_oracle_slot; | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn get_levels( | ||
| &self, | ||
| levels: u8, | ||
| taker_direction: PositionDirection, | ||
| terminal_price: u64, | ||
| ) -> DriftResult<Vec<Level>> { | ||
| let (mut base_reserve, mut quote_reserve) = match taker_direction { | ||
| PositionDirection::Long => get_spread_reserves(self, PositionDirection::Long)?, | ||
| PositionDirection::Short => get_spread_reserves(self, PositionDirection::Short)?, | ||
| }; | ||
|
|
||
| let (max_bids, max_asks) = amm::_calculate_market_open_bids_asks( | ||
| base_reserve, | ||
| self.min_base_asset_reserve, | ||
| self.max_base_asset_reserve, | ||
| )?; | ||
| let open_liquidity_u128: u128 = match taker_direction { | ||
| PositionDirection::Long => max_bids.unsigned_abs(), | ||
| PositionDirection::Short => max_asks.unsigned_abs(), | ||
| }; | ||
| let open_liquidity: u64 = open_liquidity_u128.min(u64::MAX as u128).cast()?; | ||
|
|
||
| if open_liquidity < self.min_order_size.saturating_mul(2) { | ||
| return Ok(Vec::new()); | ||
| } | ||
|
|
||
| let swap_dir = match taker_direction { | ||
| PositionDirection::Long => SwapDirection::Remove, | ||
| PositionDirection::Short => SwapDirection::Add, | ||
| }; | ||
|
|
||
| let mut remaining = open_liquidity; | ||
| let mut out: Vec<Level> = Vec::with_capacity(levels as usize); | ||
|
|
||
| for i in 0..levels { | ||
| if remaining < self.order_step_size { | ||
| break; | ||
| } | ||
|
|
||
| let remaining_levels = levels.saturating_sub(i); | ||
| let target = remaining | ||
| .checked_div(remaining_levels.max(1) as u64) | ||
| .unwrap_or(0); | ||
| if target == 0 { | ||
| break; | ||
| } | ||
|
|
||
| let candidate = target.min(remaining); | ||
| let mut base_swap = standardize_base_asset_amount(candidate, self.order_step_size)?; | ||
| if base_swap == 0 { | ||
| break; | ||
| } | ||
|
|
||
| let allowable: u128 = match taker_direction { | ||
| PositionDirection::Long => base_reserve.safe_sub(self.min_base_asset_reserve)?, | ||
| PositionDirection::Short => self.max_base_asset_reserve.safe_sub(base_reserve)?, | ||
| }; | ||
|
|
||
| let cap = allowable.min(u64::MAX as u128).cast()?; | ||
| base_swap = base_swap.min(cap); | ||
| if base_swap == 0 { | ||
| break; | ||
| } | ||
|
|
||
| // Sim swap | ||
| let (new_quote_reserve, new_base_reserve) = | ||
| calculate_swap_output(base_swap.cast()?, base_reserve, swap_dir, self.sqrt_k)?; | ||
|
|
||
| let quote_swapped = calculate_quote_asset_amount_swapped( | ||
| quote_reserve, | ||
| new_quote_reserve, | ||
| swap_dir, | ||
| self.peg_multiplier, | ||
| )?; | ||
|
|
||
| let mut price: u64 = quote_swapped | ||
| .safe_mul(PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO)? | ||
| .safe_div(base_swap.cast()?)? | ||
| .cast()?; | ||
|
|
||
| price = standardize_price(price, self.order_tick_size, taker_direction)?; | ||
|
|
||
| price = match taker_direction { | ||
| PositionDirection::Long => price.min(terminal_price), | ||
| PositionDirection::Short => price.max(terminal_price), | ||
| }; | ||
|
|
||
| out.push(Level { | ||
| price, | ||
| base_asset_amount: base_swap, | ||
| }); | ||
|
|
||
| base_reserve = new_base_reserve; | ||
| quote_reserve = new_quote_reserve; | ||
| remaining = remaining.saturating_sub(base_swap); | ||
|
|
||
| if out.len() as u8 >= levels { | ||
|
Member
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. is this redundant? |
||
| break; | ||
| } | ||
| } | ||
|
|
||
| Ok(out) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -1764,4 +1873,105 @@ impl AMM { | |
| ..AMM::default() | ||
| } | ||
| } | ||
|
|
||
| pub fn liquid_sol_test() -> Self { | ||
| AMM { | ||
| historical_oracle_data: HistoricalOracleData { | ||
| last_oracle_price: 190641285, | ||
| last_oracle_conf: 0, | ||
| last_oracle_delay: 17, | ||
| last_oracle_price_twap: 189914813, | ||
| last_oracle_price_twap_5min: 190656263, | ||
| last_oracle_price_twap_ts: 1761000653, | ||
| }, | ||
| base_asset_amount_per_lp: -213874721369, | ||
| quote_asset_amount_per_lp: -58962015125, | ||
| fee_pool: PoolBalance { | ||
| scaled_balance: 8575516773308741, | ||
| market_index: 0, | ||
| padding: [0, 0, 0, 0, 0, 0], | ||
| }, | ||
| base_asset_reserve: 24302266099492168, | ||
| quote_asset_reserve: 24291832241447530, | ||
| concentration_coef: 1004142, | ||
| min_base_asset_reserve: 24196060267862680, | ||
| max_base_asset_reserve: 24396915542699764, | ||
| sqrt_k: 24297048610394662, | ||
| peg_multiplier: 190724934, | ||
| terminal_quote_asset_reserve: 24297816895589961, | ||
| base_asset_amount_long: 917177880000000, | ||
| base_asset_amount_short: -923163630000000, | ||
| base_asset_amount_with_amm: -5985750000000, | ||
| base_asset_amount_with_unsettled_lp: 0, | ||
| max_open_interest: 2000000000000000, | ||
| quote_asset_amount: 15073495357350, | ||
| quote_entry_amount_long: -182456763836058, | ||
| quote_entry_amount_short: 182214483467437, | ||
| quote_break_even_amount_long: -181616323258115, | ||
| quote_break_even_amount_short: 180666910938502, | ||
| user_lp_shares: 0, | ||
| last_funding_rate: 142083, | ||
| last_funding_rate_long: 142083, | ||
| last_funding_rate_short: 142083, | ||
| last_24h_avg_funding_rate: -832430, | ||
| total_fee: 23504910735696, | ||
| total_mm_fee: 8412188362643, | ||
| total_exchange_fee: 15240376207986, | ||
| total_fee_minus_distributions: 12622783464171, | ||
| total_fee_withdrawn: 7622904850984, | ||
| total_liquidation_fee: 5153159954719, | ||
| cumulative_funding_rate_long: 48574028958, | ||
| cumulative_funding_rate_short: 48367829283, | ||
| total_social_loss: 4512659649, | ||
| ask_base_asset_reserve: 24307711375337898, | ||
| ask_quote_asset_reserve: 24286390522755450, | ||
| bid_base_asset_reserve: 24318446036975185, | ||
| bid_quote_asset_reserve: 24275670011080633, | ||
| last_oracle_normalised_price: 190641285, | ||
| last_oracle_reserve_price_spread_pct: 0, | ||
| last_bid_price_twap: 189801870, | ||
| last_ask_price_twap: 189877406, | ||
| last_mark_price_twap: 189839638, | ||
| last_mark_price_twap_5min: 190527180, | ||
| last_update_slot: 374711191, | ||
| last_oracle_conf_pct: 491, | ||
| net_revenue_since_last_funding: 9384752152, | ||
| last_funding_rate_ts: 1760997616, | ||
| funding_period: 3600, | ||
| order_step_size: 10000000, | ||
| order_tick_size: 100, | ||
| min_order_size: 10000000, | ||
| mm_oracle_slot: 374711192, | ||
| volume_24h: 114093279361263, | ||
| long_intensity_volume: 1572903262040, | ||
| short_intensity_volume: 3352472398103, | ||
| last_trade_ts: 1761000641, | ||
| mark_std: 623142, | ||
| oracle_std: 727888, | ||
| last_mark_price_twap_ts: 1761000646, | ||
| base_spread: 100, | ||
| max_spread: 20000, | ||
| long_spread: 40, | ||
| short_spread: 842, | ||
| mm_oracle_price: 190643458, | ||
| max_fill_reserve_fraction: 25000, | ||
| max_slippage_ratio: 50, | ||
| curve_update_intensity: 110, | ||
| amm_jit_intensity: 100, | ||
| last_oracle_valid: true, | ||
| target_base_asset_amount_per_lp: -565000000, | ||
| per_lp_base: 3, | ||
| taker_speed_bump_override: 5, | ||
| amm_spread_adjustment: -20, | ||
| oracle_slot_delay_override: -1, | ||
| mm_oracle_sequence_id: 1761000654650000, | ||
| net_unsettled_funding_pnl: 1042875, | ||
| quote_asset_amount_with_unsettled_lp: -112671203108, | ||
| reference_price_offset: -488, | ||
| amm_inventory_spread_adjustment: -20, | ||
| last_funding_oracle_twap: 189516656, | ||
| reference_price_offset_deadband_pct: 10, | ||
| ..AMM::default() | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.