Skip to main content

pallet_token_gateway/
types.rs

1// Copyright (C) Polytope Labs 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//! Pallet types
17
18use alloc::{collections::BTreeMap, vec::Vec};
19use frame_support::{pallet_prelude::*, traits::fungibles};
20use ismp::host::StateMachine;
21use polkadot_sdk::*;
22use primitive_types::H256;
23use sp_core::H160;
24
25use crate::Config;
26
27pub type AssetId<T> =
28	<<T as Config>::Assets as fungibles::Inspect<<T as frame_system::Config>::AccountId>>::AssetId;
29
30/// Size of Body struct in ABI encoding (5 fixed-size fields * 32 bytes each)
31/// uint256 (32) + bytes32 (32) + bool (32, padded) + bytes32 (32) + bytes32 (32) = 160 bytes
32pub const BODY_BYTES_SIZE: usize = 160;
33
34/// Size of Body with discriminator byte (1 byte + 160 bytes)
35pub const BODY_BYTES_SIZE_WITH_DISCRIMINATOR: usize = BODY_BYTES_SIZE + 1;
36
37/// Asset teleportation parameters
38#[derive(
39	Debug, Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq,
40)]
41pub struct TeleportParams<AssetId, Balance> {
42	/// Asset Id registered on Hyperbridge
43	pub asset_id: AssetId,
44	/// Destination state machine
45	pub destination: StateMachine,
46	/// Receiving account on destination
47	pub recepient: H256,
48	/// Amount to be sent
49	pub amount: Balance,
50	/// Request timeout
51	pub timeout: u64,
52	/// Token gateway address
53	pub token_gateway: Vec<u8>,
54	/// Relayer fee
55	pub relayer_fee: Balance,
56	/// Optional call data to be executed on the destination chain
57	pub call_data: Option<Vec<u8>>,
58	/// Redeem native erc20 assets
59	pub redeem: bool,
60}
61
62/// Local asset Id and its corresponding token gateway asset id
63#[derive(
64	Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq, RuntimeDebug,
65)]
66pub struct AssetRegistration<AssetId> {
67	/// Local Asset Id should already exist
68	pub local_id: AssetId,
69	/// MNT Asset registration details
70	pub reg: token_gateway_primitives::GatewayAssetRegistration,
71	/// Flag that asserts if this asset is custodied and originally minted on this chain
72	pub native: bool,
73	/// Precision of asset on supported chains
74	pub precision: BTreeMap<StateMachine, u8>,
75}
76
77/// Update the precision of an asset
78#[derive(
79	Clone, Encode, Decode, DecodeWithMemTracking, scale_info::TypeInfo, PartialEq, Eq, RuntimeDebug,
80)]
81pub struct PrecisionUpdate<AssetId> {
82	/// Asset Id
83	pub asset_id: AssetId,
84	/// New precisions
85	pub precisions: BTreeMap<StateMachine, u8>,
86}
87
88alloy_sol_macro::sol! {
89	#![sol(all_derives)]
90	struct Body {
91		// Amount of the asset to be sent
92		uint256 amount;
93		// The asset identifier
94		bytes32 asset_id;
95		// Flag to redeem the erc20 asset on the destination
96		bool redeem;
97		// Sender address
98		bytes32 from;
99		// Recipient address
100		bytes32 to;
101	}
102
103	struct BodyWithCall {
104		// Amount of the asset to be sent
105		uint256 amount;
106		// The asset identifier
107		bytes32 asset_id;
108		// Flag to redeem the erc20 asset on the destination
109		bool redeem;
110		// Sender address
111		bytes32 from;
112		// Recipient address
113		bytes32 to;
114		// Calldata to be passed to the asset destination
115		bytes data;
116	}
117}
118
119#[derive(Debug, Clone, Encode, Decode, scale_info::TypeInfo, PartialEq, Eq)]
120pub struct SubstrateCalldata {
121	/// A scale encoded encoded [MultiSignature](sp_runtime::MultiSignature) of the beneficiary's
122	/// account nonce and the encoded runtime call
123	pub signature: Option<Vec<u8>>,
124	/// Encoded Runtime call that should be executed
125	pub runtime_call: Vec<u8>,
126}
127
128/// Type that encapsulates both types of token gateway request bodies
129pub struct RequestBody {
130	pub amount: alloy_primitives::U256,
131	pub asset_id: alloy_primitives::FixedBytes<32>,
132	pub redeem: bool,
133	pub from: alloy_primitives::FixedBytes<32>,
134	pub to: alloy_primitives::FixedBytes<32>,
135	pub data: Option<alloy_primitives::Bytes>,
136}
137
138impl From<Body> for RequestBody {
139	fn from(value: Body) -> Self {
140		RequestBody {
141			amount: value.amount,
142			asset_id: value.asset_id,
143			redeem: value.redeem,
144			from: value.from,
145			to: value.to,
146			data: None,
147		}
148	}
149}
150
151impl From<BodyWithCall> for RequestBody {
152	fn from(value: BodyWithCall) -> Self {
153		RequestBody {
154			amount: value.amount,
155			asset_id: value.asset_id,
156			redeem: value.redeem,
157			from: value.from,
158			to: value.to,
159			data: Some(value.data),
160		}
161	}
162}
163
164pub trait EvmToSubstrate<T: frame_system::Config> {
165	fn convert(addr: H160) -> T::AccountId;
166}
167
168impl<T: frame_system::Config> EvmToSubstrate<T> for ()
169where
170	<T as frame_system::Config>::AccountId: From<[u8; 32]>,
171{
172	fn convert(addr: H160) -> <T as frame_system::Config>::AccountId {
173		let mut account = [0u8; 32];
174		account[12..].copy_from_slice(&addr.0);
175		account.into()
176	}
177}