pallet_erc20_xcm_bridge/
erc20_trap.rs

1// Copyright 2019-2025 PureStake Inc.
2// This file is part of Moonbeam.
3
4// Moonbeam 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// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16
17use sp_std::vec::Vec;
18use xcm::latest::prelude::*;
19use xcm_executor::traits::DropAssets;
20
21/// Morph a given `DropAssets` implementation into one which filter out erc20 assets.
22pub struct AssetTrapWrapper<AssetTrap, T>(core::marker::PhantomData<(AssetTrap, T)>);
23
24// Morph a given `DropAssets` implementation into one which filter out erc20 assets.
25impl<AssetTrap: DropAssets, T: crate::Config> DropAssets for AssetTrapWrapper<AssetTrap, T> {
26	fn drop_assets(
27		origin: &xcm::latest::Location,
28		mut assets: xcm_executor::AssetsInHolding,
29		context: &XcmContext,
30	) -> xcm::latest::Weight {
31		// Remove all erc20 assets
32		let assets_to_remove: Vec<_> = assets
33			.fungible_assets_iter()
34			.filter_map(|multiasset| {
35				crate::Pallet::<T>::is_erc20_asset(&multiasset).then_some(multiasset.id)
36			})
37			.collect();
38		for id in assets_to_remove {
39			assets.saturating_take(xcm::latest::AssetFilter::Wild(
40				xcm::latest::WildAsset::AllOf {
41					fun: WildFungible,
42					id,
43				},
44			));
45		}
46		AssetTrap::drop_assets(origin, assets, context)
47	}
48}