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

asset conversion runtime quote api generalised #1442

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ impl pallet_assets::Config<PoolAssetsInstance> for Runtime {
type BenchmarkHelper = ();
}

type MaxSwapLength = ConstU32<4>;

impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
Expand All @@ -322,7 +324,7 @@ impl pallet_asset_conversion::Config for Runtime {
type LPFee = ConstU32<3>;
type PalletId = AssetConversionPalletId;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = ConstU32<4>;
type MaxSwapPathLength = MaxSwapLength;
type MultiAssetId = Box<MultiLocation>;
type MultiAssetIdConverter =
MultiLocationConverter<WestendLocation, LocalAndForeignAssetsMultiLocationMatcher>;
Expand Down Expand Up @@ -1081,14 +1083,15 @@ impl_runtime_apis! {
Balance,
u128,
Box<MultiLocation>,
MaxSwapLength
> for Runtime
{
fn quote_price_exact_tokens_for_tokens(asset1: Box<MultiLocation>, asset2: Box<MultiLocation>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_exact_tokens_for_tokens(asset1, asset2, amount, include_fee)
fn quote_price_exact_tokens_for_tokens(path: BoundedVec<Box<MultiLocation>, MaxSwapLength>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_exact_tokens_for_tokens(path, amount, include_fee)
}

fn quote_price_tokens_for_exact_tokens(asset1: Box<MultiLocation>, asset2: Box<MultiLocation>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_tokens_for_exact_tokens(asset1, asset2, amount, include_fee)
fn quote_price_tokens_for_exact_tokens(path: BoundedVec<Box<MultiLocation>, MaxSwapLength>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_tokens_for_exact_tokens(path, amount, include_fee)
}

fn get_reserves(asset1: Box<MultiLocation>, asset2: Box<MultiLocation>) -> Option<(Balance, Balance)> {
Expand Down
15 changes: 9 additions & 6 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,8 @@ parameter_types! {
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0); // should be non-zero if AllowMultiAssetPools is true, otherwise can be zero.
}

type MaxSwapLength = ConstU32<4>;

impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
Expand All @@ -1657,7 +1659,7 @@ impl pallet_asset_conversion::Config for Runtime {
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type WeightInfo = pallet_asset_conversion::weights::SubstrateWeight<Runtime>;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = ConstU32<4>;
type MaxSwapPathLength = MaxSwapLength;
type MintMinLiquidity = MintMinLiquidity;
type MultiAssetIdConverter = NativeOrAssetIdConverter<u32>;
#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -2561,15 +2563,16 @@ impl_runtime_apis! {
Block,
Balance,
u128,
NativeOrAssetId<u32>
NativeOrAssetId<u32>,
MaxSwapLength
> for Runtime
{
fn quote_price_exact_tokens_for_tokens(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_exact_tokens_for_tokens(asset1, asset2, amount, include_fee)
fn quote_price_exact_tokens_for_tokens(path: BoundedVec<NativeOrAssetId<u32>, MaxSwapLength>, amount: u128, include_fee: bool) -> Option<Balance> {
Copy link
Contributor

Choose a reason for hiding this comment

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

is there any reason to have this Vec bounded?
in this PR I actually simplifying the API, migrating from BoundedVec to Vec. the pallet still ensures it's not lager then max.
#1677

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We started with Vec originally and it got switched to BoundedVec.

Copy link
Member

Choose a reason for hiding this comment

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

Using BoundedVec in a runtime api makes no real sense?

AssetConversion::quote_price_exact_tokens_for_tokens(path, amount, include_fee)
}

fn quote_price_tokens_for_exact_tokens(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_tokens_for_exact_tokens(asset1, asset2, amount, include_fee)
fn quote_price_tokens_for_exact_tokens(path: BoundedVec<NativeOrAssetId<u32>, MaxSwapLength>, amount: u128, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_tokens_for_exact_tokens(path, amount, include_fee)
}

fn get_reserves(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>) -> Option<(Balance, Balance)> {
Expand Down
68 changes: 32 additions & 36 deletions substrate/frame/asset-conversion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use sp_runtime::{
traits::{
CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Ensure, MaybeDisplay, TrailingZeroInput,
},
DispatchError,
BoundedVec, DispatchError,
};
use sp_std::prelude::*;
pub use types::*;
Expand Down Expand Up @@ -1018,48 +1018,44 @@ pub mod pallet {

/// Used by the RPC service to provide current prices.
pub fn quote_price_exact_tokens_for_tokens(
asset1: T::MultiAssetId,
asset2: T::MultiAssetId,
amount: T::AssetBalance,
path: BoundedVec<T::MultiAssetId, T::MaxSwapPathLength>,
amount_in: T::AssetBalance,
include_fee: bool,
) -> Option<T::AssetBalance> {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_account = Self::get_pool_account(&pool_id);

let balance1 = Self::get_balance(&pool_account, &asset1).ok()?;
let balance2 = Self::get_balance(&pool_account, &asset2).ok()?;
if !balance1.is_zero() {
if include_fee {
Self::get_amount_out(&amount, &balance1, &balance2).ok()
} else {
Self::quote(&amount, &balance1, &balance2).ok()
}
Self::validate_swap_path(&path).ok()?;
Some(if include_fee {
*Self::get_amounts_out(&amount_in, &path).ok()?.last()?
} else {
None
}
let mut result = amount_in;
for assets_pair in path.windows(2).rev() {
if let [asset1, asset2] = assets_pair {
let (reserve_in, reserve_out) = Self::get_reserves(asset1, asset2).ok()?;
result = Self::quote(&reserve_out, &reserve_in, &result).ok()?;
}
}
result
})
}

/// Used by the RPC service to provide current prices.
pub fn quote_price_tokens_for_exact_tokens(
asset1: T::MultiAssetId,
asset2: T::MultiAssetId,
amount: T::AssetBalance,
path: BoundedVec<T::MultiAssetId, T::MaxSwapPathLength>,
amount_out: T::AssetBalance,
include_fee: bool,
) -> Option<T::AssetBalance> {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_account = Self::get_pool_account(&pool_id);

let balance1 = Self::get_balance(&pool_account, &asset1).ok()?;
let balance2 = Self::get_balance(&pool_account, &asset2).ok()?;
if !balance1.is_zero() {
if include_fee {
Self::get_amount_in(&amount, &balance1, &balance2).ok()
} else {
Self::quote(&amount, &balance2, &balance1).ok()
}
Self::validate_swap_path(&path).ok()?;
Some(if include_fee {
*Self::get_amounts_in(&amount_out, &path).ok()?.first()?
} else {
None
}
let mut result = amount_out;
for assets_pair in path.windows(2).rev() {
if let [asset1, asset2] = assets_pair {
let (reserve_in, reserve_out) = Self::get_reserves(asset1, asset2).ok()?;
result = Self::quote(&reserve_in, &reserve_out, &result).ok()?;
}
}
result
})
}

/// Calculates the optimal amount from the reserves.
Expand Down Expand Up @@ -1285,7 +1281,7 @@ impl<T: Config> Swap<T::AccountId, T::HigherPrecisionBalance, T::MultiAssetId> f
sp_api::decl_runtime_apis! {
/// This runtime api allows people to query the size of the liquidity pools
/// and quote prices for swaps.
pub trait AssetConversionApi<Balance, AssetBalance, AssetId> where
pub trait AssetConversionApi<Balance, AssetBalance, AssetId, MaxPathLength> where
Balance: Codec + MaybeDisplay,
AssetBalance: frame_support::traits::tokens::Balance,
AssetId: Codec
Expand All @@ -1294,13 +1290,13 @@ sp_api::decl_runtime_apis! {
///
/// Note that the price may have changed by the time the transaction is executed.
/// (Use `amount_in_max` to control slippage.)
fn quote_price_tokens_for_exact_tokens(asset1: AssetId, asset2: AssetId, amount: AssetBalance, include_fee: bool) -> Option<Balance>;
fn quote_price_tokens_for_exact_tokens(path: BoundedVec<AssetId, MaxPathLength>, amount: AssetBalance, include_fee: bool) -> Option<Balance>;

/// Provides a quote for [`Pallet::swap_exact_tokens_for_tokens`].
///
/// Note that the price may have changed by the time the transaction is executed.
/// (Use `amount_out_min` to control slippage.)
fn quote_price_exact_tokens_for_tokens(asset1: AssetId, asset2: AssetId, amount: AssetBalance, include_fee: bool) -> Option<Balance>;
fn quote_price_exact_tokens_for_tokens(path: BoundedVec<AssetId, MaxPathLength>, amount: AssetBalance, include_fee: bool) -> Option<Balance>;

/// Returns the size of the liquidity pool for the given asset pair.
fn get_reserves(asset1: AssetId, asset2: AssetId) -> Option<(Balance, Balance)>;
Expand Down
Loading
Loading