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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
//! Converts messages from Ethereum to XCM messages

#[cfg(test)]
mod tests;

use codec::{Decode, Encode};
use core::marker::PhantomData;
use frame_support::{traits::tokens::Balance as BalanceT, weights::Weight, PalletError};
use scale_info::TypeInfo;
use snowbridge_core::TokenId;
use sp_core::{Get, RuntimeDebug, H160, H256};
use sp_io::hashing::blake2_256;
use sp_runtime::{traits::MaybeEquivalence, MultiAddress};
use sp_std::prelude::*;
use xcm::prelude::{Junction::AccountKey20, *};
use xcm_executor::traits::ConvertLocation;

const MINIMUM_DEPOSIT: u128 = 1;

/// Messages from Ethereum are versioned. This is because in future,
/// we may want to evolve the protocol so that the ethereum side sends XCM messages directly.
/// Instead having BridgeHub transcode the messages into XCM.
#[derive(Clone, Encode, Decode, RuntimeDebug)]
pub enum VersionedMessage {
	V1(MessageV1),
}

/// For V1, the ethereum side sends messages which are transcoded into XCM. These messages are
/// self-contained, in that they can be transcoded using only information in the message.
#[derive(Clone, Encode, Decode, RuntimeDebug)]
pub struct MessageV1 {
	/// EIP-155 chain id of the origin Ethereum network
	pub chain_id: u64,
	/// The command originating from the Gateway contract
	pub command: Command,
}

#[derive(Clone, Encode, Decode, RuntimeDebug)]
pub enum Command {
	/// Register a wrapped token on the AssetHub `ForeignAssets` pallet
	RegisterToken {
		/// The address of the ERC20 token to be bridged over to AssetHub
		token: H160,
		/// XCM execution fee on AssetHub
		fee: u128,
	},
	/// Send Ethereum token to AssetHub or another parachain
	SendToken {
		/// The address of the ERC20 token to be bridged over to AssetHub
		token: H160,
		/// The destination for the transfer
		destination: Destination,
		/// Amount to transfer
		amount: u128,
		/// XCM execution fee on AssetHub
		fee: u128,
	},
	/// Send Polkadot token back to the original parachain
	SendNativeToken {
		/// The Id of the token
		token_id: TokenId,
		/// The destination for the transfer
		destination: Destination,
		/// Amount to transfer
		amount: u128,
		/// XCM execution fee on AssetHub
		fee: u128,
	},
}

/// Destination for bridged tokens
#[derive(Clone, Encode, Decode, RuntimeDebug)]
pub enum Destination {
	/// The funds will be deposited into account `id` on AssetHub
	AccountId32 { id: [u8; 32] },
	/// The funds will deposited into the sovereign account of destination parachain `para_id` on
	/// AssetHub, Account `id` on the destination parachain will receive the funds via a
	/// reserve-backed transfer. See <https://github.com/paritytech/xcm-format#depositreserveasset>
	ForeignAccountId32 {
		para_id: u32,
		id: [u8; 32],
		/// XCM execution fee on final destination
		fee: u128,
	},
	/// The funds will deposited into the sovereign account of destination parachain `para_id` on
	/// AssetHub, Account `id` on the destination parachain will receive the funds via a
	/// reserve-backed transfer. See <https://github.com/paritytech/xcm-format#depositreserveasset>
	ForeignAccountId20 {
		para_id: u32,
		id: [u8; 20],
		/// XCM execution fee on final destination
		fee: u128,
	},
}

pub struct MessageToXcm<
	CreateAssetCall,
	CreateAssetDeposit,
	InboundQueuePalletInstance,
	AccountId,
	Balance,
	ConvertAssetId,
	EthereumUniversalLocation,
	GlobalAssetHubLocation,
> where
	CreateAssetCall: Get<CallIndex>,
	CreateAssetDeposit: Get<u128>,
	Balance: BalanceT,
	ConvertAssetId: MaybeEquivalence<TokenId, Location>,
	EthereumUniversalLocation: Get<InteriorLocation>,
	GlobalAssetHubLocation: Get<Location>,
{
	_phantom: PhantomData<(
		CreateAssetCall,
		CreateAssetDeposit,
		InboundQueuePalletInstance,
		AccountId,
		Balance,
		ConvertAssetId,
		EthereumUniversalLocation,
		GlobalAssetHubLocation,
	)>,
}

/// Reason why a message conversion failed.
#[derive(Copy, Clone, TypeInfo, PalletError, Encode, Decode, RuntimeDebug)]
pub enum ConvertMessageError {
	/// The message version is not supported for conversion.
	UnsupportedVersion,
	InvalidDestination,
	InvalidToken,
	/// The fee asset is not supported for conversion.
	UnsupportedFeeAsset,
	CannotReanchor,
}

/// convert the inbound message to xcm which will be forwarded to the destination chain
pub trait ConvertMessage {
	type Balance: BalanceT + From<u128>;
	type AccountId;
	/// Converts a versioned message into an XCM message and an optional topicID
	fn convert(
		message_id: H256,
		message: VersionedMessage,
	) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError>;
}

pub type CallIndex = [u8; 2];

impl<
		CreateAssetCall,
		CreateAssetDeposit,
		InboundQueuePalletInstance,
		AccountId,
		Balance,
		ConvertAssetId,
		EthereumUniversalLocation,
		GlobalAssetHubLocation,
	> ConvertMessage
	for MessageToXcm<
		CreateAssetCall,
		CreateAssetDeposit,
		InboundQueuePalletInstance,
		AccountId,
		Balance,
		ConvertAssetId,
		EthereumUniversalLocation,
		GlobalAssetHubLocation,
	> where
	CreateAssetCall: Get<CallIndex>,
	CreateAssetDeposit: Get<u128>,
	InboundQueuePalletInstance: Get<u8>,
	Balance: BalanceT + From<u128>,
	AccountId: Into<[u8; 32]>,
	ConvertAssetId: MaybeEquivalence<TokenId, Location>,
	EthereumUniversalLocation: Get<InteriorLocation>,
	GlobalAssetHubLocation: Get<Location>,
{
	type Balance = Balance;
	type AccountId = AccountId;

	fn convert(
		message_id: H256,
		message: VersionedMessage,
	) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError> {
		use Command::*;
		use VersionedMessage::*;
		match message {
			V1(MessageV1 { chain_id, command: RegisterToken { token, fee } }) =>
				Ok(Self::convert_register_token(message_id, chain_id, token, fee)),
			V1(MessageV1 { chain_id, command: SendToken { token, destination, amount, fee } }) =>
				Ok(Self::convert_send_token(message_id, chain_id, token, destination, amount, fee)),
			V1(MessageV1 {
				chain_id,
				command: SendNativeToken { token_id, destination, amount, fee },
			}) => Self::convert_send_native_token(
				message_id,
				chain_id,
				token_id,
				destination,
				amount,
				fee,
			),
		}
	}
}

impl<
		CreateAssetCall,
		CreateAssetDeposit,
		InboundQueuePalletInstance,
		AccountId,
		Balance,
		ConvertAssetId,
		EthereumUniversalLocation,
		GlobalAssetHubLocation,
	>
	MessageToXcm<
		CreateAssetCall,
		CreateAssetDeposit,
		InboundQueuePalletInstance,
		AccountId,
		Balance,
		ConvertAssetId,
		EthereumUniversalLocation,
		GlobalAssetHubLocation,
	> where
	CreateAssetCall: Get<CallIndex>,
	CreateAssetDeposit: Get<u128>,
	InboundQueuePalletInstance: Get<u8>,
	Balance: BalanceT + From<u128>,
	AccountId: Into<[u8; 32]>,
	ConvertAssetId: MaybeEquivalence<TokenId, Location>,
	EthereumUniversalLocation: Get<InteriorLocation>,
	GlobalAssetHubLocation: Get<Location>,
{
	fn convert_register_token(
		message_id: H256,
		chain_id: u64,
		token: H160,
		fee: u128,
	) -> (Xcm<()>, Balance) {
		let network = Ethereum { chain_id };
		let xcm_fee: Asset = (Location::parent(), fee).into();
		let deposit: Asset = (Location::parent(), CreateAssetDeposit::get()).into();

		let total_amount = fee + CreateAssetDeposit::get();
		let total: Asset = (Location::parent(), total_amount).into();

		let bridge_location: Location = (Parent, Parent, GlobalConsensus(network)).into();

		let owner = GlobalConsensusEthereumConvertsFor::<[u8; 32]>::from_chain_id(&chain_id);
		let asset_id = Self::convert_token_address(network, token);
		let create_call_index: [u8; 2] = CreateAssetCall::get();
		let inbound_queue_pallet_index = InboundQueuePalletInstance::get();

		let xcm: Xcm<()> = vec![
			// Teleport required fees.
			ReceiveTeleportedAsset(total.into()),
			// Pay for execution.
			BuyExecution { fees: xcm_fee, weight_limit: Unlimited },
			// Fund the snowbridge sovereign with the required deposit for creation.
			DepositAsset { assets: Definite(deposit.into()), beneficiary: bridge_location },
			// Only our inbound-queue pallet is allowed to invoke `UniversalOrigin`
			DescendOrigin(PalletInstance(inbound_queue_pallet_index).into()),
			// Change origin to the bridge.
			UniversalOrigin(GlobalConsensus(network)),
			// Call create_asset on foreign assets pallet.
			Transact {
				origin_kind: OriginKind::Xcm,
				require_weight_at_most: Weight::from_parts(400_000_000, 8_000),
				call: (
					create_call_index,
					asset_id,
					MultiAddress::<[u8; 32], ()>::Id(owner),
					MINIMUM_DEPOSIT,
				)
					.encode()
					.into(),
			},
			RefundSurplus,
			// Clear the origin so that remaining assets in holding
			// are claimable by the physical origin (BridgeHub)
			ClearOrigin,
			// Forward message id to Asset Hub
			SetTopic(message_id.into()),
		]
		.into();

		(xcm, total_amount.into())
	}

	fn convert_send_token(
		message_id: H256,
		chain_id: u64,
		token: H160,
		destination: Destination,
		amount: u128,
		asset_hub_fee: u128,
	) -> (Xcm<()>, Balance) {
		let network = Ethereum { chain_id };
		let asset_hub_fee_asset: Asset = (Location::parent(), asset_hub_fee).into();
		let asset: Asset = (Self::convert_token_address(network, token), amount).into();

		let (dest_para_id, beneficiary, dest_para_fee) = match destination {
			// Final destination is a 32-byte account on AssetHub
			Destination::AccountId32 { id } =>
				(None, Location::new(0, [AccountId32 { network: None, id }]), 0),
			// Final destination is a 32-byte account on a sibling of AssetHub
			Destination::ForeignAccountId32 { para_id, id, fee } => (
				Some(para_id),
				Location::new(0, [AccountId32 { network: None, id }]),
				// Total fee needs to cover execution on AssetHub and Sibling
				fee,
			),
			// Final destination is a 20-byte account on a sibling of AssetHub
			Destination::ForeignAccountId20 { para_id, id, fee } => (
				Some(para_id),
				Location::new(0, [AccountKey20 { network: None, key: id }]),
				// Total fee needs to cover execution on AssetHub and Sibling
				fee,
			),
		};

		let total_fees = asset_hub_fee.saturating_add(dest_para_fee);
		let total_fee_asset: Asset = (Location::parent(), total_fees).into();
		let inbound_queue_pallet_index = InboundQueuePalletInstance::get();

		let mut instructions = vec![
			ReceiveTeleportedAsset(total_fee_asset.into()),
			BuyExecution { fees: asset_hub_fee_asset, weight_limit: Unlimited },
			DescendOrigin(PalletInstance(inbound_queue_pallet_index).into()),
			UniversalOrigin(GlobalConsensus(network)),
			ReserveAssetDeposited(asset.clone().into()),
			ClearOrigin,
		];

		match dest_para_id {
			Some(dest_para_id) => {
				let dest_para_fee_asset: Asset = (Location::parent(), dest_para_fee).into();

				instructions.extend(vec![
					// Perform a deposit reserve to send to destination chain.
					DepositReserveAsset {
						assets: Definite(vec![dest_para_fee_asset.clone(), asset.clone()].into()),
						dest: Location::new(1, [Parachain(dest_para_id)]),
						xcm: vec![
							// Buy execution on target.
							BuyExecution { fees: dest_para_fee_asset, weight_limit: Unlimited },
							// Deposit asset to beneficiary.
							DepositAsset { assets: Definite(asset.into()), beneficiary },
							// Forward message id to destination parachain.
							SetTopic(message_id.into()),
						]
						.into(),
					},
				]);
			},
			None => {
				instructions.extend(vec![
					// Deposit both asset and fees to beneficiary so the fees will not get
					// trapped. Another benefit is when fees left more than ED on AssetHub could be
					// used to create the beneficiary account in case it does not exist.
					DepositAsset { assets: Wild(AllCounted(2)), beneficiary },
				]);
			},
		}

		// Forward message id to Asset Hub.
		instructions.push(SetTopic(message_id.into()));

		(instructions.into(), total_fees.into())
	}

	// Convert ERC20 token address to a location that can be understood by Assets Hub.
	fn convert_token_address(network: NetworkId, token: H160) -> Location {
		Location::new(
			2,
			[GlobalConsensus(network), AccountKey20 { network: None, key: token.into() }],
		)
	}

	/// Constructs an XCM message destined for AssetHub that withdraws assets from the sovereign
	/// account of the Gateway contract and either deposits those assets into a recipient account or
	/// forwards the assets to another parachain.
	fn convert_send_native_token(
		message_id: H256,
		chain_id: u64,
		token_id: TokenId,
		destination: Destination,
		amount: u128,
		asset_hub_fee: u128,
	) -> Result<(Xcm<()>, Balance), ConvertMessageError> {
		let network = Ethereum { chain_id };
		let asset_hub_fee_asset: Asset = (Location::parent(), asset_hub_fee).into();

		let beneficiary = match destination {
			// Final destination is a 32-byte account on AssetHub
			Destination::AccountId32 { id } =>
				Ok(Location::new(0, [AccountId32 { network: None, id }])),
			_ => Err(ConvertMessageError::InvalidDestination),
		}?;

		let total_fee_asset: Asset = (Location::parent(), asset_hub_fee).into();

		let asset_loc =
			ConvertAssetId::convert(&token_id).ok_or(ConvertMessageError::InvalidToken)?;

		let mut reanchored_asset_loc = asset_loc.clone();
		reanchored_asset_loc
			.reanchor(&GlobalAssetHubLocation::get(), &EthereumUniversalLocation::get())
			.map_err(|_| ConvertMessageError::CannotReanchor)?;

		let asset: Asset = (reanchored_asset_loc, amount).into();

		let inbound_queue_pallet_index = InboundQueuePalletInstance::get();

		let instructions = vec![
			ReceiveTeleportedAsset(total_fee_asset.clone().into()),
			BuyExecution { fees: asset_hub_fee_asset, weight_limit: Unlimited },
			DescendOrigin(PalletInstance(inbound_queue_pallet_index).into()),
			UniversalOrigin(GlobalConsensus(network)),
			WithdrawAsset(asset.clone().into()),
			// Deposit both asset and fees to beneficiary so the fees will not get
			// trapped. Another benefit is when fees left more than ED on AssetHub could be
			// used to create the beneficiary account in case it does not exist.
			DepositAsset { assets: Wild(AllCounted(2)), beneficiary },
			SetTopic(message_id.into()),
		];

		// `total_fees` to burn on this chain when sending `instructions` to run on AH (which also
		// teleport fees)
		Ok((instructions.into(), asset_hub_fee.into()))
	}
}

pub struct GlobalConsensusEthereumConvertsFor<AccountId>(PhantomData<AccountId>);
impl<AccountId> ConvertLocation<AccountId> for GlobalConsensusEthereumConvertsFor<AccountId>
where
	AccountId: From<[u8; 32]> + Clone,
{
	fn convert_location(location: &Location) -> Option<AccountId> {
		match location.unpack() {
			(_, [GlobalConsensus(Ethereum { chain_id })]) =>
				Some(Self::from_chain_id(chain_id).into()),
			_ => None,
		}
	}
}

impl<AccountId> GlobalConsensusEthereumConvertsFor<AccountId> {
	pub fn from_chain_id(chain_id: &u64) -> [u8; 32] {
		(b"ethereum-chain", chain_id).using_encoded(blake2_256)
	}
}