mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 17:31:03 +00:00
Asset Conversion: Pool Account ID derivation with additional Pallet ID seed (#3250)
Introduce `PalletId` as an additional seed parameter for pool's account id derivation. The PR also introduces the `pallet_asset_conversion_ops` pallet with a call to migrate a given pool to thew new account. Additionally `fungibles::lifetime::ResetTeam` and `fungible::lifetime::Refund` traits, to facilitate the migration of pools. --------- Co-authored-by: command-bot <>
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
[package]
|
||||
name = "pallet-asset-conversion-ops"
|
||||
version = "0.1.0"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://substrate.io"
|
||||
repository.workspace = true
|
||||
description = "FRAME asset conversion pallet's operations suite"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
|
||||
[dependencies]
|
||||
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
|
||||
log = { version = "0.4.20", default-features = false }
|
||||
frame-support = { path = "../../support", default-features = false }
|
||||
frame-system = { path = "../../system", default-features = false }
|
||||
frame-benchmarking = { path = "../../benchmarking", default-features = false, optional = true }
|
||||
pallet-asset-conversion = { path = "..", default-features = false }
|
||||
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
|
||||
sp-core = { path = "../../../primitives/core", default-features = false }
|
||||
sp-io = { path = "../../../primitives/io", default-features = false }
|
||||
sp-std = { path = "../../../primitives/std", default-features = false }
|
||||
sp-runtime = { path = "../../../primitives/runtime", default-features = false }
|
||||
sp-arithmetic = { path = "../../../primitives/arithmetic", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
pallet-balances = { path = "../../balances" }
|
||||
pallet-assets = { path = "../../assets" }
|
||||
primitive-types = { version = "0.12.0", default-features = false, features = ["codec", "num-traits", "scale-info"] }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
"codec/std",
|
||||
"frame-benchmarking?/std",
|
||||
"frame-support/std",
|
||||
"frame-system/std",
|
||||
"log/std",
|
||||
"pallet-asset-conversion/std",
|
||||
"pallet-assets/std",
|
||||
"pallet-balances/std",
|
||||
"primitive-types/std",
|
||||
"scale-info/std",
|
||||
"sp-arithmetic/std",
|
||||
"sp-core/std",
|
||||
"sp-io/std",
|
||||
"sp-runtime/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
runtime-benchmarks = [
|
||||
"frame-benchmarking/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
"frame-system/runtime-benchmarks",
|
||||
"pallet-asset-conversion/runtime-benchmarks",
|
||||
"pallet-assets/runtime-benchmarks",
|
||||
"pallet-balances/runtime-benchmarks",
|
||||
"sp-runtime/runtime-benchmarks",
|
||||
]
|
||||
try-runtime = [
|
||||
"frame-support/try-runtime",
|
||||
"frame-system/try-runtime",
|
||||
"pallet-asset-conversion/try-runtime",
|
||||
"pallet-assets/try-runtime",
|
||||
"pallet-balances/try-runtime",
|
||||
"sp-runtime/try-runtime",
|
||||
]
|
||||
@@ -0,0 +1,167 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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.
|
||||
|
||||
//! Asset Conversion Ops pallet benchmarking.
|
||||
|
||||
use super::*;
|
||||
use crate::Pallet as AssetConversionOps;
|
||||
use frame_benchmarking::{v2::*, whitelisted_caller};
|
||||
use frame_support::{
|
||||
assert_ok,
|
||||
traits::fungibles::{Create, Inspect, Mutate},
|
||||
};
|
||||
use frame_system::RawOrigin as SystemOrigin;
|
||||
use pallet_asset_conversion::{BenchmarkHelper, Pallet as AssetConversion};
|
||||
use sp_core::Get;
|
||||
use sp_runtime::traits::One;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
/// 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())
|
||||
));
|
||||
|
||||
(lp_token, liquidity1, liquidity2)
|
||||
}
|
||||
|
||||
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
|
||||
let events = frame_system::Pallet::<T>::events();
|
||||
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
|
||||
// compare to the last event record
|
||||
let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
|
||||
assert_eq!(event, &system_event);
|
||||
}
|
||||
|
||||
#[benchmarks(where T::Assets: Create<T::AccountId> + Mutate<T::AccountId>, T::PoolAssetId: Into<u32>,)]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
#[benchmark]
|
||||
fn migrate_to_new_account() {
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
let (asset1, asset2) = T::BenchmarkHelper::create_pair(0, 1);
|
||||
|
||||
create_fee_asset::<T>(&caller);
|
||||
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(),
|
||||
));
|
||||
|
||||
#[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 (prior_account, new_account) = AssetConversionOps::<T>::addresses(&pool_id).unwrap();
|
||||
assert_last_event::<T>(
|
||||
Event::MigratedToNewAccount { pool_id, new_account, prior_account }.into(),
|
||||
);
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(AssetConversionOps, crate::mock::new_test_ext(), crate::mock::Test);
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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.
|
||||
|
||||
//! # Asset Conversion Operations Suite.
|
||||
//!
|
||||
//! This pallet provides operational functionalities for the Asset Conversion pallet,
|
||||
//! allowing you to perform various migration and one-time-use operations. These operations
|
||||
//! are designed to facilitate updates and changes to the Asset Conversion pallet without
|
||||
//! breaking its API.
|
||||
//!
|
||||
//! ## Overview
|
||||
//!
|
||||
//! This suite allows you to perform the following operations:
|
||||
//! - Perform migration to update account ID derivation methods for existing pools. The migration
|
||||
//! operation ensures that the required accounts are created, existing account deposits are
|
||||
//! transferred, and liquidity is moved to the new accounts.
|
||||
|
||||
#![deny(missing_docs)]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
mod benchmarking;
|
||||
#[cfg(test)]
|
||||
mod mock;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub mod weights;
|
||||
pub use pallet::*;
|
||||
pub use weights::WeightInfo;
|
||||
|
||||
use frame_support::traits::{
|
||||
fungible::{Inspect as FungibleInspect, Mutate as FungibleMutate},
|
||||
fungibles::{roles::ResetTeam, Inspect, Mutate, Refund},
|
||||
tokens::{Fortitude, Precision, Preservation},
|
||||
AccountTouch,
|
||||
};
|
||||
use pallet_asset_conversion::{PoolLocator, Pools};
|
||||
use sp_runtime::traits::{TryConvert, Zero};
|
||||
use sp_std::boxed::Box;
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_system::pallet_prelude::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config:
|
||||
pallet_asset_conversion::Config<
|
||||
PoolId = (
|
||||
<Self as pallet_asset_conversion::Config>::AssetKind,
|
||||
<Self as pallet_asset_conversion::Config>::AssetKind,
|
||||
),
|
||||
> + frame_system::Config
|
||||
{
|
||||
/// Overarching event type.
|
||||
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
|
||||
/// Type previously used to derive the account ID for a pool. Indicates that the pool's
|
||||
/// liquidity assets are located at this account before the migration.
|
||||
type PriorAccountIdConverter: for<'a> TryConvert<
|
||||
&'a (Self::AssetKind, Self::AssetKind),
|
||||
Self::AccountId,
|
||||
>;
|
||||
|
||||
/// Retrieves information about an existing deposit for a given account ID and asset from
|
||||
/// the [`pallet_asset_conversion::Config::Assets`] registry and can initiate the refund.
|
||||
type AssetsRefund: Refund<
|
||||
Self::AccountId,
|
||||
AssetId = Self::AssetKind,
|
||||
Balance = <Self::DepositAsset as FungibleInspect<Self::AccountId>>::Balance,
|
||||
>;
|
||||
|
||||
/// Retrieves information about an existing deposit for a given account ID and asset from
|
||||
/// the [`pallet_asset_conversion::Config::PoolAssets`] registry and can initiate the
|
||||
/// refund.
|
||||
type PoolAssetsRefund: Refund<
|
||||
Self::AccountId,
|
||||
AssetId = Self::PoolAssetId,
|
||||
Balance = <Self::DepositAsset as FungibleInspect<Self::AccountId>>::Balance,
|
||||
>;
|
||||
|
||||
/// Means to reset the team for assets from the
|
||||
/// [`pallet_asset_conversion::Config::PoolAssets`] registry.
|
||||
type PoolAssetsTeam: ResetTeam<Self::AccountId, AssetId = Self::PoolAssetId>;
|
||||
|
||||
/// Registry of an asset used as an account deposit for the
|
||||
/// [`pallet_asset_conversion::Config::Assets`] and
|
||||
/// [`pallet_asset_conversion::Config::PoolAssets`] registries.
|
||||
type DepositAsset: FungibleMutate<Self::AccountId>;
|
||||
|
||||
/// Weight information for extrinsics in this pallet.
|
||||
type WeightInfo: WeightInfo;
|
||||
}
|
||||
|
||||
// Pallet's events.
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||
pub enum Event<T: Config> {
|
||||
/// Indicates that a pool has been migrated to the new account ID.
|
||||
MigratedToNewAccount {
|
||||
/// Pool's ID.
|
||||
pool_id: T::PoolId,
|
||||
/// Pool's prior account ID.
|
||||
prior_account: T::AccountId,
|
||||
/// Pool's new account ID.
|
||||
new_account: T::AccountId,
|
||||
},
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
/// Provided asset pair is not supported for pool.
|
||||
InvalidAssetPair,
|
||||
/// The pool doesn't exist.
|
||||
PoolNotFound,
|
||||
/// Pool's balance cannot be zero.
|
||||
ZeroBalance,
|
||||
/// Indicates a partial transfer of balance to the new account during a migration.
|
||||
PartialTransfer,
|
||||
}
|
||||
|
||||
/// Pallet's callable functions.
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
/// Migrates an existing pool to a new account ID derivation method for a given asset pair.
|
||||
/// If the migration is successful, transaction fees are refunded to the caller.
|
||||
///
|
||||
/// Must be signed.
|
||||
#[pallet::call_index(0)]
|
||||
#[pallet::weight(<T as Config>::WeightInfo::migrate_to_new_account())]
|
||||
pub fn migrate_to_new_account(
|
||||
origin: OriginFor<T>,
|
||||
asset1: Box<T::AssetKind>,
|
||||
asset2: Box<T::AssetKind>,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
let _ = ensure_signed(origin)?;
|
||||
|
||||
let pool_id = T::PoolLocator::pool_id(&asset1, &asset2)
|
||||
.map_err(|_| Error::<T>::InvalidAssetPair)?;
|
||||
let info = Pools::<T>::get(&pool_id).ok_or(Error::<T>::PoolNotFound)?;
|
||||
|
||||
let (prior_account, new_account) =
|
||||
Self::addresses(&pool_id).ok_or(Error::<T>::InvalidAssetPair)?;
|
||||
|
||||
let (asset1, asset2) = pool_id.clone();
|
||||
|
||||
// Assets that must be transferred to the new account id.
|
||||
let balance1 = T::Assets::total_balance(asset1.clone(), &prior_account);
|
||||
let balance2 = T::Assets::total_balance(asset2.clone(), &prior_account);
|
||||
let lp_balance = T::PoolAssets::total_balance(info.lp_token.clone(), &prior_account);
|
||||
|
||||
ensure!(!balance1.is_zero(), Error::<T>::ZeroBalance);
|
||||
ensure!(!balance2.is_zero(), Error::<T>::ZeroBalance);
|
||||
ensure!(!lp_balance.is_zero(), Error::<T>::ZeroBalance);
|
||||
|
||||
// Check if a deposit needs to be placed for the new account. If so, mint the
|
||||
// required deposit amount to the depositor's account to ensure the deposit can be
|
||||
// provided. Once the deposit from the prior account is returned, the minted assets will
|
||||
// be burned. Touching the new account is necessary because it's not possible to
|
||||
// transfer assets to the new account if it's required. Additionally, the deposit cannot
|
||||
// be refunded from the prior account until its balance is zero.
|
||||
|
||||
let deposit_asset_ed = T::DepositAsset::minimum_balance();
|
||||
|
||||
if let Some((depositor, deposit)) =
|
||||
T::AssetsRefund::deposit_held(asset1.clone(), prior_account.clone())
|
||||
{
|
||||
T::DepositAsset::mint_into(&depositor, deposit + deposit_asset_ed)?;
|
||||
T::Assets::touch(asset1.clone(), &new_account, &depositor)?;
|
||||
}
|
||||
|
||||
if let Some((depositor, deposit)) =
|
||||
T::AssetsRefund::deposit_held(asset2.clone(), prior_account.clone())
|
||||
{
|
||||
T::DepositAsset::mint_into(&depositor, deposit + deposit_asset_ed)?;
|
||||
T::Assets::touch(asset2.clone(), &new_account, &depositor)?;
|
||||
}
|
||||
|
||||
if let Some((depositor, deposit)) =
|
||||
T::PoolAssetsRefund::deposit_held(info.lp_token.clone(), prior_account.clone())
|
||||
{
|
||||
T::DepositAsset::mint_into(&depositor, deposit + deposit_asset_ed)?;
|
||||
T::PoolAssets::touch(info.lp_token.clone(), &new_account, &depositor)?;
|
||||
}
|
||||
|
||||
// Transfer all pool related assets to the new account.
|
||||
|
||||
ensure!(
|
||||
balance1 ==
|
||||
T::Assets::transfer(
|
||||
asset1.clone(),
|
||||
&prior_account,
|
||||
&new_account,
|
||||
balance1,
|
||||
Preservation::Expendable,
|
||||
)?,
|
||||
Error::<T>::PartialTransfer
|
||||
);
|
||||
|
||||
ensure!(
|
||||
balance2 ==
|
||||
T::Assets::transfer(
|
||||
asset2.clone(),
|
||||
&prior_account,
|
||||
&new_account,
|
||||
balance2,
|
||||
Preservation::Expendable,
|
||||
)?,
|
||||
Error::<T>::PartialTransfer
|
||||
);
|
||||
|
||||
ensure!(
|
||||
lp_balance ==
|
||||
T::PoolAssets::transfer(
|
||||
info.lp_token.clone(),
|
||||
&prior_account,
|
||||
&new_account,
|
||||
lp_balance,
|
||||
Preservation::Expendable,
|
||||
)?,
|
||||
Error::<T>::PartialTransfer
|
||||
);
|
||||
|
||||
// Refund deposits from prior accounts and burn previously minted assets.
|
||||
|
||||
if let Some((depositor, deposit)) =
|
||||
T::AssetsRefund::deposit_held(asset1.clone(), prior_account.clone())
|
||||
{
|
||||
T::AssetsRefund::refund(asset1.clone(), prior_account.clone())?;
|
||||
T::DepositAsset::burn_from(
|
||||
&depositor,
|
||||
deposit + deposit_asset_ed,
|
||||
Precision::Exact,
|
||||
Fortitude::Force,
|
||||
)?;
|
||||
}
|
||||
|
||||
if let Some((depositor, deposit)) =
|
||||
T::AssetsRefund::deposit_held(asset2.clone(), prior_account.clone())
|
||||
{
|
||||
T::AssetsRefund::refund(asset2.clone(), prior_account.clone())?;
|
||||
T::DepositAsset::burn_from(
|
||||
&depositor,
|
||||
deposit + deposit_asset_ed,
|
||||
Precision::Exact,
|
||||
Fortitude::Force,
|
||||
)?;
|
||||
}
|
||||
|
||||
if let Some((depositor, deposit)) =
|
||||
T::PoolAssetsRefund::deposit_held(info.lp_token.clone(), prior_account.clone())
|
||||
{
|
||||
T::PoolAssetsRefund::refund(info.lp_token.clone(), prior_account.clone())?;
|
||||
T::DepositAsset::burn_from(
|
||||
&depositor,
|
||||
deposit + deposit_asset_ed,
|
||||
Precision::Exact,
|
||||
Fortitude::Force,
|
||||
)?;
|
||||
}
|
||||
|
||||
T::PoolAssetsTeam::reset_team(
|
||||
info.lp_token,
|
||||
new_account.clone(),
|
||||
new_account.clone(),
|
||||
new_account.clone(),
|
||||
new_account.clone(),
|
||||
)?;
|
||||
|
||||
Self::deposit_event(Event::MigratedToNewAccount {
|
||||
pool_id,
|
||||
prior_account,
|
||||
new_account,
|
||||
});
|
||||
|
||||
Ok(Pays::No.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> Pallet<T> {
|
||||
/// Returns the prior and new account IDs for a given pool ID. The prior account ID comes
|
||||
/// first in the tuple.
|
||||
#[cfg(not(any(test, feature = "runtime-benchmarks")))]
|
||||
fn addresses(pool_id: &T::PoolId) -> Option<(T::AccountId, T::AccountId)> {
|
||||
match (
|
||||
T::PriorAccountIdConverter::try_convert(pool_id),
|
||||
T::PoolLocator::address(pool_id),
|
||||
) {
|
||||
(Ok(a), Ok(b)) if a != b => Some((a, b)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the prior and new account IDs for a given pool ID. The prior account ID comes
|
||||
/// first in the tuple.
|
||||
///
|
||||
/// This function is intended for use only in test and benchmark environments. The prior
|
||||
/// account ID represents the new account ID from [`Config::PoolLocator`], allowing the use
|
||||
/// of the main pallet's calls to set up a pool with liquidity placed in that account and
|
||||
/// migrate it to another account, which in this case is the result of
|
||||
/// [`Config::PriorAccountIdConverter`].
|
||||
#[cfg(any(test, feature = "runtime-benchmarks"))]
|
||||
pub(crate) fn addresses(pool_id: &T::PoolId) -> Option<(T::AccountId, T::AccountId)> {
|
||||
match (
|
||||
T::PoolLocator::address(pool_id),
|
||||
T::PriorAccountIdConverter::try_convert(pool_id),
|
||||
) {
|
||||
(Ok(a), Ok(b)) if a != b => Some((a, b)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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.
|
||||
|
||||
//! Test environment for Asset Conversion Ops pallet.
|
||||
|
||||
use super::*;
|
||||
use crate as pallet_asset_conversion_ops;
|
||||
use core::default::Default;
|
||||
use frame_support::{
|
||||
construct_runtime, derive_impl,
|
||||
instances::{Instance1, Instance2},
|
||||
ord_parameter_types, parameter_types,
|
||||
traits::{
|
||||
tokens::{
|
||||
fungible::{NativeFromLeft, NativeOrWithId, UnionOf},
|
||||
imbalance::ResolveAssetTo,
|
||||
},
|
||||
AsEnsureOriginWithArg, ConstU32, ConstU64,
|
||||
},
|
||||
PalletId,
|
||||
};
|
||||
use frame_system::{EnsureSigned, EnsureSignedBy};
|
||||
use pallet_asset_conversion::{self, AccountIdConverter, AccountIdConverterNoSeed, Ascending};
|
||||
use sp_arithmetic::Permill;
|
||||
use sp_runtime::{traits::AccountIdConversion, BuildStorage};
|
||||
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
|
||||
construct_runtime!(
|
||||
pub enum Test
|
||||
{
|
||||
System: frame_system,
|
||||
Balances: pallet_balances,
|
||||
Assets: pallet_assets::<Instance1>,
|
||||
PoolAssets: pallet_assets::<Instance2>,
|
||||
AssetConversion: pallet_asset_conversion,
|
||||
AssetConversionOps: pallet_asset_conversion_ops,
|
||||
}
|
||||
);
|
||||
|
||||
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
|
||||
impl frame_system::Config for Test {
|
||||
type Block = Block;
|
||||
type AccountData = pallet_balances::AccountData<u64>;
|
||||
}
|
||||
|
||||
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
|
||||
impl pallet_balances::Config for Test {
|
||||
type ReserveIdentifier = [u8; 8];
|
||||
type AccountStore = System;
|
||||
}
|
||||
|
||||
#[derive_impl(pallet_assets::config_preludes::TestDefaultConfig)]
|
||||
impl pallet_assets::Config<Instance1> for Test {
|
||||
type Currency = Balances;
|
||||
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<Self::AccountId>>;
|
||||
type ForceOrigin = frame_system::EnsureRoot<Self::AccountId>;
|
||||
type Freezer = ();
|
||||
}
|
||||
|
||||
#[derive_impl(pallet_assets::config_preludes::TestDefaultConfig)]
|
||||
impl pallet_assets::Config<Instance2> for Test {
|
||||
type Currency = Balances;
|
||||
type CreateOrigin =
|
||||
AsEnsureOriginWithArg<EnsureSignedBy<AssetConversionOrigin, Self::AccountId>>;
|
||||
type ForceOrigin = frame_system::EnsureRoot<Self::AccountId>;
|
||||
type Freezer = ();
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
|
||||
pub const Native: NativeOrWithId<u32> = NativeOrWithId::Native;
|
||||
pub storage LiquidityWithdrawalFee: Permill = Permill::from_percent(0);
|
||||
}
|
||||
|
||||
ord_parameter_types! {
|
||||
pub const AssetConversionOrigin: u64 = AccountIdConversion::<u64>::into_account_truncating(&AssetConversionPalletId::get());
|
||||
}
|
||||
|
||||
pub type NativeAndAssets = UnionOf<Balances, Assets, NativeFromLeft, NativeOrWithId<u32>, u64>;
|
||||
pub type PoolIdToAccountId =
|
||||
AccountIdConverter<AssetConversionPalletId, (NativeOrWithId<u32>, NativeOrWithId<u32>)>;
|
||||
pub type AscendingLocator = Ascending<u64, NativeOrWithId<u32>, PoolIdToAccountId>;
|
||||
|
||||
impl pallet_asset_conversion::Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
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 = AscendingLocator;
|
||||
type PoolAssetId = u32;
|
||||
type PoolAssets = PoolAssets;
|
||||
type PoolSetupFee = ConstU64<100>;
|
||||
type PoolSetupFeeAsset = Native;
|
||||
type PoolSetupFeeTarget = ResolveAssetTo<AssetConversionOrigin, Self::Assets>;
|
||||
type PalletId = AssetConversionPalletId;
|
||||
type WeightInfo = ();
|
||||
type LPFee = ConstU32<3>;
|
||||
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
|
||||
type MaxSwapPathLength = ConstU32<4>;
|
||||
type MintMinLiquidity = ConstU64<100>;
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
type BenchmarkHelper = ();
|
||||
}
|
||||
|
||||
pub type OldPoolIdToAccountId =
|
||||
AccountIdConverterNoSeed<(NativeOrWithId<u32>, NativeOrWithId<u32>)>;
|
||||
|
||||
impl pallet_asset_conversion_ops::Config for Test {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type PriorAccountIdConverter = OldPoolIdToAccountId;
|
||||
type AssetsRefund = NativeAndAssets;
|
||||
type PoolAssetsRefund = PoolAssets;
|
||||
type PoolAssetsTeam = PoolAssets;
|
||||
type DepositAsset = Balances;
|
||||
type WeightInfo = ();
|
||||
}
|
||||
|
||||
pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
|
||||
|
||||
pallet_balances::GenesisConfig::<Test> {
|
||||
balances: vec![(1, 10000), (2, 20000), (3, 30000), (4, 40000)],
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
let mut ext = sp_io::TestExternalities::new(t);
|
||||
ext.execute_with(|| System::set_block_number(1));
|
||||
ext
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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.
|
||||
|
||||
//! Asset Conversion Ops pallet tests.
|
||||
|
||||
use crate::{mock::*, *};
|
||||
use frame_support::{
|
||||
assert_noop, assert_ok,
|
||||
traits::{
|
||||
fungible::{Inspect as FungibleInspect, NativeOrWithId},
|
||||
fungibles::{Create, Inspect},
|
||||
Incrementable,
|
||||
},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn migrate_pool_account_id_with_native() {
|
||||
new_test_ext().execute_with(|| {
|
||||
type PoolLocator = <Test as pallet_asset_conversion::Config>::PoolLocator;
|
||||
let user = 1;
|
||||
let token_1 = NativeOrWithId::Native;
|
||||
let token_2 = NativeOrWithId::WithId(2);
|
||||
let pool_id = PoolLocator::pool_id(&token_1, &token_2).unwrap();
|
||||
let lp_token =
|
||||
<Test as pallet_asset_conversion::Config>::PoolAssetId::initial_value().unwrap();
|
||||
|
||||
// setup pool and provide some liquidity.
|
||||
assert_ok!(NativeAndAssets::create(token_2.clone(), user, false, 1));
|
||||
|
||||
assert_ok!(AssetConversion::create_pool(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone())
|
||||
));
|
||||
|
||||
let ed = Balances::minimum_balance();
|
||||
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), user, 10000 * 2 + ed));
|
||||
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 2, user, 1000));
|
||||
|
||||
assert_ok!(AssetConversion::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone()),
|
||||
10000,
|
||||
10,
|
||||
10000,
|
||||
10,
|
||||
user,
|
||||
));
|
||||
|
||||
// assert user's balance.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &user), 10000 + ed);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &user), 1000 - 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &user), 216);
|
||||
|
||||
// record total issuances before migration.
|
||||
let total_issuance_token1 = NativeAndAssets::total_issuance(token_1.clone());
|
||||
let total_issuance_token2 = NativeAndAssets::total_issuance(token_2.clone());
|
||||
let total_issuance_lp_token = PoolAssets::total_issuance(lp_token);
|
||||
|
||||
let pool_account = PoolLocator::address(&pool_id).unwrap();
|
||||
let (prior_pool_account, new_pool_account) =
|
||||
AssetConversionOps::addresses(&pool_id).unwrap();
|
||||
assert_eq!(pool_account, prior_pool_account);
|
||||
|
||||
// assert pool's balances before migration.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &prior_pool_account), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &prior_pool_account), 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &prior_pool_account), 100);
|
||||
|
||||
// migrate.
|
||||
assert_ok!(AssetConversionOps::migrate_to_new_account(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone()),
|
||||
));
|
||||
|
||||
// assert user's balance has not changed.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &user), 10000 + ed);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &user), 1000 - 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &user), 216);
|
||||
|
||||
// assert pool's balance on new account id is same as on prior account id.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &new_pool_account), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &new_pool_account), 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &new_pool_account), 100);
|
||||
|
||||
// assert pool's balance on prior account id is zero.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &prior_pool_account), 0);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &prior_pool_account), 0);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &prior_pool_account), 0);
|
||||
|
||||
// assert total issuance has not changed.
|
||||
assert_eq!(total_issuance_token1, NativeAndAssets::total_issuance(token_1));
|
||||
assert_eq!(total_issuance_token2, NativeAndAssets::total_issuance(token_2));
|
||||
assert_eq!(total_issuance_lp_token, PoolAssets::total_issuance(lp_token));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn migrate_pool_account_id_with_insufficient_assets() {
|
||||
new_test_ext().execute_with(|| {
|
||||
type PoolLocator = <Test as pallet_asset_conversion::Config>::PoolLocator;
|
||||
let user = 1;
|
||||
let token_1 = NativeOrWithId::WithId(1);
|
||||
let token_2 = NativeOrWithId::WithId(2);
|
||||
let pool_id = PoolLocator::pool_id(&token_1, &token_2).unwrap();
|
||||
let lp_token =
|
||||
<Test as pallet_asset_conversion::Config>::PoolAssetId::initial_value().unwrap();
|
||||
|
||||
// setup pool and provide some liquidity.
|
||||
assert_ok!(NativeAndAssets::create(token_1.clone(), user, false, 1));
|
||||
assert_ok!(NativeAndAssets::create(token_2.clone(), user, false, 1));
|
||||
|
||||
assert_ok!(AssetConversion::create_pool(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone())
|
||||
));
|
||||
|
||||
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 1, user, 20000));
|
||||
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 2, user, 1000));
|
||||
|
||||
assert_ok!(AssetConversion::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone()),
|
||||
10000,
|
||||
10,
|
||||
10000,
|
||||
10,
|
||||
user,
|
||||
));
|
||||
|
||||
// assert user's balance.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &user), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &user), 1000 - 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &user), 216);
|
||||
|
||||
// record total issuances before migration.
|
||||
let total_issuance_token1 = NativeAndAssets::total_issuance(token_1.clone());
|
||||
let total_issuance_token2 = NativeAndAssets::total_issuance(token_2.clone());
|
||||
let total_issuance_lp_token = PoolAssets::total_issuance(lp_token);
|
||||
|
||||
let pool_account = PoolLocator::address(&pool_id).unwrap();
|
||||
let (prior_pool_account, new_pool_account) =
|
||||
AssetConversionOps::addresses(&pool_id).unwrap();
|
||||
assert_eq!(pool_account, prior_pool_account);
|
||||
|
||||
// assert pool's balances before migration.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &prior_pool_account), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &prior_pool_account), 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &prior_pool_account), 100);
|
||||
|
||||
// migrate.
|
||||
assert_ok!(AssetConversionOps::migrate_to_new_account(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone()),
|
||||
));
|
||||
|
||||
// assert user's balance has not changed.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &user), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &user), 1000 - 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &user), 216);
|
||||
|
||||
// assert pool's balance on new account id is same as on prior account id.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &new_pool_account), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &new_pool_account), 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &new_pool_account), 100);
|
||||
|
||||
// assert pool's balance on prior account id is zero.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &prior_pool_account), 0);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &prior_pool_account), 0);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &prior_pool_account), 0);
|
||||
|
||||
// assert total issuance has not changed.
|
||||
assert_eq!(total_issuance_token1, NativeAndAssets::total_issuance(token_1));
|
||||
assert_eq!(total_issuance_token2, NativeAndAssets::total_issuance(token_2));
|
||||
assert_eq!(total_issuance_lp_token, PoolAssets::total_issuance(lp_token));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn migrate_pool_account_id_with_sufficient_assets() {
|
||||
new_test_ext().execute_with(|| {
|
||||
type PoolLocator = <Test as pallet_asset_conversion::Config>::PoolLocator;
|
||||
let user = 1;
|
||||
let token_1 = NativeOrWithId::WithId(1);
|
||||
let token_2 = NativeOrWithId::WithId(2);
|
||||
let pool_id = PoolLocator::pool_id(&token_1, &token_2).unwrap();
|
||||
let lp_token =
|
||||
<Test as pallet_asset_conversion::Config>::PoolAssetId::initial_value().unwrap();
|
||||
|
||||
// setup pool and provide some liquidity.
|
||||
assert_ok!(NativeAndAssets::create(token_1.clone(), user, true, 1));
|
||||
assert_ok!(NativeAndAssets::create(token_2.clone(), user, true, 1));
|
||||
|
||||
assert_ok!(AssetConversion::create_pool(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone())
|
||||
));
|
||||
|
||||
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 1, user, 20000));
|
||||
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), 2, user, 1000));
|
||||
|
||||
assert_ok!(AssetConversion::add_liquidity(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone()),
|
||||
10000,
|
||||
10,
|
||||
10000,
|
||||
10,
|
||||
user,
|
||||
));
|
||||
|
||||
// assert user's balance.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &user), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &user), 1000 - 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &user), 216);
|
||||
|
||||
// record total issuances before migration.
|
||||
let total_issuance_token1 = NativeAndAssets::total_issuance(token_1.clone());
|
||||
let total_issuance_token2 = NativeAndAssets::total_issuance(token_2.clone());
|
||||
let total_issuance_lp_token = PoolAssets::total_issuance(lp_token);
|
||||
|
||||
let pool_account = PoolLocator::address(&pool_id).unwrap();
|
||||
let (prior_pool_account, new_pool_account) =
|
||||
AssetConversionOps::addresses(&pool_id).unwrap();
|
||||
assert_eq!(pool_account, prior_pool_account);
|
||||
|
||||
// assert pool's balances before migration.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &prior_pool_account), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &prior_pool_account), 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &prior_pool_account), 100);
|
||||
|
||||
// migrate.
|
||||
assert_ok!(AssetConversionOps::migrate_to_new_account(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone()),
|
||||
));
|
||||
|
||||
// assert user's balance has not changed.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &user), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &user), 1000 - 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &user), 216);
|
||||
|
||||
// assert pool's balance on new account id is same as on prior account id.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &new_pool_account), 10000);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &new_pool_account), 10);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &new_pool_account), 100);
|
||||
|
||||
// assert pool's balance on prior account id is zero.
|
||||
assert_eq!(NativeAndAssets::balance(token_1.clone(), &prior_pool_account), 0);
|
||||
assert_eq!(NativeAndAssets::balance(token_2.clone(), &prior_pool_account), 0);
|
||||
assert_eq!(PoolAssets::balance(lp_token, &prior_pool_account), 0);
|
||||
|
||||
// assert total issuance has not changed.
|
||||
assert_eq!(total_issuance_token1, NativeAndAssets::total_issuance(token_1));
|
||||
assert_eq!(total_issuance_token2, NativeAndAssets::total_issuance(token_2));
|
||||
assert_eq!(total_issuance_lp_token, PoolAssets::total_issuance(lp_token));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn migrate_empty_pool_account_id() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let user = 1;
|
||||
let token_1 = NativeOrWithId::Native;
|
||||
let token_2 = NativeOrWithId::WithId(2);
|
||||
|
||||
// setup pool and provide some liquidity.
|
||||
assert_ok!(NativeAndAssets::create(token_2.clone(), user, false, 1));
|
||||
|
||||
assert_ok!(AssetConversion::create_pool(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone())
|
||||
));
|
||||
|
||||
// migrate.
|
||||
assert_noop!(
|
||||
AssetConversionOps::migrate_to_new_account(
|
||||
RuntimeOrigin::signed(user),
|
||||
Box::new(token_1.clone()),
|
||||
Box::new(token_2.clone()),
|
||||
),
|
||||
Error::<Test>::ZeroBalance
|
||||
);
|
||||
});
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// 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.
|
||||
|
||||
//! Autogenerated weights for `pallet_asset_conversion_ops`
|
||||
//!
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0
|
||||
//! DATE: 2024-04-13, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
|
||||
//! WORST CASE MAP SIZE: `1000000`
|
||||
//! HOSTNAME: `runner-anb7yjbi-project-674-concurrent-0`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
|
||||
//! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024`
|
||||
|
||||
// Executed Command:
|
||||
// target/production/substrate-node
|
||||
// benchmark
|
||||
// pallet
|
||||
// --steps=50
|
||||
// --repeat=20
|
||||
// --extrinsic=*
|
||||
// --wasm-execution=compiled
|
||||
// --heap-pages=4096
|
||||
// --json-file=/builds/parity/mirrors/polkadot-sdk/.git/.artifacts/bench.json
|
||||
// --pallet=pallet_asset_conversion_ops
|
||||
// --chain=dev
|
||||
// --header=./substrate/HEADER-APACHE2
|
||||
// --output=./substrate/frame/asset-conversion-ops/src/weights.rs
|
||||
// --template=./substrate/.maintain/frame-weight-template.hbs
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
|
||||
use core::marker::PhantomData;
|
||||
|
||||
/// Weight functions needed for `pallet_asset_conversion_ops`.
|
||||
pub trait WeightInfo {
|
||||
fn migrate_to_new_account() -> Weight;
|
||||
}
|
||||
|
||||
/// Weights for `pallet_asset_conversion_ops` 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:0)
|
||||
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(30), added: 2505, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:4 w:4)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `PoolAssets::Account` (r:2 w:2)
|
||||
/// Proof: `PoolAssets::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`)
|
||||
/// Storage: `Assets::Asset` (r:2 w:2)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:2 w:2)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
fn migrate_to_new_account() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1762`
|
||||
// Estimated: `11426`
|
||||
// Minimum execution time: 223_850_000 picoseconds.
|
||||
Weight::from_parts(231_676_000, 11426)
|
||||
.saturating_add(T::DbWeight::get().reads(12_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(11_u64))
|
||||
}
|
||||
}
|
||||
|
||||
// For backwards compatibility and tests.
|
||||
impl WeightInfo for () {
|
||||
/// Storage: `AssetConversion::Pools` (r:1 w:0)
|
||||
/// Proof: `AssetConversion::Pools` (`max_values`: None, `max_size`: Some(30), added: 2505, mode: `MaxEncodedLen`)
|
||||
/// Storage: `Assets::Account` (r:4 w:4)
|
||||
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
|
||||
/// Storage: `PoolAssets::Account` (r:2 w:2)
|
||||
/// Proof: `PoolAssets::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`)
|
||||
/// Storage: `Assets::Asset` (r:2 w:2)
|
||||
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
|
||||
/// Storage: `System::Account` (r:2 w:2)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
fn migrate_to_new_account() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `1762`
|
||||
// Estimated: `11426`
|
||||
// Minimum execution time: 223_850_000 picoseconds.
|
||||
Weight::from_parts(231_676_000, 11426)
|
||||
.saturating_add(RocksDbWeight::get().reads(12_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(11_u64))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user