Skip to content

Commit

Permalink
refactor: make pool price struct generic
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Nov 18, 2024
1 parent fff9c44 commit 340157e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
33 changes: 25 additions & 8 deletions state-chain/pallets/cf-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,13 +1311,21 @@ pub struct PoolPriceV1 {
pub tick: Tick,
}

#[derive(Serialize, Deserialize, Clone, Encode, Decode, TypeInfo, PartialEq, Eq, Debug)]
pub struct PoolPriceV2 {
pub sell: Option<SqrtPriceQ64F96>,
pub buy: Option<SqrtPriceQ64F96>,
pub type PoolPriceV2 = PoolPrice<SqrtPriceQ64F96>;

#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq, Serialize, Deserialize)]
pub struct PoolPrice<P> {
pub sell: Option<P>,
pub buy: Option<P>,
pub range_order: SqrtPriceQ64F96,
}

impl<P> PoolPrice<P> {
pub fn map_sell_and_buy_prices<R>(self, f: impl Fn(P) -> R) -> PoolPrice<R> {
PoolPrice { sell: self.sell.map(&f), buy: self.buy.map(&f), range_order: self.range_order }
}
}

#[derive(PartialEq, Eq)]
enum NoOpStatus {
Allow,
Expand Down Expand Up @@ -1704,12 +1712,21 @@ impl<T: Config> Pallet<T> {
})
}

pub fn pool_price(base_asset: Asset, quote_asset: Asset) -> Result<PoolPriceV2, DispatchError> {
pub fn pool_price(
base_asset: Asset,
quote_asset: Asset,
) -> Result<PoolPrice<PoolPriceV1>, DispatchError> {
let asset_pair = AssetPair::try_new::<T>(base_asset, quote_asset)?;
let mut pool = Pools::<T>::get(asset_pair).ok_or(Error::<T>::PoolDoesNotExist)?;
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),
Ok(PoolPrice {
sell: pool
.pool_state
.current_price(Side::Sell)
.map(|(price, sqrt_price, tick)| PoolPriceV1 { price, sqrt_price, tick }),
buy: pool
.pool_state
.current_price(Side::Buy)
.map(|(price, sqrt_price, tick)| PoolPriceV1 { price, sqrt_price, tick }),
range_order: pool.pool_state.current_range_order_pool_price(),
})
}
Expand Down
22 changes: 17 additions & 5 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,10 @@ impl_runtime_apis! {
}

fn cf_pool_price_v2(base_asset: Asset, quote_asset: Asset) -> Result<PoolPriceV2, DispatchErrorWithMessage> {
LiquidityPools::pool_price(base_asset, quote_asset).map_err(Into::into)
Ok(
LiquidityPools::pool_price(base_asset, quote_asset)?
.map_sell_and_buy_prices(|price| price.sqrt_price)
)
}

/// Simulates a swap and return the intermediate (if any) and final output.
Expand Down Expand Up @@ -1966,14 +1969,23 @@ impl_runtime_apis! {
let current_block = System::block_number();

pallet_cf_swapping::SwapQueue::<Runtime>::iter().flat_map(|(block, swaps_for_block)| {

// In case `block` has already passed, the swaps will be re-tried at the next block:
let execute_at = core::cmp::max(block, current_block.saturating_add(1));

let swaps: Vec<_> = swaps_for_block.iter().filter(|swap| swap.from == base_asset || swap.to == base_asset).cloned().collect();
let swaps: Vec<_> = swaps_for_block
.iter()
.filter(|swap| swap.from == base_asset || swap.to == base_asset)
.cloned()
.collect();

let pool_sell_price = LiquidityPools::pool_price(base_asset, quote_asset).
expect("Pool should exist")
.sell
.map(|price| price.sqrt_price);

let pool_sell_price = LiquidityPools::pool_price(base_asset, quote_asset).expect("Pool should exist").sell;
Swapping::get_scheduled_swap_legs(swaps, base_asset, pool_sell_price).into_iter().map(move |swap| (swap, execute_at))
Swapping::get_scheduled_swap_legs(swaps, base_asset, pool_sell_price)
.into_iter()
.map(move |swap| (swap, execute_at))
}).collect()
}

Expand Down

0 comments on commit 340157e

Please sign in to comment.