This repository was archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlib.rs
More file actions
214 lines (189 loc) · 6.95 KB
/
lib.rs
File metadata and controls
214 lines (189 loc) · 6.95 KB
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
#![cfg_attr(not(feature = "std"), no_std)]
type BaseXcm<T> = pallet_xcm::Pallet<T>;
use frame_support::{
dispatch::DispatchResult,
ensure,
traits::{Contains, EnsureOrigin, Get},
weights::Weight,
};
use frame_system::pallet_prelude::OriginFor;
pub use pallet::*;
use parity_scale_codec::Encode;
use sp_std::{boxed::Box, vec};
pub use xcm::{
opaque::latest::prelude::{Junction, Junctions, MultiLocation, OriginKind},
v3::{
AssetId, ExecuteXcm, Fungibility,
Instruction::{
BurnAsset, BuyExecution, DepositAsset, DepositReserveAsset, InitiateReserveWithdraw,
ReceiveTeleportedAsset, Transact, WithdrawAsset,
},
MultiAsset, MultiAssetFilter, MultiAssets, Parent, SendXcm, WeightLimit,
WeightLimit::Unlimited,
WildMultiAsset, Xcm, XcmHash,
},
VersionedMultiAssets, VersionedMultiLocation, VersionedResponse, VersionedXcm,
};
// #[cfg(test)]
// mod mock;
// #[cfg(test)]
// mod tests;
// #[cfg(feature = "runtime-benchmarks")]
// mod benchmarking;
// pub mod weights;
// pub use weights::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config + pallet_xcm::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
#[pallet::error]
pub enum Error<T> {
/// An error ocured during send
SendError,
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Execution of an XCM message was attempted.
Attempted { outcome: xcm::latest::Outcome },
/// A XCM message was sent.
Sent {
origin: MultiLocation,
destination: MultiLocation,
message: Xcm<()>,
message_id: XcmHash,
},
}
/// Teleport native asset from a parachain to another.
/// This function is called by the parachain that wants to teleport native assets to another
/// parachain but needs to buy execution on the destination parachain with an asset that is not
/// being teleported. We call this asset the proxy asset.
/// The parachain that wants to teleport native assets to another parachain with this method
/// need to fund its Sovereign Account with the proxy asset on the destination parachain.
/// If multiple proxy assets are included in the message, only the first one is used to buy
/// execution. Proxy assets are trapped on the destination parachain.
/// Parameters:
/// - `origin`: The origin of the call.
/// - `dest`: The destination chain of the teleport.
/// - `beneficiary`: The beneficiary of the teleport from the perspective of the destination
/// chain.
/// - `native_asset_amount`: The amount of native asset to teleport.
/// - `proxy_asset`: The proxy asset to buy execution on the destination chain.
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(10_000)]
pub fn proxy_native_teleport(
origin: OriginFor<T>,
dest: Box<VersionedMultiLocation>,
beneficiary: Box<VersionedMultiLocation>,
native_asset_amount: u128,
proxy_asset: Box<VersionedMultiAssets>,
) -> DispatchResult {
Self::do_proxy_teleport_assets(
origin,
dest,
beneficiary,
native_asset_amount,
proxy_asset,
)
}
}
}
impl<T: Config> Pallet<T> {
fn do_proxy_teleport_assets(
origin: OriginFor<T>,
dest: Box<VersionedMultiLocation>,
beneficiary: Box<VersionedMultiLocation>,
native_asset_amount: u128,
proxy_asset: Box<VersionedMultiAssets>,
) -> DispatchResult {
//Unbox origin, destination and beneficiary.
let origin_location = T::ExecuteXcmOrigin::ensure_origin(origin)?;
let dest: MultiLocation =
(*dest).try_into().map_err(|()| pallet_xcm::Error::<T>::BadVersion)?;
let beneficiary: MultiLocation =
(*beneficiary).try_into().map_err(|()| pallet_xcm::Error::<T>::BadVersion)?;
//Create assets
// Native from local perspective
let native_asset = MultiAsset {
id: AssetId::Concrete(MultiLocation::here()),
fun: Fungibility::Fungible(native_asset_amount),
};
let assets = MultiAssets::from(vec![native_asset.clone()]);
// Native from foreign perspective
let context = T::UniversalLocation::get();
let native_as_foreign = native_asset
.reanchored(&dest, context)
.map_err(|_| pallet_xcm::Error::<T>::CannotReanchor)?;
let foreing_assets = MultiAssets::from(vec![native_as_foreign]);
//Unbox proxy asset
let proxy_asset: MultiAssets =
(*proxy_asset).try_into().map_err(|()| pallet_xcm::Error::<T>::BadVersion)?;
//TeleportFilter check
let value = (origin_location, assets.into_inner());
ensure!(T::XcmTeleportFilter::contains(&value), pallet_xcm::Error::<T>::Filtered);
let (origin_location, assets) = value;
// Reanchor the proxy asset to the destination chain.
let fee_asset_item: usize = 0;
let fees = proxy_asset
.get(fee_asset_item as usize)
.ok_or(pallet_xcm::Error::<T>::Empty)?
.clone()
.reanchored(&dest, context)
.map_err(|_| pallet_xcm::Error::<T>::CannotReanchor)?;
// TODO: Define if Withdrawn proxy assets are deposited or trapped.
// Check if there is no vulnerability through RefundSurplus
//let max_assets = (assets.len() as u32).checked_add(1).ok_or(Error::<T>::TooManyAssets)?;
//Build the message to execute on origin.
let assets: MultiAssets = assets.into();
let message: Xcm<<T as frame_system::Config>::RuntimeCall> = Xcm(vec![
// Withdraw drops asset so is used as burn mechanism
WithdrawAsset(assets.clone()),
BurnAsset(assets),
]);
// Build the message to send.
// Set WeightLimit
// TODO: Implement weight_limit calculation with final instructions.
let weight_limit: WeightLimit = Unlimited;
let xcm_message: Xcm<()> = Xcm(vec![
WithdrawAsset(proxy_asset),
BuyExecution { fees, weight_limit },
ReceiveTeleportedAsset(foreing_assets.clone()),
// Intentionally trap ROC to avoid exploit
DepositAsset { assets: MultiAssetFilter::Definite(foreing_assets), beneficiary },
]);
// Temporarly hardcode weight.
// TODO: Replace for Weigher.
let weight: Weight = Weight::from_parts(1_000_000_000, 100_000);
// T::Weigher::weight(&mut message).map_err(|()| Error::<T>::UnweighableMessage)?;
// Execute Withdraw for trapping assets on origin.
let hash = message.using_encoded(sp_io::hashing::blake2_256);
let outcome =
T::XcmExecutor::execute_xcm_in_credit(origin_location, message, hash, weight, weight);
Self::deposit_event(Event::Attempted { outcome });
// Use pallet-xcm send for sending message.
let root_origin = T::SendXcmOrigin::ensure_origin(frame_system::RawOrigin::Root.into())?;
let interior: Junctions =
root_origin.try_into().map_err(|_| pallet_xcm::Error::<T>::InvalidOrigin)?;
//TODO: Check this Error population
let message_id = BaseXcm::<T>::send_xcm(interior, dest, xcm_message.clone())
.map_err(|_| Error::<T>::SendError)?;
let e = Event::Sent {
origin: origin_location,
destination: dest,
message: xcm_message,
message_id,
};
Self::deposit_event(e);
// Finish.
Ok(())
}
}