staging_xcm_executor/traits/
drop_assets.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17use crate::AssetsInHolding;
18use core::marker::PhantomData;
19use frame_support::traits::Contains;
20use xcm::latest::{Assets, Location, Weight, XcmContext};
21
22/// Define a handler for when some non-empty `AssetsInHolding` value should be dropped.
23pub trait DropAssets {
24	/// Handler for receiving dropped assets. Returns the weight consumed by this operation.
25	fn drop_assets(origin: &Location, assets: AssetsInHolding, context: &XcmContext) -> Weight;
26}
27impl DropAssets for () {
28	fn drop_assets(_origin: &Location, _assets: AssetsInHolding, _context: &XcmContext) -> Weight {
29		Weight::zero()
30	}
31}
32
33/// Morph a given `DropAssets` implementation into one which can filter based on assets. This can
34/// be used to ensure that `AssetsInHolding` values which hold no value are ignored.
35pub struct FilterAssets<D, A>(PhantomData<(D, A)>);
36
37impl<D: DropAssets, A: Contains<AssetsInHolding>> DropAssets for FilterAssets<D, A> {
38	fn drop_assets(origin: &Location, assets: AssetsInHolding, context: &XcmContext) -> Weight {
39		if A::contains(&assets) {
40			D::drop_assets(origin, assets, context)
41		} else {
42			Weight::zero()
43		}
44	}
45}
46
47/// Morph a given `DropAssets` implementation into one which can filter based on origin. This can
48/// be used to ban origins which don't have proper protections/policies against misuse of the
49/// asset trap facility don't get to use it.
50pub struct FilterOrigin<D, O>(PhantomData<(D, O)>);
51
52impl<D: DropAssets, O: Contains<Location>> DropAssets for FilterOrigin<D, O> {
53	fn drop_assets(origin: &Location, assets: AssetsInHolding, context: &XcmContext) -> Weight {
54		if O::contains(origin) {
55			D::drop_assets(origin, assets, context)
56		} else {
57			Weight::zero()
58		}
59	}
60}
61
62/// Define any handlers for the `AssetClaim` instruction.
63pub trait ClaimAssets {
64	/// Claim any assets available to `origin` and return them in a single `Assets` value, together
65	/// with the weight used by this operation.
66	fn claim_assets(
67		origin: &Location,
68		ticket: &Location,
69		what: &Assets,
70		context: &XcmContext,
71	) -> bool;
72}
73
74#[impl_trait_for_tuples::impl_for_tuples(30)]
75impl ClaimAssets for Tuple {
76	fn claim_assets(
77		origin: &Location,
78		ticket: &Location,
79		what: &Assets,
80		context: &XcmContext,
81	) -> bool {
82		for_tuples!( #(
83			if Tuple::claim_assets(origin, ticket, what, context) {
84				return true;
85			}
86		)* );
87		false
88	}
89}