Skip to content

Commit

Permalink
Added Range order pool price to the pool_price_v2 rpc call (#4548)
Browse files Browse the repository at this point in the history
Co-authored-by: dandanlen <3168260+dandanlen@users.noreply.github.com>
  • Loading branch information
syan095 and dandanlen authored Feb 29, 2024
1 parent d3d4fbd commit 2288510
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
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

0 comments on commit 2288510

Please sign in to comment.