Skip to content

Commit 2e966a2

Browse files
committed
add PoolMetadata
1 parent 0f1ba0c commit 2e966a2

File tree

3 files changed

+33
-41
lines changed

3 files changed

+33
-41
lines changed

pallets/hybrid-orderbook/src/lib.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,15 @@ pub mod pallet {
921921
return Ok(pool.to_pool_query(b_r, q_r, pool_price));
922922
}
923923

924+
pub fn get_pool_metadata(
925+
base_asset: &T::AssetKind,
926+
quote_asset: &T::AssetKind,
927+
) -> Result<PoolMetadata<T::Unit>, DispatchError> {
928+
let pool_id = T::PoolLocator::pool_id(base_asset, quote_asset).map_err(|_| Error::<T>::PoolNotFound)?;
929+
let pool = Pools::<T>::get(&pool_id).ok_or(Error::<T>::PoolNotFound)?;
930+
Ok(pool.to_pool_metadata())
931+
}
932+
924933
fn freeze_asset(
925934
who: &T::AccountId,
926935
asset: &T::AssetKind,
@@ -2043,33 +2052,11 @@ sp_api::decl_runtime_apis! {
20432052
AssetId: Codec,
20442053
Orderbook: Codec,
20452054
{
2046-
/// Provides a quote for [`Pallet::swap_tokens_for_exact_tokens`].
2047-
///
2048-
/// Note that the price may have changed by the time the transaction is executed.
2049-
/// (Use `amount_in_max` to control slippage.)
2050-
fn quote_price_tokens_for_exact_tokens(
2051-
asset1: AssetId,
2052-
asset2: AssetId,
2053-
amount: Balance,
2054-
include_fee: bool,
2055-
) -> Option<Balance>;
2056-
2057-
/// Provides a quote for [`Pallet::swap_exact_tokens_for_tokens`].
2058-
///
2059-
/// Note that the price may have changed by the time the transaction is executed.
2060-
/// (Use `amount_out_min` to control slippage.)
2061-
fn quote_price_exact_tokens_for_tokens(
2062-
asset1: AssetId,
2063-
asset2: AssetId,
2064-
amount: Balance,
2065-
include_fee: bool,
2066-
) -> Option<Balance>;
2067-
2068-
/// Returns the size of the liquidity pool for the given asset pair.
2069-
fn get_reserves(asset1: AssetId, asset2: AssetId) -> Option<(Balance, Balance)>;
2070-
20712055
/// Returns query of the `pool`
20722056
fn get_pool_query(base: AssetId, quote: AssetId) -> Option<PoolQuery<Orderbook, Balance>>;
2057+
2058+
/// Returns the metadata of the pool
2059+
fn get_pool_metadata(base: AssetId, quote: AssetId) -> Option<PoolMetadata<Balance>>;
20732060
}
20742061
}
20752062

pallets/hybrid-orderbook/src/types.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,20 @@ impl<Quantity, Account: Clone, BlockNumber> Order<Quantity, Account, BlockNumber
279279
}
280280
}
281281

282-
#[derive(Encode, Decode, Default, Debug, Clone, PartialEq, TypeInfo)]
282+
#[derive(Encode, Decode, TypeInfo)]
283283
pub struct PoolQuery<Orderbook, Unit> {
284284
bids: Orderbook,
285285
asks: Orderbook,
286-
taker_fee_rate: Permill,
287286
base_reserve: Unit,
288287
quote_reserve: Unit,
289288
pool_price: Unit,
289+
}
290+
291+
#[derive(Encode, Decode, TypeInfo)]
292+
pub struct PoolMetadata<Unit> {
293+
taker_fee_rate: Permill,
294+
lot_size: Unit,
295+
tick_size: Unit,
290296
pool_decimals: u8,
291297
base_decimals: u8,
292298
quote_decimals: u8,
@@ -350,10 +356,17 @@ impl<T: Config> Pool<T> {
350356
PoolQuery {
351357
bids: self.bids,
352358
asks: self.asks,
353-
taker_fee_rate: self.taker_fee_rate,
354359
base_reserve,
355360
quote_reserve,
356361
pool_price,
362+
}
363+
}
364+
365+
pub fn to_pool_metadata(self) -> PoolMetadata<T::Unit> {
366+
PoolMetadata {
367+
taker_fee_rate: self.taker_fee_rate,
368+
lot_size: self.lot_size,
369+
tick_size: self.tick_size,
357370
pool_decimals: self.pool_decimals,
358371
base_decimals: self.base_decimals,
359372
quote_decimals: self.quote_decimals,

runtime/src/apis.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use sp_runtime::{
4040
ApplyExtrinsicResult,
4141
};
4242
use sp_version::RuntimeVersion;
43-
use pallet_hybrid_orderbook::{CritbitTree, PoolQuery, Tick};
43+
use pallet_hybrid_orderbook::{CritbitTree, PoolQuery, PoolMetadata, Tick};
4444

4545
// Local module imports
4646
use super::{
@@ -213,21 +213,13 @@ impl_runtime_apis! {
213213
}
214214

215215
impl pallet_hybrid_orderbook::HybridOrderbookApi<Block, Balance, NativeOrWithId<u32>, CritbitTree<Balance, Tick<Balance, AccountId, BlockNumber>>> for Runtime {
216-
fn quote_price_exact_tokens_for_tokens(asset1: NativeOrWithId<u32>, asset2: NativeOrWithId<u32>, amount: Balance, include_fee: bool) -> Option<Balance> {
217-
HybridOrderbook::quote_price_exact_tokens_for_tokens(asset1, asset2, amount, include_fee)
218-
}
219-
220-
fn quote_price_tokens_for_exact_tokens(asset1: NativeOrWithId<u32>, asset2: NativeOrWithId<u32>, amount: Balance, include_fee: bool) -> Option<Balance> {
221-
HybridOrderbook::quote_price_tokens_for_exact_tokens(asset1, asset2, amount, include_fee)
222-
}
223-
224-
fn get_reserves(_asset1: NativeOrWithId<u32>, _asset2: NativeOrWithId<u32>) -> Option<(Balance, Balance)> {
225-
None
226-
}
227-
228216
fn get_pool_query(base: NativeOrWithId<u32>, quote: NativeOrWithId<u32>) -> Option<PoolQuery<CritbitTree<Balance, Tick<Balance, AccountId, BlockNumber>>, Balance>> {
229217
HybridOrderbook::get_pool_query(&base, &quote).ok()
230218
}
219+
220+
fn get_pool_metadata(base: NativeOrWithId<u32>, quote: NativeOrWithId<u32>) -> Option<PoolMetadata<Balance>> {
221+
HybridOrderbook::get_pool_metadata(&base, &quote).ok()
222+
}
231223
}
232224

233225
impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {

0 commit comments

Comments
 (0)