mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 04:41:02 +00:00
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:
@@ -18,73 +18,142 @@
|
||||
//! Asset Conversion pallet benchmarking.
|
||||
|
||||
use super::*;
|
||||
use frame_benchmarking::{benchmarks, whitelisted_caller};
|
||||
use crate::Pallet as AssetConversion;
|
||||
use frame_benchmarking::{v2::*, whitelisted_caller};
|
||||
use frame_support::{
|
||||
assert_ok,
|
||||
traits::{
|
||||
fungible::{Inspect as InspectFungible, Mutate as MutateFungible, Unbalanced},
|
||||
fungible::NativeOrWithId,
|
||||
fungibles::{Create, Inspect, Mutate},
|
||||
},
|
||||
};
|
||||
use frame_system::RawOrigin as SystemOrigin;
|
||||
use sp_core::Get;
|
||||
use sp_runtime::traits::{Bounded, StaticLookup};
|
||||
use sp_std::{ops::Div, prelude::*};
|
||||
use sp_std::{marker::PhantomData, prelude::*};
|
||||
|
||||
use crate::Pallet as AssetConversion;
|
||||
|
||||
const INITIAL_ASSET_BALANCE: u128 = 1_000_000_000_000;
|
||||
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
|
||||
type BalanceOf<T> =
|
||||
<<T as Config>::Currency as InspectFungible<<T as frame_system::Config>::AccountId>>::Balance;
|
||||
|
||||
fn get_lp_token_id<T: Config>() -> T::PoolAssetId
|
||||
where
|
||||
T::PoolAssetId: Into<u32>,
|
||||
{
|
||||
let next_id: u32 = AssetConversion::<T>::get_next_pool_asset_id().into();
|
||||
(next_id - 1).into()
|
||||
/// Benchmark Helper
|
||||
pub trait BenchmarkHelper<AssetKind> {
|
||||
/// Returns a valid assets pair for the pool creation.
|
||||
///
|
||||
/// When a specific asset, such as the native asset, is required in every pool, it should be
|
||||
/// returned for each odd-numbered seed.
|
||||
fn create_pair(seed1: u32, seed2: u32) -> (AssetKind, AssetKind);
|
||||
}
|
||||
|
||||
fn create_asset<T: Config>(asset: &T::MultiAssetId) -> (T::AccountId, AccountIdLookupOf<T>)
|
||||
impl<AssetKind> BenchmarkHelper<AssetKind> for ()
|
||||
where
|
||||
T::Balance: From<u128>,
|
||||
T::Currency: Unbalanced<T::AccountId>,
|
||||
T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
|
||||
AssetKind: From<u32>,
|
||||
{
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
let caller_lookup = T::Lookup::unlookup(caller.clone());
|
||||
if let MultiAssetIdConversionResult::Converted(asset_id) =
|
||||
T::MultiAssetIdConverter::try_convert(asset)
|
||||
{
|
||||
T::Currency::set_balance(&caller, BalanceOf::<T>::max_value().div(1000u32.into()));
|
||||
assert_ok!(T::Assets::create(asset_id.clone(), caller.clone(), true, 1.into()));
|
||||
assert_ok!(T::Assets::mint_into(asset_id, &caller, INITIAL_ASSET_BALANCE.into()));
|
||||
fn create_pair(seed1: u32, seed2: u32) -> (AssetKind, AssetKind) {
|
||||
(seed1.into(), seed2.into())
|
||||
}
|
||||
(caller, caller_lookup)
|
||||
}
|
||||
|
||||
fn create_asset_and_pool<T: Config>(
|
||||
asset1: &T::MultiAssetId,
|
||||
asset2: &T::MultiAssetId,
|
||||
) -> (T::PoolAssetId, T::AccountId, AccountIdLookupOf<T>)
|
||||
where
|
||||
T::Balance: From<u128>,
|
||||
T::Currency: Unbalanced<T::AccountId>,
|
||||
T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
|
||||
T::PoolAssetId: Into<u32>,
|
||||
/// Factory for creating a valid asset pairs with [`NativeOrWithId::Native`] always leading in the
|
||||
/// pair.
|
||||
pub struct NativeOrWithIdFactory<AssetId>(PhantomData<AssetId>);
|
||||
impl<AssetId: From<u32> + Ord> BenchmarkHelper<NativeOrWithId<AssetId>>
|
||||
for NativeOrWithIdFactory<AssetId>
|
||||
{
|
||||
let (_, _) = create_asset::<T>(asset1);
|
||||
let (caller, caller_lookup) = create_asset::<T>(asset2);
|
||||
fn create_pair(seed1: u32, seed2: u32) -> (NativeOrWithId<AssetId>, NativeOrWithId<AssetId>) {
|
||||
if seed1 % 2 == 0 {
|
||||
(NativeOrWithId::WithId(seed2.into()), NativeOrWithId::Native)
|
||||
} else {
|
||||
(NativeOrWithId::Native, NativeOrWithId::WithId(seed2.into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides a pair of amounts expected to serve as sufficient initial liquidity for a pool.
|
||||
fn valid_liquidity_amount<T: Config>(ed1: T::Balance, ed2: T::Balance) -> (T::Balance, T::Balance)
|
||||
where
|
||||
T::Assets: Inspect<T::AccountId>,
|
||||
{
|
||||
let l =
|
||||
ed1.max(ed2) + T::MintMinLiquidity::get() + T::MintMinLiquidity::get() + T::Balance::one();
|
||||
(l, l)
|
||||
}
|
||||
|
||||
/// Create the `asset` and mint the `amount` for the `caller`.
|
||||
fn create_asset<T: Config>(caller: &T::AccountId, asset: &T::AssetKind, amount: T::Balance)
|
||||
where
|
||||
T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
|
||||
{
|
||||
if !T::Assets::asset_exists(asset.clone()) {
|
||||
assert_ok!(T::Assets::create(asset.clone(), caller.clone(), true, T::Balance::one()));
|
||||
}
|
||||
assert_ok!(T::Assets::mint_into(
|
||||
asset.clone(),
|
||||
&caller,
|
||||
amount + T::Assets::minimum_balance(asset.clone())
|
||||
));
|
||||
}
|
||||
|
||||
/// Create the designated fee asset for pool creation.
|
||||
fn create_fee_asset<T: Config>(caller: &T::AccountId)
|
||||
where
|
||||
T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
|
||||
{
|
||||
let fee_asset = T::PoolSetupFeeAsset::get();
|
||||
if !T::Assets::asset_exists(fee_asset.clone()) {
|
||||
assert_ok!(T::Assets::create(fee_asset.clone(), caller.clone(), true, T::Balance::one()));
|
||||
}
|
||||
assert_ok!(T::Assets::mint_into(
|
||||
fee_asset.clone(),
|
||||
&caller,
|
||||
T::Assets::minimum_balance(fee_asset)
|
||||
));
|
||||
}
|
||||
|
||||
/// Mint the fee asset for the `caller` sufficient to cover the fee for creating a new pool.
|
||||
fn mint_setup_fee_asset<T: Config>(
|
||||
caller: &T::AccountId,
|
||||
asset1: &T::AssetKind,
|
||||
asset2: &T::AssetKind,
|
||||
lp_token: &T::PoolAssetId,
|
||||
) where
|
||||
T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
|
||||
{
|
||||
assert_ok!(T::Assets::mint_into(
|
||||
T::PoolSetupFeeAsset::get(),
|
||||
&caller,
|
||||
T::PoolSetupFee::get() +
|
||||
T::Assets::deposit_required(asset1.clone()) +
|
||||
T::Assets::deposit_required(asset2.clone()) +
|
||||
T::PoolAssets::deposit_required(lp_token.clone())
|
||||
));
|
||||
}
|
||||
|
||||
/// Creates a pool for a given asset pair.
|
||||
///
|
||||
/// This action mints the necessary amounts of the given assets for the `caller` to provide initial
|
||||
/// liquidity. It returns the LP token ID along with a pair of amounts sufficient for the pool's
|
||||
/// initial liquidity.
|
||||
fn create_asset_and_pool<T: Config>(
|
||||
caller: &T::AccountId,
|
||||
asset1: &T::AssetKind,
|
||||
asset2: &T::AssetKind,
|
||||
) -> (T::PoolAssetId, T::Balance, T::Balance)
|
||||
where
|
||||
T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
|
||||
{
|
||||
let (liquidity1, liquidity2) = valid_liquidity_amount::<T>(
|
||||
T::Assets::minimum_balance(asset1.clone()),
|
||||
T::Assets::minimum_balance(asset2.clone()),
|
||||
);
|
||||
create_asset::<T>(caller, asset1, liquidity1);
|
||||
create_asset::<T>(caller, asset2, liquidity2);
|
||||
let lp_token = AssetConversion::<T>::get_next_pool_asset_id();
|
||||
|
||||
mint_setup_fee_asset::<T>(caller, asset1, asset2, &lp_token);
|
||||
|
||||
assert_ok!(AssetConversion::<T>::create_pool(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone())
|
||||
));
|
||||
let lp_token = get_lp_token_id::<T>();
|
||||
|
||||
(lp_token, caller, caller_lookup)
|
||||
(lp_token, liquidity1, liquidity2)
|
||||
}
|
||||
|
||||
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
|
||||
@@ -95,280 +164,198 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
|
||||
assert_eq!(event, &system_event);
|
||||
}
|
||||
|
||||
benchmarks! {
|
||||
where_clause {
|
||||
where
|
||||
T::Currency: Unbalanced<T::AccountId>,
|
||||
T::Balance: From<u128> + Into<u128>,
|
||||
T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
|
||||
T::PoolAssetId: Into<u32>,
|
||||
}
|
||||
#[benchmarks(where T::Assets: Create<T::AccountId> + Mutate<T::AccountId>, T::PoolAssetId: Into<u32>,)]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
create_pool {
|
||||
let asset1 = T::MultiAssetIdConverter::get_native();
|
||||
let asset2 = T::BenchmarkHelper::multiasset_id(0);
|
||||
let (caller, _) = create_asset::<T>(&asset2);
|
||||
}: _(SystemOrigin::Signed(caller.clone()), Box::new(asset1.clone()), Box::new(asset2.clone()))
|
||||
verify {
|
||||
let lp_token = get_lp_token_id::<T>();
|
||||
let pool_id = (asset1.clone(), asset2.clone());
|
||||
assert_last_event::<T>(Event::PoolCreated {
|
||||
creator: caller.clone(),
|
||||
pool_account: AssetConversion::<T>::get_pool_account(&pool_id),
|
||||
pool_id,
|
||||
lp_token,
|
||||
}.into());
|
||||
}
|
||||
#[benchmark]
|
||||
fn create_pool() {
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
let (asset1, asset2) = T::BenchmarkHelper::create_pair(0, 1);
|
||||
create_asset::<T>(&caller, &asset1, T::Assets::minimum_balance(asset1.clone()));
|
||||
create_asset::<T>(&caller, &asset2, T::Assets::minimum_balance(asset2.clone()));
|
||||
|
||||
add_liquidity {
|
||||
let asset1 = T::MultiAssetIdConverter::get_native();
|
||||
let asset2 = T::BenchmarkHelper::multiasset_id(0);
|
||||
let (lp_token, caller, _) = create_asset_and_pool::<T>(&asset1, &asset2);
|
||||
let ed: u128 = T::Currency::minimum_balance().into();
|
||||
let add_amount = 1000 + ed;
|
||||
}: _(SystemOrigin::Signed(caller.clone()), Box::new(asset1.clone()), Box::new(asset2.clone()), add_amount.into(), 1000.into(), 0.into(), 0.into(), caller.clone())
|
||||
verify {
|
||||
let pool_id = (asset1.clone(), asset2.clone());
|
||||
let lp_minted = AssetConversion::<T>::calc_lp_amount_for_zero_supply(&add_amount.into(), &1000.into()).unwrap().into();
|
||||
assert_eq!(
|
||||
T::PoolAssets::balance(lp_token, &caller),
|
||||
lp_minted.into()
|
||||
);
|
||||
assert_eq!(
|
||||
T::Currency::balance(&AssetConversion::<T>::get_pool_account(&pool_id)),
|
||||
add_amount.into()
|
||||
);
|
||||
assert_eq!(
|
||||
T::Assets::balance(T::BenchmarkHelper::asset_id(0), &AssetConversion::<T>::get_pool_account(&pool_id)),
|
||||
1000.into()
|
||||
let lp_token = AssetConversion::<T>::get_next_pool_asset_id();
|
||||
create_fee_asset::<T>(&caller);
|
||||
mint_setup_fee_asset::<T>(&caller, &asset1, &asset2, &lp_token);
|
||||
|
||||
#[extrinsic_call]
|
||||
_(SystemOrigin::Signed(caller.clone()), Box::new(asset1.clone()), Box::new(asset2.clone()));
|
||||
|
||||
let pool_id = T::PoolLocator::pool_id(&asset1, &asset2).unwrap();
|
||||
let pool_account = T::PoolLocator::address(&pool_id).unwrap();
|
||||
assert_last_event::<T>(
|
||||
Event::PoolCreated { creator: caller, pool_account, pool_id, lp_token }.into(),
|
||||
);
|
||||
}
|
||||
|
||||
remove_liquidity {
|
||||
let asset1 = T::MultiAssetIdConverter::get_native();
|
||||
let asset2 = T::BenchmarkHelper::multiasset_id(0);
|
||||
let (lp_token, caller, _) = create_asset_and_pool::<T>(&asset1, &asset2);
|
||||
let ed: u128 = T::Currency::minimum_balance().into();
|
||||
let add_amount = 100 * ed;
|
||||
let lp_minted = AssetConversion::<T>::calc_lp_amount_for_zero_supply(&add_amount.into(), &1000.into()).unwrap().into();
|
||||
let remove_lp_amount = lp_minted.checked_div(10).unwrap();
|
||||
#[benchmark]
|
||||
fn add_liquidity() {
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
let (asset1, asset2) = T::BenchmarkHelper::create_pair(0, 1);
|
||||
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
create_fee_asset::<T>(&caller);
|
||||
let (lp_token, liquidity1, liquidity2) =
|
||||
create_asset_and_pool::<T>(&caller, &asset1, &asset2);
|
||||
|
||||
#[extrinsic_call]
|
||||
_(
|
||||
SystemOrigin::Signed(caller.clone()),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
liquidity1,
|
||||
liquidity2,
|
||||
T::Balance::one(),
|
||||
T::Balance::zero(),
|
||||
caller.clone(),
|
||||
);
|
||||
|
||||
let pool_account = T::PoolLocator::pool_address(&asset1, &asset2).unwrap();
|
||||
let lp_minted =
|
||||
AssetConversion::<T>::calc_lp_amount_for_zero_supply(&liquidity1, &liquidity2).unwrap();
|
||||
assert_eq!(T::PoolAssets::balance(lp_token, &caller), lp_minted);
|
||||
assert_eq!(T::Assets::balance(asset1, &pool_account), liquidity1);
|
||||
assert_eq!(T::Assets::balance(asset2, &pool_account), liquidity2);
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn remove_liquidity() {
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
let (asset1, asset2) = T::BenchmarkHelper::create_pair(0, 1);
|
||||
|
||||
create_fee_asset::<T>(&caller);
|
||||
let (lp_token, liquidity1, liquidity2) =
|
||||
create_asset_and_pool::<T>(&caller, &asset1, &asset2);
|
||||
|
||||
let remove_lp_amount = T::Balance::one();
|
||||
|
||||
assert_ok!(AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
add_amount.into(),
|
||||
1000.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
liquidity1,
|
||||
liquidity2,
|
||||
T::Balance::one(),
|
||||
T::Balance::zero(),
|
||||
caller.clone(),
|
||||
));
|
||||
let total_supply =
|
||||
<T::PoolAssets as Inspect<T::AccountId>>::total_issuance(lp_token.clone());
|
||||
|
||||
#[extrinsic_call]
|
||||
_(
|
||||
SystemOrigin::Signed(caller.clone()),
|
||||
Box::new(asset1),
|
||||
Box::new(asset2),
|
||||
remove_lp_amount,
|
||||
T::Balance::zero(),
|
||||
T::Balance::zero(),
|
||||
caller.clone(),
|
||||
)?;
|
||||
let total_supply = <T::PoolAssets as Inspect<T::AccountId>>::total_issuance(lp_token.clone());
|
||||
}: _(SystemOrigin::Signed(caller.clone()), Box::new(asset1), Box::new(asset2), remove_lp_amount.into(), 0.into(), 0.into(), caller.clone())
|
||||
verify {
|
||||
let new_total_supply = <T::PoolAssets as Inspect<T::AccountId>>::total_issuance(lp_token.clone());
|
||||
assert_eq!(
|
||||
new_total_supply,
|
||||
total_supply - remove_lp_amount.into()
|
||||
);
|
||||
|
||||
let new_total_supply = <T::PoolAssets as Inspect<T::AccountId>>::total_issuance(lp_token);
|
||||
assert_eq!(new_total_supply, total_supply - remove_lp_amount);
|
||||
}
|
||||
|
||||
swap_exact_tokens_for_tokens {
|
||||
let native = T::MultiAssetIdConverter::get_native();
|
||||
let asset1 = T::BenchmarkHelper::multiasset_id(1);
|
||||
let asset2 = T::BenchmarkHelper::multiasset_id(2);
|
||||
let (_, caller, _) = create_asset_and_pool::<T>(&native, &asset1);
|
||||
let (_, _) = create_asset::<T>(&asset2);
|
||||
let ed: u128 = T::Currency::minimum_balance().into();
|
||||
#[benchmark]
|
||||
fn swap_exact_tokens_for_tokens(n: Linear<2, { T::MaxSwapPathLength::get() }>) {
|
||||
let mut swap_amount = T::Balance::one();
|
||||
let mut path = vec![];
|
||||
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset1.clone()),
|
||||
(100 * ed).into(),
|
||||
200.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
create_fee_asset::<T>(&caller);
|
||||
for n in 1..n {
|
||||
let (asset1, asset2) = T::BenchmarkHelper::create_pair(n - 1, n);
|
||||
swap_amount = swap_amount + T::Balance::one();
|
||||
if path.len() == 0 {
|
||||
path = vec![Box::new(asset1.clone()), Box::new(asset2.clone())];
|
||||
} else {
|
||||
path.push(Box::new(asset2.clone()));
|
||||
}
|
||||
|
||||
let (_, liquidity1, liquidity2) = create_asset_and_pool::<T>(&caller, &asset1, &asset2);
|
||||
|
||||
assert_ok!(AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
liquidity1,
|
||||
liquidity2,
|
||||
T::Balance::one(),
|
||||
T::Balance::zero(),
|
||||
caller.clone(),
|
||||
));
|
||||
}
|
||||
|
||||
let asset_in = *path.first().unwrap().clone();
|
||||
assert_ok!(T::Assets::mint_into(
|
||||
asset_in.clone(),
|
||||
&caller,
|
||||
swap_amount + T::Balance::one()
|
||||
));
|
||||
let init_caller_balance = T::Assets::balance(asset_in.clone(), &caller);
|
||||
|
||||
#[extrinsic_call]
|
||||
_(
|
||||
SystemOrigin::Signed(caller.clone()),
|
||||
path,
|
||||
swap_amount,
|
||||
T::Balance::one(),
|
||||
caller.clone(),
|
||||
)?;
|
||||
true,
|
||||
);
|
||||
|
||||
let path;
|
||||
let swap_amount;
|
||||
// if we only allow the native-asset pools, then the worst case scenario would be to swap
|
||||
// asset1-native-asset2
|
||||
if !T::AllowMultiAssetPools::get() {
|
||||
AssetConversion::<T>::create_pool(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset2.clone())
|
||||
)?;
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
(500 * ed).into(),
|
||||
1000.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
caller.clone(),
|
||||
)?;
|
||||
path = vec![
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset2.clone())
|
||||
];
|
||||
swap_amount = 100.into();
|
||||
} else {
|
||||
let asset3 = T::BenchmarkHelper::multiasset_id(3);
|
||||
AssetConversion::<T>::create_pool(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone())
|
||||
)?;
|
||||
let (_, _) = create_asset::<T>(&asset3);
|
||||
AssetConversion::<T>::create_pool(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset2.clone()),
|
||||
Box::new(asset3.clone())
|
||||
)?;
|
||||
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
200.into(),
|
||||
2000.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
caller.clone(),
|
||||
)?;
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset2.clone()),
|
||||
Box::new(asset3.clone()),
|
||||
2000.into(),
|
||||
2000.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
caller.clone(),
|
||||
)?;
|
||||
path = vec![
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
Box::new(asset3.clone())
|
||||
];
|
||||
swap_amount = ed.into();
|
||||
}
|
||||
let native_balance = T::Currency::balance(&caller);
|
||||
let asset1_balance = T::Assets::balance(T::BenchmarkHelper::asset_id(1), &caller);
|
||||
}: _(SystemOrigin::Signed(caller.clone()), path, swap_amount, 1.into(), caller.clone(), false)
|
||||
verify {
|
||||
if !T::AllowMultiAssetPools::get() {
|
||||
let new_asset1_balance = T::Assets::balance(T::BenchmarkHelper::asset_id(1), &caller);
|
||||
assert_eq!(new_asset1_balance, asset1_balance - 100.into());
|
||||
} else {
|
||||
let new_native_balance = T::Currency::balance(&caller);
|
||||
assert_eq!(new_native_balance, native_balance - ed.into());
|
||||
}
|
||||
let actual_balance = T::Assets::balance(asset_in, &caller);
|
||||
assert_eq!(actual_balance, init_caller_balance - swap_amount);
|
||||
}
|
||||
|
||||
swap_tokens_for_exact_tokens {
|
||||
let native = T::MultiAssetIdConverter::get_native();
|
||||
let asset1 = T::BenchmarkHelper::multiasset_id(1);
|
||||
let asset2 = T::BenchmarkHelper::multiasset_id(2);
|
||||
let (_, caller, _) = create_asset_and_pool::<T>(&native, &asset1);
|
||||
let (_, _) = create_asset::<T>(&asset2);
|
||||
let ed: u128 = T::Currency::minimum_balance().into();
|
||||
#[benchmark]
|
||||
fn swap_tokens_for_exact_tokens(n: Linear<2, { T::MaxSwapPathLength::get() }>) {
|
||||
let mut max_swap_amount = T::Balance::one();
|
||||
let mut path = vec![];
|
||||
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset1.clone()),
|
||||
(1000 * ed).into(),
|
||||
500.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
create_fee_asset::<T>(&caller);
|
||||
for n in 1..n {
|
||||
let (asset1, asset2) = T::BenchmarkHelper::create_pair(n - 1, n);
|
||||
max_swap_amount = max_swap_amount + T::Balance::one() + T::Balance::one();
|
||||
if path.len() == 0 {
|
||||
path = vec![Box::new(asset1.clone()), Box::new(asset2.clone())];
|
||||
} else {
|
||||
path.push(Box::new(asset2.clone()));
|
||||
}
|
||||
|
||||
let (_, liquidity1, liquidity2) = create_asset_and_pool::<T>(&caller, &asset1, &asset2);
|
||||
|
||||
assert_ok!(AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
liquidity1,
|
||||
liquidity2,
|
||||
T::Balance::one(),
|
||||
T::Balance::zero(),
|
||||
caller.clone(),
|
||||
));
|
||||
}
|
||||
|
||||
let asset_in = *path.first().unwrap().clone();
|
||||
let asset_out = *path.last().unwrap().clone();
|
||||
assert_ok!(T::Assets::mint_into(asset_in, &caller, max_swap_amount));
|
||||
let init_caller_balance = T::Assets::balance(asset_out.clone(), &caller);
|
||||
|
||||
#[extrinsic_call]
|
||||
_(
|
||||
SystemOrigin::Signed(caller.clone()),
|
||||
path,
|
||||
T::Balance::one(),
|
||||
max_swap_amount,
|
||||
caller.clone(),
|
||||
)?;
|
||||
true,
|
||||
);
|
||||
|
||||
let path;
|
||||
// if we only allow the native-asset pools, then the worst case scenario would be to swap
|
||||
// asset1-native-asset2
|
||||
if !T::AllowMultiAssetPools::get() {
|
||||
AssetConversion::<T>::create_pool(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset2.clone())
|
||||
)?;
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
(500 * ed).into(),
|
||||
1000.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
caller.clone(),
|
||||
)?;
|
||||
path = vec![
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset2.clone())
|
||||
];
|
||||
} else {
|
||||
AssetConversion::<T>::create_pool(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone())
|
||||
)?;
|
||||
let asset3 = T::BenchmarkHelper::multiasset_id(3);
|
||||
let (_, _) = create_asset::<T>(&asset3);
|
||||
AssetConversion::<T>::create_pool(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset2.clone()),
|
||||
Box::new(asset3.clone())
|
||||
)?;
|
||||
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
2000.into(),
|
||||
2000.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
caller.clone(),
|
||||
)?;
|
||||
AssetConversion::<T>::add_liquidity(
|
||||
SystemOrigin::Signed(caller.clone()).into(),
|
||||
Box::new(asset2.clone()),
|
||||
Box::new(asset3.clone()),
|
||||
2000.into(),
|
||||
2000.into(),
|
||||
0.into(),
|
||||
0.into(),
|
||||
caller.clone(),
|
||||
)?;
|
||||
path = vec![
|
||||
Box::new(native.clone()),
|
||||
Box::new(asset1.clone()),
|
||||
Box::new(asset2.clone()),
|
||||
Box::new(asset3.clone())
|
||||
];
|
||||
}
|
||||
|
||||
let asset2_balance = T::Assets::balance(T::BenchmarkHelper::asset_id(2), &caller);
|
||||
let asset3_balance = T::Assets::balance(T::BenchmarkHelper::asset_id(3), &caller);
|
||||
}: _(SystemOrigin::Signed(caller.clone()), path.clone(), 100.into(), (1000 * ed).into(), caller.clone(), false)
|
||||
verify {
|
||||
if !T::AllowMultiAssetPools::get() {
|
||||
let new_asset2_balance = T::Assets::balance(T::BenchmarkHelper::asset_id(2), &caller);
|
||||
assert_eq!(new_asset2_balance, asset2_balance + 100.into());
|
||||
} else {
|
||||
let new_asset3_balance = T::Assets::balance(T::BenchmarkHelper::asset_id(3), &caller);
|
||||
assert_eq!(new_asset3_balance, asset3_balance + 100.into());
|
||||
}
|
||||
let actual_balance = T::Assets::balance(asset_out, &caller);
|
||||
assert_eq!(actual_balance, init_caller_balance + T::Balance::one());
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(AssetConversion, crate::mock::new_test_ext(), crate::mock::Test);
|
||||
|
||||
@@ -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)>;
|
||||
|
||||
@@ -19,12 +19,17 @@
|
||||
|
||||
use super::*;
|
||||
use crate as pallet_asset_conversion;
|
||||
|
||||
use frame_support::{
|
||||
construct_runtime, derive_impl,
|
||||
instances::{Instance1, Instance2},
|
||||
ord_parameter_types, parameter_types,
|
||||
traits::{AsEnsureOriginWithArg, ConstU128, ConstU32, ConstU64},
|
||||
traits::{
|
||||
tokens::{
|
||||
fungible::{NativeFromLeft, NativeOrWithId, UnionOf},
|
||||
imbalance::ResolveAssetTo,
|
||||
},
|
||||
AsEnsureOriginWithArg, ConstU128, ConstU32, ConstU64,
|
||||
},
|
||||
PalletId,
|
||||
};
|
||||
use frame_system::{EnsureSigned, EnsureSignedBy};
|
||||
@@ -34,6 +39,7 @@ use sp_runtime::{
|
||||
traits::{AccountIdConversion, BlakeTwo256, IdentityLookup},
|
||||
BuildStorage,
|
||||
};
|
||||
use sp_std::default::Default;
|
||||
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
|
||||
@@ -143,37 +149,37 @@ impl pallet_assets::Config<Instance2> for Test {
|
||||
|
||||
parameter_types! {
|
||||
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
|
||||
pub storage AllowMultiAssetPools: bool = true;
|
||||
pub storage LiquidityWithdrawalFee: Permill = Permill::from_percent(0); // should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
|
||||
pub const Native: NativeOrWithId<u32> = NativeOrWithId::Native;
|
||||
pub storage LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
|
||||
}
|
||||
|
||||
ord_parameter_types! {
|
||||
pub const AssetConversionOrigin: u128 = AccountIdConversion::<u128>::into_account_truncating(&AssetConversionPalletId::get());
|
||||
}
|
||||
|
||||
pub type NativeAndAssets = UnionOf<Balances, Assets, NativeFromLeft, NativeOrWithId<u32>, u128>;
|
||||
pub type AscendingLocator = Ascending<u128, NativeOrWithId<u32>>;
|
||||
pub type WithFirstAssetLocator = WithFirstAsset<Native, u128, NativeOrWithId<u32>>;
|
||||
|
||||
impl Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type Currency = Balances;
|
||||
type AssetId = u32;
|
||||
type Balance = <Self as pallet_balances::Config>::Balance;
|
||||
type HigherPrecisionBalance = sp_core::U256;
|
||||
type AssetKind = NativeOrWithId<u32>;
|
||||
type Assets = NativeAndAssets;
|
||||
type PoolId = (Self::AssetKind, Self::AssetKind);
|
||||
type PoolLocator = Chain<WithFirstAssetLocator, AscendingLocator>;
|
||||
type PoolAssetId = u32;
|
||||
type Assets = Assets;
|
||||
type PoolAssets = PoolAssets;
|
||||
type PoolSetupFee = ConstU128<100>; // should be more or equal to the existential deposit
|
||||
type PoolSetupFeeAsset = Native;
|
||||
type PoolSetupFeeTarget = ResolveAssetTo<AssetConversionOrigin, Self::Assets>;
|
||||
type PalletId = AssetConversionPalletId;
|
||||
type WeightInfo = ();
|
||||
type LPFee = ConstU32<3>; // means 0.3%
|
||||
type PoolSetupFee = ConstU128<100>; // should be more or equal to the existential deposit
|
||||
type PoolSetupFeeReceiver = AssetConversionOrigin;
|
||||
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
|
||||
type AllowMultiAssetPools = AllowMultiAssetPools;
|
||||
type MaxSwapPathLength = ConstU32<4>;
|
||||
type MintMinLiquidity = ConstU128<100>; // 100 is good enough when the main currency has 12 decimals.
|
||||
|
||||
type Balance = <Self as pallet_balances::Config>::Balance;
|
||||
type HigherPrecisionBalance = sp_core::U256;
|
||||
|
||||
type MultiAssetId = NativeOrAssetId<u32>;
|
||||
type MultiAssetIdConverter = NativeOrAssetIdConverter<u32>;
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
type BenchmarkHelper = ();
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ pub trait Swap<AccountId> {
|
||||
/// Measure units of the asset classes for swapping.
|
||||
type Balance: Balance;
|
||||
/// Kind of assets that are going to be swapped.
|
||||
type MultiAssetId;
|
||||
type AssetKind;
|
||||
|
||||
/// Returns the upper limit on the length of the swap path.
|
||||
fn max_path_len() -> u32;
|
||||
@@ -41,7 +41,7 @@ pub trait Swap<AccountId> {
|
||||
/// This operation is expected to be atomic.
|
||||
fn swap_exact_tokens_for_tokens(
|
||||
sender: AccountId,
|
||||
path: Vec<Self::MultiAssetId>,
|
||||
path: Vec<Self::AssetKind>,
|
||||
amount_in: Self::Balance,
|
||||
amount_out_min: Option<Self::Balance>,
|
||||
send_to: AccountId,
|
||||
@@ -60,7 +60,7 @@ pub trait Swap<AccountId> {
|
||||
/// This operation is expected to be atomic.
|
||||
fn swap_tokens_for_exact_tokens(
|
||||
sender: AccountId,
|
||||
path: Vec<Self::MultiAssetId>,
|
||||
path: Vec<Self::AssetKind>,
|
||||
amount_out: Self::Balance,
|
||||
amount_in_max: Option<Self::Balance>,
|
||||
send_to: AccountId,
|
||||
@@ -73,7 +73,7 @@ pub trait SwapCredit<AccountId> {
|
||||
/// Measure units of the asset classes for swapping.
|
||||
type Balance: Balance;
|
||||
/// Kind of assets that are going to be swapped.
|
||||
type MultiAssetId;
|
||||
type AssetKind;
|
||||
/// Credit implying a negative imbalance in the system that can be placed into an account or
|
||||
/// alter the total supply.
|
||||
type Credit;
|
||||
@@ -90,7 +90,7 @@ pub trait SwapCredit<AccountId> {
|
||||
///
|
||||
/// This operation is expected to be atomic.
|
||||
fn swap_exact_tokens_for_tokens(
|
||||
path: Vec<Self::MultiAssetId>,
|
||||
path: Vec<Self::AssetKind>,
|
||||
credit_in: Self::Credit,
|
||||
amount_out_min: Option<Self::Balance>,
|
||||
) -> Result<Self::Credit, (Self::Credit, DispatchError)>;
|
||||
@@ -106,7 +106,7 @@ pub trait SwapCredit<AccountId> {
|
||||
///
|
||||
/// This operation is expected to be atomic.
|
||||
fn swap_tokens_for_exact_tokens(
|
||||
path: Vec<Self::MultiAssetId>,
|
||||
path: Vec<Self::AssetKind>,
|
||||
credit_in: Self::Credit,
|
||||
amount_out: Self::Balance,
|
||||
) -> Result<(Self::Credit, Self::Credit), (Self::Credit, DispatchError)>;
|
||||
@@ -114,7 +114,7 @@ pub trait SwapCredit<AccountId> {
|
||||
|
||||
impl<T: Config> Swap<T::AccountId> for Pallet<T> {
|
||||
type Balance = T::Balance;
|
||||
type MultiAssetId = T::MultiAssetId;
|
||||
type AssetKind = T::AssetKind;
|
||||
|
||||
fn max_path_len() -> u32 {
|
||||
T::MaxSwapPathLength::get()
|
||||
@@ -122,7 +122,7 @@ impl<T: Config> Swap<T::AccountId> for Pallet<T> {
|
||||
|
||||
fn swap_exact_tokens_for_tokens(
|
||||
sender: T::AccountId,
|
||||
path: Vec<Self::MultiAssetId>,
|
||||
path: Vec<Self::AssetKind>,
|
||||
amount_in: Self::Balance,
|
||||
amount_out_min: Option<Self::Balance>,
|
||||
send_to: T::AccountId,
|
||||
@@ -138,12 +138,12 @@ impl<T: Config> Swap<T::AccountId> for Pallet<T> {
|
||||
keep_alive,
|
||||
)
|
||||
})?;
|
||||
Ok(amount_out.into())
|
||||
Ok(amount_out)
|
||||
}
|
||||
|
||||
fn swap_tokens_for_exact_tokens(
|
||||
sender: T::AccountId,
|
||||
path: Vec<Self::MultiAssetId>,
|
||||
path: Vec<Self::AssetKind>,
|
||||
amount_out: Self::Balance,
|
||||
amount_in_max: Option<Self::Balance>,
|
||||
send_to: T::AccountId,
|
||||
@@ -159,24 +159,25 @@ impl<T: Config> Swap<T::AccountId> for Pallet<T> {
|
||||
keep_alive,
|
||||
)
|
||||
})?;
|
||||
Ok(amount_in.into())
|
||||
Ok(amount_in)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> SwapCredit<T::AccountId> for Pallet<T> {
|
||||
type Balance = T::Balance;
|
||||
type MultiAssetId = T::MultiAssetId;
|
||||
type Credit = Credit<T>;
|
||||
type AssetKind = T::AssetKind;
|
||||
type Credit = CreditOf<T>;
|
||||
|
||||
fn max_path_len() -> u32 {
|
||||
T::MaxSwapPathLength::get()
|
||||
}
|
||||
|
||||
fn swap_exact_tokens_for_tokens(
|
||||
path: Vec<Self::MultiAssetId>,
|
||||
path: Vec<Self::AssetKind>,
|
||||
credit_in: Self::Credit,
|
||||
amount_out_min: Option<Self::Balance>,
|
||||
) -> Result<Self::Credit, (Self::Credit, DispatchError)> {
|
||||
let credit_asset = credit_in.asset();
|
||||
with_transaction(|| -> TransactionOutcome<Result<_, DispatchError>> {
|
||||
let res = Self::do_swap_exact_credit_tokens_for_tokens(path, credit_in, amount_out_min);
|
||||
match &res {
|
||||
@@ -187,14 +188,15 @@ impl<T: Config> SwapCredit<T::AccountId> for Pallet<T> {
|
||||
}
|
||||
})
|
||||
// should never map an error since `with_transaction` above never returns it.
|
||||
.map_err(|_| (Self::Credit::native_zero(), DispatchError::Corruption))?
|
||||
.map_err(|_| (Self::Credit::zero(credit_asset), DispatchError::Corruption))?
|
||||
}
|
||||
|
||||
fn swap_tokens_for_exact_tokens(
|
||||
path: Vec<Self::MultiAssetId>,
|
||||
path: Vec<Self::AssetKind>,
|
||||
credit_in: Self::Credit,
|
||||
amount_out: Self::Balance,
|
||||
) -> Result<(Self::Credit, Self::Credit), (Self::Credit, DispatchError)> {
|
||||
let credit_asset = credit_in.asset();
|
||||
with_transaction(|| -> TransactionOutcome<Result<_, DispatchError>> {
|
||||
let res = Self::do_swap_credit_tokens_for_exact_tokens(path, credit_in, amount_out);
|
||||
match &res {
|
||||
@@ -205,6 +207,6 @@ impl<T: Config> SwapCredit<T::AccountId> for Pallet<T> {
|
||||
}
|
||||
})
|
||||
// should never map an error since `with_transaction` above never returns it.
|
||||
.map_err(|_| (Self::Credit::native_zero(), DispatchError::Corruption))?
|
||||
.map_err(|_| (Self::Credit::zero(credit_asset), DispatchError::Corruption))?
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,16 +16,9 @@
|
||||
// limitations under the License.
|
||||
|
||||
use super::*;
|
||||
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use scale_info::TypeInfo;
|
||||
use sp_std::{cmp::Ordering, marker::PhantomData};
|
||||
|
||||
/// Pool ID.
|
||||
///
|
||||
/// The pool's `AccountId` is derived from this type. Any changes to the type may necessitate a
|
||||
/// migration.
|
||||
pub(super) type PoolIdOf<T> = (<T as Config>::MultiAssetId, <T as Config>::MultiAssetId);
|
||||
use sp_std::marker::PhantomData;
|
||||
|
||||
/// Represents a swap path with associated asset amounts indicating how much of the asset needs to
|
||||
/// be deposited to get the following asset's amount withdrawn (this is inclusive of fees).
|
||||
@@ -35,7 +28,10 @@ pub(super) type PoolIdOf<T> = (<T as Config>::MultiAssetId, <T as Config>::Multi
|
||||
/// 1. `asset(asset1, amount_in)` take from `user` and move to the pool(asset1, asset2);
|
||||
/// 2. `asset(asset2, amount_out2)` transfer from pool(asset1, asset2) to pool(asset2, asset3);
|
||||
/// 3. `asset(asset3, amount_out3)` move from pool(asset2, asset3) to `user`.
|
||||
pub(super) type BalancePath<T> = Vec<(<T as Config>::MultiAssetId, <T as Config>::Balance)>;
|
||||
pub(super) type BalancePath<T> = Vec<(<T as Config>::AssetKind, <T as Config>::Balance)>;
|
||||
|
||||
/// Credit of [Config::Assets].
|
||||
pub type CreditOf<T> = Credit<<T as frame_system::Config>::AccountId, <T as Config>::Assets>;
|
||||
|
||||
/// Stores the lp_token asset id a particular pool has been assigned.
|
||||
#[derive(Decode, Encode, Default, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
|
||||
@@ -44,214 +40,94 @@ pub struct PoolInfo<PoolAssetId> {
|
||||
pub lp_token: PoolAssetId,
|
||||
}
|
||||
|
||||
/// A trait that converts between a MultiAssetId and either the native currency or an AssetId.
|
||||
pub trait MultiAssetIdConverter<MultiAssetId, AssetId> {
|
||||
/// Returns the MultiAssetId representing the native currency of the chain.
|
||||
fn get_native() -> MultiAssetId;
|
||||
|
||||
/// Returns true if the given MultiAssetId is the native currency.
|
||||
fn is_native(asset: &MultiAssetId) -> bool;
|
||||
|
||||
/// If it's not native, returns the AssetId for the given MultiAssetId.
|
||||
fn try_convert(asset: &MultiAssetId) -> MultiAssetIdConversionResult<MultiAssetId, AssetId>;
|
||||
}
|
||||
|
||||
/// Result of `MultiAssetIdConverter::try_convert`.
|
||||
#[cfg_attr(feature = "std", derive(PartialEq, Debug))]
|
||||
pub enum MultiAssetIdConversionResult<MultiAssetId, AssetId> {
|
||||
/// Input asset is successfully converted. Means that converted asset is supported.
|
||||
Converted(AssetId),
|
||||
/// Means that input asset is the chain's native asset, if it has one, so no conversion (see
|
||||
/// `MultiAssetIdConverter::get_native`).
|
||||
Native,
|
||||
/// Means input asset is not supported for pool.
|
||||
Unsupported(MultiAssetId),
|
||||
}
|
||||
|
||||
/// Benchmark Helper
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
pub trait BenchmarkHelper<AssetId, MultiAssetId> {
|
||||
/// Returns an `AssetId` from a given integer.
|
||||
fn asset_id(asset_id: u32) -> AssetId;
|
||||
|
||||
/// Returns a `MultiAssetId` from a given integer.
|
||||
fn multiasset_id(asset_id: u32) -> MultiAssetId;
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
impl<AssetId, MultiAssetId> BenchmarkHelper<AssetId, MultiAssetId> for ()
|
||||
where
|
||||
AssetId: From<u32>,
|
||||
MultiAssetId: From<u32>,
|
||||
{
|
||||
fn asset_id(asset_id: u32) -> AssetId {
|
||||
asset_id.into()
|
||||
}
|
||||
|
||||
fn multiasset_id(asset_id: u32) -> MultiAssetId {
|
||||
asset_id.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// An implementation of MultiAssetId that can be either Native or an asset.
|
||||
#[derive(Decode, Encode, Default, MaxEncodedLen, TypeInfo, Clone, Copy, Debug)]
|
||||
pub enum NativeOrAssetId<AssetId>
|
||||
where
|
||||
AssetId: Ord,
|
||||
{
|
||||
/// Native asset. For example, on the Polkadot Asset Hub this would be DOT.
|
||||
#[default]
|
||||
Native,
|
||||
/// A non-native asset id.
|
||||
Asset(AssetId),
|
||||
}
|
||||
|
||||
impl<AssetId: Ord> From<AssetId> for NativeOrAssetId<AssetId> {
|
||||
fn from(asset: AssetId) -> Self {
|
||||
Self::Asset(asset)
|
||||
}
|
||||
}
|
||||
|
||||
impl<AssetId: Ord> Ord for NativeOrAssetId<AssetId> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
match (self, other) {
|
||||
(Self::Native, Self::Native) => Ordering::Equal,
|
||||
(Self::Native, Self::Asset(_)) => Ordering::Less,
|
||||
(Self::Asset(_), Self::Native) => Ordering::Greater,
|
||||
(Self::Asset(id1), Self::Asset(id2)) => <AssetId as Ord>::cmp(id1, id2),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<AssetId: Ord> PartialOrd for NativeOrAssetId<AssetId> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(<Self as Ord>::cmp(self, other))
|
||||
}
|
||||
}
|
||||
impl<AssetId: Ord> PartialEq for NativeOrAssetId<AssetId> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.cmp(other) == Ordering::Equal
|
||||
}
|
||||
}
|
||||
impl<AssetId: Ord> Eq for NativeOrAssetId<AssetId> {}
|
||||
|
||||
/// Converts between a MultiAssetId and an AssetId (or the native currency).
|
||||
pub struct NativeOrAssetIdConverter<AssetId> {
|
||||
_phantom: PhantomData<AssetId>,
|
||||
}
|
||||
|
||||
impl<AssetId: Ord + Clone> MultiAssetIdConverter<NativeOrAssetId<AssetId>, AssetId>
|
||||
for NativeOrAssetIdConverter<AssetId>
|
||||
{
|
||||
fn get_native() -> NativeOrAssetId<AssetId> {
|
||||
NativeOrAssetId::Native
|
||||
}
|
||||
|
||||
fn is_native(asset: &NativeOrAssetId<AssetId>) -> bool {
|
||||
*asset == Self::get_native()
|
||||
}
|
||||
|
||||
fn try_convert(
|
||||
asset: &NativeOrAssetId<AssetId>,
|
||||
) -> MultiAssetIdConversionResult<NativeOrAssetId<AssetId>, AssetId> {
|
||||
match asset {
|
||||
NativeOrAssetId::Asset(asset) => MultiAssetIdConversionResult::Converted(asset.clone()),
|
||||
NativeOrAssetId::Native => MultiAssetIdConversionResult::Native,
|
||||
/// Provides means to resolve the `PoolId` and `AccountId` from a pair of assets.
|
||||
///
|
||||
/// Resulting `PoolId` remains consistent whether the asset pair is presented as (asset1, asset2)
|
||||
/// or (asset2, asset1). The derived `AccountId` may serve as an address for liquidity provider
|
||||
/// tokens.
|
||||
pub trait PoolLocator<AccountId, AssetKind, PoolId> {
|
||||
/// Retrieves the account address associated with a valid `PoolId`.
|
||||
fn address(id: &PoolId) -> Result<AccountId, ()>;
|
||||
/// Identifies the `PoolId` for a given pair of assets.
|
||||
///
|
||||
/// Returns an error if the asset pair isn't supported.
|
||||
fn pool_id(asset1: &AssetKind, asset2: &AssetKind) -> Result<PoolId, ()>;
|
||||
/// Retrieves the account address associated with a given asset pair.
|
||||
///
|
||||
/// Returns an error if the asset pair isn't supported.
|
||||
fn pool_address(asset1: &AssetKind, asset2: &AssetKind) -> Result<AccountId, ()> {
|
||||
if let Ok(id) = Self::pool_id(asset1, asset2) {
|
||||
Self::address(&id)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Credit of [Config::Currency].
|
||||
/// Pool locator that mandates the inclusion of the specified `FirstAsset` in every asset pair.
|
||||
///
|
||||
/// Implies a negative imbalance in the system that can be placed into an account or alter the total
|
||||
/// supply.
|
||||
pub type NativeCredit<T> =
|
||||
CreditFungible<<T as frame_system::Config>::AccountId, <T as Config>::Currency>;
|
||||
|
||||
/// Credit (aka negative imbalance) of [Config::Assets].
|
||||
///
|
||||
/// Implies a negative imbalance in the system that can be placed into an account or alter the total
|
||||
/// supply.
|
||||
pub type AssetCredit<T> =
|
||||
CreditFungibles<<T as frame_system::Config>::AccountId, <T as Config>::Assets>;
|
||||
|
||||
/// Credit that can be either [`NativeCredit`] or [`AssetCredit`].
|
||||
///
|
||||
/// Implies a negative imbalance in the system that can be placed into an account or alter the total
|
||||
/// supply.
|
||||
#[derive(RuntimeDebug, Eq, PartialEq)]
|
||||
pub enum Credit<T: Config> {
|
||||
/// Native credit.
|
||||
Native(NativeCredit<T>),
|
||||
/// Asset credit.
|
||||
Asset(AssetCredit<T>),
|
||||
}
|
||||
|
||||
impl<T: Config> From<NativeCredit<T>> for Credit<T> {
|
||||
fn from(value: NativeCredit<T>) -> Self {
|
||||
Credit::Native(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> From<AssetCredit<T>> for Credit<T> {
|
||||
fn from(value: AssetCredit<T>) -> Self {
|
||||
Credit::Asset(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> TryInto<NativeCredit<T>> for Credit<T> {
|
||||
type Error = ();
|
||||
fn try_into(self) -> Result<NativeCredit<T>, ()> {
|
||||
match self {
|
||||
Credit::Native(c) => Ok(c),
|
||||
/// The `PoolId` is represented as a tuple of `AssetKind`s with `FirstAsset` always positioned as
|
||||
/// the first element.
|
||||
pub struct WithFirstAsset<FirstAsset, AccountId, AssetKind>(
|
||||
PhantomData<(FirstAsset, AccountId, AssetKind)>,
|
||||
);
|
||||
impl<FirstAsset, AccountId, AssetKind> PoolLocator<AccountId, AssetKind, (AssetKind, AssetKind)>
|
||||
for WithFirstAsset<FirstAsset, AccountId, AssetKind>
|
||||
where
|
||||
AssetKind: Eq + Clone + Encode,
|
||||
AccountId: Decode,
|
||||
FirstAsset: Get<AssetKind>,
|
||||
{
|
||||
fn pool_id(asset1: &AssetKind, asset2: &AssetKind) -> Result<(AssetKind, AssetKind), ()> {
|
||||
let first = FirstAsset::get();
|
||||
match true {
|
||||
_ if asset1 == asset2 => Err(()),
|
||||
_ if first == *asset1 => Ok((first, asset2.clone())),
|
||||
_ if first == *asset2 => Ok((first, asset1.clone())),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
fn address(id: &(AssetKind, AssetKind)) -> Result<AccountId, ()> {
|
||||
let encoded = sp_io::hashing::blake2_256(&Encode::encode(id)[..]);
|
||||
Decode::decode(&mut TrailingZeroInput::new(encoded.as_ref())).map_err(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> TryInto<AssetCredit<T>> for Credit<T> {
|
||||
type Error = ();
|
||||
fn try_into(self) -> Result<AssetCredit<T>, ()> {
|
||||
match self {
|
||||
Credit::Asset(c) => Ok(c),
|
||||
/// Pool locator where the `PoolId` is a tuple of `AssetKind`s arranged in ascending order.
|
||||
pub struct Ascending<AccountId, AssetKind>(PhantomData<(AccountId, AssetKind)>);
|
||||
impl<AccountId, AssetKind> PoolLocator<AccountId, AssetKind, (AssetKind, AssetKind)>
|
||||
for Ascending<AccountId, AssetKind>
|
||||
where
|
||||
AssetKind: Ord + Clone + Encode,
|
||||
AccountId: Decode,
|
||||
{
|
||||
fn pool_id(asset1: &AssetKind, asset2: &AssetKind) -> Result<(AssetKind, AssetKind), ()> {
|
||||
match true {
|
||||
_ if asset1 > asset2 => Ok((asset2.clone(), asset1.clone())),
|
||||
_ if asset1 < asset2 => Ok((asset1.clone(), asset2.clone())),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> Credit<T> {
|
||||
/// Create zero native credit.
|
||||
pub fn native_zero() -> Self {
|
||||
NativeCredit::<T>::zero().into()
|
||||
}
|
||||
|
||||
/// Amount of `self`.
|
||||
pub fn peek(&self) -> T::Balance {
|
||||
match self {
|
||||
Credit::Native(c) => c.peek(),
|
||||
Credit::Asset(c) => c.peek(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Asset class of `self`.
|
||||
pub fn asset(&self) -> T::MultiAssetId {
|
||||
match self {
|
||||
Credit::Native(_) => T::MultiAssetIdConverter::get_native(),
|
||||
Credit::Asset(c) => c.asset().into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Consume `self` and return two independent instances; the first is guaranteed to be at most
|
||||
/// `amount` and the second will be the remainder.
|
||||
pub fn split(self, amount: T::Balance) -> (Self, Self) {
|
||||
match self {
|
||||
Credit::Native(c) => {
|
||||
let (left, right) = c.split(amount);
|
||||
(left.into(), right.into())
|
||||
},
|
||||
Credit::Asset(c) => {
|
||||
let (left, right) = c.split(amount);
|
||||
(left.into(), right.into())
|
||||
},
|
||||
}
|
||||
fn address(id: &(AssetKind, AssetKind)) -> Result<AccountId, ()> {
|
||||
let encoded = sp_io::hashing::blake2_256(&Encode::encode(id)[..]);
|
||||
Decode::decode(&mut TrailingZeroInput::new(encoded.as_ref())).map_err(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
/// Pool locator that chains the `First` and `Second` implementations of [`PoolLocator`].
|
||||
///
|
||||
/// If the `First` implementation fails, it falls back to the `Second`.
|
||||
pub struct Chain<First, Second>(PhantomData<(First, Second)>);
|
||||
impl<First, Second, AccountId, AssetKind> PoolLocator<AccountId, AssetKind, (AssetKind, AssetKind)>
|
||||
for Chain<First, Second>
|
||||
where
|
||||
First: PoolLocator<AccountId, AssetKind, (AssetKind, AssetKind)>,
|
||||
Second: PoolLocator<AccountId, AssetKind, (AssetKind, AssetKind)>,
|
||||
{
|
||||
fn pool_id(asset1: &AssetKind, asset2: &AssetKind) -> Result<(AssetKind, AssetKind), ()> {
|
||||
First::pool_id(asset1, asset2).or(Second::pool_id(asset1, asset2))
|
||||
}
|
||||
fn address(id: &(AssetKind, AssetKind)) -> Result<AccountId, ()> {
|
||||
First::address(id).or(Second::address(id))
|
||||
}
|
||||
}
|
||||
|
||||
+115
-117
@@ -15,29 +15,27 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Autogenerated weights for pallet_asset_conversion
|
||||
//! Autogenerated weights for `pallet_asset_conversion`
|
||||
//!
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
|
||||
//! DATE: 2023-07-18, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
|
||||
//! DATE: 2023-10-30, STEPS: `5`, REPEAT: `2`, LOW RANGE: `[]`, HIGH RANGE: `[]`
|
||||
//! WORST CASE MAP SIZE: `1000000`
|
||||
//! HOSTNAME: `runner-gghbxkbs-project-145-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
|
||||
//! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024`
|
||||
//! HOSTNAME: `cob`, CPU: `<UNKNOWN>`
|
||||
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024`
|
||||
|
||||
// Executed Command:
|
||||
// target/production/substrate
|
||||
// ./target/debug/substrate-node
|
||||
// benchmark
|
||||
// pallet
|
||||
// --steps=50
|
||||
// --repeat=20
|
||||
// --chain=dev
|
||||
// --steps=5
|
||||
// --repeat=2
|
||||
// --pallet=pallet-asset-conversion
|
||||
// --extrinsic=*
|
||||
// --wasm-execution=compiled
|
||||
// --heap-pages=4096
|
||||
// --json-file=/builds/parity/mirrors/substrate/.git/.artifacts/bench.json
|
||||
// --pallet=pallet_asset_conversion
|
||||
// --chain=dev
|
||||
// --header=./HEADER-APACHE2
|
||||
// --output=./frame/asset-conversion/src/weights.rs
|
||||
// --template=./.maintain/frame-weight-template.hbs
|
||||
// --output=./substrate/frame/asset-conversion/src/weights.rs
|
||||
// --template=./substrate/.maintain/frame-weight-template.hbs
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
#![allow(unused_parens)]
|
||||
@@ -47,25 +45,25 @@
|
||||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
|
||||
use core::marker::PhantomData;
|
||||
|
||||
/// Weight functions needed for pallet_asset_conversion.
|
||||
/// Weight functions needed for `pallet_asset_conversion`.
|
||||
pub trait WeightInfo {
|
||||
fn create_pool() -> Weight;
|
||||
fn add_liquidity() -> Weight;
|
||||
fn remove_liquidity() -> Weight;
|
||||
fn swap_exact_tokens_for_tokens() -> Weight;
|
||||
fn swap_tokens_for_exact_tokens() -> Weight;
|
||||
fn swap_exact_tokens_for_tokens(n: u32, ) -> Weight;
|
||||
fn swap_tokens_for_exact_tokens(n: u32, ) -> Weight;
|
||||
}
|
||||
|
||||
/// Weights for pallet_asset_conversion using the Substrate node and recommended hardware.
|
||||
/// Weights for `pallet_asset_conversion` using the Substrate node and recommended hardware.
|
||||
pub struct SubstrateWeight<T>(PhantomData<T>);
|
||||
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
|
||||
/// Storage: `AssetConversion::Pools` (r:1 w:1)
|
||||
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(30), added: 2505, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:2 w:2)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:1 w:1)
|
||||
/// Storage: `Assets::Account` (r:2 w:2)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Asset` (r:1 w:1)
|
||||
/// Storage: `Assets::Asset` (r:2 w:2)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `AssetConversion::NextPoolAssetId` (r:1 w:1)
|
||||
/// Proof: `AssetConversion::NextPoolAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
|
||||
@@ -75,20 +73,18 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
|
||||
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
fn create_pool() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `729`
|
||||
// Estimated: `6196`
|
||||
// Minimum execution time: 131_688_000 picoseconds.
|
||||
Weight::from_parts(134_092_000, 6196)
|
||||
.saturating_add(T::DbWeight::get().reads(8_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(8_u64))
|
||||
// Measured: `1081`
|
||||
// Estimated: `6360`
|
||||
// Minimum execution time: 1_576_000_000 picoseconds.
|
||||
Weight::from_parts(1_668_000_000, 6360)
|
||||
.saturating_add(T::DbWeight::get().reads(10_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(10_u64))
|
||||
}
|
||||
/// Storage: `AssetConversion::Pools` (r:1 w:0)
|
||||
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(30), added: 2505, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Asset` (r:1 w:1)
|
||||
/// Storage: `Assets::Asset` (r:2 w:2)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:2 w:2)
|
||||
/// Storage: `Assets::Account` (r:4 w:4)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `PoolAssets::Asset` (r:1 w:1)
|
||||
/// Proof: `PoolAssets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
@@ -96,20 +92,18 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
|
||||
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
fn add_liquidity() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1382`
|
||||
// Estimated: `6208`
|
||||
// Minimum execution time: 157_310_000 picoseconds.
|
||||
Weight::from_parts(161_547_000, 6208)
|
||||
.saturating_add(T::DbWeight::get().reads(8_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(7_u64))
|
||||
// Measured: `1761`
|
||||
// Estimated: `11426`
|
||||
// Minimum execution time: 1_636_000_000 picoseconds.
|
||||
Weight::from_parts(1_894_000_000, 11426)
|
||||
.saturating_add(T::DbWeight::get().reads(10_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(9_u64))
|
||||
}
|
||||
/// Storage: `AssetConversion::Pools` (r:1 w:0)
|
||||
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(30), added: 2505, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Asset` (r:1 w:1)
|
||||
/// Storage: `Assets::Asset` (r:2 w:2)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:2 w:2)
|
||||
/// Storage: `Assets::Account` (r:4 w:4)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `PoolAssets::Asset` (r:1 w:1)
|
||||
/// Proof: `PoolAssets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
@@ -117,42 +111,46 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
|
||||
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
fn remove_liquidity() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1371`
|
||||
// Estimated: `6208`
|
||||
// Minimum execution time: 142_769_000 picoseconds.
|
||||
Weight::from_parts(145_139_000, 6208)
|
||||
.saturating_add(T::DbWeight::get().reads(7_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(6_u64))
|
||||
// Measured: `1750`
|
||||
// Estimated: `11426`
|
||||
// Minimum execution time: 1_507_000_000 picoseconds.
|
||||
Weight::from_parts(1_524_000_000, 11426)
|
||||
.saturating_add(T::DbWeight::get().reads(9_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(8_u64))
|
||||
}
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Asset` (r:3 w:3)
|
||||
/// Storage: `Assets::Asset` (r:4 w:4)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:6 w:6)
|
||||
/// Storage: `Assets::Account` (r:8 w:8)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
fn swap_exact_tokens_for_tokens() -> Weight {
|
||||
/// The range of component `n` is `[2, 4]`.
|
||||
fn swap_exact_tokens_for_tokens(n: u32, ) -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1738`
|
||||
// Estimated: `16644`
|
||||
// Minimum execution time: 213_186_000 picoseconds.
|
||||
Weight::from_parts(217_471_000, 16644)
|
||||
.saturating_add(T::DbWeight::get().reads(10_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(10_u64))
|
||||
// Measured: `0 + n * (522 ±0)`
|
||||
// Estimated: `990 + n * (5218 ±0)`
|
||||
// Minimum execution time: 937_000_000 picoseconds.
|
||||
Weight::from_parts(941_000_000, 990)
|
||||
// Standard Error: 40_863_477
|
||||
.saturating_add(Weight::from_parts(205_862_068, 0).saturating_mul(n.into()))
|
||||
.saturating_add(T::DbWeight::get().reads((3_u64).saturating_mul(n.into())))
|
||||
.saturating_add(T::DbWeight::get().writes((3_u64).saturating_mul(n.into())))
|
||||
.saturating_add(Weight::from_parts(0, 5218).saturating_mul(n.into()))
|
||||
}
|
||||
/// Storage: `Assets::Asset` (r:3 w:3)
|
||||
/// Storage: `Assets::Asset` (r:4 w:4)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:6 w:6)
|
||||
/// Storage: `Assets::Account` (r:8 w:8)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
fn swap_tokens_for_exact_tokens() -> Weight {
|
||||
/// The range of component `n` is `[2, 4]`.
|
||||
fn swap_tokens_for_exact_tokens(n: u32, ) -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1738`
|
||||
// Estimated: `16644`
|
||||
// Minimum execution time: 213_793_000 picoseconds.
|
||||
Weight::from_parts(218_584_000, 16644)
|
||||
.saturating_add(T::DbWeight::get().reads(10_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(10_u64))
|
||||
// Measured: `0 + n * (522 ±0)`
|
||||
// Estimated: `990 + n * (5218 ±0)`
|
||||
// Minimum execution time: 935_000_000 picoseconds.
|
||||
Weight::from_parts(947_000_000, 990)
|
||||
// Standard Error: 46_904_620
|
||||
.saturating_add(Weight::from_parts(218_275_862, 0).saturating_mul(n.into()))
|
||||
.saturating_add(T::DbWeight::get().reads((3_u64).saturating_mul(n.into())))
|
||||
.saturating_add(T::DbWeight::get().writes((3_u64).saturating_mul(n.into())))
|
||||
.saturating_add(Weight::from_parts(0, 5218).saturating_mul(n.into()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,9 +160,9 @@ impl WeightInfo for () {
|
||||
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(30), added: 2505, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:2 w:2)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:1 w:1)
|
||||
/// Storage: `Assets::Account` (r:2 w:2)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Asset` (r:1 w:1)
|
||||
/// Storage: `Assets::Asset` (r:2 w:2)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `AssetConversion::NextPoolAssetId` (r:1 w:1)
|
||||
/// Proof: `AssetConversion::NextPoolAssetId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
|
||||
@@ -174,20 +172,18 @@ impl WeightInfo for () {
|
||||
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
fn create_pool() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `729`
|
||||
// Estimated: `6196`
|
||||
// Minimum execution time: 131_688_000 picoseconds.
|
||||
Weight::from_parts(134_092_000, 6196)
|
||||
.saturating_add(RocksDbWeight::get().reads(8_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(8_u64))
|
||||
// Measured: `1081`
|
||||
// Estimated: `6360`
|
||||
// Minimum execution time: 1_576_000_000 picoseconds.
|
||||
Weight::from_parts(1_668_000_000, 6360)
|
||||
.saturating_add(RocksDbWeight::get().reads(10_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(10_u64))
|
||||
}
|
||||
/// Storage: `AssetConversion::Pools` (r:1 w:0)
|
||||
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(30), added: 2505, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Asset` (r:1 w:1)
|
||||
/// Storage: `Assets::Asset` (r:2 w:2)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:2 w:2)
|
||||
/// Storage: `Assets::Account` (r:4 w:4)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `PoolAssets::Asset` (r:1 w:1)
|
||||
/// Proof: `PoolAssets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
@@ -195,20 +191,18 @@ impl WeightInfo for () {
|
||||
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
fn add_liquidity() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1382`
|
||||
// Estimated: `6208`
|
||||
// Minimum execution time: 157_310_000 picoseconds.
|
||||
Weight::from_parts(161_547_000, 6208)
|
||||
.saturating_add(RocksDbWeight::get().reads(8_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(7_u64))
|
||||
// Measured: `1761`
|
||||
// Estimated: `11426`
|
||||
// Minimum execution time: 1_636_000_000 picoseconds.
|
||||
Weight::from_parts(1_894_000_000, 11426)
|
||||
.saturating_add(RocksDbWeight::get().reads(10_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(9_u64))
|
||||
}
|
||||
/// Storage: `AssetConversion::Pools` (r:1 w:0)
|
||||
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(30), added: 2505, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Asset` (r:1 w:1)
|
||||
/// Storage: `Assets::Asset` (r:2 w:2)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:2 w:2)
|
||||
/// Storage: `Assets::Account` (r:4 w:4)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `PoolAssets::Asset` (r:1 w:1)
|
||||
/// Proof: `PoolAssets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
@@ -216,41 +210,45 @@ impl WeightInfo for () {
|
||||
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
fn remove_liquidity() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1371`
|
||||
// Estimated: `6208`
|
||||
// Minimum execution time: 142_769_000 picoseconds.
|
||||
Weight::from_parts(145_139_000, 6208)
|
||||
.saturating_add(RocksDbWeight::get().reads(7_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(6_u64))
|
||||
// Measured: `1750`
|
||||
// Estimated: `11426`
|
||||
// Minimum execution time: 1_507_000_000 picoseconds.
|
||||
Weight::from_parts(1_524_000_000, 11426)
|
||||
.saturating_add(RocksDbWeight::get().reads(9_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(8_u64))
|
||||
}
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Asset` (r:3 w:3)
|
||||
/// Storage: `Assets::Asset` (r:4 w:4)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:6 w:6)
|
||||
/// Storage: `Assets::Account` (r:8 w:8)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
fn swap_exact_tokens_for_tokens() -> Weight {
|
||||
/// The range of component `n` is `[2, 4]`.
|
||||
fn swap_exact_tokens_for_tokens(n: u32, ) -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1738`
|
||||
// Estimated: `16644`
|
||||
// Minimum execution time: 213_186_000 picoseconds.
|
||||
Weight::from_parts(217_471_000, 16644)
|
||||
.saturating_add(RocksDbWeight::get().reads(10_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(10_u64))
|
||||
// Measured: `0 + n * (522 ±0)`
|
||||
// Estimated: `990 + n * (5218 ±0)`
|
||||
// Minimum execution time: 937_000_000 picoseconds.
|
||||
Weight::from_parts(941_000_000, 990)
|
||||
// Standard Error: 40_863_477
|
||||
.saturating_add(Weight::from_parts(205_862_068, 0).saturating_mul(n.into()))
|
||||
.saturating_add(RocksDbWeight::get().reads((3_u64).saturating_mul(n.into())))
|
||||
.saturating_add(RocksDbWeight::get().writes((3_u64).saturating_mul(n.into())))
|
||||
.saturating_add(Weight::from_parts(0, 5218).saturating_mul(n.into()))
|
||||
}
|
||||
/// Storage: `Assets::Asset` (r:3 w:3)
|
||||
/// Storage: `Assets::Asset` (r:4 w:4)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:6 w:6)
|
||||
/// Storage: `Assets::Account` (r:8 w:8)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
fn swap_tokens_for_exact_tokens() -> Weight {
|
||||
/// The range of component `n` is `[2, 4]`.
|
||||
fn swap_tokens_for_exact_tokens(n: u32, ) -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1738`
|
||||
// Estimated: `16644`
|
||||
// Minimum execution time: 213_793_000 picoseconds.
|
||||
Weight::from_parts(218_584_000, 16644)
|
||||
.saturating_add(RocksDbWeight::get().reads(10_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(10_u64))
|
||||
// Measured: `0 + n * (522 ±0)`
|
||||
// Estimated: `990 + n * (5218 ±0)`
|
||||
// Minimum execution time: 935_000_000 picoseconds.
|
||||
Weight::from_parts(947_000_000, 990)
|
||||
// Standard Error: 46_904_620
|
||||
.saturating_add(Weight::from_parts(218_275_862, 0).saturating_mul(n.into()))
|
||||
.saturating_add(RocksDbWeight::get().reads((3_u64).saturating_mul(n.into())))
|
||||
.saturating_add(RocksDbWeight::get().writes((3_u64).saturating_mul(n.into())))
|
||||
.saturating_add(Weight::from_parts(0, 5218).saturating_mul(n.into()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user