referrerpolicy=no-referrer-when-downgrade

pallet_asset_conversion_ops/
benchmarking.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10//  http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Asset Conversion Ops pallet benchmarking.
19
20use super::*;
21use crate::Pallet as AssetConversionOps;
22use frame_benchmarking::{v2::*, whitelisted_caller};
23use frame_support::{
24	assert_ok,
25	traits::fungibles::{Create, Inspect, Mutate},
26};
27use frame_system::RawOrigin as SystemOrigin;
28use pallet_asset_conversion::{BenchmarkHelper, Pallet as AssetConversion};
29use sp_core::Get;
30use sp_runtime::traits::One;
31
32/// Provides a pair of amounts expected to serve as sufficient initial liquidity for a pool.
33fn valid_liquidity_amount<T: Config>(ed1: T::Balance, ed2: T::Balance) -> (T::Balance, T::Balance)
34where
35	T::Assets: Inspect<T::AccountId>,
36{
37	let l =
38		ed1.max(ed2) + T::MintMinLiquidity::get() + T::MintMinLiquidity::get() + T::Balance::one();
39	(l, l)
40}
41
42/// Create the `asset` and mint the `amount` for the `caller`.
43fn create_asset<T: Config>(caller: &T::AccountId, asset: &T::AssetKind, amount: T::Balance)
44where
45	T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
46{
47	if !T::Assets::asset_exists(asset.clone()) {
48		assert_ok!(T::Assets::create(asset.clone(), caller.clone(), true, T::Balance::one()));
49	}
50	assert_ok!(T::Assets::mint_into(
51		asset.clone(),
52		&caller,
53		amount + T::Assets::minimum_balance(asset.clone())
54	));
55}
56
57/// Create the designated fee asset for pool creation.
58fn create_fee_asset<T: Config>(caller: &T::AccountId)
59where
60	T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
61{
62	let fee_asset = T::PoolSetupFeeAsset::get();
63	if !T::Assets::asset_exists(fee_asset.clone()) {
64		assert_ok!(T::Assets::create(fee_asset.clone(), caller.clone(), true, T::Balance::one()));
65	}
66	assert_ok!(T::Assets::mint_into(
67		fee_asset.clone(),
68		&caller,
69		T::Assets::minimum_balance(fee_asset)
70	));
71}
72
73/// Mint the fee asset for the `caller` sufficient to cover the fee for creating a new pool.
74fn mint_setup_fee_asset<T: Config>(
75	caller: &T::AccountId,
76	asset1: &T::AssetKind,
77	asset2: &T::AssetKind,
78	lp_token: &T::PoolAssetId,
79) where
80	T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
81{
82	assert_ok!(T::Assets::mint_into(
83		T::PoolSetupFeeAsset::get(),
84		&caller,
85		T::PoolSetupFee::get() +
86			T::Assets::deposit_required(asset1.clone()) +
87			T::Assets::deposit_required(asset2.clone()) +
88			T::PoolAssets::deposit_required(lp_token.clone())
89	));
90}
91
92/// Creates a pool for a given asset pair.
93///
94/// This action mints the necessary amounts of the given assets for the `caller` to provide initial
95/// liquidity. It returns the LP token ID along with a pair of amounts sufficient for the pool's
96/// initial liquidity.
97fn create_asset_and_pool<T: Config>(
98	caller: &T::AccountId,
99	asset1: &T::AssetKind,
100	asset2: &T::AssetKind,
101) -> (T::PoolAssetId, T::Balance, T::Balance)
102where
103	T::Assets: Create<T::AccountId> + Mutate<T::AccountId>,
104{
105	let (liquidity1, liquidity2) = valid_liquidity_amount::<T>(
106		T::Assets::minimum_balance(asset1.clone()),
107		T::Assets::minimum_balance(asset2.clone()),
108	);
109	create_asset::<T>(caller, asset1, liquidity1);
110	create_asset::<T>(caller, asset2, liquidity2);
111	let lp_token = AssetConversion::<T>::get_next_pool_asset_id();
112
113	mint_setup_fee_asset::<T>(caller, asset1, asset2, &lp_token);
114
115	assert_ok!(AssetConversion::<T>::create_pool(
116		SystemOrigin::Signed(caller.clone()).into(),
117		Box::new(asset1.clone()),
118		Box::new(asset2.clone())
119	));
120
121	(lp_token, liquidity1, liquidity2)
122}
123
124fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
125	let events = frame_system::Pallet::<T>::events();
126	let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
127	// compare to the last event record
128	let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
129	assert_eq!(event, &system_event);
130}
131
132#[benchmarks(where T::Assets: Create<T::AccountId> + Mutate<T::AccountId>, T::PoolAssetId: Into<u32>,)]
133mod benchmarks {
134	use super::*;
135
136	#[benchmark]
137	fn migrate_to_new_account() {
138		let caller: T::AccountId = whitelisted_caller();
139		let (asset1, asset2) = T::BenchmarkHelper::create_pair(0, 1);
140
141		create_fee_asset::<T>(&caller);
142		let (_, liquidity1, liquidity2) = create_asset_and_pool::<T>(&caller, &asset1, &asset2);
143
144		assert_ok!(AssetConversion::<T>::add_liquidity(
145			SystemOrigin::Signed(caller.clone()).into(),
146			Box::new(asset1.clone()),
147			Box::new(asset2.clone()),
148			liquidity1,
149			liquidity2,
150			T::Balance::one(),
151			T::Balance::zero(),
152			caller.clone(),
153		));
154
155		#[extrinsic_call]
156		_(SystemOrigin::Signed(caller.clone()), Box::new(asset1.clone()), Box::new(asset2.clone()));
157
158		let pool_id = T::PoolLocator::pool_id(&asset1, &asset2).unwrap();
159		let (prior_account, new_account) = AssetConversionOps::<T>::addresses(&pool_id).unwrap();
160		assert_last_event::<T>(
161			Event::MigratedToNewAccount { pool_id, new_account, prior_account }.into(),
162		);
163	}
164
165	impl_benchmark_test_suite!(AssetConversionOps, crate::mock::new_test_ext(), crate::mock::Test);
166}