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

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

Decoupling Pallet from the Concept of Native Currency

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

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

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

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

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

---------

Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
This commit is contained in:
Muharem
2023-12-20 13:57:26 +01:00
committed by GitHub
parent d32f66fb8f
commit 4f832ea865
26 changed files with 1750 additions and 4571 deletions
@@ -266,7 +266,7 @@ fn cannot_create_pool_from_pool_assets() {
Box::new(asset_native),
Box::new(asset_one),
),
Err(DispatchError::Module(ModuleError{index: _, error: _, message})) => assert_eq!(message, Some("UnsupportedAsset"))
Err(DispatchError::Module(ModuleError{index: _, error: _, message})) => assert_eq!(message, Some("Unknown"))
);
});
}
@@ -260,7 +260,7 @@ fn cannot_create_pool_from_pool_assets() {
Box::new(asset_native),
Box::new(asset_one),
),
Err(DispatchError::Module(ModuleError{index: _, error: _, message})) => assert_eq!(message, Some("UnsupportedAsset"))
Err(DispatchError::Module(ModuleError{index: _, error: _, message})) => assert_eq!(message, Some("Unknown"))
);
});
}
File diff suppressed because it is too large Load Diff
@@ -1,640 +0,0 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::{
AccountId, AllPalletsWithSystem, Assets, Authorship, Balance, Balances, ParachainInfo,
ParachainSystem, PolkadotXcm, PoolAssets, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin,
TransactionByteFee, TrustBackedAssetsInstance, WeightToFee, XcmpQueue,
};
use crate::{ForeignAssets, CENTS};
use assets_common::{
local_and_foreign_assets::MatchesLocalAndForeignAssetsMultiLocation,
matching::{FromSiblingParachain, IsForeignConcreteAsset},
};
use frame_support::{
match_types, parameter_types,
traits::{ConstU32, Contains, Everything, Nothing, PalletInfoAccess},
};
use frame_system::EnsureRoot;
use pallet_xcm::XcmPassthrough;
use parachains_common::{
impls::ToStakingPot,
xcm_config::{AssetFeeAsExistentialDepositMultiplier, ConcreteAssetFromSystem},
};
use polkadot_parachain_primitives::primitives::Sibling;
use polkadot_runtime_common::xcm_sender::ExponentialPrice;
use sp_runtime::traits::ConvertInto;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, CurrencyAdapter,
DenyReserveTransferToRelayChain, DenyThenTry, DescribeAllTerminal, DescribeFamily,
EnsureXcmOrigin, FungiblesAdapter, HashedDescription, IsConcrete, LocalMint, NoChecking,
ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, StartsWith, StartsWithExplicitGlobalConsensus, TakeWeightCredit,
TrailingSetTopicAsId, UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};
#[cfg(feature = "runtime-benchmarks")]
use {cumulus_primitives_core::ParaId, sp_core::Get};
parameter_types! {
pub const KsmLocation: MultiLocation = MultiLocation::parent();
pub const RelayNetwork: Option<NetworkId> = Some(NetworkId::Kusama);
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation =
X2(GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into()));
pub UniversalLocationNetworkId: NetworkId = UniversalLocation::get().global_consensus().unwrap();
pub TrustBackedAssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
pub ForeignAssetsPalletLocation: MultiLocation =
PalletInstance(<ForeignAssets as PalletInfoAccess>::index() as u8).into();
pub PoolAssetsPalletLocation: MultiLocation =
PalletInstance(<PoolAssets as PalletInfoAccess>::index() as u8).into();
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
pub const GovernanceLocation: MultiLocation = MultiLocation::parent();
pub const FellowshipLocation: MultiLocation = MultiLocation::parent();
}
/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used
/// when determining ownership of accounts for asset transacting and when attempting to use XCM
/// `Transact` in order to determine the dispatch Origin.
pub type LocationToAccountId = (
// The parent (Relay-chain) origin converts to the parent `AccountId`.
ParentIsPreset<AccountId>,
// Sibling parachain origins convert to AccountId via the `ParaId::into`.
SiblingParachainConvertsVia<Sibling, AccountId>,
// Straight up local `AccountId32` origins just alias directly to `AccountId`.
AccountId32Aliases<RelayNetwork, AccountId>,
// Foreign locations alias into accounts according to a hash of their standard description.
HashedDescription<AccountId, DescribeFamily<DescribeAllTerminal>>,
);
/// Means for transacting the native currency on this chain.
pub type CurrencyTransactor = CurrencyAdapter<
// Use this currency:
Balances,
// Use this currency when it is a fungible asset matching the given location or name:
IsConcrete<KsmLocation>,
// Convert an XCM MultiLocation into a local account id:
LocationToAccountId,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
AccountId,
// We don't track any teleports of `Balances`.
(),
>;
/// `AssetId`/`Balance` converter for `PoolAssets`.
pub type TrustBackedAssetsConvertedConcreteId =
assets_common::TrustBackedAssetsConvertedConcreteId<TrustBackedAssetsPalletLocation, Balance>;
/// Means for transacting assets besides the native currency on this chain.
pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation:
Assets,
// Use this currency when it is a fungible asset matching the given location or name:
TrustBackedAssetsConvertedConcreteId,
// Convert an XCM MultiLocation into a local account id:
LocationToAccountId,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
AccountId,
// We only want to allow teleports of known assets. We use non-zero issuance as an indication
// that this asset is known.
LocalMint<parachains_common::impls::NonZeroIssuance<AccountId, Assets>>,
// The account to use for tracking teleports.
CheckingAccount,
>;
/// `AssetId/Balance` converter for `TrustBackedAssets`
pub type ForeignAssetsConvertedConcreteId = assets_common::ForeignAssetsConvertedConcreteId<
(
// Ignore `TrustBackedAssets` explicitly
StartsWith<TrustBackedAssetsPalletLocation>,
// Ignore assets that start explicitly with our `GlobalConsensus(NetworkId)`, means:
// - foreign assets from our consensus should be: `MultiLocation {parents: 1,
// X*(Parachain(xyz), ..)}`
// - foreign assets outside our consensus with the same `GlobalConsensus(NetworkId)` won't
// be accepted here
StartsWithExplicitGlobalConsensus<UniversalLocationNetworkId>,
),
Balance,
>;
/// Means for transacting foreign assets from different global consensus.
pub type ForeignFungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation:
ForeignAssets,
// Use this currency when it is a fungible asset matching the given location or name:
ForeignAssetsConvertedConcreteId,
// Convert an XCM MultiLocation into a local account id:
LocationToAccountId,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
AccountId,
// We dont need to check teleports here.
NoChecking,
// The account to use for tracking teleports.
CheckingAccount,
>;
/// `AssetId`/`Balance` converter for `PoolAssets`.
pub type PoolAssetsConvertedConcreteId =
assets_common::PoolAssetsConvertedConcreteId<PoolAssetsPalletLocation, Balance>;
/// Means for transacting asset conversion pool assets on this chain.
pub type PoolFungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation:
PoolAssets,
// Use this currency when it is a fungible asset matching the given location or name:
PoolAssetsConvertedConcreteId,
// Convert an XCM MultiLocation into a local account id:
LocationToAccountId,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
AccountId,
// We only want to allow teleports of known assets. We use non-zero issuance as an indication
// that this asset is known.
LocalMint<parachains_common::impls::NonZeroIssuance<AccountId, PoolAssets>>,
// The account to use for tracking teleports.
CheckingAccount,
>;
/// Means for transacting assets on this chain.
pub type AssetTransactors =
(CurrencyTransactor, FungiblesTransactor, ForeignFungiblesTransactor, PoolFungiblesTransactor);
/// Simple `MultiLocation` matcher for Local and Foreign asset `MultiLocation`.
pub struct LocalAndForeignAssetsMultiLocationMatcher;
impl MatchesLocalAndForeignAssetsMultiLocation for LocalAndForeignAssetsMultiLocationMatcher {
fn is_local(location: &MultiLocation) -> bool {
use assets_common::fungible_conversion::MatchesMultiLocation;
TrustBackedAssetsConvertedConcreteId::contains(location)
}
fn is_foreign(location: &MultiLocation) -> bool {
use assets_common::fungible_conversion::MatchesMultiLocation;
ForeignAssetsConvertedConcreteId::contains(location)
}
}
impl Contains<MultiLocation> for LocalAndForeignAssetsMultiLocationMatcher {
fn contains(location: &MultiLocation) -> bool {
Self::is_local(location) || Self::is_foreign(location)
}
}
/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
/// biases the kind of local `Origin` it will become.
pub type XcmOriginToTransactDispatchOrigin = (
// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
// using `LocationToAccountId` and then turn that into the usual `Signed` origin. Useful for
// foreign chains who want to have a local sovereign account on this chain which they control.
SovereignSignedViaLocation<LocationToAccountId, RuntimeOrigin>,
// Native converter for Relay-chain (Parent) location; will convert to a `Relay` origin when
// recognised.
RelayChainAsNative<RelayChainOrigin, RuntimeOrigin>,
// Native converter for sibling Parachains; will convert to a `SiblingPara` origin when
// recognised.
SiblingParachainAsNative<cumulus_pallet_xcm::Origin, RuntimeOrigin>,
// Superuser converter for the Relay-chain (Parent) location. This will allow it to issue a
// transaction from the Root origin.
ParentAsSuperuser<RuntimeOrigin>,
// Native signed account converter; this just converts an `AccountId32` origin into a normal
// `RuntimeOrigin::Signed` origin of the same 32-byte value.
SignedAccountId32AsNative<RelayNetwork, RuntimeOrigin>,
// Xcm origins can be represented natively under the Xcm pallet's Xcm origin.
XcmPassthrough<RuntimeOrigin>,
);
parameter_types! {
pub const MaxInstructions: u32 = 100;
pub const MaxAssetsIntoHolding: u32 = 64;
pub XcmAssetFeesReceiver: Option<AccountId> = Authorship::author();
}
match_types! {
pub type ParentOrParentsPlurality: impl Contains<MultiLocation> = {
MultiLocation { parents: 1, interior: Here } |
MultiLocation { parents: 1, interior: X1(Plurality { .. }) }
};
pub type ParentOrSiblings: impl Contains<MultiLocation> = {
MultiLocation { parents: 1, interior: Here } |
MultiLocation { parents: 1, interior: X1(_) }
};
}
/// A call filter for the XCM Transact instruction. This is a temporary measure until we properly
/// account for proof size weights.
///
/// Calls that are allowed through this filter must:
/// 1. Have a fixed weight;
/// 2. Cannot lead to another call being made;
/// 3. Have a defined proof size weight, e.g. no unbounded vecs in call parameters.
pub struct SafeCallFilter;
impl Contains<RuntimeCall> for SafeCallFilter {
fn contains(call: &RuntimeCall) -> bool {
#[cfg(feature = "runtime-benchmarks")]
{
if matches!(call, RuntimeCall::System(frame_system::Call::remark_with_event { .. })) {
return true
}
}
matches!(
call,
RuntimeCall::PolkadotXcm(pallet_xcm::Call::force_xcm_version { .. }) |
RuntimeCall::System(
frame_system::Call::set_heap_pages { .. } |
frame_system::Call::set_code { .. } |
frame_system::Call::set_code_without_checks { .. } |
frame_system::Call::kill_prefix { .. },
) | RuntimeCall::ParachainSystem(..) |
RuntimeCall::Timestamp(..) |
RuntimeCall::Balances(..) |
RuntimeCall::CollatorSelection(
pallet_collator_selection::Call::set_desired_candidates { .. } |
pallet_collator_selection::Call::set_candidacy_bond { .. } |
pallet_collator_selection::Call::register_as_candidate { .. } |
pallet_collator_selection::Call::leave_intent { .. } |
pallet_collator_selection::Call::set_invulnerables { .. } |
pallet_collator_selection::Call::add_invulnerable { .. } |
pallet_collator_selection::Call::remove_invulnerable { .. },
) | RuntimeCall::Session(pallet_session::Call::purge_keys { .. }) |
RuntimeCall::XcmpQueue(..) |
RuntimeCall::DmpQueue(..) |
RuntimeCall::Assets(
pallet_assets::Call::create { .. } |
pallet_assets::Call::force_create { .. } |
pallet_assets::Call::start_destroy { .. } |
pallet_assets::Call::destroy_accounts { .. } |
pallet_assets::Call::destroy_approvals { .. } |
pallet_assets::Call::finish_destroy { .. } |
pallet_assets::Call::block { .. } |
pallet_assets::Call::mint { .. } |
pallet_assets::Call::burn { .. } |
pallet_assets::Call::transfer { .. } |
pallet_assets::Call::transfer_keep_alive { .. } |
pallet_assets::Call::force_transfer { .. } |
pallet_assets::Call::freeze { .. } |
pallet_assets::Call::thaw { .. } |
pallet_assets::Call::freeze_asset { .. } |
pallet_assets::Call::thaw_asset { .. } |
pallet_assets::Call::transfer_ownership { .. } |
pallet_assets::Call::set_team { .. } |
pallet_assets::Call::set_metadata { .. } |
pallet_assets::Call::clear_metadata { .. } |
pallet_assets::Call::force_set_metadata { .. } |
pallet_assets::Call::force_clear_metadata { .. } |
pallet_assets::Call::force_asset_status { .. } |
pallet_assets::Call::approve_transfer { .. } |
pallet_assets::Call::cancel_approval { .. } |
pallet_assets::Call::force_cancel_approval { .. } |
pallet_assets::Call::transfer_approved { .. } |
pallet_assets::Call::touch { .. } |
pallet_assets::Call::touch_other { .. } |
pallet_assets::Call::refund { .. } |
pallet_assets::Call::refund_other { .. },
) | RuntimeCall::ForeignAssets(
pallet_assets::Call::create { .. } |
pallet_assets::Call::force_create { .. } |
pallet_assets::Call::start_destroy { .. } |
pallet_assets::Call::destroy_accounts { .. } |
pallet_assets::Call::destroy_approvals { .. } |
pallet_assets::Call::finish_destroy { .. } |
pallet_assets::Call::block { .. } |
pallet_assets::Call::mint { .. } |
pallet_assets::Call::burn { .. } |
pallet_assets::Call::transfer { .. } |
pallet_assets::Call::transfer_keep_alive { .. } |
pallet_assets::Call::force_transfer { .. } |
pallet_assets::Call::freeze { .. } |
pallet_assets::Call::thaw { .. } |
pallet_assets::Call::freeze_asset { .. } |
pallet_assets::Call::thaw_asset { .. } |
pallet_assets::Call::transfer_ownership { .. } |
pallet_assets::Call::set_team { .. } |
pallet_assets::Call::set_metadata { .. } |
pallet_assets::Call::clear_metadata { .. } |
pallet_assets::Call::force_set_metadata { .. } |
pallet_assets::Call::force_clear_metadata { .. } |
pallet_assets::Call::force_asset_status { .. } |
pallet_assets::Call::approve_transfer { .. } |
pallet_assets::Call::cancel_approval { .. } |
pallet_assets::Call::force_cancel_approval { .. } |
pallet_assets::Call::transfer_approved { .. } |
pallet_assets::Call::touch { .. } |
pallet_assets::Call::touch_other { .. } |
pallet_assets::Call::refund { .. } |
pallet_assets::Call::refund_other { .. },
) | RuntimeCall::PoolAssets(
pallet_assets::Call::force_create { .. } |
pallet_assets::Call::block { .. } |
pallet_assets::Call::burn { .. } |
pallet_assets::Call::transfer { .. } |
pallet_assets::Call::transfer_keep_alive { .. } |
pallet_assets::Call::force_transfer { .. } |
pallet_assets::Call::freeze { .. } |
pallet_assets::Call::thaw { .. } |
pallet_assets::Call::freeze_asset { .. } |
pallet_assets::Call::thaw_asset { .. } |
pallet_assets::Call::transfer_ownership { .. } |
pallet_assets::Call::set_team { .. } |
pallet_assets::Call::set_metadata { .. } |
pallet_assets::Call::clear_metadata { .. } |
pallet_assets::Call::force_set_metadata { .. } |
pallet_assets::Call::force_clear_metadata { .. } |
pallet_assets::Call::force_asset_status { .. } |
pallet_assets::Call::approve_transfer { .. } |
pallet_assets::Call::cancel_approval { .. } |
pallet_assets::Call::force_cancel_approval { .. } |
pallet_assets::Call::transfer_approved { .. } |
pallet_assets::Call::touch { .. } |
pallet_assets::Call::touch_other { .. } |
pallet_assets::Call::refund { .. } |
pallet_assets::Call::refund_other { .. },
) | RuntimeCall::AssetConversion(
pallet_asset_conversion::Call::create_pool { .. } |
pallet_asset_conversion::Call::add_liquidity { .. } |
pallet_asset_conversion::Call::remove_liquidity { .. } |
pallet_asset_conversion::Call::swap_tokens_for_exact_tokens { .. } |
pallet_asset_conversion::Call::swap_exact_tokens_for_tokens { .. },
) | RuntimeCall::NftFractionalization(
pallet_nft_fractionalization::Call::fractionalize { .. } |
pallet_nft_fractionalization::Call::unify { .. },
) | RuntimeCall::Nfts(
pallet_nfts::Call::create { .. } |
pallet_nfts::Call::force_create { .. } |
pallet_nfts::Call::destroy { .. } |
pallet_nfts::Call::mint { .. } |
pallet_nfts::Call::force_mint { .. } |
pallet_nfts::Call::burn { .. } |
pallet_nfts::Call::transfer { .. } |
pallet_nfts::Call::lock_item_transfer { .. } |
pallet_nfts::Call::unlock_item_transfer { .. } |
pallet_nfts::Call::lock_collection { .. } |
pallet_nfts::Call::transfer_ownership { .. } |
pallet_nfts::Call::set_team { .. } |
pallet_nfts::Call::force_collection_owner { .. } |
pallet_nfts::Call::force_collection_config { .. } |
pallet_nfts::Call::approve_transfer { .. } |
pallet_nfts::Call::cancel_approval { .. } |
pallet_nfts::Call::clear_all_transfer_approvals { .. } |
pallet_nfts::Call::lock_item_properties { .. } |
pallet_nfts::Call::set_attribute { .. } |
pallet_nfts::Call::force_set_attribute { .. } |
pallet_nfts::Call::clear_attribute { .. } |
pallet_nfts::Call::approve_item_attributes { .. } |
pallet_nfts::Call::cancel_item_attributes_approval { .. } |
pallet_nfts::Call::set_metadata { .. } |
pallet_nfts::Call::clear_metadata { .. } |
pallet_nfts::Call::set_collection_metadata { .. } |
pallet_nfts::Call::clear_collection_metadata { .. } |
pallet_nfts::Call::set_accept_ownership { .. } |
pallet_nfts::Call::set_collection_max_supply { .. } |
pallet_nfts::Call::update_mint_settings { .. } |
pallet_nfts::Call::set_price { .. } |
pallet_nfts::Call::buy_item { .. } |
pallet_nfts::Call::pay_tips { .. } |
pallet_nfts::Call::create_swap { .. } |
pallet_nfts::Call::cancel_swap { .. } |
pallet_nfts::Call::claim_swap { .. },
) | RuntimeCall::Uniques(
pallet_uniques::Call::create { .. } |
pallet_uniques::Call::force_create { .. } |
pallet_uniques::Call::destroy { .. } |
pallet_uniques::Call::mint { .. } |
pallet_uniques::Call::burn { .. } |
pallet_uniques::Call::transfer { .. } |
pallet_uniques::Call::freeze { .. } |
pallet_uniques::Call::thaw { .. } |
pallet_uniques::Call::freeze_collection { .. } |
pallet_uniques::Call::thaw_collection { .. } |
pallet_uniques::Call::transfer_ownership { .. } |
pallet_uniques::Call::set_team { .. } |
pallet_uniques::Call::approve_transfer { .. } |
pallet_uniques::Call::cancel_approval { .. } |
pallet_uniques::Call::force_item_status { .. } |
pallet_uniques::Call::set_attribute { .. } |
pallet_uniques::Call::clear_attribute { .. } |
pallet_uniques::Call::set_metadata { .. } |
pallet_uniques::Call::clear_metadata { .. } |
pallet_uniques::Call::set_collection_metadata { .. } |
pallet_uniques::Call::clear_collection_metadata { .. } |
pallet_uniques::Call::set_accept_ownership { .. } |
pallet_uniques::Call::set_collection_max_supply { .. } |
pallet_uniques::Call::set_price { .. } |
pallet_uniques::Call::buy_item { .. }
)
)
}
}
pub type Barrier = TrailingSetTopicAsId<
DenyThenTry<
DenyReserveTransferToRelayChain,
(
TakeWeightCredit,
// Expected responses are OK.
AllowKnownQueryResponses<PolkadotXcm>,
// Allow XCMs with some computed origins to pass through.
WithComputedOrigin<
(
// If the message is one that immediately attempts to pay for execution, then
// allow it.
AllowTopLevelPaidExecutionFrom<Everything>,
// Parent and its pluralities (i.e. governance bodies) get free execution.
AllowExplicitUnpaidExecutionFrom<ParentOrParentsPlurality>,
// Subscriptions for version tracking are OK.
AllowSubscriptionsFrom<ParentOrSiblings>,
),
UniversalLocation,
ConstU32<8>,
>,
),
>,
>;
pub type AssetFeeAsExistentialDepositMultiplierFeeCharger = AssetFeeAsExistentialDepositMultiplier<
Runtime,
WeightToFee,
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto, TrustBackedAssetsInstance>,
TrustBackedAssetsInstance,
>;
/// Cases where a remote origin is accepted as trusted Teleporter for a given asset:
///
/// - KSM with the parent Relay Chain and sibling system parachains; and
/// - Sibling parachains' assets from where they originate (as `ForeignCreators`).
pub type TrustedTeleporters = (
ConcreteAssetFromSystem<KsmLocation>,
IsForeignConcreteAsset<FromSiblingParachain<parachain_info::Pallet<Runtime>>>,
);
pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = XcmRouter;
type AssetTransactor = AssetTransactors;
type OriginConverter = XcmOriginToTransactDispatchOrigin;
// Asset Hub Kusama does not recognize a reserve location for any asset. This does not prevent
// Asset Hub acting _as_ a reserve location for KSM and assets created under `pallet-assets`.
// For KSM, users must use teleport where allowed (e.g. with the Relay Chain).
type IsReserve = ();
type IsTeleporter = TrustedTeleporters;
type UniversalLocation = UniversalLocation;
type Barrier = Barrier;
type Weigher = WeightInfoBounds<
crate::weights::xcm::AssetHubKusamaXcmWeight<RuntimeCall>,
RuntimeCall,
MaxInstructions,
>;
type Trader = (
UsingComponents<WeightToFee, KsmLocation, AccountId, Balances, ToStakingPot<Runtime>>,
cumulus_primitives_utility::TakeFirstAssetTrader<
AccountId,
AssetFeeAsExistentialDepositMultiplierFeeCharger,
TrustBackedAssetsConvertedConcreteId,
Assets,
cumulus_primitives_utility::XcmFeesTo32ByteAccount<
FungiblesTransactor,
AccountId,
XcmAssetFeesReceiver,
>,
>,
);
type ResponseHandler = PolkadotXcm;
type AssetTrap = PolkadotXcm;
type AssetClaims = PolkadotXcm;
type SubscriptionService = PolkadotXcm;
type PalletInstancesInfo = AllPalletsWithSystem;
type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
type AssetLocker = ();
type AssetExchanger = ();
type FeeManager = ();
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = WithOriginFilter<SafeCallFilter>;
type SafeCallFilter = SafeCallFilter;
type Aliasers = Nothing;
}
/// Converts a local signed origin into an XCM multilocation.
/// Forms the basis for local origins sending/executing XCMs.
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
parameter_types! {
/// The asset ID for the asset that we use to pay for message delivery fees.
pub FeeAssetId: AssetId = Concrete(KsmLocation::get());
/// The base fee for the message delivery fees.
pub const BaseDeliveryFee: u128 = CENTS.saturating_mul(3);
}
pub type PriceForParentDelivery =
ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, ParachainSystem>;
/// The means for routing XCM messages which are not for local execution into the right message
/// queues.
pub type XcmRouter = WithUniqueTopic<(
// Two routers - use UMP to communicate with the relay chain:
cumulus_primitives_utility::ParentAsUmp<ParachainSystem, PolkadotXcm, PriceForParentDelivery>,
// ..and XCMP to communicate with the sibling chains.
XcmpQueue,
)>;
#[cfg(feature = "runtime-benchmarks")]
parameter_types! {
pub ReachableDest: Option<MultiLocation> = Some(Parent.into());
}
impl pallet_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
// We want to disallow users sending (arbitrary) XCMs from this chain.
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, ()>;
type XcmRouter = XcmRouter;
// We support local origins dispatching XCM executions in principle...
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
// ... but disallow generic XCM execution. As a result only teleports and reserve transfers are
// allowed.
type XcmExecuteFilter = Nothing;
type XcmExecutor = XcmExecutor<XcmConfig>;
type XcmTeleportFilter = Everything;
type XcmReserveTransferFilter = Everything;
type Weigher = WeightInfoBounds<
crate::weights::xcm::AssetHubKusamaXcmWeight<RuntimeCall>,
RuntimeCall,
MaxInstructions,
>;
type UniversalLocation = UniversalLocation;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type Currency = Balances;
type CurrencyMatcher = ();
type TrustedLockers = ();
type SovereignAccountOf = LocationToAccountId;
type MaxLockers = ConstU32<8>;
type WeightInfo = crate::weights::pallet_xcm::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type ReachableDest = ReachableDest;
type AdminOrigin = EnsureRoot<AccountId>;
type MaxRemoteLockConsumers = ConstU32<0>;
type RemoteLockConsumerIdentifier = ();
}
impl cumulus_pallet_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type XcmExecutor = XcmExecutor<XcmConfig>;
}
pub type ForeignCreatorsSovereignAccountOf = (
SiblingParachainConvertsVia<Sibling, AccountId>,
AccountId32Aliases<RelayNetwork, AccountId>,
ParentIsPreset<AccountId>,
);
/// Simple conversion of `u32` into an `AssetId` for use in benchmarking.
pub struct XcmBenchmarkHelper;
#[cfg(feature = "runtime-benchmarks")]
impl pallet_assets::BenchmarkHelper<MultiLocation> for XcmBenchmarkHelper {
fn create_asset_id_parameter(id: u32) -> MultiLocation {
MultiLocation { parents: 1, interior: X1(Parachain(id)) }
}
}
#[cfg(feature = "runtime-benchmarks")]
pub struct BenchmarkMultiLocationConverter<SelfParaId> {
_phantom: sp_std::marker::PhantomData<SelfParaId>,
}
#[cfg(feature = "runtime-benchmarks")]
impl<SelfParaId> pallet_asset_conversion::BenchmarkHelper<MultiLocation, MultiLocation>
for BenchmarkMultiLocationConverter<SelfParaId>
where
SelfParaId: Get<ParaId>,
{
fn asset_id(asset_id: u32) -> MultiLocation {
MultiLocation {
parents: 1,
interior: X3(
Parachain(SelfParaId::get().into()),
PalletInstance(<Assets as PalletInfoAccess>::index() as u8),
GeneralIndex(asset_id.into()),
),
}
}
fn multiasset_id(asset_id: u32) -> MultiLocation {
Self::asset_id(asset_id)
}
}
@@ -29,7 +29,7 @@ pub mod xcm_config;
use assets_common::{
foreign_creators::ForeignCreators,
local_and_foreign_assets::{LocalAndForeignAssets, MultiLocationConverter},
local_and_foreign_assets::{LocalFromLeft, TargetFromLeft},
matching::FromSiblingParachain,
AssetIdForTrustBackedAssetsConvert, MultiLocationForAssetId,
};
@@ -57,8 +57,9 @@ use frame_support::{
genesis_builder_helper::{build_config, create_default_config},
ord_parameter_types, parameter_types,
traits::{
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, EitherOfDiverse,
Equals, InstanceFilter, TransformOrigin,
fungible, fungibles, tokens::imbalance::ResolveAssetTo, AsEnsureOriginWithArg, ConstBool,
ConstU128, ConstU32, ConstU64, ConstU8, EitherOfDiverse, Equals, InstanceFilter,
TransformOrigin,
},
weights::{ConstantMultiplier, Weight},
BoundedVec, PalletId,
@@ -82,8 +83,9 @@ use parachains_common::{
use sp_runtime::{Perbill, RuntimeDebug};
use xcm::opaque::v3::MultiLocation;
use xcm_config::{
ForeignAssetsConvertedConcreteId, GovernanceLocation, PoolAssetsConvertedConcreteId,
TokenLocation, TrustBackedAssetsConvertedConcreteId,
ForeignAssetsConvertedConcreteId, ForeignCreatorsSovereignAccountOf, GovernanceLocation,
PoolAssetsConvertedConcreteId, TokenLocation, TrustBackedAssetsConvertedConcreteId,
TrustBackedAssetsPalletLocation,
};
#[cfg(any(feature = "std", test))]
@@ -94,10 +96,6 @@ use pallet_xcm::{EnsureXcm, IsVoiceOfBody};
use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use xcm::latest::prelude::*;
use crate::xcm_config::{
ForeignCreatorsSovereignAccountOf, LocalAndForeignAssetsMultiLocationMatcher,
TrustBackedAssetsPalletLocation,
};
use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};
impl_opaque_keys! {
@@ -279,8 +277,6 @@ impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub const AllowMultiAssetPools: bool = false;
// should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
}
@@ -315,35 +311,50 @@ impl pallet_assets::Config<PoolAssetsInstance> for Runtime {
type BenchmarkHelper = ();
}
/// Union fungibles implementation for `Assets`` and `ForeignAssets`.
pub type LocalAndForeignAssets = fungibles::UnionOf<
Assets,
ForeignAssets,
LocalFromLeft<
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
AssetIdForTrustBackedAssets,
>,
MultiLocation,
AccountId,
>;
impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type HigherPrecisionBalance = sp_core::U256;
type Currency = Balances;
type AssetId = MultiLocation;
type Assets = LocalAndForeignAssets<
Assets,
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
ForeignAssets,
type AssetKind = MultiLocation;
type Assets = fungible::UnionOf<
Balances,
LocalAndForeignAssets,
TargetFromLeft<TokenLocation>,
Self::AssetKind,
Self::AccountId,
>;
type PoolAssets = PoolAssets;
type PoolId = (Self::AssetKind, Self::AssetKind);
type PoolLocator =
pallet_asset_conversion::WithFirstAsset<TokenLocation, AccountId, Self::AssetKind>;
type PoolAssetId = u32;
type PoolAssets = PoolAssets;
type PoolSetupFee = ConstU128<0>; // Asset class deposit fees are sufficient to prevent spam
type PoolSetupFeeReceiver = AssetConversionOrigin;
// should be non-zero if `AllowMultiAssetPools` is true, otherwise can be zero.
type PoolSetupFeeAsset = TokenLocation;
type PoolSetupFeeTarget = ResolveAssetTo<AssetConversionOrigin, Self::Assets>;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type LPFee = ConstU32<3>;
type PalletId = AssetConversionPalletId;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = ConstU32<4>;
type MultiAssetId = MultiLocation;
type MultiAssetIdConverter =
MultiLocationConverter<TokenLocation, LocalAndForeignAssetsMultiLocationMatcher>;
type MaxSwapPathLength = ConstU32<3>;
type MintMinLiquidity = ConstU128<100>;
type WeightInfo = weights::pallet_asset_conversion::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper =
crate::xcm_config::BenchmarkMultiLocationConverter<parachain_info::Pallet<Runtime>>;
type BenchmarkHelper = assets_common::benchmarks::AssetPairFactory<
TokenLocation,
parachain_info::Pallet<Runtime>,
xcm_config::AssetsPalletIndex,
>;
}
parameter_types! {
@@ -733,12 +744,9 @@ impl pallet_collator_selection::Config for Runtime {
impl pallet_asset_conversion_tx_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = LocalAndForeignAssets<
Assets,
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
ForeignAssets,
>;
type OnChargeAssetTransaction = AssetConversionAdapter<Balances, AssetConversion>;
type Fungibles = LocalAndForeignAssets;
type OnChargeAssetTransaction =
AssetConversionAdapter<Balances, AssetConversion, TokenLocation>;
}
parameter_types! {
@@ -1154,7 +1162,7 @@ impl_runtime_apis! {
}
fn get_reserves(asset1: MultiLocation, asset2: MultiLocation) -> Option<(Balance, Balance)> {
AssetConversion::get_reserves(&asset1, &asset2).ok()
AssetConversion::get_reserves(asset1, asset2).ok()
}
}
@@ -17,25 +17,23 @@
//! Autogenerated weights for `pallet_asset_conversion`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-07-26, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-10-30, STEPS: `20`, REPEAT: `2`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `runner-ynta1nyy-project-238-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
//! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("asset-hub-rococo-dev")`, DB CACHE: 1024
//! HOSTNAME: `cob`, CPU: `<UNKNOWN>`
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("asset-hub-rococo-dev")`, DB CACHE: 1024
// Executed Command:
// target/production/polkadot-parachain
// ./target/debug/polkadot-parachain
// benchmark
// pallet
// --steps=50
// --repeat=20
// --chain=asset-hub-rococo-dev
// --steps=20
// --repeat=2
// --pallet=pallet-asset-conversion
// --extrinsic=*
// --wasm-execution=compiled
// --heap-pages=4096
// --json-file=/builds/parity/mirrors/cumulus/.git/.artifacts/bench.json
// --pallet=pallet_asset_conversion
// --chain=asset-hub-rococo-dev
// --header=./file_header.txt
// --output=./parachains/runtimes/assets/asset-hub-rococo/src/weights/
// --output=./cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_asset_conversion.rs
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
@@ -50,9 +48,7 @@ pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo<T> {
/// Storage: `AssetConversion::Pools` (r:1 w:1)
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(1224), added: 3699, mode: `MaxEncodedLen`)
/// Storage: UNKNOWN KEY `0x76a2c49709deec21d9c05f96c1f47351` (r:1 w:0)
/// Proof: UNKNOWN KEY `0x76a2c49709deec21d9c05f96c1f47351` (r:1 w:0)
/// Storage: `System::Account` (r:2 w:1)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Account` (r:1 w:1)
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
@@ -66,22 +62,22 @@ impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
fn create_pool() -> Weight {
// Proof Size summary in bytes:
// Measured: `480`
// Estimated: `6196`
// Minimum execution time: 88_484_000 picoseconds.
Weight::from_parts(92_964_000, 0)
.saturating_add(Weight::from_parts(0, 6196))
.saturating_add(T::DbWeight::get().reads(9))
// Measured: `408`
// Estimated: `4689`
// Minimum execution time: 906_000_000 picoseconds.
Weight::from_parts(945_000_000, 0)
.saturating_add(Weight::from_parts(0, 4689))
.saturating_add(T::DbWeight::get().reads(7))
.saturating_add(T::DbWeight::get().writes(7))
}
/// Storage: `AssetConversion::Pools` (r:1 w:0)
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(1224), added: 3699, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Asset` (r:1 w:1)
/// Proof: `ForeignAssets::Asset` (`max_values`: None, `max_size`: Some(808), added: 3283, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Account` (r:2 w:2)
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `PoolAssets::Asset` (r:1 w:1)
/// Proof: `PoolAssets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
/// Storage: `PoolAssets::Account` (r:2 w:2)
@@ -90,34 +86,32 @@ impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo
// Proof Size summary in bytes:
// Measured: `1117`
// Estimated: `7404`
// Minimum execution time: 153_015_000 picoseconds.
Weight::from_parts(157_018_000, 0)
// Minimum execution time: 1_609_000_000 picoseconds.
Weight::from_parts(1_631_000_000, 0)
.saturating_add(Weight::from_parts(0, 7404))
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(7))
}
/// Storage: `AssetConversion::Pools` (r:1 w:0)
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(1224), added: 3699, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Asset` (r:1 w:1)
/// Proof: `ForeignAssets::Asset` (`max_values`: None, `max_size`: Some(808), added: 3283, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Account` (r:2 w:2)
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `PoolAssets::Asset` (r:1 w:1)
/// Proof: `PoolAssets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
/// Storage: UNKNOWN KEY `0x2433d831722b1f4aeb1666953f1c0e77` (r:1 w:0)
/// Proof: UNKNOWN KEY `0x2433d831722b1f4aeb1666953f1c0e77` (r:1 w:0)
/// Storage: `PoolAssets::Account` (r:1 w:1)
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
fn remove_liquidity() -> Weight {
// Proof Size summary in bytes:
// Measured: `1106`
// Estimated: `7404`
// Minimum execution time: 141_726_000 picoseconds.
Weight::from_parts(147_865_000, 0)
// Minimum execution time: 1_480_000_000 picoseconds.
Weight::from_parts(1_506_000_000, 0)
.saturating_add(Weight::from_parts(0, 7404))
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().reads(7))
.saturating_add(T::DbWeight::get().writes(6))
}
/// Storage: `ForeignAssets::Asset` (r:2 w:2)
@@ -126,15 +120,19 @@ impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:2 w:2)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
fn swap_exact_tokens_for_tokens() -> Weight {
/// The range of component `n` is `[2, 3]`.
fn swap_exact_tokens_for_tokens(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `1148`
// Estimated: `13818`
// Minimum execution time: 168_619_000 picoseconds.
Weight::from_parts(174_283_000, 0)
.saturating_add(Weight::from_parts(0, 13818))
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(8))
// Measured: `0 + n * (557 ±0)`
// Estimated: `7404 + n * (393 ±73)`
// Minimum execution time: 933_000_000 picoseconds.
Weight::from_parts(950_000_000, 0)
.saturating_add(Weight::from_parts(0, 7404))
// Standard Error: 18_792_550
.saturating_add(Weight::from_parts(46_683_673, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(4))
.saturating_add(T::DbWeight::get().writes(4))
.saturating_add(Weight::from_parts(0, 393).saturating_mul(n.into()))
}
/// Storage: `System::Account` (r:2 w:2)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
@@ -142,14 +140,18 @@ impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo
/// Proof: `ForeignAssets::Asset` (`max_values`: None, `max_size`: Some(808), added: 3283, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Account` (r:4 w:4)
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
fn swap_tokens_for_exact_tokens() -> Weight {
/// The range of component `n` is `[2, 3]`.
fn swap_tokens_for_exact_tokens(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `1148`
// Estimated: `13818`
// Minimum execution time: 171_565_000 picoseconds.
Weight::from_parts(173_702_000, 0)
.saturating_add(Weight::from_parts(0, 13818))
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(8))
// Measured: `0 + n * (557 ±0)`
// Estimated: `7404 + n * (393 ±180)`
// Minimum execution time: 936_000_000 picoseconds.
Weight::from_parts(954_000_000, 0)
.saturating_add(Weight::from_parts(0, 7404))
// Standard Error: 15_942_881
.saturating_add(Weight::from_parts(39_755_102, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(4))
.saturating_add(T::DbWeight::get().writes(4))
.saturating_add(Weight::from_parts(0, 393).saturating_mul(n.into()))
}
}
@@ -56,9 +56,6 @@ use xcm_builder::{
};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};
#[cfg(feature = "runtime-benchmarks")]
use cumulus_primitives_core::ParaId;
parameter_types! {
pub const TokenLocation: MultiLocation = MultiLocation::parent();
pub const RelayNetwork: NetworkId = NetworkId::Rococo;
@@ -66,8 +63,8 @@ parameter_types! {
pub UniversalLocation: InteriorMultiLocation =
X2(GlobalConsensus(RelayNetwork::get()), Parachain(ParachainInfo::parachain_id().into()));
pub UniversalLocationNetworkId: NetworkId = UniversalLocation::get().global_consensus().unwrap();
pub TrustBackedAssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
pub AssetsPalletIndex: u32 = <Assets as PalletInfoAccess>::index() as u32;
pub TrustBackedAssetsPalletLocation: MultiLocation = PalletInstance(AssetsPalletIndex::get() as u8).into();
pub ForeignAssetsPalletLocation: MultiLocation =
PalletInstance(<ForeignAssets as PalletInfoAccess>::index() as u8).into();
pub PoolAssetsPalletLocation: MultiLocation =
@@ -672,32 +669,6 @@ impl pallet_assets::BenchmarkHelper<MultiLocation> for XcmBenchmarkHelper {
}
}
#[cfg(feature = "runtime-benchmarks")]
pub struct BenchmarkMultiLocationConverter<SelfParaId> {
_phantom: sp_std::marker::PhantomData<SelfParaId>,
}
#[cfg(feature = "runtime-benchmarks")]
impl<SelfParaId> pallet_asset_conversion::BenchmarkHelper<MultiLocation, MultiLocation>
for BenchmarkMultiLocationConverter<SelfParaId>
where
SelfParaId: frame_support::traits::Get<ParaId>,
{
fn asset_id(asset_id: u32) -> MultiLocation {
MultiLocation {
parents: 1,
interior: X3(
Parachain(SelfParaId::get().into()),
PalletInstance(<Assets as PalletInfoAccess>::index() as u8),
GeneralIndex(asset_id.into()),
),
}
}
fn multiasset_id(asset_id: u32) -> MultiLocation {
Self::asset_id(asset_id)
}
}
/// All configuration related to bridging
pub mod bridging {
use super::*;
@@ -27,11 +27,8 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
mod weights;
pub mod xcm_config;
use crate::xcm_config::{
LocalAndForeignAssetsMultiLocationMatcher, TrustBackedAssetsPalletLocation,
};
use assets_common::{
local_and_foreign_assets::{LocalAndForeignAssets, MultiLocationConverter},
local_and_foreign_assets::{LocalFromLeft, TargetFromLeft},
AssetIdForTrustBackedAssetsConvert,
};
use codec::{Decode, Encode, MaxEncodedLen};
@@ -43,8 +40,10 @@ use frame_support::{
genesis_builder_helper::{build_config, create_default_config},
ord_parameter_types, parameter_types,
traits::{
tokens::nonfungibles_v2::Inspect, AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32,
ConstU64, ConstU8, Equals, InstanceFilter, TransformOrigin,
fungible, fungibles,
tokens::{imbalance::ResolveAssetTo, nonfungibles_v2::Inspect},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, Equals,
InstanceFilter, TransformOrigin,
},
weights::{ConstantMultiplier, Weight},
BoundedVec, PalletId,
@@ -80,7 +79,8 @@ use sp_version::RuntimeVersion;
use xcm::opaque::v3::MultiLocation;
use xcm_config::{
ForeignAssetsConvertedConcreteId, PoolAssetsConvertedConcreteId,
TrustBackedAssetsConvertedConcreteId, WestendLocation, XcmOriginToTransactDispatchOrigin,
TrustBackedAssetsConvertedConcreteId, TrustBackedAssetsPalletLocation, WestendLocation,
XcmOriginToTransactDispatchOrigin,
};
#[cfg(any(feature = "std", test))]
@@ -262,8 +262,6 @@ impl pallet_assets::Config<TrustBackedAssetsInstance> for Runtime {
parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub const AllowMultiAssetPools: bool = false;
// should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
}
@@ -297,34 +295,50 @@ impl pallet_assets::Config<PoolAssetsInstance> for Runtime {
type BenchmarkHelper = ();
}
/// Union fungibles implementation for `Assets`` and `ForeignAssets`.
pub type LocalAndForeignAssets = fungibles::UnionOf<
Assets,
ForeignAssets,
LocalFromLeft<
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
AssetIdForTrustBackedAssets,
>,
MultiLocation,
AccountId,
>;
impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type HigherPrecisionBalance = sp_core::U256;
type Currency = Balances;
type AssetId = MultiLocation;
type Assets = LocalAndForeignAssets<
Assets,
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
ForeignAssets,
type AssetKind = MultiLocation;
type Assets = fungible::UnionOf<
Balances,
LocalAndForeignAssets,
TargetFromLeft<WestendLocation>,
Self::AssetKind,
Self::AccountId,
>;
type PoolAssets = PoolAssets;
type PoolId = (Self::AssetKind, Self::AssetKind);
type PoolLocator =
pallet_asset_conversion::WithFirstAsset<WestendLocation, AccountId, Self::AssetKind>;
type PoolAssetId = u32;
type PoolAssets = PoolAssets;
type PoolSetupFee = ConstU128<0>; // Asset class deposit fees are sufficient to prevent spam
type PoolSetupFeeReceiver = AssetConversionOrigin;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee; // should be non-zero if AllowMultiAssetPools is true, otherwise can be zero.
type PoolSetupFeeAsset = WestendLocation;
type PoolSetupFeeTarget = ResolveAssetTo<AssetConversionOrigin, Self::Assets>;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type LPFee = ConstU32<3>;
type PalletId = AssetConversionPalletId;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = ConstU32<4>;
type MultiAssetId = MultiLocation;
type MultiAssetIdConverter =
MultiLocationConverter<WestendLocation, LocalAndForeignAssetsMultiLocationMatcher>;
type MaxSwapPathLength = ConstU32<3>;
type MintMinLiquidity = ConstU128<100>;
type WeightInfo = weights::pallet_asset_conversion::WeightInfo<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper =
crate::xcm_config::BenchmarkMultiLocationConverter<parachain_info::Pallet<Runtime>>;
type BenchmarkHelper = assets_common::benchmarks::AssetPairFactory<
WestendLocation,
parachain_info::Pallet<Runtime>,
xcm_config::AssetsPalletIndex,
>;
}
parameter_types! {
@@ -709,12 +723,9 @@ impl pallet_collator_selection::Config for Runtime {
impl pallet_asset_conversion_tx_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = LocalAndForeignAssets<
Assets,
AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation>,
ForeignAssets,
>;
type OnChargeAssetTransaction = AssetConversionAdapter<Balances, AssetConversion>;
type Fungibles = LocalAndForeignAssets;
type OnChargeAssetTransaction =
AssetConversionAdapter<Balances, AssetConversion, WestendLocation>;
}
parameter_types! {
@@ -924,8 +935,6 @@ pub type Migrations = (
// unreleased
pallet_collator_selection::migration::v1::MigrateToV1<Runtime>,
// unreleased
migrations::NativeAssetParents0ToParents1Migration<Runtime>,
// unreleased
pallet_multisig::migrations::v1::MigrateToV1<Runtime>,
// unreleased
InitStorageVersions,
@@ -1229,7 +1238,7 @@ impl_runtime_apis! {
}
fn get_reserves(asset1: MultiLocation, asset2: MultiLocation) -> Option<(Balance, Balance)> {
AssetConversion::get_reserves(&asset1, &asset2).ok()
AssetConversion::get_reserves(asset1, asset2).ok()
}
}
@@ -1689,117 +1698,3 @@ cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
}
pub mod migrations {
use super::*;
use frame_support::{
pallet_prelude::Get,
traits::{
fungibles::{Inspect, Mutate},
tokens::Preservation,
OnRuntimeUpgrade, OriginTrait,
},
};
use parachains_common::impls::AccountIdOf;
use sp_runtime::{traits::StaticLookup, Saturating};
/// Temporary migration because of bug with native asset, it can be removed once applied on
/// `AssetHubWestend`. Migrates pools with `MultiLocation { parents: 0, interior: Here }` to
/// `MultiLocation { parents: 1, interior: Here }`
pub struct NativeAssetParents0ToParents1Migration<T>(sp_std::marker::PhantomData<T>);
impl<
T: pallet_asset_conversion::Config<MultiAssetId = MultiLocation, AssetId = MultiLocation>,
> OnRuntimeUpgrade for NativeAssetParents0ToParents1Migration<T>
where
<T as pallet_asset_conversion::Config>::PoolAssetId: Into<u32>,
AccountIdOf<Runtime>: Into<[u8; 32]>,
<T as frame_system::Config>::AccountId:
Into<<<T as frame_system::Config>::RuntimeOrigin as OriginTrait>::AccountId>,
<<T as frame_system::Config>::Lookup as StaticLookup>::Source:
From<<T as frame_system::Config>::AccountId>,
sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,
{
fn on_runtime_upgrade() -> Weight {
let invalid_native_asset = MultiLocation { parents: 0, interior: Here };
let valid_native_asset = WestendLocation::get();
let mut reads: u64 = 1;
let mut writes: u64 = 0;
// migrate pools with invalid native asset
let pools = pallet_asset_conversion::Pools::<T>::iter().collect::<Vec<_>>();
reads.saturating_accrue(1);
for (old_pool_id, pool_info) in pools {
let old_pool_account =
pallet_asset_conversion::Pallet::<T>::get_pool_account(&old_pool_id);
reads.saturating_accrue(1);
let pool_asset_id = pool_info.lp_token.clone();
if old_pool_id.0 != invalid_native_asset {
// skip, if ok
continue
}
// fix new account
let new_pool_id = pallet_asset_conversion::Pallet::<T>::get_pool_id(
valid_native_asset,
old_pool_id.1,
);
let new_pool_account =
pallet_asset_conversion::Pallet::<T>::get_pool_account(&new_pool_id);
frame_system::Pallet::<T>::inc_providers(&new_pool_account);
reads.saturating_accrue(2);
writes.saturating_accrue(1);
// move currency
let _ = Balances::transfer_all(
RuntimeOrigin::signed(sp_runtime::AccountId32::from(old_pool_account.clone())),
sp_runtime::AccountId32::from(new_pool_account.clone()).into(),
false,
);
reads.saturating_accrue(2);
writes.saturating_accrue(2);
// move LP token
let _ = T::PoolAssets::transfer(
pool_asset_id.clone(),
&old_pool_account,
&new_pool_account,
T::PoolAssets::balance(pool_asset_id.clone(), &old_pool_account),
Preservation::Expendable,
);
reads.saturating_accrue(1);
writes.saturating_accrue(2);
// change the ownership of LP token
let _ = pallet_assets::Pallet::<Runtime, PoolAssetsInstance>::transfer_ownership(
RuntimeOrigin::signed(sp_runtime::AccountId32::from(old_pool_account.clone())),
pool_asset_id.into(),
sp_runtime::AccountId32::from(new_pool_account.clone()).into(),
);
reads.saturating_accrue(1);
writes.saturating_accrue(2);
// move LocalOrForeignAssets
let _ = T::Assets::transfer(
old_pool_id.1,
&old_pool_account,
&new_pool_account,
T::Assets::balance(old_pool_id.1, &old_pool_account),
Preservation::Expendable,
);
reads.saturating_accrue(1);
writes.saturating_accrue(2);
// dec providers for old account
let _ = frame_system::Pallet::<T>::dec_providers(&old_pool_account);
writes.saturating_accrue(1);
// change pool key
pallet_asset_conversion::Pools::<T>::insert(new_pool_id, pool_info);
pallet_asset_conversion::Pools::<T>::remove(old_pool_id);
}
T::DbWeight::get().reads_writes(reads, writes)
}
}
}
@@ -16,27 +16,23 @@
//! Autogenerated weights for `pallet_asset_conversion`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-07-31, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-10-30, STEPS: `20`, REPEAT: `2`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `runner-ynta1nyy-project-238-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
//! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("asset-hub-westend-dev")`, DB CACHE: 1024
//! HOSTNAME: `cob`, CPU: `<UNKNOWN>`
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("asset-hub-westend-dev")`, DB CACHE: 1024
// Executed Command:
// ./target/production/polkadot-parachain
// ./target/debug/polkadot-parachain
// benchmark
// pallet
// --chain=asset-hub-westend-dev
// --wasm-execution=compiled
// --pallet=pallet_asset_conversion
// --no-storage-info
// --no-median-slopes
// --no-min-squares
// --steps=20
// --repeat=2
// --pallet=pallet-asset-conversion
// --extrinsic=*
// --steps=50
// --repeat=20
// --json
// --header=./file_header.txt
// --output=./parachains/runtimes/assets/asset-hub-westend/src/weights/
// --wasm-execution=compiled
// --heap-pages=4096
// --output=./cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_asset_conversion.rs
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
@@ -51,9 +47,7 @@ pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo<T> {
/// Storage: `AssetConversion::Pools` (r:1 w:1)
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(1224), added: 3699, mode: `MaxEncodedLen`)
/// Storage: UNKNOWN KEY `0x76a2c49709deec21d9c05f96c1f47351` (r:1 w:0)
/// Proof: UNKNOWN KEY `0x76a2c49709deec21d9c05f96c1f47351` (r:1 w:0)
/// Storage: `System::Account` (r:2 w:1)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Account` (r:1 w:1)
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
@@ -67,22 +61,22 @@ impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
fn create_pool() -> Weight {
// Proof Size summary in bytes:
// Measured: `480`
// Estimated: `6196`
// Minimum execution time: 90_011_000 picoseconds.
Weight::from_parts(92_372_000, 0)
.saturating_add(Weight::from_parts(0, 6196))
.saturating_add(T::DbWeight::get().reads(9))
// Measured: `408`
// Estimated: `4689`
// Minimum execution time: 922_000_000 picoseconds.
Weight::from_parts(1_102_000_000, 0)
.saturating_add(Weight::from_parts(0, 4689))
.saturating_add(T::DbWeight::get().reads(7))
.saturating_add(T::DbWeight::get().writes(7))
}
/// Storage: `AssetConversion::Pools` (r:1 w:0)
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(1224), added: 3699, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Asset` (r:1 w:1)
/// Proof: `ForeignAssets::Asset` (`max_values`: None, `max_size`: Some(808), added: 3283, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Account` (r:2 w:2)
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `PoolAssets::Asset` (r:1 w:1)
/// Proof: `PoolAssets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
/// Storage: `PoolAssets::Account` (r:2 w:2)
@@ -91,34 +85,32 @@ impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo
// Proof Size summary in bytes:
// Measured: `1117`
// Estimated: `7404`
// Minimum execution time: 153_484_000 picoseconds.
Weight::from_parts(155_465_000, 0)
// Minimum execution time: 1_597_000_000 picoseconds.
Weight::from_parts(1_655_000_000, 0)
.saturating_add(Weight::from_parts(0, 7404))
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(7))
}
/// Storage: `AssetConversion::Pools` (r:1 w:0)
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(1224), added: 3699, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Asset` (r:1 w:1)
/// Proof: `ForeignAssets::Asset` (`max_values`: None, `max_size`: Some(808), added: 3283, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Account` (r:2 w:2)
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
/// Storage: `PoolAssets::Asset` (r:1 w:1)
/// Proof: `PoolAssets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
/// Storage: UNKNOWN KEY `0x2433d831722b1f4aeb1666953f1c0e77` (r:1 w:0)
/// Proof: UNKNOWN KEY `0x2433d831722b1f4aeb1666953f1c0e77` (r:1 w:0)
/// Storage: `PoolAssets::Account` (r:1 w:1)
/// Proof: `PoolAssets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
fn remove_liquidity() -> Weight {
// Proof Size summary in bytes:
// Measured: `1106`
// Estimated: `7404`
// Minimum execution time: 141_326_000 picoseconds.
Weight::from_parts(143_882_000, 0)
// Minimum execution time: 1_500_000_000 picoseconds.
Weight::from_parts(1_633_000_000, 0)
.saturating_add(Weight::from_parts(0, 7404))
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().reads(7))
.saturating_add(T::DbWeight::get().writes(6))
}
/// Storage: `ForeignAssets::Asset` (r:2 w:2)
@@ -127,15 +119,19 @@ impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
/// Storage: `System::Account` (r:2 w:2)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
fn swap_exact_tokens_for_tokens() -> Weight {
/// The range of component `n` is `[2, 3]`.
fn swap_exact_tokens_for_tokens(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `1148`
// Estimated: `13818`
// Minimum execution time: 168_556_000 picoseconds.
Weight::from_parts(170_313_000, 0)
.saturating_add(Weight::from_parts(0, 13818))
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(8))
// Measured: `0 + n * (557 ±0)`
// Estimated: `7404 + n * (393 ±92)`
// Minimum execution time: 930_000_000 picoseconds.
Weight::from_parts(960_000_000, 0)
.saturating_add(Weight::from_parts(0, 7404))
// Standard Error: 17_993_720
.saturating_add(Weight::from_parts(41_959_183, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(4))
.saturating_add(T::DbWeight::get().writes(4))
.saturating_add(Weight::from_parts(0, 393).saturating_mul(n.into()))
}
/// Storage: `System::Account` (r:2 w:2)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
@@ -143,14 +139,18 @@ impl<T: frame_system::Config> pallet_asset_conversion::WeightInfo for WeightInfo
/// Proof: `ForeignAssets::Asset` (`max_values`: None, `max_size`: Some(808), added: 3283, mode: `MaxEncodedLen`)
/// Storage: `ForeignAssets::Account` (r:4 w:4)
/// Proof: `ForeignAssets::Account` (`max_values`: None, `max_size`: Some(732), added: 3207, mode: `MaxEncodedLen`)
fn swap_tokens_for_exact_tokens() -> Weight {
/// The range of component `n` is `[2, 3]`.
fn swap_tokens_for_exact_tokens(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `1148`
// Estimated: `13818`
// Minimum execution time: 167_704_000 picoseconds.
Weight::from_parts(170_034_000, 0)
.saturating_add(Weight::from_parts(0, 13818))
.saturating_add(T::DbWeight::get().reads(8))
.saturating_add(T::DbWeight::get().writes(8))
// Measured: `0 + n * (557 ±0)`
// Estimated: `7404 + n * (393 ±92)`
// Minimum execution time: 940_000_000 picoseconds.
Weight::from_parts(956_000_000, 0)
.saturating_add(Weight::from_parts(0, 7404))
// Standard Error: 15_746_647
.saturating_add(Weight::from_parts(39_193_877, 0).saturating_mul(n.into()))
.saturating_add(T::DbWeight::get().reads(4))
.saturating_add(T::DbWeight::get().writes(4))
.saturating_add(Weight::from_parts(0, 393).saturating_mul(n.into()))
}
}
@@ -56,9 +56,6 @@ use xcm_builder::{
};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};
#[cfg(feature = "runtime-benchmarks")]
use {cumulus_primitives_core::ParaId, sp_core::Get};
parameter_types! {
pub const WestendLocation: MultiLocation = MultiLocation::parent();
pub const RelayNetwork: Option<NetworkId> = Some(NetworkId::Westend);
@@ -66,8 +63,8 @@ parameter_types! {
pub UniversalLocation: InteriorMultiLocation =
X2(GlobalConsensus(RelayNetwork::get().unwrap()), Parachain(ParachainInfo::parachain_id().into()));
pub UniversalLocationNetworkId: NetworkId = UniversalLocation::get().global_consensus().unwrap();
pub TrustBackedAssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
pub AssetsPalletIndex: u32 = <Assets as PalletInfoAccess>::index() as u32;
pub TrustBackedAssetsPalletLocation: MultiLocation = PalletInstance(AssetsPalletIndex::get() as u8).into();
pub ForeignAssetsPalletLocation: MultiLocation =
PalletInstance(<ForeignAssets as PalletInfoAccess>::index() as u8).into();
pub PoolAssetsPalletLocation: MultiLocation =
@@ -694,33 +691,6 @@ impl pallet_assets::BenchmarkHelper<MultiLocation> for XcmBenchmarkHelper {
}
}
#[cfg(feature = "runtime-benchmarks")]
pub struct BenchmarkMultiLocationConverter<SelfParaId> {
_phantom: sp_std::marker::PhantomData<SelfParaId>,
}
#[cfg(feature = "runtime-benchmarks")]
impl<SelfParaId> pallet_asset_conversion::BenchmarkHelper<MultiLocation, MultiLocation>
for BenchmarkMultiLocationConverter<SelfParaId>
where
SelfParaId: Get<ParaId>,
{
fn asset_id(asset_id: u32) -> MultiLocation {
MultiLocation {
parents: 1,
interior: X3(
Parachain(SelfParaId::get().into()),
PalletInstance(<Assets as PalletInfoAccess>::index() as u8),
GeneralIndex(asset_id.into()),
),
}
}
fn multiasset_id(asset_id: u32) -> MultiLocation {
Self::asset_id(asset_id)
}
}
/// All configuration related to bridging
pub mod bridging {
use super::*;
@@ -0,0 +1,44 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use cumulus_primitives_core::ParaId;
use sp_runtime::traits::Get;
use sp_std::marker::PhantomData;
use xcm::latest::prelude::*;
/// Creates asset pairs for liquidity pools with `Target` always being the first asset.
pub struct AssetPairFactory<Target, SelfParaId, PalletId>(
PhantomData<(Target, SelfParaId, PalletId)>,
);
impl<Target: Get<MultiLocation>, SelfParaId: Get<ParaId>, PalletId: Get<u32>>
pallet_asset_conversion::BenchmarkHelper<MultiLocation>
for AssetPairFactory<Target, SelfParaId, PalletId>
{
fn create_pair(seed1: u32, seed2: u32) -> (MultiLocation, MultiLocation) {
let with_id = MultiLocation::new(
1,
X3(
Parachain(SelfParaId::get().into()),
PalletInstance(PalletId::get() as u8),
GeneralIndex(seed2.into()),
),
);
if seed1 % 2 == 0 {
(with_id, Target::get())
} else {
(Target::get(), with_id)
}
}
}
@@ -15,6 +15,8 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarks;
pub mod foreign_creators;
pub mod fungible_conversion;
pub mod local_and_foreign_assets;
@@ -13,48 +13,42 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use frame_support::traits::{
fungibles::{Balanced, Create, HandleImbalanceDrop, Inspect, Mutate, Unbalanced},
tokens::{
DepositConsequence, Fortitude, Precision, Preservation, Provenance, WithdrawConsequence,
},
AccountTouch, Contains, ContainsPair, Get, PalletInfoAccess,
use frame_support::traits::Get;
use sp_runtime::{
traits::{Convert, MaybeEquivalence},
Either,
Either::{Left, Right},
};
use pallet_asset_conversion::{MultiAssetIdConversionResult, MultiAssetIdConverter};
use parachains_common::AccountId;
use sp_runtime::{traits::MaybeEquivalence, DispatchError, DispatchResult};
use sp_std::marker::PhantomData;
use xcm::latest::MultiLocation;
pub struct MultiLocationConverter<NativeAssetLocation: Get<MultiLocation>, MultiLocationMatcher> {
_phantom: PhantomData<(NativeAssetLocation, MultiLocationMatcher)>,
/// Converts a given [`MultiLocation`] to [`Either::Left`] when equal to `Target`, or
/// [`Either::Right`] otherwise.
///
/// Suitable for use as a `Criterion` with [`frame_support::traits::tokens::fungible::UnionOf`].
pub struct TargetFromLeft<Target>(PhantomData<Target>);
impl<Target: Get<MultiLocation>> Convert<MultiLocation, Either<(), MultiLocation>>
for TargetFromLeft<Target>
{
fn convert(l: MultiLocation) -> Either<(), MultiLocation> {
Target::get().eq(&l).then(|| Left(())).map_or(Right(l), |n| n)
}
}
impl<NativeAssetLocation, MultiLocationMatcher> MultiAssetIdConverter<MultiLocation, MultiLocation>
for MultiLocationConverter<NativeAssetLocation, MultiLocationMatcher>
/// Converts a given [`MultiLocation`] to [`Either::Left`] based on the `Equivalence` criteria.
/// Returns [`Either::Right`] if not equivalent.
///
/// Suitable for use as a `Criterion` with [`frame_support::traits::tokens::fungibles::UnionOf`].
pub struct LocalFromLeft<Equivalence, AssetId>(PhantomData<(Equivalence, AssetId)>);
impl<Equivalence, AssetId> Convert<MultiLocation, Either<AssetId, MultiLocation>>
for LocalFromLeft<Equivalence, AssetId>
where
NativeAssetLocation: Get<MultiLocation>,
MultiLocationMatcher: Contains<MultiLocation>,
Equivalence: MaybeEquivalence<MultiLocation, AssetId>,
{
fn get_native() -> MultiLocation {
NativeAssetLocation::get()
}
fn is_native(asset_id: &MultiLocation) -> bool {
*asset_id == Self::get_native()
}
fn try_convert(
asset_id: &MultiLocation,
) -> MultiAssetIdConversionResult<MultiLocation, MultiLocation> {
if Self::is_native(asset_id) {
return MultiAssetIdConversionResult::Native
}
if MultiLocationMatcher::contains(asset_id) {
MultiAssetIdConversionResult::Converted(*asset_id)
} else {
MultiAssetIdConversionResult::Unsupported(*asset_id)
fn convert(l: MultiLocation) -> Either<AssetId, MultiLocation> {
match Equivalence::convert(&l) {
Some(id) => Left(id),
None => Right(l),
}
}
}
@@ -63,415 +57,3 @@ pub trait MatchesLocalAndForeignAssetsMultiLocation {
fn is_local(location: &MultiLocation) -> bool;
fn is_foreign(location: &MultiLocation) -> bool;
}
pub struct LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets> {
_phantom: PhantomData<(Assets, LocalAssetIdConverter, ForeignAssets)>,
}
impl<Assets, LocalAssetIdConverter, ForeignAssets> Unbalanced<AccountId>
for LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets: Inspect<AccountId, Balance = u128, AssetId = u32>
+ Unbalanced<AccountId>
+ Balanced<AccountId>
+ PalletInfoAccess,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets: Inspect<AccountId, Balance = u128, AssetId = MultiLocation>
+ Unbalanced<AccountId>
+ Balanced<AccountId>,
{
fn handle_dust(dust: frame_support::traits::fungibles::Dust<AccountId, Self>) {
let credit = dust.into_credit();
if let Some(asset) = LocalAssetIdConverter::convert(&credit.asset()) {
Assets::handle_raw_dust(asset, credit.peek());
} else {
ForeignAssets::handle_raw_dust(credit.asset(), credit.peek());
}
// As we have already handled the dust, we must stop credit's drop from happening:
sp_std::mem::forget(credit);
}
fn write_balance(
asset: <Self as Inspect<AccountId>>::AssetId,
who: &AccountId,
amount: <Self as Inspect<AccountId>>::Balance,
) -> Result<Option<<Self as Inspect<AccountId>>::Balance>, DispatchError> {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::write_balance(asset, who, amount)
} else {
ForeignAssets::write_balance(asset, who, amount)
}
}
/// Set the total issuance of `asset` to `amount`.
fn set_total_issuance(asset: Self::AssetId, amount: Self::Balance) {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::set_total_issuance(asset, amount)
} else {
ForeignAssets::set_total_issuance(asset, amount)
}
}
fn decrease_balance(
asset: Self::AssetId,
who: &AccountId,
amount: Self::Balance,
precision: Precision,
preservation: Preservation,
force: Fortitude,
) -> Result<Self::Balance, DispatchError> {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::decrease_balance(asset, who, amount, precision, preservation, force)
} else {
ForeignAssets::decrease_balance(asset, who, amount, precision, preservation, force)
}
}
fn increase_balance(
asset: Self::AssetId,
who: &AccountId,
amount: Self::Balance,
precision: Precision,
) -> Result<Self::Balance, DispatchError> {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::increase_balance(asset, who, amount, precision)
} else {
ForeignAssets::increase_balance(asset, who, amount, precision)
}
}
}
impl<Assets, LocalAssetIdConverter, ForeignAssets> Inspect<AccountId>
for LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets: Inspect<AccountId, Balance = u128, AssetId = u32>,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets: Inspect<AccountId, Balance = u128, AssetId = MultiLocation>,
{
type AssetId = MultiLocation;
type Balance = u128;
/// The total amount of issuance in the system.
fn total_issuance(asset: Self::AssetId) -> Self::Balance {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::total_issuance(asset)
} else {
ForeignAssets::total_issuance(asset)
}
}
/// The minimum balance any single account may have.
fn minimum_balance(asset: Self::AssetId) -> Self::Balance {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::minimum_balance(asset)
} else {
ForeignAssets::minimum_balance(asset)
}
}
fn total_balance(
asset: <Self as Inspect<AccountId>>::AssetId,
account: &AccountId,
) -> <Self as Inspect<AccountId>>::Balance {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::total_balance(asset, account)
} else {
ForeignAssets::total_balance(asset, account)
}
}
/// Get the `asset` balance of `who`.
fn balance(asset: Self::AssetId, who: &AccountId) -> Self::Balance {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::balance(asset, who)
} else {
ForeignAssets::balance(asset, who)
}
}
/// Get the maximum amount of `asset` that `who` can withdraw/transfer successfully.
fn reducible_balance(
asset: Self::AssetId,
who: &AccountId,
presevation: Preservation,
fortitude: Fortitude,
) -> Self::Balance {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::reducible_balance(asset, who, presevation, fortitude)
} else {
ForeignAssets::reducible_balance(asset, who, presevation, fortitude)
}
}
/// Returns `true` if the `asset` balance of `who` may be increased by `amount`.
///
/// - `asset`: The asset that should be deposited.
/// - `who`: The account of which the balance should be increased by `amount`.
/// - `amount`: How much should the balance be increased?
/// - `mint`: Will `amount` be minted to deposit it into `account`?
fn can_deposit(
asset: Self::AssetId,
who: &AccountId,
amount: Self::Balance,
mint: Provenance,
) -> DepositConsequence {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::can_deposit(asset, who, amount, mint)
} else {
ForeignAssets::can_deposit(asset, who, amount, mint)
}
}
/// Returns `Failed` if the `asset` balance of `who` may not be decreased by `amount`, otherwise
/// the consequence.
fn can_withdraw(
asset: Self::AssetId,
who: &AccountId,
amount: Self::Balance,
) -> WithdrawConsequence<Self::Balance> {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::can_withdraw(asset, who, amount)
} else {
ForeignAssets::can_withdraw(asset, who, amount)
}
}
/// Returns `true` if an `asset` exists.
fn asset_exists(asset: Self::AssetId) -> bool {
if let Some(asset) = LocalAssetIdConverter::convert(&asset) {
Assets::asset_exists(asset)
} else {
ForeignAssets::asset_exists(asset)
}
}
}
impl<Assets, LocalAssetIdConverter, ForeignAssets> Mutate<AccountId>
for LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets: Mutate<AccountId>
+ Inspect<AccountId, Balance = u128, AssetId = u32>
+ Balanced<AccountId>
+ PalletInfoAccess,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets: Mutate<AccountId, Balance = u128>
+ Inspect<AccountId, Balance = u128, AssetId = MultiLocation>
+ Balanced<AccountId>,
{
/// Transfer funds from one account into another.
fn transfer(
asset: MultiLocation,
source: &AccountId,
dest: &AccountId,
amount: Self::Balance,
keep_alive: Preservation,
) -> Result<Self::Balance, DispatchError> {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset) {
Assets::transfer(asset_id, source, dest, amount, keep_alive)
} else {
ForeignAssets::transfer(asset, source, dest, amount, keep_alive)
}
}
}
impl<Assets, LocalAssetIdConverter, ForeignAssets> Create<AccountId>
for LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets: Create<AccountId> + Inspect<AccountId, Balance = u128, AssetId = u32>,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets: Create<AccountId> + Inspect<AccountId, Balance = u128, AssetId = MultiLocation>,
{
/// Create a new fungible asset.
fn create(
asset_id: Self::AssetId,
admin: AccountId,
is_sufficient: bool,
min_balance: Self::Balance,
) -> DispatchResult {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset_id) {
Assets::create(asset_id, admin, is_sufficient, min_balance)
} else {
ForeignAssets::create(asset_id, admin, is_sufficient, min_balance)
}
}
}
impl<Assets, LocalAssetIdConverter, ForeignAssets> AccountTouch<MultiLocation, AccountId>
for LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets: AccountTouch<u32, AccountId, Balance = u128>,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets: AccountTouch<MultiLocation, AccountId, Balance = u128>,
{
type Balance = u128;
fn deposit_required(
asset_id: MultiLocation,
) -> <Self as AccountTouch<MultiLocation, AccountId>>::Balance {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset_id) {
Assets::deposit_required(asset_id)
} else {
ForeignAssets::deposit_required(asset_id)
}
}
fn should_touch(asset_id: MultiLocation, who: &AccountId) -> bool {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset_id) {
Assets::should_touch(asset_id, who)
} else {
ForeignAssets::should_touch(asset_id, who)
}
}
fn touch(
asset_id: MultiLocation,
who: &AccountId,
depositor: &AccountId,
) -> Result<(), DispatchError> {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset_id) {
Assets::touch(asset_id, who, depositor)
} else {
ForeignAssets::touch(asset_id, who, depositor)
}
}
}
/// Implements [`ContainsPair`] trait for a pair of asset and account IDs.
impl<Assets, LocalAssetIdConverter, ForeignAssets> ContainsPair<MultiLocation, AccountId>
for LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets: PalletInfoAccess + ContainsPair<u32, AccountId>,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets: ContainsPair<MultiLocation, AccountId>,
{
/// Check if an account with the given asset ID and account address exists.
fn contains(asset_id: &MultiLocation, who: &AccountId) -> bool {
if let Some(asset_id) = LocalAssetIdConverter::convert(asset_id) {
Assets::contains(&asset_id, &who)
} else {
ForeignAssets::contains(&asset_id, &who)
}
}
}
impl<Assets, LocalAssetIdConverter, ForeignAssets> Balanced<AccountId>
for LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets:
Balanced<AccountId> + Inspect<AccountId, Balance = u128, AssetId = u32> + PalletInfoAccess,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets:
Balanced<AccountId> + Inspect<AccountId, Balance = u128, AssetId = MultiLocation>,
{
type OnDropDebt = DebtDropIndirection<Assets, LocalAssetIdConverter, ForeignAssets>;
type OnDropCredit = CreditDropIndirection<Assets, LocalAssetIdConverter, ForeignAssets>;
}
pub struct DebtDropIndirection<Assets, LocalAssetIdConverter, ForeignAssets> {
_phantom: PhantomData<LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>>,
}
impl<Assets, LocalAssetIdConverter, ForeignAssets> HandleImbalanceDrop<MultiLocation, u128>
for DebtDropIndirection<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets: Balanced<AccountId> + Inspect<AccountId, Balance = u128, AssetId = u32>,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets:
Balanced<AccountId> + Inspect<AccountId, Balance = u128, AssetId = MultiLocation>,
{
fn handle(asset: MultiLocation, amount: u128) {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset) {
Assets::OnDropDebt::handle(asset_id, amount);
} else {
ForeignAssets::OnDropDebt::handle(asset, amount);
}
}
}
pub struct CreditDropIndirection<Assets, LocalAssetIdConverter, ForeignAssets> {
_phantom: PhantomData<LocalAndForeignAssets<Assets, LocalAssetIdConverter, ForeignAssets>>,
}
impl<Assets, LocalAssetIdConverter, ForeignAssets> HandleImbalanceDrop<MultiLocation, u128>
for CreditDropIndirection<Assets, LocalAssetIdConverter, ForeignAssets>
where
Assets: Balanced<AccountId> + Inspect<AccountId, Balance = u128, AssetId = u32>,
LocalAssetIdConverter: MaybeEquivalence<MultiLocation, u32>,
ForeignAssets:
Balanced<AccountId> + Inspect<AccountId, Balance = u128, AssetId = MultiLocation>,
{
fn handle(asset: MultiLocation, amount: u128) {
if let Some(asset_id) = LocalAssetIdConverter::convert(&asset) {
Assets::OnDropCredit::handle(asset_id, amount);
} else {
ForeignAssets::OnDropCredit::handle(asset, amount);
}
}
}
#[cfg(test)]
mod tests {
use crate::{
local_and_foreign_assets::MultiLocationConverter, AssetIdForPoolAssetsConvert,
AssetIdForTrustBackedAssetsConvert,
};
use frame_support::traits::EverythingBut;
use pallet_asset_conversion::{MultiAssetIdConversionResult, MultiAssetIdConverter};
use sp_runtime::traits::MaybeEquivalence;
use xcm::latest::prelude::*;
use xcm_builder::StartsWith;
#[test]
fn test_multi_location_converter_works() {
frame_support::parameter_types! {
pub const WestendLocation: MultiLocation = MultiLocation::parent();
pub TrustBackedAssetsPalletLocation: MultiLocation = PalletInstance(50_u8).into();
pub PoolAssetsPalletLocation: MultiLocation = PalletInstance(55_u8).into();
}
type C = MultiLocationConverter<
WestendLocation,
EverythingBut<StartsWith<PoolAssetsPalletLocation>>,
>;
let native_asset = WestendLocation::get();
let local_asset =
AssetIdForTrustBackedAssetsConvert::<TrustBackedAssetsPalletLocation>::convert_back(
&123,
)
.unwrap();
let pool_asset =
AssetIdForPoolAssetsConvert::<PoolAssetsPalletLocation>::convert_back(&456).unwrap();
let foreign_asset1 = MultiLocation { parents: 1, interior: X1(Parachain(2222)) };
let foreign_asset2 = MultiLocation {
parents: 2,
interior: X2(GlobalConsensus(ByGenesis([1; 32])), Parachain(2222)),
};
assert!(C::is_native(&native_asset));
assert!(!C::is_native(&local_asset));
assert!(!C::is_native(&pool_asset));
assert!(!C::is_native(&foreign_asset1));
assert!(!C::is_native(&foreign_asset2));
assert_eq!(C::try_convert(&native_asset), MultiAssetIdConversionResult::Native);
assert_eq!(
C::try_convert(&local_asset),
MultiAssetIdConversionResult::Converted(local_asset)
);
assert_eq!(
C::try_convert(&pool_asset),
MultiAssetIdConversionResult::Unsupported(pool_asset)
);
assert_eq!(
C::try_convert(&foreign_asset1),
MultiAssetIdConversionResult::Converted(foreign_asset1)
);
assert_eq!(
C::try_convert(&foreign_asset2),
MultiAssetIdConversionResult::Converted(foreign_asset2)
);
}
}
+29
View File
@@ -0,0 +1,29 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
title: "pallet-asset-conversion: Decoupling Native Currency Dependancy"
doc:
- audience: Runtime Dev
description: |
Decoupling Pallet from the Concept of Native Currency
Currently, the pallet used to 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;
crates:
- name: pallet-asset-conversion
+32 -22
View File
@@ -36,8 +36,13 @@ use frame_support::{
pallet_prelude::Get,
parameter_types,
traits::{
fungible::{Balanced, Credit, HoldConsideration, ItemOf},
tokens::{nonfungibles_v2::Inspect, pay::PayAssetFromAccount, GetSalary, PayFromAccount},
fungible::{
Balanced, Credit, HoldConsideration, ItemOf, NativeFromLeft, NativeOrWithId, UnionOf,
},
tokens::{
imbalance::ResolveAssetTo, nonfungibles_v2::Inspect, pay::PayAssetFromAccount,
GetSalary, PayFromAccount,
},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, Contains, Currency,
EitherOfDiverse, EqualPrivilegeOnly, Imbalance, InsideBoth, InstanceFilter,
KeyOwnerProofSystem, LinearStoragePrice, LockIdentifier, Nothing, OnUnbalanced,
@@ -57,7 +62,7 @@ use frame_system::{
};
pub use node_primitives::{AccountId, Signature};
use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Moment, Nonce};
use pallet_asset_conversion::{NativeOrAssetId, NativeOrAssetIdConverter};
use pallet_asset_conversion::{Ascending, Chain, WithFirstAsset};
use pallet_broker::{CoreAssignment, CoreIndex, CoretimeInterface, PartsOf57600};
use pallet_election_provider_multi_phase::{GeometricDepositBase, SolutionAccuracyOf};
use pallet_identity::legacy::IdentityInfo;
@@ -563,8 +568,11 @@ impl pallet_asset_tx_payment::Config for Runtime {
impl pallet_asset_conversion_tx_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = Assets;
type OnChargeAssetTransaction =
pallet_asset_conversion_tx_payment::AssetConversionAdapter<Balances, AssetConversion>;
type OnChargeAssetTransaction = pallet_asset_conversion_tx_payment::AssetConversionAdapter<
Balances,
AssetConversion,
Native,
>;
}
impl pallet_skip_feeless_payment::Config for Runtime {
@@ -1644,32 +1652,34 @@ impl pallet_assets::Config<Instance2> for Runtime {
parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub AllowMultiAssetPools: bool = true;
pub const PoolSetupFee: Balance = 1 * DOLLARS; // should be more or equal to the existential deposit
pub const MintMinLiquidity: Balance = 100; // 100 is good enough when the main currency has 10-12 decimals.
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0); // should be non-zero if AllowMultiAssetPools is true, otherwise can be zero.
pub const LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
pub const Native: NativeOrWithId<u32> = NativeOrWithId::Native;
}
impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type HigherPrecisionBalance = sp_core::U256;
type Assets = Assets;
type Balance = u128;
type PoolAssets = PoolAssets;
type AssetId = <Self as pallet_assets::Config<Instance1>>::AssetId;
type MultiAssetId = NativeOrAssetId<u32>;
type HigherPrecisionBalance = sp_core::U256;
type AssetKind = NativeOrWithId<u32>;
type Assets = UnionOf<Balances, Assets, NativeFromLeft, NativeOrWithId<u32>, AccountId>;
type PoolId = (Self::AssetKind, Self::AssetKind);
type PoolLocator = Chain<
WithFirstAsset<Native, AccountId, NativeOrWithId<u32>>,
Ascending<AccountId, NativeOrWithId<u32>>,
>;
type PoolAssetId = <Self as pallet_assets::Config<Instance2>>::AssetId;
type PoolAssets = PoolAssets;
type PoolSetupFee = PoolSetupFee;
type PoolSetupFeeAsset = Native;
type PoolSetupFeeTarget = ResolveAssetTo<AssetConversionOrigin, Self::Assets>;
type PalletId = AssetConversionPalletId;
type LPFee = ConstU32<3>; // means 0.3%
type PoolSetupFee = PoolSetupFee;
type PoolSetupFeeReceiver = AssetConversionOrigin;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type WeightInfo = pallet_asset_conversion::weights::SubstrateWeight<Runtime>;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = ConstU32<4>;
type MintMinLiquidity = MintMinLiquidity;
type MultiAssetIdConverter = NativeOrAssetIdConverter<u32>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
@@ -2579,19 +2589,19 @@ impl_runtime_apis! {
impl pallet_asset_conversion::AssetConversionApi<
Block,
Balance,
NativeOrAssetId<u32>
NativeOrWithId<u32>
> for Runtime
{
fn quote_price_exact_tokens_for_tokens(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>, amount: Balance, include_fee: bool) -> Option<Balance> {
fn quote_price_exact_tokens_for_tokens(asset1: NativeOrWithId<u32>, asset2: NativeOrWithId<u32>, amount: Balance, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_exact_tokens_for_tokens(asset1, asset2, amount, include_fee)
}
fn quote_price_tokens_for_exact_tokens(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>, amount: Balance, include_fee: bool) -> Option<Balance> {
fn quote_price_tokens_for_exact_tokens(asset1: NativeOrWithId<u32>, asset2: NativeOrWithId<u32>, amount: Balance, include_fee: bool) -> Option<Balance> {
AssetConversion::quote_price_tokens_for_exact_tokens(asset1, asset2, amount, include_fee)
}
fn get_reserves(asset1: NativeOrAssetId<u32>, asset2: NativeOrAssetId<u32>) -> Option<(Balance, Balance)> {
AssetConversion::get_reserves(&asset1, &asset2).ok()
fn get_reserves(asset1: NativeOrWithId<u32>, asset2: NativeOrWithId<u32>) -> Option<(Balance, Balance)> {
AssetConversion::get_reserves(asset1, asset2).ok()
}
}
@@ -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);
+204 -332
View File
@@ -63,7 +63,8 @@ mod swap;
mod tests;
mod types;
pub mod weights;
#[cfg(feature = "runtime-benchmarks")]
pub use benchmarking::{BenchmarkHelper, NativeOrWithIdFactory};
pub use pallet::*;
pub use swap::*;
pub use types::*;
@@ -73,18 +74,14 @@ use codec::Codec;
use frame_support::{
storage::{with_storage_layer, with_transaction},
traits::{
fungible::{
Balanced as BalancedFungible, Credit as CreditFungible, Inspect as InspectFungible,
Mutate as MutateFungible,
},
fungibles::{Balanced, Create, Credit as CreditFungibles, Inspect, Mutate},
fungibles::{Balanced, Create, Credit, Inspect, Mutate},
tokens::{
AssetId, Balance,
Fortitude::Polite,
Precision::Exact,
Preservation::{Expendable, Preserve},
},
AccountTouch, ContainsPair, Imbalance, Incrementable,
AccountTouch, Incrementable, OnUnbalanced,
},
PalletId,
};
@@ -94,7 +91,7 @@ use sp_runtime::{
CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Ensure, IntegerSquareRoot, MaybeDisplay,
One, TrailingZeroInput, Zero,
},
DispatchError, RuntimeDebug, Saturating, TokenError, TransactionOutcome,
DispatchError, Saturating, TokenError, TransactionOutcome,
};
use sp_std::{boxed::Box, collections::btree_set::BTreeSet, vec::Vec};
@@ -113,11 +110,6 @@ pub mod pallet {
/// Overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Currency type that this works on.
type Currency: InspectFungible<Self::AccountId, Balance = Self::Balance>
+ MutateFungible<Self::AccountId>
+ BalancedFungible<Self::AccountId>;
/// The type in which the assets for swapping are measured.
type Balance: Balance;
@@ -130,37 +122,34 @@ pub mod pallet {
+ From<Self::Balance>
+ TryInto<Self::Balance>;
/// Identifier for the class of non-native asset.
/// Note: A `From<u32>` bound here would prevent `MultiLocation` from being used as an
/// `AssetId`.
type AssetId: AssetId;
/// Type of asset class, sourced from [`Config::Assets`], utilized to offer liquidity to a
/// pool.
type AssetKind: Parameter + MaxEncodedLen;
/// Type that identifies either the native currency or a token class from `Assets`.
/// `Ord` is added because of `get_pool_id`.
///
/// The pool's `AccountId` is derived from this type. Any changes to the type may
/// necessitate a migration.
type MultiAssetId: AssetId + Ord + From<Self::AssetId>;
/// Type to convert an `AssetId` into `MultiAssetId`.
type MultiAssetIdConverter: MultiAssetIdConverter<Self::MultiAssetId, Self::AssetId>;
/// `AssetId` to address the lp tokens by.
type PoolAssetId: AssetId + PartialOrd + Incrementable + From<u32>;
/// Registry for the assets.
type Assets: Inspect<Self::AccountId, AssetId = Self::AssetId, Balance = Self::Balance>
/// Registry of assets utilized for providing liquidity to pools.
type Assets: Inspect<Self::AccountId, AssetId = Self::AssetKind, Balance = Self::Balance>
+ Mutate<Self::AccountId>
+ AccountTouch<Self::AssetId, Self::AccountId>
+ ContainsPair<Self::AssetId, Self::AccountId>
+ AccountTouch<Self::AssetKind, Self::AccountId, Balance = Self::Balance>
+ Balanced<Self::AccountId>;
/// Liquidity pool identifier.
type PoolId: Parameter + MaxEncodedLen + Ord;
/// Provides means to resolve the [`Config::PoolId`] and it's `AccountId` from a pair
/// of [`Config::AssetKind`]s.
///
/// Examples: [`crate::types::WithFirstAsset`], [`crate::types::Ascending`].
type PoolLocator: PoolLocator<Self::AccountId, Self::AssetKind, Self::PoolId>;
/// Asset class for the lp tokens from [`Self::PoolAssets`].
type PoolAssetId: AssetId + PartialOrd + Incrementable + From<u32>;
/// Registry for the lp tokens. Ideally only this pallet should have create permissions on
/// the assets.
type PoolAssets: Inspect<Self::AccountId, AssetId = Self::PoolAssetId, Balance = Self::Balance>
+ Create<Self::AccountId>
+ Mutate<Self::AccountId>
+ AccountTouch<Self::PoolAssetId, Self::AccountId>;
+ AccountTouch<Self::PoolAssetId, Self::AccountId, Balance = Self::Balance>;
/// A % the liquidity providers will take of every swap. Represents 10ths of a percent.
#[pallet::constant]
@@ -170,8 +159,12 @@ pub mod pallet {
#[pallet::constant]
type PoolSetupFee: Get<Self::Balance>;
/// An account that receives the pool setup fee.
type PoolSetupFeeReceiver: Get<Self::AccountId>;
/// Asset class from [`Config::Assets`] used to pay the [`Config::PoolSetupFee`].
#[pallet::constant]
type PoolSetupFeeAsset: Get<Self::AssetKind>;
/// Handler for the [`Config::PoolSetupFee`].
type PoolSetupFeeTarget: OnUnbalanced<CreditOf<Self>>;
/// A fee to withdraw the liquidity.
#[pallet::constant]
@@ -189,23 +182,19 @@ pub mod pallet {
#[pallet::constant]
type PalletId: Get<PalletId>;
/// A setting to allow creating pools with both non-native assets.
#[pallet::constant]
type AllowMultiAssetPools: Get<bool>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
/// The benchmarks need a way to create asset ids from u32s.
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: BenchmarkHelper<Self::AssetId, Self::MultiAssetId>;
type BenchmarkHelper: BenchmarkHelper<Self::AssetKind>;
}
/// Map from `PoolAssetId` to `PoolInfo`. This establishes whether a pool has been officially
/// created rather than people sending tokens directly to a pool's public account.
#[pallet::storage]
pub type Pools<T: Config> =
StorageMap<_, Blake2_128Concat, PoolIdOf<T>, PoolInfo<T::PoolAssetId>, OptionQuery>;
StorageMap<_, Blake2_128Concat, T::PoolId, PoolInfo<T::PoolAssetId>, OptionQuery>;
/// Stores the `PoolAssetId` that is going to be used for the next lp token.
/// This gets incremented whenever a new lp pool is created.
@@ -222,7 +211,7 @@ pub mod pallet {
creator: T::AccountId,
/// The pool id associated with the pool. Note that the order of the assets may not be
/// the same as the order specified in the create pool extrinsic.
pool_id: PoolIdOf<T>,
pool_id: T::PoolId,
/// The account ID of the pool.
pool_account: T::AccountId,
/// The id of the liquidity tokens that will be minted when assets are added to this
@@ -237,7 +226,7 @@ pub mod pallet {
/// The account that the liquidity tokens were minted to.
mint_to: T::AccountId,
/// The pool id of the pool that the liquidity was added to.
pool_id: PoolIdOf<T>,
pool_id: T::PoolId,
/// The amount of the first asset that was added to the pool.
amount1_provided: T::Balance,
/// The amount of the second asset that was added to the pool.
@@ -255,7 +244,7 @@ pub mod pallet {
/// The account that the assets were transferred to.
withdraw_to: T::AccountId,
/// The pool id that the liquidity was removed from.
pool_id: PoolIdOf<T>,
pool_id: T::PoolId,
/// The amount of the first asset that was removed from the pool.
amount1: T::Balance,
/// The amount of the second asset that was removed from the pool.
@@ -296,10 +285,8 @@ pub mod pallet {
#[pallet::error]
pub enum Error<T> {
/// Provided assets are equal.
EqualAssets,
/// Provided asset is not supported for pool.
UnsupportedAsset,
/// Provided asset pair is not supported for pool.
InvalidAssetPair,
/// Pool already exists.
PoolExists,
/// Desired amount can't be zero.
@@ -335,18 +322,12 @@ pub mod pallet {
ZeroLiquidity,
/// Amount can't be zero.
ZeroAmount,
/// Insufficient liquidity in the pool.
InsufficientLiquidity,
/// Calculated amount out is less than provided minimum amount.
ProvidedMinimumNotSufficientForSwap,
/// Provided maximum amount is not sufficient for swap.
ProvidedMaximumNotSufficientForSwap,
/// Only pools with native on one side are valid.
PoolMustContainNativeCurrency,
/// The provided path must consists of 2 assets at least.
InvalidPath,
/// It was not possible to calculate path data.
PathError,
/// The provided path must consists of unique assets.
NonUniquePath,
/// It was not possible to get or increment the Id of the pool.
@@ -376,48 +357,32 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::create_pool())]
pub fn create_pool(
origin: OriginFor<T>,
asset1: Box<T::MultiAssetId>,
asset2: Box<T::MultiAssetId>,
asset1: Box<T::AssetKind>,
asset2: Box<T::AssetKind>,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
ensure!(asset1 != asset2, Error::<T>::EqualAssets);
ensure!(asset1 != asset2, Error::<T>::InvalidAssetPair);
// prepare pool_id
let pool_id = Self::get_pool_id(*asset1, *asset2);
let pool_id = T::PoolLocator::pool_id(&asset1, &asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
ensure!(!Pools::<T>::contains_key(&pool_id), Error::<T>::PoolExists);
let (asset1, asset2) = &pool_id;
if !T::AllowMultiAssetPools::get() && !T::MultiAssetIdConverter::is_native(asset1) {
Err(Error::<T>::PoolMustContainNativeCurrency)?;
}
let pool_account = Self::get_pool_account(&pool_id);
frame_system::Pallet::<T>::inc_providers(&pool_account);
let pool_account =
T::PoolLocator::address(&pool_id).map_err(|_| Error::<T>::InvalidAssetPair)?;
// pay the setup fee
T::Currency::transfer(
&sender,
&T::PoolSetupFeeReceiver::get(),
T::PoolSetupFee::get(),
Preserve,
)?;
let fee =
Self::withdraw(T::PoolSetupFeeAsset::get(), &sender, T::PoolSetupFee::get(), true)?;
T::PoolSetupFeeTarget::on_unbalanced(fee);
// try to convert both assets
match T::MultiAssetIdConverter::try_convert(asset1) {
MultiAssetIdConversionResult::Converted(asset) =>
if !T::Assets::contains(&asset, &pool_account) {
T::Assets::touch(asset, &pool_account, &sender)?
},
MultiAssetIdConversionResult::Unsupported(_) => Err(Error::<T>::UnsupportedAsset)?,
MultiAssetIdConversionResult::Native => (),
}
match T::MultiAssetIdConverter::try_convert(asset2) {
MultiAssetIdConversionResult::Converted(asset) =>
if !T::Assets::contains(&asset, &pool_account) {
T::Assets::touch(asset, &pool_account, &sender)?
},
MultiAssetIdConversionResult::Unsupported(_) => Err(Error::<T>::UnsupportedAsset)?,
MultiAssetIdConversionResult::Native => (),
}
if T::Assets::should_touch(*asset1.clone(), &pool_account) {
T::Assets::touch(*asset1, &pool_account, &sender)?
};
if T::Assets::should_touch(*asset2.clone(), &pool_account) {
T::Assets::touch(*asset2, &pool_account, &sender)?
};
let lp_token = NextPoolAssetId::<T>::get()
.or(T::PoolAssetId::initial_value())
@@ -454,8 +419,8 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::add_liquidity())]
pub fn add_liquidity(
origin: OriginFor<T>,
asset1: Box<T::MultiAssetId>,
asset2: Box<T::MultiAssetId>,
asset1: Box<T::AssetKind>,
asset2: Box<T::AssetKind>,
amount1_desired: T::Balance,
amount2_desired: T::Balance,
amount1_min: T::Balance,
@@ -464,26 +429,20 @@ pub mod pallet {
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let pool_id = Self::get_pool_id(*asset1.clone(), *asset2);
// swap params if needed
let (amount1_desired, amount2_desired, amount1_min, amount2_min) =
if pool_id.0 == *asset1 {
(amount1_desired, amount2_desired, amount1_min, amount2_min)
} else {
(amount2_desired, amount1_desired, amount2_min, amount1_min)
};
let pool_id = T::PoolLocator::pool_id(&asset1, &asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
ensure!(
amount1_desired > Zero::zero() && amount2_desired > Zero::zero(),
Error::<T>::WrongDesiredAmount
);
let maybe_pool = Pools::<T>::get(&pool_id);
let pool = maybe_pool.as_ref().ok_or(Error::<T>::PoolNotFound)?;
let pool_account = Self::get_pool_account(&pool_id);
let pool = Pools::<T>::get(&pool_id).ok_or(Error::<T>::PoolNotFound)?;
let pool_account =
T::PoolLocator::address(&pool_id).map_err(|_| Error::<T>::InvalidAssetPair)?;
let (asset1, asset2) = &pool_id;
let reserve1 = Self::get_balance(&pool_account, asset1)?;
let reserve2 = Self::get_balance(&pool_account, asset2)?;
let reserve1 = Self::get_balance(&pool_account, *asset1.clone());
let reserve2 = Self::get_balance(&pool_account, *asset2.clone());
let amount1: T::Balance;
let amount2: T::Balance;
@@ -515,13 +474,17 @@ pub mod pallet {
}
}
Self::validate_minimal_amount(amount1.saturating_add(reserve1), asset1)
.map_err(|_| Error::<T>::AmountOneLessThanMinimal)?;
Self::validate_minimal_amount(amount2.saturating_add(reserve2), asset2)
.map_err(|_| Error::<T>::AmountTwoLessThanMinimal)?;
ensure!(
amount1.saturating_add(reserve1) >= T::Assets::minimum_balance(*asset1.clone()),
Error::<T>::AmountOneLessThanMinimal
);
ensure!(
amount2.saturating_add(reserve2) >= T::Assets::minimum_balance(*asset2.clone()),
Error::<T>::AmountTwoLessThanMinimal
);
Self::transfer(asset1, &sender, &pool_account, amount1, true)?;
Self::transfer(asset2, &sender, &pool_account, amount2, true)?;
T::Assets::transfer(*asset1, &sender, &pool_account, amount1, Preserve)?;
T::Assets::transfer(*asset2, &sender, &pool_account, amount2, Preserve)?;
let total_supply = T::PoolAssets::total_issuance(pool.lp_token.clone());
@@ -552,7 +515,7 @@ pub mod pallet {
pool_id,
amount1_provided: amount1,
amount2_provided: amount2,
lp_token: pool.lp_token.clone(),
lp_token: pool.lp_token,
lp_token_minted: lp_token_amount,
});
@@ -566,8 +529,8 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::remove_liquidity())]
pub fn remove_liquidity(
origin: OriginFor<T>,
asset1: Box<T::MultiAssetId>,
asset2: Box<T::MultiAssetId>,
asset1: Box<T::AssetKind>,
asset2: Box<T::AssetKind>,
lp_token_burn: T::Balance,
amount1_min_receive: T::Balance,
amount2_min_receive: T::Balance,
@@ -575,23 +538,17 @@ pub mod pallet {
) -> DispatchResult {
let sender = ensure_signed(origin)?;
let pool_id = Self::get_pool_id(*asset1.clone(), *asset2);
// swap params if needed
let (amount1_min_receive, amount2_min_receive) = if pool_id.0 == *asset1 {
(amount1_min_receive, amount2_min_receive)
} else {
(amount2_min_receive, amount1_min_receive)
};
let (asset1, asset2) = pool_id.clone();
let pool_id = T::PoolLocator::pool_id(&asset1, &asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
ensure!(lp_token_burn > Zero::zero(), Error::<T>::ZeroLiquidity);
let maybe_pool = Pools::<T>::get(&pool_id);
let pool = maybe_pool.as_ref().ok_or(Error::<T>::PoolNotFound)?;
let pool = Pools::<T>::get(&pool_id).ok_or(Error::<T>::PoolNotFound)?;
let pool_account = Self::get_pool_account(&pool_id);
let reserve1 = Self::get_balance(&pool_account, &asset1)?;
let reserve2 = Self::get_balance(&pool_account, &asset2)?;
let pool_account =
T::PoolLocator::address(&pool_id).map_err(|_| Error::<T>::InvalidAssetPair)?;
let reserve1 = Self::get_balance(&pool_account, *asset1.clone());
let reserve2 = Self::get_balance(&pool_account, *asset2.clone());
let total_supply = T::PoolAssets::total_issuance(pool.lp_token.clone());
let withdrawal_fee_amount = T::LiquidityWithdrawalFee::get() * lp_token_burn;
@@ -610,16 +567,20 @@ pub mod pallet {
);
let reserve1_left = reserve1.saturating_sub(amount1);
let reserve2_left = reserve2.saturating_sub(amount2);
Self::validate_minimal_amount(reserve1_left, &asset1)
.map_err(|_| Error::<T>::ReserveLeftLessThanMinimal)?;
Self::validate_minimal_amount(reserve2_left, &asset2)
.map_err(|_| Error::<T>::ReserveLeftLessThanMinimal)?;
ensure!(
reserve1_left >= T::Assets::minimum_balance(*asset1.clone()),
Error::<T>::ReserveLeftLessThanMinimal
);
ensure!(
reserve2_left >= T::Assets::minimum_balance(*asset2.clone()),
Error::<T>::ReserveLeftLessThanMinimal
);
// burn the provided lp token amount that includes the fee
T::PoolAssets::burn_from(pool.lp_token.clone(), &sender, lp_token_burn, Exact, Polite)?;
Self::transfer(&asset1, &pool_account, &withdraw_to, amount1, false)?;
Self::transfer(&asset2, &pool_account, &withdraw_to, amount2, false)?;
T::Assets::transfer(*asset1, &pool_account, &withdraw_to, amount1, Expendable)?;
T::Assets::transfer(*asset2, &pool_account, &withdraw_to, amount2, Expendable)?;
Self::deposit_event(Event::LiquidityRemoved {
who: sender,
@@ -627,7 +588,7 @@ pub mod pallet {
pool_id,
amount1,
amount2,
lp_token: pool.lp_token.clone(),
lp_token: pool.lp_token,
lp_token_burned: lp_token_burn,
withdrawal_fee: T::LiquidityWithdrawalFee::get(),
});
@@ -642,10 +603,10 @@ pub mod pallet {
/// [`AssetConversionApi::quote_price_exact_tokens_for_tokens`] runtime call can be called
/// for a quote.
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::swap_exact_tokens_for_tokens())]
#[pallet::weight(T::WeightInfo::swap_exact_tokens_for_tokens(path.len() as u32))]
pub fn swap_exact_tokens_for_tokens(
origin: OriginFor<T>,
path: Vec<Box<T::MultiAssetId>>,
path: Vec<Box<T::AssetKind>>,
amount_in: T::Balance,
amount_out_min: T::Balance,
send_to: T::AccountId,
@@ -670,10 +631,10 @@ pub mod pallet {
/// [`AssetConversionApi::quote_price_tokens_for_exact_tokens`] runtime call can be called
/// for a quote.
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::swap_tokens_for_exact_tokens())]
#[pallet::weight(T::WeightInfo::swap_tokens_for_exact_tokens(path.len() as u32))]
pub fn swap_tokens_for_exact_tokens(
origin: OriginFor<T>,
path: Vec<Box<T::MultiAssetId>>,
path: Vec<Box<T::AssetKind>>,
amount_out: T::Balance,
amount_in_max: T::Balance,
send_to: T::AccountId,
@@ -707,7 +668,7 @@ pub mod pallet {
/// rollback.
pub(crate) fn do_swap_exact_tokens_for_tokens(
sender: T::AccountId,
path: Vec<T::MultiAssetId>,
path: Vec<T::AssetKind>,
amount_in: T::Balance,
amount_out_min: Option<T::Balance>,
send_to: T::AccountId,
@@ -755,7 +716,7 @@ pub mod pallet {
/// rollback.
pub(crate) fn do_swap_tokens_for_exact_tokens(
sender: T::AccountId,
path: Vec<T::MultiAssetId>,
path: Vec<T::AssetKind>,
amount_out: T::Balance,
amount_in_max: Option<T::Balance>,
send_to: T::AccountId,
@@ -801,13 +762,16 @@ pub mod pallet {
/// only inside a transactional storage context and an Err result must imply a storage
/// rollback.
pub(crate) fn do_swap_exact_credit_tokens_for_tokens(
path: Vec<T::MultiAssetId>,
credit_in: Credit<T>,
path: Vec<T::AssetKind>,
credit_in: CreditOf<T>,
amount_out_min: Option<T::Balance>,
) -> Result<Credit<T>, (Credit<T>, DispatchError)> {
) -> Result<CreditOf<T>, (CreditOf<T>, DispatchError)> {
let amount_in = credit_in.peek();
let inspect_path = |credit_asset| {
ensure!(path.get(0).map_or(false, |a| *a == credit_asset), Error::<T>::InvalidPath);
ensure!(
path.first().map_or(false, |a| *a == credit_asset),
Error::<T>::InvalidPath
);
ensure!(!amount_in.is_zero(), Error::<T>::ZeroAmount);
ensure!(amount_out_min.map_or(true, |a| !a.is_zero()), Error::<T>::ZeroAmount);
@@ -846,13 +810,16 @@ pub mod pallet {
/// only inside a transactional storage context and an Err result must imply a storage
/// rollback.
pub(crate) fn do_swap_credit_tokens_for_exact_tokens(
path: Vec<T::MultiAssetId>,
credit_in: Credit<T>,
path: Vec<T::AssetKind>,
credit_in: CreditOf<T>,
amount_out: T::Balance,
) -> Result<(Credit<T>, Credit<T>), (Credit<T>, DispatchError)> {
) -> Result<(CreditOf<T>, CreditOf<T>), (CreditOf<T>, DispatchError)> {
let amount_in_max = credit_in.peek();
let inspect_path = |credit_asset| {
ensure!(path.get(0).map_or(false, |a| a == &credit_asset), Error::<T>::InvalidPath);
ensure!(
path.first().map_or(false, |a| a == &credit_asset),
Error::<T>::InvalidPath
);
ensure!(amount_in_max > Zero::zero(), Error::<T>::ZeroAmount);
ensure!(amount_out > Zero::zero(), Error::<T>::ZeroAmount);
@@ -880,75 +847,6 @@ pub mod pallet {
Ok((credit_out, credit_change))
}
/// Transfer an `amount` of `asset_id`, respecting the `keep_alive` requirements.
fn transfer(
asset_id: &T::MultiAssetId,
from: &T::AccountId,
to: &T::AccountId,
amount: T::Balance,
keep_alive: bool,
) -> Result<T::Balance, DispatchError> {
let preservation = match keep_alive {
true => Preserve,
false => Expendable,
};
match T::MultiAssetIdConverter::try_convert(asset_id) {
MultiAssetIdConversionResult::Converted(asset_id) =>
T::Assets::transfer(asset_id, from, to, amount, preservation),
MultiAssetIdConversionResult::Native =>
Ok(T::Currency::transfer(from, to, amount, preservation)?),
MultiAssetIdConversionResult::Unsupported(_) =>
Err(Error::<T>::UnsupportedAsset.into()),
}
}
/// The balance of `who` is increased in order to counter `credit`. If the whole of `credit`
/// cannot be countered, then nothing is changed and the original `credit` is returned in an
/// `Err`.
fn resolve(who: &T::AccountId, credit: Credit<T>) -> Result<(), Credit<T>> {
match credit {
Credit::Native(c) => T::Currency::resolve(who, c).map_err(|c| c.into()),
Credit::Asset(c) => T::Assets::resolve(who, c).map_err(|c| c.into()),
}
}
/// Removes `value` balance of `asset` from `who` account if possible.
fn withdraw(
asset: &T::MultiAssetId,
who: &T::AccountId,
value: T::Balance,
keep_alive: bool,
) -> Result<Credit<T>, DispatchError> {
let preservation = match keep_alive {
true => Preserve,
false => Expendable,
};
match T::MultiAssetIdConverter::try_convert(asset) {
MultiAssetIdConversionResult::Converted(asset) => {
if preservation == Preserve {
// TODO drop the ensure! when this issue addressed
// https://github.com/paritytech/polkadot-sdk/issues/1698
let free =
T::Assets::reducible_balance(asset.clone(), who, preservation, Polite);
ensure!(free >= value, TokenError::NotExpendable);
}
T::Assets::withdraw(asset, who, value, Exact, preservation, Polite)
.map(|c| c.into())
},
MultiAssetIdConversionResult::Native => {
if preservation == Preserve {
// TODO drop the ensure! when this issue addressed
// https://github.com/paritytech/polkadot-sdk/issues/1698
let free = T::Currency::reducible_balance(who, preservation, Polite);
ensure!(free >= value, TokenError::NotExpendable);
}
T::Currency::withdraw(who, value, Exact, preservation, Polite).map(|c| c.into())
},
MultiAssetIdConversionResult::Unsupported(_) =>
Err(Error::<T>::UnsupportedAsset.into()),
}
}
/// Swap assets along the `path`, withdrawing from `sender` and depositing in `send_to`.
///
/// Note: It's assumed that the provided `path` is valid.
@@ -963,10 +861,10 @@ pub mod pallet {
keep_alive: bool,
) -> Result<(), DispatchError> {
let (asset_in, amount_in) = path.first().ok_or(Error::<T>::InvalidPath)?;
let credit_in = Self::withdraw(asset_in, sender, *amount_in, keep_alive)?;
let credit_in = Self::withdraw(asset_in.clone(), sender, *amount_in, keep_alive)?;
let credit_out = Self::credit_swap(credit_in, path).map_err(|(_, e)| e)?;
Self::resolve(send_to, credit_out).map_err(|_| Error::<T>::BelowMinimum)?;
T::Assets::resolve(send_to, credit_out).map_err(|_| Error::<T>::BelowMinimum)?;
Ok(())
}
@@ -983,29 +881,34 @@ pub mod pallet {
/// only inside a transactional storage context and an Err result must imply a storage
/// rollback.
fn credit_swap(
credit_in: Credit<T>,
credit_in: CreditOf<T>,
path: &BalancePath<T>,
) -> Result<Credit<T>, (Credit<T>, DispatchError)> {
let resolve_path = || -> Result<Credit<T>, DispatchError> {
) -> Result<CreditOf<T>, (CreditOf<T>, DispatchError)> {
let resolve_path = || -> Result<CreditOf<T>, DispatchError> {
for pos in 0..=path.len() {
if let Some([(asset1, _), (asset2, amount_out)]) = path.get(pos..=pos + 1) {
let pool_from = Self::get_pool_account(&Self::get_pool_id(
asset1.clone(),
asset2.clone(),
));
let pool_from = T::PoolLocator::pool_address(asset1, asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
if let Some((asset3, _)) = path.get(pos + 2) {
let pool_to = Self::get_pool_account(&Self::get_pool_id(
let pool_to = T::PoolLocator::pool_address(asset2, asset3)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
T::Assets::transfer(
asset2.clone(),
asset3.clone(),
));
Self::transfer(asset2, &pool_from, &pool_to, *amount_out, true)?;
&pool_from,
&pool_to,
*amount_out,
Preserve,
)?;
} else {
let credit_out = Self::withdraw(asset2, &pool_from, *amount_out, true)?;
let credit_out =
Self::withdraw(asset2.clone(), &pool_from, *amount_out, true)?;
return Ok(credit_out)
}
}
}
return Err(Error::<T>::InvalidPath.into())
Err(Error::<T>::InvalidPath.into())
};
let credit_out = match resolve_path() {
@@ -1014,79 +917,57 @@ pub mod pallet {
};
let pool_to = if let Some([(asset1, _), (asset2, _)]) = path.get(0..2) {
Self::get_pool_account(&Self::get_pool_id(asset1.clone(), asset2.clone()))
match T::PoolLocator::pool_address(asset1, asset2) {
Ok(address) => address,
Err(_) => return Err((credit_in, Error::<T>::InvalidAssetPair.into())),
}
} else {
return Err((credit_in, Error::<T>::InvalidPath.into()))
};
Self::resolve(&pool_to, credit_in).map_err(|c| (c, Error::<T>::BelowMinimum.into()))?;
T::Assets::resolve(&pool_to, credit_in)
.map_err(|c| (c, Error::<T>::BelowMinimum.into()))?;
Ok(credit_out)
}
/// The account ID of the pool.
///
/// This actually does computation. If you need to keep using it, then make sure you cache
/// the value and only call this once.
pub fn get_pool_account(pool_id: &PoolIdOf<T>) -> T::AccountId {
let encoded_pool_id = sp_io::hashing::blake2_256(&Encode::encode(pool_id)[..]);
Decode::decode(&mut TrailingZeroInput::new(encoded_pool_id.as_ref()))
.expect("infinite length input; no invalid inputs for type; qed")
/// Removes `value` balance of `asset` from `who` account if possible.
fn withdraw(
asset: T::AssetKind,
who: &T::AccountId,
value: T::Balance,
keep_alive: bool,
) -> Result<CreditOf<T>, DispatchError> {
let preservation = match keep_alive {
true => Preserve,
false => Expendable,
};
if preservation == Preserve {
// TODO drop the ensure! when this issue addressed
// https://github.com/paritytech/polkadot-sdk/issues/1698
let free = T::Assets::reducible_balance(asset.clone(), who, preservation, Polite);
ensure!(free >= value, TokenError::NotExpendable);
}
T::Assets::withdraw(asset, who, value, Exact, preservation, Polite)
}
/// Get the `owner`'s balance of `asset`, which could be the chain's native asset or another
/// fungible. Returns a value in the form of an `Balance`.
fn get_balance(
owner: &T::AccountId,
asset: &T::MultiAssetId,
) -> Result<T::Balance, Error<T>> {
match T::MultiAssetIdConverter::try_convert(asset) {
MultiAssetIdConversionResult::Converted(asset_id) => Ok(
<<T as Config>::Assets>::reducible_balance(asset_id, owner, Expendable, Polite),
),
MultiAssetIdConversionResult::Native =>
Ok(<<T as Config>::Currency>::reducible_balance(owner, Expendable, Polite)),
MultiAssetIdConversionResult::Unsupported(_) =>
Err(Error::<T>::UnsupportedAsset.into()),
}
}
/// Returns a pool id constructed from 2 assets.
/// 1. Native asset should be lower than the other asset ids.
/// 2. Two native or two non-native assets are compared by their `Ord` implementation.
///
/// We expect deterministic order, so (asset1, asset2) or (asset2, asset1) returns the same
/// result.
pub fn get_pool_id(asset1: T::MultiAssetId, asset2: T::MultiAssetId) -> PoolIdOf<T> {
match (
T::MultiAssetIdConverter::is_native(&asset1),
T::MultiAssetIdConverter::is_native(&asset2),
) {
(true, false) => return (asset1, asset2),
(false, true) => return (asset2, asset1),
_ => {
// else we want to be deterministic based on `Ord` implementation
if asset1 <= asset2 {
(asset1, asset2)
} else {
(asset2, asset1)
}
},
}
fn get_balance(owner: &T::AccountId, asset: T::AssetKind) -> T::Balance {
T::Assets::reducible_balance(asset, owner, Expendable, Polite)
}
/// Returns the balance of each asset in the pool.
/// The tuple result is in the order requested (not necessarily the same as pool order).
pub fn get_reserves(
asset1: &T::MultiAssetId,
asset2: &T::MultiAssetId,
asset1: T::AssetKind,
asset2: T::AssetKind,
) -> Result<(T::Balance, T::Balance), Error<T>> {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_account = Self::get_pool_account(&pool_id);
let pool_account = T::PoolLocator::pool_address(&asset1, &asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
let balance1 = Self::get_balance(&pool_account, asset1)?;
let balance2 = Self::get_balance(&pool_account, asset2)?;
let balance1 = Self::get_balance(&pool_account, asset1);
let balance2 = Self::get_balance(&pool_account, asset2);
if balance1.is_zero() || balance2.is_zero() {
Err(Error::<T>::PoolNotFound)?;
@@ -1098,7 +979,7 @@ pub mod pallet {
/// Leading to an amount at the end of a `path`, get the required amounts in.
pub(crate) fn balance_path_from_amount_out(
amount_out: T::Balance,
path: Vec<T::MultiAssetId>,
path: Vec<T::AssetKind>,
) -> Result<BalancePath<T>, DispatchError> {
let mut balance_path: BalancePath<T> = Vec::with_capacity(path.len());
let mut amount_in: T::Balance = amount_out;
@@ -1112,7 +993,7 @@ pub mod pallet {
break
},
};
let (reserve_in, reserve_out) = Self::get_reserves(asset1, &asset2)?;
let (reserve_in, reserve_out) = Self::get_reserves(asset1.clone(), asset2.clone())?;
balance_path.push((asset2, amount_in));
amount_in = Self::get_amount_in(&amount_in, &reserve_in, &reserve_out)?;
}
@@ -1124,7 +1005,7 @@ pub mod pallet {
/// Following an amount into a `path`, get the corresponding amounts out.
pub(crate) fn balance_path_from_amount_in(
amount_in: T::Balance,
path: Vec<T::MultiAssetId>,
path: Vec<T::AssetKind>,
) -> Result<BalancePath<T>, DispatchError> {
let mut balance_path: BalancePath<T> = Vec::with_capacity(path.len());
let mut amount_out: T::Balance = amount_in;
@@ -1138,7 +1019,7 @@ pub mod pallet {
break
},
};
let (reserve_in, reserve_out) = Self::get_reserves(&asset1, asset2)?;
let (reserve_in, reserve_out) = Self::get_reserves(asset1.clone(), asset2.clone())?;
balance_path.push((asset1, amount_out));
amount_out = Self::get_amount_out(&amount_out, &reserve_in, &reserve_out)?;
}
@@ -1147,16 +1028,15 @@ pub mod pallet {
/// Used by the RPC service to provide current prices.
pub fn quote_price_exact_tokens_for_tokens(
asset1: T::MultiAssetId,
asset2: T::MultiAssetId,
asset1: T::AssetKind,
asset2: T::AssetKind,
amount: T::Balance,
include_fee: bool,
) -> Option<T::Balance> {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_account = Self::get_pool_account(&pool_id);
let pool_account = T::PoolLocator::pool_address(&asset1, &asset2).ok()?;
let balance1 = Self::get_balance(&pool_account, &asset1).ok()?;
let balance2 = Self::get_balance(&pool_account, &asset2).ok()?;
let balance1 = Self::get_balance(&pool_account, asset1);
let balance2 = Self::get_balance(&pool_account, asset2);
if !balance1.is_zero() {
if include_fee {
Self::get_amount_out(&amount, &balance1, &balance2).ok()
@@ -1170,16 +1050,15 @@ pub mod pallet {
/// Used by the RPC service to provide current prices.
pub fn quote_price_tokens_for_exact_tokens(
asset1: T::MultiAssetId,
asset2: T::MultiAssetId,
asset1: T::AssetKind,
asset2: T::AssetKind,
amount: T::Balance,
include_fee: bool,
) -> Option<T::Balance> {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_account = Self::get_pool_account(&pool_id);
let pool_account = T::PoolLocator::pool_address(&asset1, &asset2).ok()?;
let balance1 = Self::get_balance(&pool_account, &asset1).ok()?;
let balance2 = Self::get_balance(&pool_account, &asset2).ok()?;
let balance1 = Self::get_balance(&pool_account, asset1);
let balance2 = Self::get_balance(&pool_account, asset2);
if !balance1.is_zero() {
if include_fee {
Self::get_amount_in(&amount, &balance1, &balance2).ok()
@@ -1246,7 +1125,7 @@ pub mod pallet {
let reserve_out = T::HigherPrecisionBalance::from(*reserve_out);
if reserve_in.is_zero() || reserve_out.is_zero() {
return Err(Error::<T>::ZeroLiquidity.into())
return Err(Error::<T>::ZeroLiquidity)
}
let amount_in_with_fee = amount_in
@@ -1281,11 +1160,11 @@ pub mod pallet {
let reserve_out = T::HigherPrecisionBalance::from(*reserve_out);
if reserve_in.is_zero() || reserve_out.is_zero() {
Err(Error::<T>::ZeroLiquidity.into())?
Err(Error::<T>::ZeroLiquidity)?
}
if amount_out >= reserve_out {
Err(Error::<T>::AmountOutTooHigh.into())?
Err(Error::<T>::AmountOutTooHigh)?
}
let numerator = reserve_in
@@ -1309,36 +1188,18 @@ pub mod pallet {
result.try_into().map_err(|_| Error::<T>::Overflow)
}
/// Ensure that a `value` meets the minimum balance requirements of an `asset` class.
fn validate_minimal_amount(value: T::Balance, asset: &T::MultiAssetId) -> Result<(), ()> {
if T::MultiAssetIdConverter::is_native(asset) {
let ed = T::Currency::minimum_balance();
ensure!(
T::HigherPrecisionBalance::from(value) >= T::HigherPrecisionBalance::from(ed),
()
);
} else {
let MultiAssetIdConversionResult::Converted(asset_id) =
T::MultiAssetIdConverter::try_convert(asset)
else {
return Err(())
};
let minimal = T::Assets::minimum_balance(asset_id);
ensure!(value >= minimal, ());
}
Ok(())
}
/// Ensure that a path is valid.
fn validate_swap_path(path: &Vec<T::MultiAssetId>) -> Result<(), DispatchError> {
fn validate_swap_path(path: &Vec<T::AssetKind>) -> Result<(), DispatchError> {
ensure!(path.len() >= 2, Error::<T>::InvalidPath);
ensure!(path.len() as u32 <= T::MaxSwapPathLength::get(), Error::<T>::InvalidPath);
// validate all the pools in the path are unique
let mut pools = BTreeSet::<PoolIdOf<T>>::new();
let mut pools = BTreeSet::<T::PoolId>::new();
for assets_pair in path.windows(2) {
if let [asset1, asset2] = assets_pair {
let pool_id = Self::get_pool_id(asset1.clone(), asset2.clone());
let pool_id = T::PoolLocator::pool_id(asset1, asset2)
.map_err(|_| Error::<T>::InvalidAssetPair)?;
let new_element = pools.insert(pool_id);
if !new_element {
return Err(Error::<T>::NonUniquePath.into())
@@ -1361,21 +1222,32 @@ pub mod pallet {
sp_api::decl_runtime_apis! {
/// This runtime api allows people to query the size of the liquidity pools
/// and quote prices for swaps.
pub trait AssetConversionApi<Balance, AssetId> where
pub trait AssetConversionApi<Balance, AssetId>
where
Balance: frame_support::traits::tokens::Balance + MaybeDisplay,
AssetId: Codec
AssetId: Codec,
{
/// Provides a quote for [`Pallet::swap_tokens_for_exact_tokens`].
///
/// Note that the price may have changed by the time the transaction is executed.
/// (Use `amount_in_max` to control slippage.)
fn quote_price_tokens_for_exact_tokens(asset1: AssetId, asset2: AssetId, amount: Balance, include_fee: bool) -> Option<Balance>;
fn quote_price_tokens_for_exact_tokens(
asset1: AssetId,
asset2: AssetId,
amount: Balance,
include_fee: bool,
) -> Option<Balance>;
/// Provides a quote for [`Pallet::swap_exact_tokens_for_tokens`].
///
/// Note that the price may have changed by the time the transaction is executed.
/// (Use `amount_out_min` to control slippage.)
fn quote_price_exact_tokens_for_tokens(asset1: AssetId, asset2: AssetId, amount: Balance, include_fee: bool) -> Option<Balance>;
fn quote_price_exact_tokens_for_tokens(
asset1: AssetId,
asset2: AssetId,
amount: Balance,
include_fee: bool,
) -> Option<Balance>;
/// Returns the size of the liquidity pool for the given asset pair.
fn get_reserves(asset1: AssetId, asset2: AssetId) -> Option<(Balance, Balance)>;
+23 -17
View File
@@ -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 = ();
}
+19 -17
View File
@@ -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
+81 -205
View File
@@ -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
View File
@@ -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()))
}
}
@@ -69,7 +69,6 @@ mod tests;
mod payment;
use frame_support::traits::tokens::AssetId;
use pallet_asset_conversion::MultiAssetIdConverter;
pub use payment::*;
/// Type aliases used for interaction with `OnChargeTransaction`.
@@ -16,7 +16,6 @@
use super::*;
use crate as pallet_asset_conversion_tx_payment;
use codec;
use frame_support::{
derive_impl,
dispatch::DispatchClass,
@@ -24,13 +23,19 @@ use frame_support::{
ord_parameter_types,
pallet_prelude::*,
parameter_types,
traits::{AsEnsureOriginWithArg, ConstU32, ConstU64, ConstU8, Imbalance, OnUnbalanced},
traits::{
tokens::{
fungible::{NativeFromLeft, NativeOrWithId, UnionOf},
imbalance::ResolveAssetTo,
},
AsEnsureOriginWithArg, ConstU32, ConstU64, ConstU8, Imbalance, OnUnbalanced,
},
weights::{Weight, WeightToFee as WeightToFeeT},
PalletId,
};
use frame_system as system;
use frame_system::{EnsureRoot, EnsureSignedBy};
use pallet_asset_conversion::{NativeOrAssetId, NativeOrAssetIdConverter};
use pallet_asset_conversion::{Ascending, Chain, WithFirstAsset};
use pallet_transaction_payment::CurrencyAdapter;
use sp_core::H256;
use sp_runtime::{
@@ -225,10 +230,9 @@ impl pallet_assets::Config<Instance2> for Runtime {
parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub storage AllowMultiAssetPools: bool = false;
// should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
pub storage LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
pub const MaxSwapPathLength: u32 = 4;
pub const Native: NativeOrWithId<u32> = NativeOrWithId::Native;
}
ord_parameter_types! {
@@ -237,27 +241,26 @@ ord_parameter_types! {
impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type AssetId = u32;
type Balance = Balance;
type HigherPrecisionBalance = u128;
type AssetKind = NativeOrWithId<u32>;
type Assets = UnionOf<Balances, Assets, NativeFromLeft, NativeOrWithId<u32>, AccountId>;
type PoolId = (Self::AssetKind, Self::AssetKind);
type PoolLocator = Chain<
WithFirstAsset<Native, AccountId, NativeOrWithId<u32>>,
Ascending<AccountId, NativeOrWithId<u32>>,
>;
type PoolAssetId = u32;
type Assets = Assets;
type PoolAssets = PoolAssets;
type PalletId = AssetConversionPalletId;
type WeightInfo = ();
type LPFee = ConstU32<3>; // means 0.3%
type PoolSetupFee = ConstU64<100>; // should be more or equal to the existential deposit
type PoolSetupFeeReceiver = AssetConversionOrigin;
type PoolSetupFeeAsset = Native;
type PoolSetupFeeTarget = ResolveAssetTo<AssetConversionOrigin, Self::Assets>;
type PalletId = AssetConversionPalletId;
type LPFee = ConstU32<3>; // means 0.3%
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = MaxSwapPathLength;
type MintMinLiquidity = ConstU64<100>; // 100 is good enough when the main currency has 12 decimals.
type Balance = u64;
type HigherPrecisionBalance = u128;
type MultiAssetId = NativeOrAssetId<u32>;
type MultiAssetIdConverter = NativeOrAssetIdConverter<u32>;
type WeightInfo = ();
pallet_asset_conversion::runtime_benchmarks_enabled! {
type BenchmarkHelper = ();
}
@@ -266,5 +269,5 @@ impl pallet_asset_conversion::Config for Runtime {
impl Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = Assets;
type OnChargeAssetTransaction = AssetConversionAdapter<Balances, AssetConversion>;
type OnChargeAssetTransaction = AssetConversionAdapter<Balances, AssetConversion, Native>;
}
@@ -24,7 +24,7 @@ use frame_support::{
};
use pallet_asset_conversion::Swap;
use sp_runtime::{
traits::{DispatchInfoOf, PostDispatchInfoOf, Zero},
traits::{DispatchInfoOf, Get, PostDispatchInfoOf, Zero},
transaction_validity::InvalidTransaction,
Saturating,
};
@@ -76,16 +76,17 @@ pub trait OnChargeAssetTransaction<T: Config> {
/// Implements the asset transaction for a balance to asset converter (implementing [`Swap`]).
///
/// The converter is given the complete fee in terms of the asset used for the transaction.
pub struct AssetConversionAdapter<C, CON>(PhantomData<(C, CON)>);
pub struct AssetConversionAdapter<C, CON, N>(PhantomData<(C, CON, N)>);
/// Default implementation for a runtime instantiating this pallet, an asset to native swapper.
impl<T, C, CON> OnChargeAssetTransaction<T> for AssetConversionAdapter<C, CON>
impl<T, C, CON, N> OnChargeAssetTransaction<T> for AssetConversionAdapter<C, CON, N>
where
N: Get<CON::AssetKind>,
T: Config,
C: Inspect<<T as frame_system::Config>::AccountId>,
CON: Swap<T::AccountId, Balance = BalanceOf<T>, MultiAssetId = T::MultiAssetId>,
CON: Swap<T::AccountId, Balance = BalanceOf<T>, AssetKind = T::AssetKind>,
BalanceOf<T>: Into<AssetBalanceOf<T>>,
T::MultiAssetId: From<AssetIdOf<T>>,
T::AssetKind: From<AssetIdOf<T>>,
BalanceOf<T>: IsType<<C as Inspect<<T as frame_system::Config>::AccountId>>::Balance>,
{
type Balance = BalanceOf<T>;
@@ -116,7 +117,7 @@ where
let asset_consumed = CON::swap_tokens_for_exact_tokens(
who.clone(),
vec![asset_id.into(), T::MultiAssetIdConverter::get_native()],
vec![asset_id.into(), N::get()],
native_asset_required,
None,
who.clone(),
@@ -168,8 +169,8 @@ where
match CON::swap_exact_tokens_for_tokens(
who.clone(), // we already deposited the native to `who`
vec![
T::MultiAssetIdConverter::get_native(), // we provide the native
asset_id.into(), // we want asset_id back
N::get(), // we provide the native
asset_id.into(), // we want asset_id back
],
swap_back, // amount of the native asset to convert to `asset_id`
None, // no minimum amount back
@@ -20,14 +20,13 @@ use frame_support::{
dispatch::{DispatchInfo, PostDispatchInfo},
pallet_prelude::*,
traits::{
fungible::Inspect,
fungible::{Inspect, NativeOrWithId},
fungibles::{Inspect as FungiblesInspect, Mutate},
},
weights::Weight,
};
use frame_system as system;
use mock::{ExtrinsicBaseWeight, *};
use pallet_asset_conversion::NativeOrAssetId;
use pallet_balances::Call as BalancesCall;
use sp_runtime::{traits::StaticLookup, BuildStorage};
@@ -127,12 +126,12 @@ fn setup_lp(asset_id: u32, balance_factor: u64) {
10_000 * balance_factor + ed_asset
));
let token_1 = NativeOrAssetId::Native;
let token_2 = NativeOrAssetId::Asset(asset_id);
let token_1 = NativeOrWithId::Native;
let token_2 = NativeOrWithId::WithId(asset_id);
assert_ok!(AssetConversion::create_pool(
RuntimeOrigin::signed(lp_provider),
Box::new(token_1),
Box::new(token_2)
Box::new(token_1.clone()),
Box::new(token_2.clone())
));
assert_ok!(AssetConversion::add_liquidity(
@@ -228,8 +227,8 @@ fn transaction_payment_in_asset_possible() {
let fee_in_native = base_weight + tx_weight + len as u64;
let input_quote = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(asset_id),
NativeOrAssetId::Native,
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
fee_in_native,
true,
);
@@ -337,8 +336,8 @@ fn transaction_payment_without_fee() {
let len = 10;
let fee_in_native = base_weight + weight + len as u64;
let input_quote = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(asset_id),
NativeOrAssetId::Native,
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
fee_in_native,
true,
);
@@ -355,8 +354,8 @@ fn transaction_payment_without_fee() {
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
let refund = AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(asset_id),
NativeOrWithId::Native,
NativeOrWithId::WithId(asset_id),
fee_in_native,
true,
)
@@ -412,8 +411,8 @@ fn asset_transaction_payment_with_tip_and_refund() {
let len = 10;
let fee_in_native = base_weight + weight + len as u64 + tip;
let input_quote = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(asset_id),
NativeOrAssetId::Native,
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
fee_in_native,
true,
);
@@ -428,8 +427,8 @@ fn asset_transaction_payment_with_tip_and_refund() {
let final_weight = 50;
let expected_fee = fee_in_native - final_weight - tip;
let expected_token_refund = AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(asset_id),
NativeOrWithId::Native,
NativeOrWithId::WithId(asset_id),
fee_in_native - expected_fee - tip,
true,
)
@@ -493,8 +492,8 @@ fn payment_from_account_with_only_assets() {
let fee_in_native = base_weight + weight + len as u64;
let ed = Balances::minimum_balance();
let fee_in_asset = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(asset_id),
NativeOrAssetId::Native,
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
fee_in_native + ed,
true,
)
@@ -509,8 +508,8 @@ fn payment_from_account_with_only_assets() {
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);
let refund = AssetConversion::quote_price_exact_tokens_for_tokens(
NativeOrAssetId::Native,
NativeOrAssetId::Asset(asset_id),
NativeOrWithId::Native,
NativeOrWithId::WithId(asset_id),
ed,
true,
)
@@ -585,8 +584,8 @@ fn converted_fee_is_never_zero_if_input_fee_is_not() {
// validate even a small fee gets converted to asset.
let fee_in_native = base_weight + weight + len as u64;
let fee_in_asset = AssetConversion::quote_price_tokens_for_exact_tokens(
NativeOrAssetId::Asset(asset_id),
NativeOrAssetId::Native,
NativeOrWithId::WithId(asset_id),
NativeOrWithId::Native,
fee_in_native,
true,
)