From b18d361965620d7e867ea2393abdf57df29e5f59 Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Tue, 20 Feb 2024 13:41:54 +1300 Subject: [PATCH 1/2] Added Range order pool price to the pool_price_v2 rpc call --- state-chain/amm/src/lib.rs | 8 ++++++++ state-chain/pallets/cf-pools/src/lib.rs | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/state-chain/amm/src/lib.rs b/state-chain/amm/src/lib.rs index 2bbaf679ea..bd1acb3f24 100644 --- a/state-chain/amm/src/lib.rs +++ b/state-chain/amm/src/lib.rs @@ -57,6 +57,14 @@ impl PoolState { }) } + /// 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) -> Option<(Price, SqrtPriceQ64F96, Tick)> { + self.range_orders.current_sqrt_price::().map(|sqrt_price| { + (sqrt_price_to_price(sqrt_price), sqrt_price, tick_at_sqrt_price(sqrt_price)) + }) + } + /// Returns the current sqrt price for a given direction of swap. The price is measured in units /// of the specified Side argument pub fn current_sqrt_price(&mut self, order: Order) -> Option { diff --git a/state-chain/pallets/cf-pools/src/lib.rs b/state-chain/pallets/cf-pools/src/lib.rs index 773f6b58c6..7a752a1bcc 100644 --- a/state-chain/pallets/cf-pools/src/lib.rs +++ b/state-chain/pallets/cf-pools/src/lib.rs @@ -1262,6 +1262,7 @@ pub struct PoolPriceV1 { pub struct PoolPriceV2 { pub sell: Option, pub buy: Option, + pub range_order: Option, } impl Pallet { @@ -1629,6 +1630,10 @@ impl Pallet { Ok(PoolPriceV2 { sell: pool.pool_state.current_price(Order::Sell).map(|(_, sqrt_price, _)| sqrt_price), buy: pool.pool_state.current_price(Order::Buy).map(|(_, sqrt_price, _)| sqrt_price), + range_order: pool + .pool_state + .current_range_order_pool_price() + .map(|(_, sqrt_price, _)| sqrt_price), }) } From c620a2ccbe2a70a034fe7b2e2b3ff97743fbc55b Mon Sep 17 00:00:00 2001 From: Roy Yang Date: Tue, 27 Feb 2024 12:27:09 +1300 Subject: [PATCH 2/2] Updated range order to use the raw sqrt price value --- state-chain/amm/src/lib.rs | 6 ++---- state-chain/amm/src/range_orders.rs | 5 +++++ state-chain/pallets/cf-pools/src/lib.rs | 7 ++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/state-chain/amm/src/lib.rs b/state-chain/amm/src/lib.rs index bd1acb3f24..a64d1ad496 100644 --- a/state-chain/amm/src/lib.rs +++ b/state-chain/amm/src/lib.rs @@ -59,10 +59,8 @@ impl PoolState { /// 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) -> Option<(Price, SqrtPriceQ64F96, Tick)> { - self.range_orders.current_sqrt_price::().map(|sqrt_price| { - (sqrt_price_to_price(sqrt_price), sqrt_price, tick_at_sqrt_price(sqrt_price)) - }) + 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 diff --git a/state-chain/amm/src/range_orders.rs b/state-chain/amm/src/range_orders.rs index 1bbc98c3b4..042eb89168 100644 --- a/state-chain/amm/src/range_orders.rs +++ b/state-chain/amm/src/range_orders.rs @@ -520,6 +520,11 @@ impl PoolState { 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 diff --git a/state-chain/pallets/cf-pools/src/lib.rs b/state-chain/pallets/cf-pools/src/lib.rs index 7a752a1bcc..f96c5b9bd0 100644 --- a/state-chain/pallets/cf-pools/src/lib.rs +++ b/state-chain/pallets/cf-pools/src/lib.rs @@ -1262,7 +1262,7 @@ pub struct PoolPriceV1 { pub struct PoolPriceV2 { pub sell: Option, pub buy: Option, - pub range_order: Option, + pub range_order: SqrtPriceQ64F96, } impl Pallet { @@ -1630,10 +1630,7 @@ impl Pallet { Ok(PoolPriceV2 { sell: pool.pool_state.current_price(Order::Sell).map(|(_, sqrt_price, _)| sqrt_price), buy: pool.pool_state.current_price(Order::Buy).map(|(_, sqrt_price, _)| sqrt_price), - range_order: pool - .pool_state - .current_range_order_pool_price() - .map(|(_, sqrt_price, _)| sqrt_price), + range_order: pool.pool_state.current_range_order_pool_price(), }) }