referrerpolicy=no-referrer-when-downgrade
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
// 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/>.

//! Various implementations of `ContainsPair<Asset, Location>` or
//! `Contains<(Location, Vec<Asset>)>`.

use alloc::vec::Vec;
use core::marker::PhantomData;
use frame_support::traits::{Contains, ContainsPair, Get};
use xcm::latest::{Asset, AssetFilter, AssetId, Location, WildAsset};

/// Accepts an asset iff it is a native asset.
pub struct NativeAsset;
impl ContainsPair<Asset, Location> for NativeAsset {
	fn contains(asset: &Asset, origin: &Location) -> bool {
		log::trace!(target: "xcm::contains", "NativeAsset asset: {:?}, origin: {:?}", asset, origin);
		matches!(asset.id, AssetId(ref id) if id == origin)
	}
}

/// Accepts an asset if it is contained in the given `T`'s `Get` implementation.
pub struct Case<T>(PhantomData<T>);
impl<T: Get<(AssetFilter, Location)>> ContainsPair<Asset, Location> for Case<T> {
	fn contains(asset: &Asset, origin: &Location) -> bool {
		log::trace!(target: "xcm::contains", "Case asset: {:?}, origin: {:?}", asset, origin);
		let (a, o) = T::get();
		a.matches(asset) && &o == origin
	}
}

/// Accepts a tuple `(location, assets)` if the `location` is contained in the `Contains`
/// implementation of the given `Location` and if every asset from `assets` matches at least one of
/// the `AssetFilter` instances provided by the `Get` implementation of `AssetFilters`.
pub struct LocationWithAssetFilters<LocationFilter, AssetFilters>(
	core::marker::PhantomData<(LocationFilter, AssetFilters)>,
);
impl<LocationFilter: Contains<Location>, AssetFilters: Get<Vec<AssetFilter>>>
	Contains<(Location, Vec<Asset>)> for LocationWithAssetFilters<LocationFilter, AssetFilters>
{
	fn contains((location, assets): &(Location, Vec<Asset>)) -> bool {
		log::trace!(target: "xcm::contains", "LocationWithAssetFilters location: {:?}, assets: {:?}", location, assets);

		// `location` must match the `Location` filter.
		if !LocationFilter::contains(location) {
			return false
		}

		// All `assets` must match at least one of the `AssetFilters`.
		let filters = AssetFilters::get();
		assets.iter().all(|asset| {
			for filter in &filters {
				if filter.matches(asset) {
					return true
				}
			}
			false
		})
	}
}

/// Implementation of `Get<Vec<AssetFilter>>` which accepts every asset.
/// (For example, it can be used with `LocationWithAssetFilters`).
pub struct AllAssets;
impl Get<Vec<AssetFilter>> for AllAssets {
	fn get() -> Vec<AssetFilter> {
		alloc::vec![AssetFilter::Wild(WildAsset::All)]
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use frame_support::traits::Equals;
	use xcm::latest::prelude::*;

	#[test]
	fn location_with_asset_filters_works() {
		frame_support::parameter_types! {
			pub ParaA: Location = Location::new(1, [Parachain(1001)]);
			pub ParaB: Location = Location::new(1, [Parachain(1002)]);
			pub ParaC: Location = Location::new(1, [Parachain(1003)]);

			pub AssetXLocation: Location = Location::new(1, [GeneralIndex(1111)]);
			pub AssetYLocation: Location = Location::new(1, [GeneralIndex(2222)]);
			pub AssetZLocation: Location = Location::new(1, [GeneralIndex(3333)]);

			pub OnlyAssetXOrAssetY: alloc::vec::Vec<AssetFilter> = alloc::vec![
				Wild(AllOf { fun: WildFungible, id: AssetId(AssetXLocation::get()) }),
				Wild(AllOf { fun: WildFungible, id: AssetId(AssetYLocation::get()) }),
			];
			pub OnlyAssetZ: alloc::vec::Vec<AssetFilter> = alloc::vec![
				Wild(AllOf { fun: WildFungible, id: AssetId(AssetZLocation::get()) })
			];
		}

		let test_data: Vec<(Location, Vec<Asset>, bool)> = vec![
			(ParaA::get(), vec![(AssetXLocation::get(), 1).into()], true),
			(ParaA::get(), vec![(AssetYLocation::get(), 1).into()], true),
			(ParaA::get(), vec![(AssetZLocation::get(), 1).into()], false),
			(
				ParaA::get(),
				vec![(AssetXLocation::get(), 1).into(), (AssetYLocation::get(), 1).into()],
				true,
			),
			(
				ParaA::get(),
				vec![(AssetXLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
				false,
			),
			(
				ParaA::get(),
				vec![(AssetYLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
				false,
			),
			(
				ParaA::get(),
				vec![
					(AssetXLocation::get(), 1).into(),
					(AssetYLocation::get(), 1).into(),
					(AssetZLocation::get(), 1).into(),
				],
				false,
			),
			(ParaB::get(), vec![(AssetXLocation::get(), 1).into()], false),
			(ParaB::get(), vec![(AssetYLocation::get(), 1).into()], false),
			(ParaB::get(), vec![(AssetZLocation::get(), 1).into()], true),
			(
				ParaB::get(),
				vec![(AssetXLocation::get(), 1).into(), (AssetYLocation::get(), 1).into()],
				false,
			),
			(
				ParaB::get(),
				vec![(AssetXLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
				false,
			),
			(
				ParaB::get(),
				vec![(AssetYLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
				false,
			),
			(
				ParaB::get(),
				vec![
					(AssetXLocation::get(), 1).into(),
					(AssetYLocation::get(), 1).into(),
					(AssetZLocation::get(), 1).into(),
				],
				false,
			),
			(ParaC::get(), vec![(AssetXLocation::get(), 1).into()], true),
			(ParaC::get(), vec![(AssetYLocation::get(), 1).into()], true),
			(ParaC::get(), vec![(AssetZLocation::get(), 1).into()], true),
			(
				ParaC::get(),
				vec![(AssetXLocation::get(), 1).into(), (AssetYLocation::get(), 1).into()],
				true,
			),
			(
				ParaC::get(),
				vec![(AssetXLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
				true,
			),
			(
				ParaC::get(),
				vec![(AssetYLocation::get(), 1).into(), (AssetZLocation::get(), 1).into()],
				true,
			),
			(
				ParaC::get(),
				vec![
					(AssetXLocation::get(), 1).into(),
					(AssetYLocation::get(), 1).into(),
					(AssetZLocation::get(), 1).into(),
				],
				true,
			),
		];

		type Filter = (
			// For ParaA accept only asset X and Y.
			LocationWithAssetFilters<Equals<ParaA>, OnlyAssetXOrAssetY>,
			// For ParaB accept only asset Z.
			LocationWithAssetFilters<Equals<ParaB>, OnlyAssetZ>,
			// For ParaC accept all assets.
			LocationWithAssetFilters<Equals<ParaC>, AllAssets>,
		);

		for (location, assets, expected_result) in test_data {
			assert_eq!(
				Filter::contains(&(location.clone(), assets.clone())),
				expected_result,
				"expected_result: {expected_result} not matched for (location, assets): ({:?}, {:?})!", location, assets,
			)
		}
	}
}