referrerpolicy=no-referrer-when-downgrade

asset_hub_westend_runtime/
bridge_to_ethereum_config.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
16#[cfg(not(feature = "runtime-benchmarks"))]
17use crate::xcm_config::XcmRouter;
18use crate::{
19	weights, xcm_config,
20	xcm_config::{
21		bridging::SiblingBridgeHub, AssetTransactors, LocationToAccountId, RootLocation,
22		TrustBackedAssetsPalletLocation, UniversalLocation, XcmConfig,
23	},
24	AccountId, AssetConversion, Assets, ForeignAssets, Runtime, RuntimeEvent,
25};
26use assets_common::{matching::FromSiblingParachain, AssetIdForTrustBackedAssetsConvert};
27use frame_support::{parameter_types, traits::EitherOf};
28use frame_system::EnsureRootWithSuccess;
29use parachains_common::AssetIdForTrustBackedAssets;
30use snowbridge_runtime_common::{ForeignAssetOwner, LocalAssetOwner};
31use testnet_parachains_constants::westend::snowbridge::{EthereumNetwork, FRONTEND_PALLET_INDEX};
32use xcm::prelude::{InteriorLocation, Location, PalletInstance};
33use xcm_executor::XcmExecutor;
34
35parameter_types! {
36	pub storage FeeAsset: Location = Location::new(
37			2,
38			[
39				EthereumNetwork::get().into(),
40			],
41	);
42	pub SystemFrontendPalletLocation: InteriorLocation = [PalletInstance(FRONTEND_PALLET_INDEX)].into();
43}
44
45impl snowbridge_pallet_system_frontend::Config for Runtime {
46	type RuntimeEvent = RuntimeEvent;
47	type WeightInfo = weights::snowbridge_pallet_system_frontend::WeightInfo<Runtime>;
48	#[cfg(feature = "runtime-benchmarks")]
49	type Helper = ();
50	type RegisterTokenOrigin = EitherOf<
51		EitherOf<
52			LocalAssetOwner<
53				AssetIdForTrustBackedAssetsConvert<TrustBackedAssetsPalletLocation, Location>,
54				Assets,
55				AccountId,
56				AssetIdForTrustBackedAssets,
57				Location,
58			>,
59			ForeignAssetOwner<
60				(
61					FromSiblingParachain<parachain_info::Pallet<Runtime>, Location>,
62					xcm_config::bridging::to_rococo::RococoAssetFromAssetHubRococo,
63				),
64				ForeignAssets,
65				AccountId,
66				LocationToAccountId,
67				Location,
68			>,
69		>,
70		EnsureRootWithSuccess<AccountId, RootLocation>,
71	>;
72	#[cfg(not(feature = "runtime-benchmarks"))]
73	type XcmSender = XcmRouter;
74	#[cfg(feature = "runtime-benchmarks")]
75	type XcmSender = benchmark_helpers::DoNothingRouter;
76	type AssetTransactor = AssetTransactors;
77	type EthereumLocation = FeeAsset;
78	type XcmExecutor = XcmExecutor<XcmConfig>;
79	type BridgeHubLocation = SiblingBridgeHub;
80	type UniversalLocation = UniversalLocation;
81	type PalletLocation = SystemFrontendPalletLocation;
82	type Swap = AssetConversion;
83	type BackendWeightInfo = weights::snowbridge_pallet_system_backend::WeightInfo<Runtime>;
84	type AccountIdConverter = xcm_config::LocationToAccountId;
85}
86
87#[cfg(test)]
88mod tests {
89	use super::*;
90	use crate::SnowbridgeSystemFrontend;
91
92	#[test]
93	fn bridge_hub_inbound_queue_pallet_index_is_correct() {
94		assert_eq!(
95			FRONTEND_PALLET_INDEX,
96			<SnowbridgeSystemFrontend as frame_support::traits::PalletInfoAccess>::index() as u8
97		);
98	}
99}
100
101#[cfg(feature = "runtime-benchmarks")]
102pub mod benchmark_helpers {
103	use crate::{
104		xcm_config::LocationToAccountId, AccountId, AssetConversion, Balances, ForeignAssets,
105		RuntimeOrigin,
106	};
107	use alloc::boxed::Box;
108	use codec::Encode;
109	use xcm::prelude::*;
110	use xcm_executor::traits::ConvertLocation;
111
112	pub struct DoNothingRouter;
113	impl SendXcm for DoNothingRouter {
114		type Ticket = Xcm<()>;
115
116		fn validate(
117			_dest: &mut Option<Location>,
118			xcm: &mut Option<Xcm<()>>,
119		) -> SendResult<Self::Ticket> {
120			Ok((xcm.clone().unwrap(), Assets::new()))
121		}
122		fn deliver(xcm: Xcm<()>) -> Result<XcmHash, SendError> {
123			let hash = xcm.using_encoded(sp_io::hashing::blake2_256);
124			Ok(hash)
125		}
126	}
127
128	impl snowbridge_pallet_system_frontend::BenchmarkHelper<RuntimeOrigin, AccountId> for () {
129		fn make_xcm_origin(location: Location) -> RuntimeOrigin {
130			RuntimeOrigin::from(pallet_xcm::Origin::Xcm(location))
131		}
132
133		fn initialize_storage(asset_location: Location, asset_owner_location: Location) {
134			let asset_owner = LocationToAccountId::convert_location(&asset_owner_location).unwrap();
135			ForeignAssets::force_create(
136				RuntimeOrigin::root(),
137				asset_location,
138				asset_owner.clone().into(),
139				true,
140				1,
141			)
142			.unwrap();
143		}
144
145		fn setup_pools(caller: AccountId, asset: Location) {
146			// Prefund the caller's account with DOT
147			Balances::force_set_balance(
148				RuntimeOrigin::root(),
149				caller.clone().into(),
150				10_000_000_000_000,
151			)
152			.unwrap();
153
154			let asset_owner = caller.clone();
155			ForeignAssets::force_create(
156				RuntimeOrigin::root(),
157				asset.clone(),
158				asset_owner.clone().into(),
159				true,
160				1,
161			)
162			.unwrap();
163
164			let signed_owner = RuntimeOrigin::signed(asset_owner.clone());
165
166			// Prefund the asset owner's account with DOT and Ether to create the pools
167			ForeignAssets::mint(
168				signed_owner.clone(),
169				asset.clone().into(),
170				asset_owner.clone().into(),
171				10_000_000_000_000,
172			)
173			.unwrap();
174			Balances::force_set_balance(
175				RuntimeOrigin::root(),
176				asset_owner.clone().into(),
177				10_000_000_000_000,
178			)
179			.unwrap();
180
181			// Create the pool so the swap will succeed
182			let native_asset: Location = Parent.into();
183			AssetConversion::create_pool(
184				signed_owner.clone(),
185				Box::new(native_asset.clone()),
186				Box::new(asset.clone()),
187			)
188			.unwrap();
189			AssetConversion::add_liquidity(
190				signed_owner,
191				Box::new(native_asset),
192				Box::new(asset),
193				1_000_000_000_000,
194				2_000_000_000_000,
195				0,
196				0,
197				asset_owner.into(),
198			)
199			.unwrap();
200		}
201	}
202}