pallet-asset-conversion: Decoupling Native Currency Dependancy (#2031)

closes https://github.com/paritytech/polkadot-sdk/issues/1842

Decoupling Pallet from the Concept of Native Currency

Currently, the pallet is intrinsically linked with the concept of native
currency, requiring users to provide implementations of the
`fungible::*` and `fungibles::*` traits to interact with native and non
native assets. This incapsulates some non-related to the pallet
complexity and makes it less adaptable in contexts where the native
currency concept is absent.

With this PR, the dependence on `fungible::*` for liquidity-supplying
assets has been removed. Instead, the native and non-native currencies'
handling is now overseen by a single type that implements the
`fungibles::*` traits. To simplify this integration, types have been
introduced to facilitate the creation of a union between `fungible::*`
and `fungibles::*` implementations, producing a unified `fungibles::*`
type.

One of the reasons driving these changes is the ambition to create a
more user-friendly API for the `SwapCredit` implementation. Given that
it interacts with two distinct credit types from `fungible` and
`fungibles`, a unified type was introduced. Clients now manage potential
conversion failures for those credit types. In certain contexts, it's
vital to guarantee that operations are fail-safe, like in this impl -
[PR](https://github.com/paritytech/polkadot-sdk/pull/1845), place in
[code](https://github.com/paritytech/polkadot-sdk/blob/20b85a5fada8f55c98ba831964f5866ffeadf4da/cumulus/primitives/utility/src/lib.rs#L429).

Additional Updates:
- abstracted the pool ID and its account derivation logic via trait
bounds, along with common implementation offerings;
- removed `inc_providers` on a pool creation for the pool account;
- benchmarks:
-- swap complexity is N, not const;
-- removed `From<u128> + Into<u128>` bound from `T::Balance`;
-- removed swap/liquidity/.. amount constants, resolve them dynamically
based on pallet configuration;
-- migrated to v2 API;
- `OnUnbalanced` handler for the pool creation fee, replacing direct
transfers to a specified account ID;
- renamed `MultiAssetId` to `AssetKind` aligning with naming across
frame crates;

related PRs:
- (depends) https://github.com/paritytech/polkadot-sdk/pull/1677
- (caused) https://github.com/paritytech/polkadot-sdk/pull/2033
- (caused) https://github.com/paritytech/polkadot-sdk/pull/1876

---------

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
This commit is contained in:
Muharem
2023-12-20 13:57:26 +01:00
committed by GitHub
parent d32f66fb8f
commit 4f832ea865
26 changed files with 1750 additions and 4571 deletions
+204 -332
View File
@@ -63,7 +63,8 @@ mod swap;
mod tests;
mod types;
pub mod weights;
#[cfg(feature = "runtime-benchmarks")]
pub use benchmarking::{BenchmarkHelper, NativeOrWithIdFactory};
pub use pallet::*;
pub use swap::*;
pub use types::*;
@@ -73,18 +74,14 @@ use codec::Codec;
use frame_support::{
storage::{with_storage_layer, with_transaction},
traits::{
fungible::{
Balanced as BalancedFungible, Credit as CreditFungible, Inspect as InspectFungible,
Mutate as MutateFungible,
},
fungibles::{Balanced, Create, Credit as CreditFungibles, Inspect, Mutate},
fungibles::{Balanced, Create, Credit, Inspect, Mutate},
tokens::{
AssetId, Balance,
Fortitude::Polite,
Precision::Exact,
Preservation::{Expendable, Preserve},
},
AccountTouch, ContainsPair, Imbalance, Incrementable,
AccountTouch, Incrementable, OnUnbalanced,
},
PalletId,
};
@@ -94,7 +91,7 @@ use sp_runtime::{
CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Ensure, IntegerSquareRoot, MaybeDisplay,
One, TrailingZeroInput, Zero,
},
DispatchError, RuntimeDebug, Saturating, TokenError, TransactionOutcome,
DispatchError, Saturating, TokenError, TransactionOutcome,
};
use sp_std::{boxed::Box, collections::btree_set::BTreeSet, vec::Vec};
@@ -113,11 +110,6 @@ pub mod pallet {
/// Overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Currency type that this works on.
type Currency: InspectFungible<Self::AccountId, Balance = Self::Balance>
+ MutateFungible<Self::AccountId>
+ BalancedFungible<Self::AccountId>;
/// The type in which the assets for swapping are measured.
type Balance: Balance;
@@ -130,37 +122,34 @@ pub mod pallet {
+ From<Self::Balance>
+ TryInto<Self::Balance>;
/// Identifier for the class of non-native asset.
/// Note: A `From<u32>` bound here would prevent `MultiLocation` from being used as an
/// `AssetId`.
type AssetId: AssetId;
/// Type of asset class, sourced from [`Config::Assets`], utilized to offer liquidity to a
/// pool.
type AssetKind: Parameter + MaxEncodedLen;
/// Type that identifies either the native currency or a token class from `Assets`.
/// `Ord` is added because of `get_pool_id`.
///
/// The pool's `AccountId` is derived from this type. Any changes to the type may
/// necessitate a migration.
type MultiAssetId: AssetId + Ord + From<Self::AssetId>;
/// Type to convert an `AssetId` into `MultiAssetId`.
type MultiAssetIdConverter: MultiAssetIdConverter<Self::MultiAssetId, Self::AssetId>;
/// `AssetId` to address the lp tokens by.
type PoolAssetId: AssetId + PartialOrd + Incrementable + From<u32>;
/// Registry for the assets.
type Assets: Inspect<Self::AccountId, AssetId = Self::AssetId, Balance = Self::Balance>
/// Registry of assets utilized for providing liquidity to pools.
type Assets: Inspect<Self::AccountId, AssetId = Self::AssetKind, Balance = Self::Balance>
+ Mutate<Self::AccountId>
+ AccountTouch<Self::AssetId, Self::AccountId>
+ ContainsPair<Self::AssetId, Self::AccountId>
+ AccountTouch<Self::AssetKind, Self::AccountId, Balance = Self::Balance>
+ Balanced<Self::AccountId>;
/// Liquidity pool identifier.
type PoolId: Parameter + MaxEncodedLen + Ord;
/// Provides means to resolve the [`Config::PoolId`] and it's `AccountId` from a pair
/// of [`Config::AssetKind`]s.
///
/// Examples: [`crate::types::WithFirstAsset`], [`crate::types::Ascending`].
type PoolLocator: PoolLocator<Self::AccountId, Self::AssetKind, Self::PoolId>;
/// Asset class for the lp tokens from [`Self::PoolAssets`].
type PoolAssetId: AssetId + PartialOrd + Incrementable + From<u32>;
/// Registry for the lp tokens. Ideally only this pallet should have create permissions on
/// the assets.
type PoolAssets: Inspect<Self::AccountId, AssetId = Self::PoolAssetId, Balance = Self::Balance>
+ Create<Self::AccountId>
+ Mutate<Self::AccountId>
+ AccountTouch<Self::PoolAssetId, Self::AccountId>;
+ AccountTouch<Self::PoolAssetId, Self::AccountId, Balance = Self::Balance>;
/// A % the liquidity providers will take of every swap. Represents 10ths of a percent.
#[pallet::constant]
@@ -170,8 +159,12 @@ pub mod pallet {
#[pallet::constant]
type PoolSetupFee: Get<Self::Balance>;
/// An account that receives the pool setup fee.
type PoolSetupFeeReceiver: Get<Self::AccountId>;
/// Asset class from [`Config::Assets`] used to pay the [`Config::PoolSetupFee`].
#[pallet::constant]
type PoolSetupFeeAsset: Get<Self::AssetKind>;
/// Handler for the [`Config::PoolSetupFee`].
type PoolSetupFeeTarget: OnUnbalanced<CreditOf<Self>>;
/// A fee to withdraw the liquidity.
#[pallet::constant]
@@ -189,23 +182,19 @@ pub mod pallet {
#[pallet::constant]
type PalletId: Get<PalletId>;
/// A setting to allow creating pools with both non-native assets.
#[pallet::constant]
type AllowMultiAssetPools: Get<bool>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
/// The benchmarks need a way to create asset ids from u32s.
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: BenchmarkHelper<Self::AssetId, Self::MultiAssetId>;
type BenchmarkHelper: BenchmarkHelper<Self::AssetKind>;
}
/// Map from `PoolAssetId` to `PoolInfo`. This establishes whether a pool has been officially
/// created rather than people sending tokens directly to a pool's public account.
#[pallet::storage]
pub type Pools<T: Config> =
StorageMap<_, Blake2_128Concat, PoolIdOf<T>, PoolInfo<T::PoolAssetId>, OptionQuery>;
StorageMap<_, Blake2_128Concat, T::PoolId, PoolInfo<T::PoolAssetId>, OptionQuery>;
/// Stores the `PoolAssetId` that is going to be used for the next lp token.
/// This gets incremented whenever a new lp pool is created.
@@ -222,7 +211,7 @@ pub mod pallet {
creator: T::AccountId,
/// The pool id associated with the pool. Note that the order of the assets may not be
/// the same as the order specified in the create pool extrinsic.
pool_id: PoolIdOf<T>,
pool_id: T::PoolId,
/// The account ID of the pool.
pool_account: T::AccountId,
/// The id of the liquidity tokens that will be minted when assets are added to this
@@ -237,7 +226,7 @@ pub mod pallet {
/// The account that the liquidity tokens were minted to.
mint_to: T::AccountId,
/// The pool id of the pool that the liquidity was added to.
pool_id: PoolIdOf<T>,
pool_id: T::PoolId,
/// The amount of the first asset that was added to the pool.
amount1_provided: T::Balance,
/// The amount of the second asset that was added to the pool.
@@ -255,7 +244,7 @@ pub mod pallet {
/// The account that the assets were transferred to.
withdraw_to: T::AccountId,
/// The pool id that the liquidity was removed from.
pool_id: PoolIdOf<T>,
pool_id: T::PoolId,
/// The amount of the first asset that was removed from the pool.
amount1: T::Balance,
/// The amount of the second asset that was removed from the pool.
@@ -296,10 +285,8 @@ pub mod pallet {
#[pallet::error]
pub enum Error<T> {
/// Provided assets are equal.
EqualAssets,
/// Provided asset is not supported for pool.
UnsupportedAsset,
/// Provided asset pair is not supported for pool.
InvalidAssetPair,
/// Pool already exists.
PoolExists,
/// Desired amount can't be zero.
@@ -335,18 +322,12 @@ pub mod pallet {
ZeroLiquidity,
/// Amount can't be zero.
ZeroAmount,
/// Insufficient liquidity in the pool.
InsufficientLiquidity,
/// Calculated amount out is less than provided minimum amount.
ProvidedMinimumNotSufficientForSwap,
/// Provided maximum amount is not sufficient for swap.
ProvidedMaximumNotSufficientForSwap,
/// Only pools with native on one side are valid.
PoolMustContainNativeCurrency,
/// The provided path must consists of 2 assets at least.
InvalidPath,
/// It was not possible to calculate path data.
PathError,
/// The provided path must consists of unique assets.
NonUniquePath,
/// It was not possible to get or increment the Id of the pool.
@@ -376,48 +357,32 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::create_pool())]
pub fn create_pool(
origin: OriginFor<T>,
asset1: Box<T::MultiAssetId>,
asset2: Box<T::MultiAssetId>,
asset1: Box<T::AssetKind>,
asset2: Box<T::AssetKind>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(asset1 != asset2, Error::<T>::EqualAssets);
ensure!(asset1 != asset2, Error::<T>::InvalidAssetPair);
// prepare pool_id
let pool_id = Self::get_pool_id(*asset1, *asset2);
let pool_id = T::PoolLocator::pool_id(&asset1, &asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
ensure!(!Pools::<T>::contains_key(&pool_id), Error::<T>::PoolExists);
let (asset1, asset2) = &pool_id;
if !T::AllowMultiAssetPools::get() && !T::MultiAssetIdConverter::is_native(asset1) {
Err(Error::<T>::PoolMustContainNativeCurrency)?;
}
let pool_account = Self::get_pool_account(&pool_id);
frame_system::Pallet::<T>::inc_providers(&pool_account);
let pool_account =
T::PoolLocator::address(&pool_id).map_err(|_| Error::<T>::InvalidAssetPair)?;
// pay the setup fee
T::Currency::transfer(
&sender,
&T::PoolSetupFeeReceiver::get(),
T::PoolSetupFee::get(),
Preserve,
)?;
let fee =
Self::withdraw(T::PoolSetupFeeAsset::get(), &sender, T::PoolSetupFee::get(), true)?;
T::PoolSetupFeeTarget::on_unbalanced(fee);
// try to convert both assets
match T::MultiAssetIdConverter::try_convert(asset1) {
MultiAssetIdConversionResult::Converted(asset) =>
if !T::Assets::contains(&asset, &pool_account) {
T::Assets::touch(asset, &pool_account, &sender)?
},
MultiAssetIdConversionResult::Unsupported(_) => Err(Error::<T>::UnsupportedAsset)?,
MultiAssetIdConversionResult::Native => (),
}
match T::MultiAssetIdConverter::try_convert(asset2) {
MultiAssetIdConversionResult::Converted(asset) =>
if !T::Assets::contains(&asset, &pool_account) {
T::Assets::touch(asset, &pool_account, &sender)?
},
MultiAssetIdConversionResult::Unsupported(_) => Err(Error::<T>::UnsupportedAsset)?,
MultiAssetIdConversionResult::Native => (),
}
if T::Assets::should_touch(*asset1.clone(), &pool_account) {
T::Assets::touch(*asset1, &pool_account, &sender)?
};
if T::Assets::should_touch(*asset2.clone(), &pool_account) {
T::Assets::touch(*asset2, &pool_account, &sender)?
};
let lp_token = NextPoolAssetId::<T>::get()
.or(T::PoolAssetId::initial_value())
@@ -454,8 +419,8 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::add_liquidity())]
pub fn add_liquidity(
origin: OriginFor<T>,
asset1: Box<T::MultiAssetId>,
asset2: Box<T::MultiAssetId>,
asset1: Box<T::AssetKind>,
asset2: Box<T::AssetKind>,
amount1_desired: T::Balance,
amount2_desired: T::Balance,
amount1_min: T::Balance,
@@ -464,26 +429,20 @@ pub mod pallet {
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let pool_id = Self::get_pool_id(*asset1.clone(), *asset2);
// swap params if needed
let (amount1_desired, amount2_desired, amount1_min, amount2_min) =
if pool_id.0 == *asset1 {
(amount1_desired, amount2_desired, amount1_min, amount2_min)
} else {
(amount2_desired, amount1_desired, amount2_min, amount1_min)
};
let pool_id = T::PoolLocator::pool_id(&asset1, &asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
ensure!(
amount1_desired > Zero::zero() && amount2_desired > Zero::zero(),
Error::<T>::WrongDesiredAmount
);
let maybe_pool = Pools::<T>::get(&pool_id);
let pool = maybe_pool.as_ref().ok_or(Error::<T>::PoolNotFound)?;
let pool_account = Self::get_pool_account(&pool_id);
let pool = Pools::<T>::get(&pool_id).ok_or(Error::<T>::PoolNotFound)?;
let pool_account =
T::PoolLocator::address(&pool_id).map_err(|_| Error::<T>::InvalidAssetPair)?;
let (asset1, asset2) = &pool_id;
let reserve1 = Self::get_balance(&pool_account, asset1)?;
let reserve2 = Self::get_balance(&pool_account, asset2)?;
let reserve1 = Self::get_balance(&pool_account, *asset1.clone());
let reserve2 = Self::get_balance(&pool_account, *asset2.clone());
let amount1: T::Balance;
let amount2: T::Balance;
@@ -515,13 +474,17 @@ pub mod pallet {
}
}
Self::validate_minimal_amount(amount1.saturating_add(reserve1), asset1)
.map_err(|_| Error::<T>::AmountOneLessThanMinimal)?;
Self::validate_minimal_amount(amount2.saturating_add(reserve2), asset2)
.map_err(|_| Error::<T>::AmountTwoLessThanMinimal)?;
ensure!(
amount1.saturating_add(reserve1) >= T::Assets::minimum_balance(*asset1.clone()),
Error::<T>::AmountOneLessThanMinimal
);
ensure!(
amount2.saturating_add(reserve2) >= T::Assets::minimum_balance(*asset2.clone()),
Error::<T>::AmountTwoLessThanMinimal
);
Self::transfer(asset1, &sender, &pool_account, amount1, true)?;
Self::transfer(asset2, &sender, &pool_account, amount2, true)?;
T::Assets::transfer(*asset1, &sender, &pool_account, amount1, Preserve)?;
T::Assets::transfer(*asset2, &sender, &pool_account, amount2, Preserve)?;
let total_supply = T::PoolAssets::total_issuance(pool.lp_token.clone());
@@ -552,7 +515,7 @@ pub mod pallet {
pool_id,
amount1_provided: amount1,
amount2_provided: amount2,
lp_token: pool.lp_token.clone(),
lp_token: pool.lp_token,
lp_token_minted: lp_token_amount,
});
@@ -566,8 +529,8 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::remove_liquidity())]
pub fn remove_liquidity(
origin: OriginFor<T>,
asset1: Box<T::MultiAssetId>,
asset2: Box<T::MultiAssetId>,
asset1: Box<T::AssetKind>,
asset2: Box<T::AssetKind>,
lp_token_burn: T::Balance,
amount1_min_receive: T::Balance,
amount2_min_receive: T::Balance,
@@ -575,23 +538,17 @@ pub mod pallet {
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let pool_id = Self::get_pool_id(*asset1.clone(), *asset2);
// swap params if needed
let (amount1_min_receive, amount2_min_receive) = if pool_id.0 == *asset1 {
(amount1_min_receive, amount2_min_receive)
} else {
(amount2_min_receive, amount1_min_receive)
};
let (asset1, asset2) = pool_id.clone();
let pool_id = T::PoolLocator::pool_id(&asset1, &asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
ensure!(lp_token_burn > Zero::zero(), Error::<T>::ZeroLiquidity);
let maybe_pool = Pools::<T>::get(&pool_id);
let pool = maybe_pool.as_ref().ok_or(Error::<T>::PoolNotFound)?;
let pool = Pools::<T>::get(&pool_id).ok_or(Error::<T>::PoolNotFound)?;
let pool_account = Self::get_pool_account(&pool_id);
let reserve1 = Self::get_balance(&pool_account, &asset1)?;
let reserve2 = Self::get_balance(&pool_account, &asset2)?;
let pool_account =
T::PoolLocator::address(&pool_id).map_err(|_| Error::<T>::InvalidAssetPair)?;
let reserve1 = Self::get_balance(&pool_account, *asset1.clone());
let reserve2 = Self::get_balance(&pool_account, *asset2.clone());
let total_supply = T::PoolAssets::total_issuance(pool.lp_token.clone());
let withdrawal_fee_amount = T::LiquidityWithdrawalFee::get() * lp_token_burn;
@@ -610,16 +567,20 @@ pub mod pallet {
);
let reserve1_left = reserve1.saturating_sub(amount1);
let reserve2_left = reserve2.saturating_sub(amount2);
Self::validate_minimal_amount(reserve1_left, &asset1)
.map_err(|_| Error::<T>::ReserveLeftLessThanMinimal)?;
Self::validate_minimal_amount(reserve2_left, &asset2)
.map_err(|_| Error::<T>::ReserveLeftLessThanMinimal)?;
ensure!(
reserve1_left >= T::Assets::minimum_balance(*asset1.clone()),
Error::<T>::ReserveLeftLessThanMinimal
);
ensure!(
reserve2_left >= T::Assets::minimum_balance(*asset2.clone()),
Error::<T>::ReserveLeftLessThanMinimal
);
// burn the provided lp token amount that includes the fee
T::PoolAssets::burn_from(pool.lp_token.clone(), &sender, lp_token_burn, Exact, Polite)?;
Self::transfer(&asset1, &pool_account, &withdraw_to, amount1, false)?;
Self::transfer(&asset2, &pool_account, &withdraw_to, amount2, false)?;
T::Assets::transfer(*asset1, &pool_account, &withdraw_to, amount1, Expendable)?;
T::Assets::transfer(*asset2, &pool_account, &withdraw_to, amount2, Expendable)?;
Self::deposit_event(Event::LiquidityRemoved {
who: sender,
@@ -627,7 +588,7 @@ pub mod pallet {
pool_id,
amount1,
amount2,
lp_token: pool.lp_token.clone(),
lp_token: pool.lp_token,
lp_token_burned: lp_token_burn,
withdrawal_fee: T::LiquidityWithdrawalFee::get(),
});
@@ -642,10 +603,10 @@ pub mod pallet {
/// [`AssetConversionApi::quote_price_exact_tokens_for_tokens`] runtime call can be called
/// for a quote.
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::swap_exact_tokens_for_tokens())]
#[pallet::weight(T::WeightInfo::swap_exact_tokens_for_tokens(path.len() as u32))]
pub fn swap_exact_tokens_for_tokens(
origin: OriginFor<T>,
path: Vec<Box<T::MultiAssetId>>,
path: Vec<Box<T::AssetKind>>,
amount_in: T::Balance,
amount_out_min: T::Balance,
send_to: T::AccountId,
@@ -670,10 +631,10 @@ pub mod pallet {
/// [`AssetConversionApi::quote_price_tokens_for_exact_tokens`] runtime call can be called
/// for a quote.
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::swap_tokens_for_exact_tokens())]
#[pallet::weight(T::WeightInfo::swap_tokens_for_exact_tokens(path.len() as u32))]
pub fn swap_tokens_for_exact_tokens(
origin: OriginFor<T>,
path: Vec<Box<T::MultiAssetId>>,
path: Vec<Box<T::AssetKind>>,
amount_out: T::Balance,
amount_in_max: T::Balance,
send_to: T::AccountId,
@@ -707,7 +668,7 @@ pub mod pallet {
/// rollback.
pub(crate) fn do_swap_exact_tokens_for_tokens(
sender: T::AccountId,
path: Vec<T::MultiAssetId>,
path: Vec<T::AssetKind>,
amount_in: T::Balance,
amount_out_min: Option<T::Balance>,
send_to: T::AccountId,
@@ -755,7 +716,7 @@ pub mod pallet {
/// rollback.
pub(crate) fn do_swap_tokens_for_exact_tokens(
sender: T::AccountId,
path: Vec<T::MultiAssetId>,
path: Vec<T::AssetKind>,
amount_out: T::Balance,
amount_in_max: Option<T::Balance>,
send_to: T::AccountId,
@@ -801,13 +762,16 @@ pub mod pallet {
/// only inside a transactional storage context and an Err result must imply a storage
/// rollback.
pub(crate) fn do_swap_exact_credit_tokens_for_tokens(
path: Vec<T::MultiAssetId>,
credit_in: Credit<T>,
path: Vec<T::AssetKind>,
credit_in: CreditOf<T>,
amount_out_min: Option<T::Balance>,
) -> Result<Credit<T>, (Credit<T>, DispatchError)> {
) -> Result<CreditOf<T>, (CreditOf<T>, DispatchError)> {
let amount_in = credit_in.peek();
let inspect_path = |credit_asset| {
ensure!(path.get(0).map_or(false, |a| *a == credit_asset), Error::<T>::InvalidPath);
ensure!(
path.first().map_or(false, |a| *a == credit_asset),
Error::<T>::InvalidPath
);
ensure!(!amount_in.is_zero(), Error::<T>::ZeroAmount);
ensure!(amount_out_min.map_or(true, |a| !a.is_zero()), Error::<T>::ZeroAmount);
@@ -846,13 +810,16 @@ pub mod pallet {
/// only inside a transactional storage context and an Err result must imply a storage
/// rollback.
pub(crate) fn do_swap_credit_tokens_for_exact_tokens(
path: Vec<T::MultiAssetId>,
credit_in: Credit<T>,
path: Vec<T::AssetKind>,
credit_in: CreditOf<T>,
amount_out: T::Balance,
) -> Result<(Credit<T>, Credit<T>), (Credit<T>, DispatchError)> {
) -> Result<(CreditOf<T>, CreditOf<T>), (CreditOf<T>, DispatchError)> {
let amount_in_max = credit_in.peek();
let inspect_path = |credit_asset| {
ensure!(path.get(0).map_or(false, |a| a == &credit_asset), Error::<T>::InvalidPath);
ensure!(
path.first().map_or(false, |a| a == &credit_asset),
Error::<T>::InvalidPath
);
ensure!(amount_in_max > Zero::zero(), Error::<T>::ZeroAmount);
ensure!(amount_out > Zero::zero(), Error::<T>::ZeroAmount);
@@ -880,75 +847,6 @@ pub mod pallet {
Ok((credit_out, credit_change))
}
/// Transfer an `amount` of `asset_id`, respecting the `keep_alive` requirements.
fn transfer(
asset_id: &T::MultiAssetId,
from: &T::AccountId,
to: &T::AccountId,
amount: T::Balance,
keep_alive: bool,
) -> Result<T::Balance, DispatchError> {
let preservation = match keep_alive {
true => Preserve,
false => Expendable,
};
match T::MultiAssetIdConverter::try_convert(asset_id) {
MultiAssetIdConversionResult::Converted(asset_id) =>
T::Assets::transfer(asset_id, from, to, amount, preservation),
MultiAssetIdConversionResult::Native =>
Ok(T::Currency::transfer(from, to, amount, preservation)?),
MultiAssetIdConversionResult::Unsupported(_) =>
Err(Error::<T>::UnsupportedAsset.into()),
}
}
/// The balance of `who` is increased in order to counter `credit`. If the whole of `credit`
/// cannot be countered, then nothing is changed and the original `credit` is returned in an
/// `Err`.
fn resolve(who: &T::AccountId, credit: Credit<T>) -> Result<(), Credit<T>> {
match credit {
Credit::Native(c) => T::Currency::resolve(who, c).map_err(|c| c.into()),
Credit::Asset(c) => T::Assets::resolve(who, c).map_err(|c| c.into()),
}
}
/// Removes `value` balance of `asset` from `who` account if possible.
fn withdraw(
asset: &T::MultiAssetId,
who: &T::AccountId,
value: T::Balance,
keep_alive: bool,
) -> Result<Credit<T>, DispatchError> {
let preservation = match keep_alive {
true => Preserve,
false => Expendable,
};
match T::MultiAssetIdConverter::try_convert(asset) {
MultiAssetIdConversionResult::Converted(asset) => {
if preservation == Preserve {
// TODO drop the ensure! when this issue addressed
// https://github.com/paritytech/polkadot-sdk/issues/1698
let free =
T::Assets::reducible_balance(asset.clone(), who, preservation, Polite);
ensure!(free >= value, TokenError::NotExpendable);
}
T::Assets::withdraw(asset, who, value, Exact, preservation, Polite)
.map(|c| c.into())
},
MultiAssetIdConversionResult::Native => {
if preservation == Preserve {
// TODO drop the ensure! when this issue addressed
// https://github.com/paritytech/polkadot-sdk/issues/1698
let free = T::Currency::reducible_balance(who, preservation, Polite);
ensure!(free >= value, TokenError::NotExpendable);
}
T::Currency::withdraw(who, value, Exact, preservation, Polite).map(|c| c.into())
},
MultiAssetIdConversionResult::Unsupported(_) =>
Err(Error::<T>::UnsupportedAsset.into()),
}
}
/// Swap assets along the `path`, withdrawing from `sender` and depositing in `send_to`.
///
/// Note: It's assumed that the provided `path` is valid.
@@ -963,10 +861,10 @@ pub mod pallet {
keep_alive: bool,
) -> Result<(), DispatchError> {
let (asset_in, amount_in) = path.first().ok_or(Error::<T>::InvalidPath)?;
let credit_in = Self::withdraw(asset_in, sender, *amount_in, keep_alive)?;
let credit_in = Self::withdraw(asset_in.clone(), sender, *amount_in, keep_alive)?;
let credit_out = Self::credit_swap(credit_in, path).map_err(|(_, e)| e)?;
Self::resolve(send_to, credit_out).map_err(|_| Error::<T>::BelowMinimum)?;
T::Assets::resolve(send_to, credit_out).map_err(|_| Error::<T>::BelowMinimum)?;
Ok(())
}
@@ -983,29 +881,34 @@ pub mod pallet {
/// only inside a transactional storage context and an Err result must imply a storage
/// rollback.
fn credit_swap(
credit_in: Credit<T>,
credit_in: CreditOf<T>,
path: &BalancePath<T>,
) -> Result<Credit<T>, (Credit<T>, DispatchError)> {
let resolve_path = || -> Result<Credit<T>, DispatchError> {
) -> Result<CreditOf<T>, (CreditOf<T>, DispatchError)> {
let resolve_path = || -> Result<CreditOf<T>, DispatchError> {
for pos in 0..=path.len() {
if let Some([(asset1, _), (asset2, amount_out)]) = path.get(pos..=pos + 1) {
let pool_from = Self::get_pool_account(&Self::get_pool_id(
asset1.clone(),
asset2.clone(),
));
let pool_from = T::PoolLocator::pool_address(asset1, asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
if let Some((asset3, _)) = path.get(pos + 2) {
let pool_to = Self::get_pool_account(&Self::get_pool_id(
let pool_to = T::PoolLocator::pool_address(asset2, asset3)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
T::Assets::transfer(
asset2.clone(),
asset3.clone(),
));
Self::transfer(asset2, &pool_from, &pool_to, *amount_out, true)?;
&pool_from,
&pool_to,
*amount_out,
Preserve,
)?;
} else {
let credit_out = Self::withdraw(asset2, &pool_from, *amount_out, true)?;
let credit_out =
Self::withdraw(asset2.clone(), &pool_from, *amount_out, true)?;
return Ok(credit_out)
}
}
}
return Err(Error::<T>::InvalidPath.into())
Err(Error::<T>::InvalidPath.into())
};
let credit_out = match resolve_path() {
@@ -1014,79 +917,57 @@ pub mod pallet {
};
let pool_to = if let Some([(asset1, _), (asset2, _)]) = path.get(0..2) {
Self::get_pool_account(&Self::get_pool_id(asset1.clone(), asset2.clone()))
match T::PoolLocator::pool_address(asset1, asset2) {
Ok(address) => address,
Err(_) => return Err((credit_in, Error::<T>::InvalidAssetPair.into())),
}
} else {
return Err((credit_in, Error::<T>::InvalidPath.into()))
};
Self::resolve(&pool_to, credit_in).map_err(|c| (c, Error::<T>::BelowMinimum.into()))?;
T::Assets::resolve(&pool_to, credit_in)
.map_err(|c| (c, Error::<T>::BelowMinimum.into()))?;
Ok(credit_out)
}
/// The account ID of the pool.
///
/// This actually does computation. If you need to keep using it, then make sure you cache
/// the value and only call this once.
pub fn get_pool_account(pool_id: &PoolIdOf<T>) -> T::AccountId {
let encoded_pool_id = sp_io::hashing::blake2_256(&Encode::encode(pool_id)[..]);
Decode::decode(&mut TrailingZeroInput::new(encoded_pool_id.as_ref()))
.expect("infinite length input; no invalid inputs for type; qed")
/// Removes `value` balance of `asset` from `who` account if possible.
fn withdraw(
asset: T::AssetKind,
who: &T::AccountId,
value: T::Balance,
keep_alive: bool,
) -> Result<CreditOf<T>, DispatchError> {
let preservation = match keep_alive {
true => Preserve,
false => Expendable,
};
if preservation == Preserve {
// TODO drop the ensure! when this issue addressed
// https://github.com/paritytech/polkadot-sdk/issues/1698
let free = T::Assets::reducible_balance(asset.clone(), who, preservation, Polite);
ensure!(free >= value, TokenError::NotExpendable);
}
T::Assets::withdraw(asset, who, value, Exact, preservation, Polite)
}
/// Get the `owner`'s balance of `asset`, which could be the chain's native asset or another
/// fungible. Returns a value in the form of an `Balance`.
fn get_balance(
owner: &T::AccountId,
asset: &T::MultiAssetId,
) -> Result<T::Balance, Error<T>> {
match T::MultiAssetIdConverter::try_convert(asset) {
MultiAssetIdConversionResult::Converted(asset_id) => Ok(
<<T as Config>::Assets>::reducible_balance(asset_id, owner, Expendable, Polite),
),
MultiAssetIdConversionResult::Native =>
Ok(<<T as Config>::Currency>::reducible_balance(owner, Expendable, Polite)),
MultiAssetIdConversionResult::Unsupported(_) =>
Err(Error::<T>::UnsupportedAsset.into()),
}
}
/// Returns a pool id constructed from 2 assets.
/// 1. Native asset should be lower than the other asset ids.
/// 2. Two native or two non-native assets are compared by their `Ord` implementation.
///
/// We expect deterministic order, so (asset1, asset2) or (asset2, asset1) returns the same
/// result.
pub fn get_pool_id(asset1: T::MultiAssetId, asset2: T::MultiAssetId) -> PoolIdOf<T> {
match (
T::MultiAssetIdConverter::is_native(&asset1),
T::MultiAssetIdConverter::is_native(&asset2),
) {
(true, false) => return (asset1, asset2),
(false, true) => return (asset2, asset1),
_ => {
// else we want to be deterministic based on `Ord` implementation
if asset1 <= asset2 {
(asset1, asset2)
} else {
(asset2, asset1)
}
},
}
fn get_balance(owner: &T::AccountId, asset: T::AssetKind) -> T::Balance {
T::Assets::reducible_balance(asset, owner, Expendable, Polite)
}
/// Returns the balance of each asset in the pool.
/// The tuple result is in the order requested (not necessarily the same as pool order).
pub fn get_reserves(
asset1: &T::MultiAssetId,
asset2: &T::MultiAssetId,
asset1: T::AssetKind,
asset2: T::AssetKind,
) -> Result<(T::Balance, T::Balance), Error<T>> {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_account = Self::get_pool_account(&pool_id);
let pool_account = T::PoolLocator::pool_address(&asset1, &asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
let balance1 = Self::get_balance(&pool_account, asset1)?;
let balance2 = Self::get_balance(&pool_account, asset2)?;
let balance1 = Self::get_balance(&pool_account, asset1);
let balance2 = Self::get_balance(&pool_account, asset2);
if balance1.is_zero() || balance2.is_zero() {
Err(Error::<T>::PoolNotFound)?;
@@ -1098,7 +979,7 @@ pub mod pallet {
/// Leading to an amount at the end of a `path`, get the required amounts in.
pub(crate) fn balance_path_from_amount_out(
amount_out: T::Balance,
path: Vec<T::MultiAssetId>,
path: Vec<T::AssetKind>,
) -> Result<BalancePath<T>, DispatchError> {
let mut balance_path: BalancePath<T> = Vec::with_capacity(path.len());
let mut amount_in: T::Balance = amount_out;
@@ -1112,7 +993,7 @@ pub mod pallet {
break
},
};
let (reserve_in, reserve_out) = Self::get_reserves(asset1, &asset2)?;
let (reserve_in, reserve_out) = Self::get_reserves(asset1.clone(), asset2.clone())?;
balance_path.push((asset2, amount_in));
amount_in = Self::get_amount_in(&amount_in, &reserve_in, &reserve_out)?;
}
@@ -1124,7 +1005,7 @@ pub mod pallet {
/// Following an amount into a `path`, get the corresponding amounts out.
pub(crate) fn balance_path_from_amount_in(
amount_in: T::Balance,
path: Vec<T::MultiAssetId>,
path: Vec<T::AssetKind>,
) -> Result<BalancePath<T>, DispatchError> {
let mut balance_path: BalancePath<T> = Vec::with_capacity(path.len());
let mut amount_out: T::Balance = amount_in;
@@ -1138,7 +1019,7 @@ pub mod pallet {
break
},
};
let (reserve_in, reserve_out) = Self::get_reserves(&asset1, asset2)?;
let (reserve_in, reserve_out) = Self::get_reserves(asset1.clone(), asset2.clone())?;
balance_path.push((asset1, amount_out));
amount_out = Self::get_amount_out(&amount_out, &reserve_in, &reserve_out)?;
}
@@ -1147,16 +1028,15 @@ 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,
asset1: T::AssetKind,
asset2: T::AssetKind,
amount: T::Balance,
include_fee: bool,
) -> Option<T::Balance> {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_account = Self::get_pool_account(&pool_id);
let pool_account = T::PoolLocator::pool_address(&asset1, &asset2).ok()?;
let balance1 = Self::get_balance(&pool_account, &asset1).ok()?;
let balance2 = Self::get_balance(&pool_account, &asset2).ok()?;
let balance1 = Self::get_balance(&pool_account, asset1);
let balance2 = Self::get_balance(&pool_account, asset2);
if !balance1.is_zero() {
if include_fee {
Self::get_amount_out(&amount, &balance1, &balance2).ok()
@@ -1170,16 +1050,15 @@ pub mod pallet {
/// Used by the RPC service to provide current prices.
pub fn quote_price_tokens_for_exact_tokens(
asset1: T::MultiAssetId,
asset2: T::MultiAssetId,
asset1: T::AssetKind,
asset2: T::AssetKind,
amount: T::Balance,
include_fee: bool,
) -> Option<T::Balance> {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_account = Self::get_pool_account(&pool_id);
let pool_account = T::PoolLocator::pool_address(&asset1, &asset2).ok()?;
let balance1 = Self::get_balance(&pool_account, &asset1).ok()?;
let balance2 = Self::get_balance(&pool_account, &asset2).ok()?;
let balance1 = Self::get_balance(&pool_account, asset1);
let balance2 = Self::get_balance(&pool_account, asset2);
if !balance1.is_zero() {
if include_fee {
Self::get_amount_in(&amount, &balance1, &balance2).ok()
@@ -1246,7 +1125,7 @@ pub mod pallet {
let reserve_out = T::HigherPrecisionBalance::from(*reserve_out);
if reserve_in.is_zero() || reserve_out.is_zero() {
return Err(Error::<T>::ZeroLiquidity.into())
return Err(Error::<T>::ZeroLiquidity)
}
let amount_in_with_fee = amount_in
@@ -1281,11 +1160,11 @@ pub mod pallet {
let reserve_out = T::HigherPrecisionBalance::from(*reserve_out);
if reserve_in.is_zero() || reserve_out.is_zero() {
Err(Error::<T>::ZeroLiquidity.into())?
Err(Error::<T>::ZeroLiquidity)?
}
if amount_out >= reserve_out {
Err(Error::<T>::AmountOutTooHigh.into())?
Err(Error::<T>::AmountOutTooHigh)?
}
let numerator = reserve_in
@@ -1309,36 +1188,18 @@ pub mod pallet {
result.try_into().map_err(|_| Error::<T>::Overflow)
}
/// Ensure that a `value` meets the minimum balance requirements of an `asset` class.
fn validate_minimal_amount(value: T::Balance, asset: &T::MultiAssetId) -> Result<(), ()> {
if T::MultiAssetIdConverter::is_native(asset) {
let ed = T::Currency::minimum_balance();
ensure!(
T::HigherPrecisionBalance::from(value) >= T::HigherPrecisionBalance::from(ed),
()
);
} else {
let MultiAssetIdConversionResult::Converted(asset_id) =
T::MultiAssetIdConverter::try_convert(asset)
else {
return Err(())
};
let minimal = T::Assets::minimum_balance(asset_id);
ensure!(value >= minimal, ());
}
Ok(())
}
/// Ensure that a path is valid.
fn validate_swap_path(path: &Vec<T::MultiAssetId>) -> Result<(), DispatchError> {
fn validate_swap_path(path: &Vec<T::AssetKind>) -> Result<(), DispatchError> {
ensure!(path.len() >= 2, Error::<T>::InvalidPath);
ensure!(path.len() as u32 <= T::MaxSwapPathLength::get(), Error::<T>::InvalidPath);
// validate all the pools in the path are unique
let mut pools = BTreeSet::<PoolIdOf<T>>::new();
let mut pools = BTreeSet::<T::PoolId>::new();
for assets_pair in path.windows(2) {
if let [asset1, asset2] = assets_pair {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_id = T::PoolLocator::pool_id(asset1, asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
let new_element = pools.insert(pool_id);
if !new_element {
return Err(Error::<T>::NonUniquePath.into())
@@ -1361,21 +1222,32 @@ pub mod pallet {
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, AssetId> where
pub trait AssetConversionApi<Balance, AssetId>
where
Balance: frame_support::traits::tokens::Balance + MaybeDisplay,
AssetId: Codec
AssetId: Codec,
{
/// Provides a quote for [`Pallet::swap_tokens_for_exact_tokens`].
///
/// 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: Balance, include_fee: bool) -> Option<Balance>;
fn quote_price_tokens_for_exact_tokens(
asset1: AssetId,
asset2: AssetId,
amount: Balance,
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: Balance, include_fee: bool) -> Option<Balance>;
fn quote_price_exact_tokens_for_tokens(
asset1: AssetId,
asset2: AssetId,
amount: Balance,
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)>;