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 1 commit
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
8 changes: 8 additions & 0 deletions state-chain/amm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ 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) -> Option<(Price, SqrtPriceQ64F96, Tick)> {
Copy link
Contributor

@AlastairHolmes AlastairHolmes Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we want to just use the value in range_orders::PoolState::current_sqrt_price (which is not optional), because this is for calculating range order values, not for swaps.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

self.range_orders.current_sqrt_price::<ZeroToOne>().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<SqrtPriceQ64F96> {
Expand Down
5 changes: 5 additions & 0 deletions state-chain/pallets/cf-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ pub struct PoolPriceV1 {
pub struct PoolPriceV2 {
pub sell: Option<SqrtPriceQ64F96>,
pub buy: Option<SqrtPriceQ64F96>,
pub range_order: Option<SqrtPriceQ64F96>,
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -1629,6 +1630,10 @@ impl<T: Config> Pallet<T> {
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),
})
}

Expand Down
Loading