referrerpolicy=no-referrer-when-downgrade

emulated_integration_tests_common/
pools.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16pub use paste;
17
18pub use pallet_asset_conversion;
19
20/// This function assumes that the asset is prefunded.
21///
22/// Usually, this function is only called from the other macros in this module.
23#[macro_export]
24macro_rules! create_pool_with_native_location_on {
25	( $chain:ident, $native_location:expr, $asset_id:expr, $asset_owner:expr, $wnd_amount:expr, $asset_amount:expr ) => {
26		$crate::pools::paste::paste! {
27			<$chain>::execute_with(|| {
28				type RuntimeEvent = <$chain as Chain>::RuntimeEvent;
29				let owner = $asset_owner;
30				let signed_owner = <$chain as Chain>::RuntimeOrigin::signed(owner.clone());
31				let native_location: Location = $native_location;
32
33				assert_ok!(<$chain as [<$chain Pallet>]>::AssetConversion::create_pool(
34					signed_owner.clone(),
35					Box::new(native_location.clone()),
36					Box::new($asset_id.clone()),
37				));
38
39				assert_expected_events!(
40					$chain,
41					vec![
42						RuntimeEvent::AssetConversion($crate::pools::pallet_asset_conversion::Event::PoolCreated { .. }) => {},
43					]
44				);
45
46				assert_ok!(<$chain as [<$chain Pallet>]>::AssetConversion::add_liquidity(
47					signed_owner,
48					Box::new(native_location),
49					Box::new($asset_id),
50					$wnd_amount,
51					$asset_amount,
52					0,
53					0,
54					owner.into()
55				));
56
57				assert_expected_events!(
58					$chain,
59					vec![
60						RuntimeEvent::AssetConversion($crate::pools::pallet_asset_conversion::Event::LiquidityAdded { .. }) => {},
61					]
62				);
63			});
64		}
65	};
66}
67
68#[macro_export]
69macro_rules! create_pool_with_relay_native_on {
70	// default amounts
71	( $chain:ident, $asset_id:expr, $asset_owner:expr ) => {
72		$crate::create_pool_with_relay_native_on!(
73			$chain,
74			$asset_id,
75			$asset_owner,
76			1_000_000_000_000,
77			2_000_000_000_000
78		);
79	};
80
81	// custom amounts
82	( $chain:ident, $asset_id:expr, $asset_owner:expr, $wnd_amount:expr, $asset_amount:expr ) => {
83		$crate::pools::paste::paste! {
84			<$chain>::execute_with(|| {
85				let owner = $asset_owner;
86				let signed_owner = <$chain as Chain>::RuntimeOrigin::signed(owner.clone());
87
88				let asset_id = match $asset_id.interior.last() {
89					Some(GeneralIndex(id)) => *id as u32,
90					_ => unreachable!(),
91				};
92				assert_ok!(<$chain as [<$chain Pallet>]>::Assets::mint(
93					signed_owner.clone(),
94					asset_id.into(),
95					owner.clone().into(),
96					10_000_000_000_000, // For it to have more than enough.
97				));
98			});
99
100			let parent_location: Location = Parent.into();
101			$crate::create_pool_with_native_location_on!($chain, parent_location, $asset_id, $asset_owner, $wnd_amount, $asset_amount);
102		}
103	};
104}
105
106#[macro_export]
107macro_rules! create_foreign_pool_with_parent_native_on {
108	// default amounts and pallet name
109	( $chain:ident, $asset_id:expr, $asset_owner:expr ) => {
110		$crate::create_foreign_pool_with_parent_native_on!(
111			$chain,
112			ForeignAssets,
113			$asset_id,
114			$asset_owner,
115			1_000_000_000_000,
116			2_000_000_000_000
117		);
118	};
119
120	// default amounts, custom pallet name
121	( $chain:ident, $foreign_pallet_assets:ident, $asset_id:expr, $asset_owner:expr ) => {
122		$crate::create_foreign_pool_with_parent_native_on!(
123			$chain,
124			$foreign_pallet_assets,
125			$asset_id,
126			$asset_owner,
127			1_000_000_000_000,
128			2_000_000_000_000
129		);
130	};
131
132	// custom amounts, default pallet name
133	( $chain:ident, $asset_id:expr, $asset_owner:expr, $wnd_amount:expr, $asset_amount:expr ) => {
134		$crate::create_foreign_pool_with_parent_native_on!(
135			$chain,
136			ForeignAssets,
137			$asset_id,
138			$asset_owner,
139			$wnd_amount,
140			$asset_amount
141		);
142	};
143
144	// custom amounts, custom pallet name
145	( $chain:ident, $foreign_pallet_assets:ident, $asset_id:expr, $asset_owner:expr, $wnd_amount:expr, $asset_amount:expr ) => {
146		$crate::pools::paste::paste! {
147			<$chain>::execute_with(|| {
148				let owner = $asset_owner;
149				let signed_owner = <$chain as Chain>::RuntimeOrigin::signed(owner.clone());
150
151				assert_ok!(<$chain as [<$chain Pallet>]>::$foreign_pallet_assets::mint(
152						signed_owner.clone(),
153						$asset_id.clone().into(),
154						owner.clone().into(),
155						10_000_000_000_000, // For it to have more than enough.
156				));
157			});
158
159			let wnd_location: Location = Parent.into();
160			$crate::create_pool_with_native_location_on!($chain, wnd_location, $asset_id, $asset_owner, $wnd_amount, $asset_amount);
161		}
162	};
163}
164
165#[macro_export]
166macro_rules! create_foreign_pool_with_native_on {
167	// default amounts and pallet name
168	( $chain:ident, $asset_id:expr, $asset_owner:expr ) => {
169		$crate::create_foreign_pool_with_native_on!(
170			$chain,
171			Assets,
172			$asset_id,
173			$asset_owner,
174			1_000_000_000_000,
175			2_000_000_000_000
176		);
177	};
178
179	// default amounts, custom pallet name
180	( $chain:ident, $foreign_pallet_asset:ident, $asset_id:expr, $asset_owner:expr ) => {
181		$crate::create_foreign_pool_with_native_on!(
182			$chain,
183			$foreign_pallet_asset,
184			$asset_id,
185			$asset_owner,
186			1_000_000_000_000,
187			2_000_000_000_000
188		);
189	};
190
191	// custom amounts, default pallet name
192	( $chain:ident, $asset_id:expr, $asset_owner:expr, $wnd_amount:expr, $asset_amount:expr ) => {
193		$crate::create_foreign_pool_with_native_on!(
194			$chain,
195			Assets,
196			$asset_id,
197			$asset_owner,
198			$wnd_amount,
199			$asset_amount
200		);
201	};
202
203	// custom amounts, custom pallet name
204	( $chain:ident, $foreign_asset_pallet:ident, $asset_id:expr, $asset_owner:expr, $wnd_amount:expr, $asset_amount:expr ) => {
205		$crate::pools::paste::paste! {
206			<$chain>::execute_with(|| {
207				let owner = $asset_owner;
208				let signed_owner = <$chain as Chain>::RuntimeOrigin::signed(owner.clone());
209
210				assert_ok!(<$chain as [<$chain Pallet>]>::$foreign_asset_pallet::mint(
211						signed_owner.clone(),
212						$asset_id.clone().into(),
213						owner.clone().into(),
214						10_000_000_000_000, // For it to have more than enough.
215				));
216			});
217
218			let native_location: Location = Here.into();
219			$crate::create_pool_with_native_location_on!($chain, native_location, $asset_id, $asset_owner, $wnd_amount, $asset_amount);
220		}
221	};
222}