referrerpolicy=no-referrer-when-downgrade

snowbridge_inbound_queue_primitives/v2/
message.rs

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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
//! Converts messages from Ethereum to XCM messages

use crate::{v2::IGatewayV2::Payload, Log};
use alloy_core::{
	primitives::B256,
	sol,
	sol_types::{SolEvent, SolType},
};
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_core::{RuntimeDebug, H160, H256};
use sp_std::prelude::*;

sol! {
	interface IGatewayV2 {
		struct AsNativeTokenERC20 {
			address token_id;
			uint128 value;
		}
		struct AsForeignTokenERC20 {
			bytes32 token_id;
			uint128 value;
		}
		struct EthereumAsset {
			uint8 kind;
			bytes data;
		}
		struct Xcm {
			uint8 kind;
			bytes data;
		}
		struct XcmCreateAsset {
			address token;
			uint8 network;
		}
		struct Payload {
			address origin;
			EthereumAsset[] assets;
			Xcm xcm;
			bytes claimer;
			uint128 value;
			uint128 executionFee;
			uint128 relayerFee;
		}
		event OutboundMessageAccepted(uint64 nonce, Payload payload);
	}
}

impl core::fmt::Debug for IGatewayV2::Payload {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		f.debug_struct("Payload")
			.field("origin", &self.origin)
			.field("assets", &self.assets)
			.field("xcm", &self.xcm)
			.field("claimer", &self.claimer)
			.field("value", &self.value)
			.field("executionFee", &self.executionFee)
			.field("relayerFee", &self.relayerFee)
			.finish()
	}
}

impl core::fmt::Debug for IGatewayV2::EthereumAsset {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		f.debug_struct("EthereumAsset")
			.field("kind", &self.kind)
			.field("data", &self.data)
			.finish()
	}
}

impl core::fmt::Debug for IGatewayV2::Xcm {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		f.debug_struct("Xcm")
			.field("kind", &self.kind)
			.field("data", &self.data)
			.finish()
	}
}

#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum XcmPayload {
	/// Represents raw XCM bytes
	Raw(Vec<u8>),
	/// A token registration template
	CreateAsset { token: H160, network: Network },
}

/// Network enum for cross-chain message destination
#[derive(Clone, Copy, Debug, Eq, PartialEq, Encode, Decode, TypeInfo)]
pub enum Network {
	/// Polkadot network
	Polkadot,
}

/// The ethereum side sends messages which are transcoded into XCM on BH. These messages are
/// self-contained, in that they can be transcoded using only information in the message.
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct Message {
	/// The address of the outbound queue on Ethereum that emitted this message as an event log
	pub gateway: H160,
	/// A nonce for enforcing replay protection and ordering.
	pub nonce: u64,
	/// The address on Ethereum that initiated the message.
	pub origin: H160,
	/// The assets sent from Ethereum (ERC-20s).
	pub assets: Vec<EthereumAsset>,
	/// The command originating from the Gateway contract.
	pub xcm: XcmPayload,
	/// The claimer in the case that funds get trapped. Expected to be an XCM::v5::Location.
	pub claimer: Option<Vec<u8>>,
	/// Native ether bridged over from Ethereum
	pub value: u128,
	/// Fee in eth to cover the xcm execution on AH.
	pub execution_fee: u128,
	/// Relayer reward in eth. Needs to cover all costs of sending a message.
	pub relayer_fee: u128,
}

/// An asset that will be transacted on AH. The asset will be reserved/withdrawn and placed into
/// the holding register. The user needs to provide additional xcm to deposit the asset
/// in a beneficiary account.
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
pub enum EthereumAsset {
	NativeTokenERC20 {
		/// The native token ID
		token_id: H160,
		/// The monetary value of the asset
		value: u128,
	},
	ForeignTokenERC20 {
		/// The foreign token ID
		token_id: H256,
		/// The monetary value of the asset
		value: u128,
	},
}

#[derive(Copy, Clone, RuntimeDebug)]
pub struct MessageDecodeError;

impl TryFrom<&Log> for Message {
	type Error = MessageDecodeError;

	fn try_from(log: &Log) -> Result<Self, Self::Error> {
		// Convert to B256 for Alloy decoding
		let topics: Vec<B256> = log.topics.iter().map(|x| B256::from_slice(x.as_ref())).collect();

		// Decode the Solidity event from raw logs
		let event = IGatewayV2::OutboundMessageAccepted::decode_raw_log(topics, &log.data, true)
			.map_err(|_| MessageDecodeError)?;

		let payload = event.payload;

		let substrate_assets = Self::extract_assets(&payload)?;

		let xcm = XcmPayload::try_from(&payload)?;

		let mut claimer = None;
		if payload.claimer.len() > 0 {
			claimer = Some(payload.claimer.to_vec());
		}

		let message = Message {
			gateway: log.address,
			nonce: event.nonce,
			origin: H160::from(payload.origin.as_ref()),
			assets: substrate_assets,
			xcm,
			claimer,
			value: payload.value,
			execution_fee: payload.executionFee,
			relayer_fee: payload.relayerFee,
		};

		Ok(message)
	}
}

impl Message {
	fn extract_assets(
		payload: &IGatewayV2::Payload,
	) -> Result<Vec<EthereumAsset>, MessageDecodeError> {
		let mut substrate_assets = vec![];
		for asset in &payload.assets {
			substrate_assets.push(EthereumAsset::try_from(asset)?);
		}
		Ok(substrate_assets)
	}
}

impl TryFrom<&IGatewayV2::Payload> for XcmPayload {
	type Error = MessageDecodeError;

	fn try_from(payload: &Payload) -> Result<Self, Self::Error> {
		let xcm = match payload.xcm.kind {
			0 => XcmPayload::Raw(payload.xcm.data.to_vec()),
			1 => {
				let create_asset = IGatewayV2::XcmCreateAsset::abi_decode(&payload.xcm.data, true)
					.map_err(|_| MessageDecodeError)?;
				// Convert u8 network to Network enum
				let network = match create_asset.network {
					0 => Network::Polkadot,
					_ => return Err(MessageDecodeError),
				};
				XcmPayload::CreateAsset { token: H160::from(create_asset.token.as_ref()), network }
			},
			_ => return Err(MessageDecodeError),
		};
		Ok(xcm)
	}
}

impl TryFrom<&IGatewayV2::EthereumAsset> for EthereumAsset {
	type Error = MessageDecodeError;

	fn try_from(asset: &IGatewayV2::EthereumAsset) -> Result<EthereumAsset, Self::Error> {
		let asset = match asset.kind {
			0 => {
				let native_data = IGatewayV2::AsNativeTokenERC20::abi_decode(&asset.data, true)
					.map_err(|_| MessageDecodeError)?;
				EthereumAsset::NativeTokenERC20 {
					token_id: H160::from(native_data.token_id.as_ref()),
					value: native_data.value,
				}
			},
			1 => {
				let foreign_data = IGatewayV2::AsForeignTokenERC20::abi_decode(&asset.data, true)
					.map_err(|_| MessageDecodeError)?;
				EthereumAsset::ForeignTokenERC20 {
					token_id: H256::from(foreign_data.token_id.as_ref()),
					value: foreign_data.value,
				}
			},
			_ => return Err(MessageDecodeError),
		};
		Ok(asset)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use frame_support::assert_ok;
	use hex_literal::hex;
	use sp_core::H160;

	#[test]
	fn test_decode() {
		let log = Log{
			address: hex!("b1185ede04202fe62d38f5db72f71e38ff3e8305").into(),
			topics: vec![hex!("550e2067494b1736ea5573f2d19cdc0ac95b410fff161bf16f11c6229655ec9c").into()],
			data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000b1185ede04202fe62d38f5db72f71e38ff3e830500000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000009184e72a0000000000000000000000000000000000000000000000000000000015d3ef798000000000000000000000000000000000000000000000000000000015d3ef798000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000b8ea8cb425d85536b158d661da1ef0895bb92f1d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").to_vec(),
		};

		let result = Message::try_from(&log);
		assert_ok!(result.clone());
		let message = result.unwrap();

		assert_eq!(H160::from(hex!("b1185ede04202fe62d38f5db72f71e38ff3e8305")), message.gateway);
		assert_eq!(H160::from(hex!("b1185ede04202fe62d38f5db72f71e38ff3e8305")), message.origin);
	}
}