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
@@ -24,7 +24,7 @@ use frame_support::{
};
use pallet_asset_conversion::Swap;
use sp_runtime::{
traits::{DispatchInfoOf, PostDispatchInfoOf, Zero},
traits::{DispatchInfoOf, Get, PostDispatchInfoOf, Zero},
transaction_validity::InvalidTransaction,
Saturating,
};
@@ -76,16 +76,17 @@ pub trait OnChargeAssetTransaction<T: Config> {
/// Implements the asset transaction for a balance to asset converter (implementing [`Swap`]).
///
/// The converter is given the complete fee in terms of the asset used for the transaction.
pub struct AssetConversionAdapter<C, CON>(PhantomData<(C, CON)>);
pub struct AssetConversionAdapter<C, CON, N>(PhantomData<(C, CON, N)>);
/// Default implementation for a runtime instantiating this pallet, an asset to native swapper.
impl<T, C, CON> OnChargeAssetTransaction<T> for AssetConversionAdapter<C, CON>
impl<T, C, CON, N> OnChargeAssetTransaction<T> for AssetConversionAdapter<C, CON, N>
where
N: Get<CON::AssetKind>,
T: Config,
C: Inspect<<T as frame_system::Config>::AccountId>,
CON: Swap<T::AccountId, Balance = BalanceOf<T>, MultiAssetId = T::MultiAssetId>,
CON: Swap<T::AccountId, Balance = BalanceOf<T>, AssetKind = T::AssetKind>,
BalanceOf<T>: Into<AssetBalanceOf<T>>,
T::MultiAssetId: From<AssetIdOf<T>>,
T::AssetKind: From<AssetIdOf<T>>,
BalanceOf<T>: IsType<<C as Inspect<<T as frame_system::Config>::AccountId>>::Balance>,
{
type Balance = BalanceOf<T>;
@@ -116,7 +117,7 @@ where
let asset_consumed = CON::swap_tokens_for_exact_tokens(
who.clone(),
vec![asset_id.into(), T::MultiAssetIdConverter::get_native()],
vec![asset_id.into(), N::get()],
native_asset_required,
None,
who.clone(),
@@ -168,8 +169,8 @@ where
match CON::swap_exact_tokens_for_tokens(
who.clone(), // we already deposited the native to `who`
vec![
T::MultiAssetIdConverter::get_native(), // we provide the native
asset_id.into(), // we want asset_id back
N::get(), // we provide the native
asset_id.into(), // we want asset_id back
],
swap_back, // amount of the native asset to convert to `asset_id`
None, // no minimum amount back