1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

// Shared test utilities and implementations for the XCM Builder.

use frame_support::{
	parameter_types,
	traits::{Contains, CrateVersion, PalletInfoData, PalletsInfoAccess},
};
use sp_std::vec::Vec;
pub use xcm::latest::{prelude::*, Weight};
use xcm_executor::traits::{ClaimAssets, DropAssets, VersionChangeNotifier};
pub use xcm_executor::{
	traits::{
		AssetExchange, AssetLock, ConvertOrigin, Enact, LockError, OnResponse, TransactAsset,
	},
	Assets, Config,
};

parameter_types! {
	pub static SubscriptionRequests: Vec<(MultiLocation, Option<(QueryId, Weight)>)> = vec![];
	pub static MaxAssetsIntoHolding: u32 = 4;
}

pub struct TestSubscriptionService;

impl VersionChangeNotifier for TestSubscriptionService {
	fn start(
		location: &MultiLocation,
		query_id: QueryId,
		max_weight: Weight,
		_context: &XcmContext,
	) -> XcmResult {
		let mut r = SubscriptionRequests::get();
		r.push((*location, Some((query_id, max_weight))));
		SubscriptionRequests::set(r);
		Ok(())
	}
	fn stop(location: &MultiLocation, _context: &XcmContext) -> XcmResult {
		let mut r = SubscriptionRequests::get();
		r.retain(|(l, _q)| l != location);
		r.push((*location, None));
		SubscriptionRequests::set(r);
		Ok(())
	}
	fn is_subscribed(location: &MultiLocation) -> bool {
		let r = SubscriptionRequests::get();
		r.iter().any(|(l, q)| l == location && q.is_some())
	}
}

parameter_types! {
	pub static TrappedAssets: Vec<(MultiLocation, MultiAssets)> = vec![];
}

pub struct TestAssetTrap;

impl DropAssets for TestAssetTrap {
	fn drop_assets(origin: &MultiLocation, assets: Assets, _context: &XcmContext) -> Weight {
		let mut t: Vec<(MultiLocation, MultiAssets)> = TrappedAssets::get();
		t.push((*origin, assets.into()));
		TrappedAssets::set(t);
		Weight::from_parts(5, 5)
	}
}

impl ClaimAssets for TestAssetTrap {
	fn claim_assets(
		origin: &MultiLocation,
		ticket: &MultiLocation,
		what: &MultiAssets,
		_context: &XcmContext,
	) -> bool {
		let mut t: Vec<(MultiLocation, MultiAssets)> = TrappedAssets::get();
		if let (0, X1(GeneralIndex(i))) = (ticket.parents, &ticket.interior) {
			if let Some((l, a)) = t.get(*i as usize) {
				if l == origin && a == what {
					t.swap_remove(*i as usize);
					TrappedAssets::set(t);
					return true
				}
			}
		}
		false
	}
}

pub struct TestAssetExchanger;

impl AssetExchange for TestAssetExchanger {
	fn exchange_asset(
		_origin: Option<&MultiLocation>,
		_give: Assets,
		want: &MultiAssets,
		_maximal: bool,
	) -> Result<Assets, Assets> {
		Ok(want.clone().into())
	}
}

pub struct TestPalletsInfo;
impl PalletsInfoAccess for TestPalletsInfo {
	fn count() -> usize {
		2
	}
	fn infos() -> Vec<PalletInfoData> {
		vec![
			PalletInfoData {
				index: 0,
				name: "System",
				module_name: "pallet_system",
				crate_version: CrateVersion { major: 1, minor: 10, patch: 1 },
			},
			PalletInfoData {
				index: 1,
				name: "Balances",
				module_name: "pallet_balances",
				crate_version: CrateVersion { major: 1, minor: 42, patch: 69 },
			},
		]
	}
}

pub struct TestUniversalAliases;
impl Contains<(MultiLocation, Junction)> for TestUniversalAliases {
	fn contains(aliases: &(MultiLocation, Junction)) -> bool {
		&aliases.0 == &Here.into_location() && &aliases.1 == &GlobalConsensus(ByGenesis([0; 32]))
	}
}

parameter_types! {
	pub static LockedAssets: Vec<(MultiLocation, MultiAsset)> = vec![];
}

pub struct TestLockTicket(MultiLocation, MultiAsset);
impl Enact for TestLockTicket {
	fn enact(self) -> Result<(), LockError> {
		let mut locked_assets = LockedAssets::get();
		locked_assets.push((self.0, self.1));
		LockedAssets::set(locked_assets);
		Ok(())
	}
}
pub struct TestUnlockTicket(MultiLocation, MultiAsset);
impl Enact for TestUnlockTicket {
	fn enact(self) -> Result<(), LockError> {
		let mut locked_assets = LockedAssets::get();
		if let Some((idx, _)) = locked_assets
			.iter()
			.enumerate()
			.find(|(_, (origin, asset))| origin == &self.0 && asset == &self.1)
		{
			locked_assets.remove(idx);
		}
		LockedAssets::set(locked_assets);
		Ok(())
	}
}
pub struct TestReduceTicket;
impl Enact for TestReduceTicket {
	fn enact(self) -> Result<(), LockError> {
		Ok(())
	}
}

pub struct TestAssetLocker;
impl AssetLock for TestAssetLocker {
	type LockTicket = TestLockTicket;
	type UnlockTicket = TestUnlockTicket;
	type ReduceTicket = TestReduceTicket;

	fn prepare_lock(
		unlocker: MultiLocation,
		asset: MultiAsset,
		_owner: MultiLocation,
	) -> Result<TestLockTicket, LockError> {
		Ok(TestLockTicket(unlocker, asset))
	}

	fn prepare_unlock(
		unlocker: MultiLocation,
		asset: MultiAsset,
		_owner: MultiLocation,
	) -> Result<TestUnlockTicket, LockError> {
		Ok(TestUnlockTicket(unlocker, asset))
	}

	fn note_unlockable(
		_locker: MultiLocation,
		_asset: MultiAsset,
		_owner: MultiLocation,
	) -> Result<(), LockError> {
		Ok(())
	}

	fn prepare_reduce_unlockable(
		_locker: MultiLocation,
		_asset: MultiAsset,
		_owner: MultiLocation,
	) -> Result<TestReduceTicket, LockError> {
		Ok(TestReduceTicket)
	}
}