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

feat: add Range order pool price to the pool_price_v2 rpc call #4548

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions state-chain/amm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
})
}

/// Returns the Range Order sub-pool's current price.
/// SwapDirection is ignored as the price are the same for both directions.
pub fn current_range_order_pool_price(&mut self) -> SqrtPriceQ64F96 {
self.range_orders.raw_current_sqrt_price()
}

/// Returns the current sqrt price for a given direction of swap. The price is measured in units
/// of the specified Pairs argument
pub fn current_sqrt_price(&mut self, order: Side) -> Option<SqrtPriceQ64F96> {
Expand Down
5 changes: 5 additions & 0 deletions state-chain/amm/src/range_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@ impl<LiquidityProvider: Clone + Ord> PoolState<LiquidityProvider> {
SD::further_liquidity(self.current_tick).then_some(self.current_sqrt_price)
}

/// Returns the raw current sqrt price of the pool, without liquidity checks.
pub(super) fn raw_current_sqrt_price(&self) -> SqrtPriceQ64F96 {
self.current_sqrt_price
}

/// Calculates the fees owed to the specified position, resets the fees owed for that position
/// to zero, calls `try_debit` passing the Amounts required to add the `minted_liquidity` to the
/// position. If `try_debit` returns `Ok(t)` the position will be created if it didn't already
Expand Down
2 changes: 2 additions & 0 deletions state-chain/pallets/cf-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ pub struct PoolPriceV1 {
pub struct PoolPriceV2 {
pub sell: Option<SqrtPriceQ64F96>,
pub buy: Option<SqrtPriceQ64F96>,
pub range_order: SqrtPriceQ64F96,
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -1558,6 +1559,7 @@ impl<T: Config> Pallet<T> {
Ok(PoolPriceV2 {
sell: pool.pool_state.current_price(Side::Sell).map(|(_, sqrt_price, _)| sqrt_price),
buy: pool.pool_state.current_price(Side::Buy).map(|(_, sqrt_price, _)| sqrt_price),
range_order: pool.pool_state.current_range_order_pool_price(),
})
}

Expand Down
Loading