|
1 |
| -diff --git a/fundamentals/src/pallet_xcm.rs b/fundamentals/src/pallet_xcm.rs |
2 |
| -index 8b13789..7be7118 100644 |
3 |
| ---- a/fundamentals/src/pallet_xcm.rs |
4 |
| -+++ b/fundamentals/src/pallet_xcm.rs |
5 |
| -@@ -1 +1,166 @@ |
6 |
| -+//! # Fundamentals lesson 6: Pallet XCM |
7 |
| -+//! |
8 |
| -+//! Implement the core functionality of Pallet XCM |
9 |
| - |
10 |
| -+use crate::xcm_executor::ExecuteXcm; |
11 |
| -+use frame_support::pallet_prelude::*; |
12 |
| -+use frame_system::pallet_prelude::*; |
13 |
| -+use xcm::{prelude::*, VersionedAssets, VersionedLocation, VersionedXcm}; |
14 |
| -+ |
15 |
| -+pub use pallet::*; |
16 |
| -+ |
17 |
| -+#[frame_support::pallet] |
18 |
| -+pub mod pallet { |
19 |
| -+ use super::*; |
20 |
| -+ |
21 |
| -+ #[pallet::pallet] |
22 |
| -+ pub struct Pallet<T>(_); |
23 |
| -+ |
24 |
| -+ #[pallet::config] |
25 |
| -+ pub trait Config: frame_system::Config { |
26 |
| -+ /// Something to execute an XCM message. |
27 |
| -+ type XcmExecutor: ExecuteXcm; |
28 |
| -+ /// Required origin for executing XCM messages, including the teleport functionality. If |
29 |
| -+ /// successful, then it resolves to `Location` which exists as an interior location |
30 |
| -+ /// within this chain's XCM context. |
31 |
| -+ type ExecuteXcmOrigin: EnsureOrigin< |
32 |
| -+ <Self as frame_system::Config>::RuntimeOrigin, |
33 |
| -+ Success = Location, |
34 |
| -+ >; |
35 |
| -+ /// Required origin for sending XCM messages. If successful, it resolves to `Location` |
36 |
| -+ /// which exists as an interior location within this chain's XCM context. |
37 |
| -+ type SendXcmOrigin: EnsureOrigin< |
38 |
| -+ <Self as frame_system::Config>::RuntimeOrigin, |
39 |
| -+ Success = Location, |
40 |
| -+ >; |
41 |
| -+ /// The type used to actually dispatch an XCM to its destination. |
42 |
| -+ type XcmRouter: SendXcm; |
43 |
| -+ /// This chain's Universal Location. |
44 |
| -+ type UniversalLocation: Get<InteriorLocation>; |
45 |
| -+ } |
46 |
| -+ |
47 |
| -+ #[pallet::error] |
48 |
| -+ pub enum Error<T> { |
49 |
| -+ /// The version of the `Versioned` value used is not able to be interpreted. |
50 |
| -+ BadVersion, |
51 |
| -+ /// Origin is invalid for sending. |
52 |
| -+ InvalidOrigin, |
53 |
| -+ /// Could not re-anchor the assets to declare the fees for the destination chain. |
54 |
| -+ CannotReanchor, |
55 |
| -+ /// A general error indicating something went wrong with the XCM Executor. |
56 |
| -+ ExecutorError, |
57 |
| -+ /// A general error indicating something went wrong with the XCM Router. |
58 |
| -+ RouterError, |
59 |
| -+ } |
60 |
| -+ |
61 |
| -+ #[pallet::call] |
62 |
| -+ impl<T: Config> Pallet<T> { |
63 |
| -+ /// Execute an XCM from a local, signed, origin. |
64 |
| -+ #[pallet::call_index(0)] |
65 |
| -+ #[pallet::weight(Weight::default())] |
66 |
| -+ pub fn execute( |
67 |
| -+ origin: OriginFor<T>, |
68 |
| -+ message: Box<VersionedXcm<()>>, |
69 |
| -+ _max_weight: Weight, |
70 |
| -+ ) -> DispatchResult { |
71 |
| -+ let message = (*message).try_into().map_err(|()| Error::<T>::BadVersion)?; |
72 |
| -+ // Actually execute the XCM. |
73 |
| -+ Self::do_execute(origin, message) |
74 |
| -+ } |
75 |
| -+ |
76 |
| -+ /// Send an XCM to another consensus system. |
77 |
| -+ #[pallet::call_index(1)] |
78 |
| -+ #[pallet::weight(Weight::default())] |
79 |
| -+ pub fn send( |
80 |
| -+ origin: OriginFor<T>, |
81 |
| -+ dest: Box<VersionedLocation>, |
82 |
| -+ message: Box<VersionedXcm<()>>, |
83 |
| -+ ) -> DispatchResult { |
84 |
| -+ let dest = Location::try_from(*dest).map_err(|()| Error::<T>::BadVersion)?; |
85 |
| -+ let message: Xcm<()> = (*message).try_into().map_err(|()| Error::<T>::BadVersion)?; |
86 |
| -+ // Actually send the XCM. |
87 |
| -+ Self::do_send(origin, dest, message) |
88 |
| -+ } |
89 |
| -+ |
90 |
| -+ /// Teleport some assets from the local chain to some destination chain. |
91 |
| -+ #[pallet::call_index(2)] |
92 |
| -+ #[pallet::weight(Weight::default())] |
93 |
| -+ pub fn teleport_assets( |
94 |
| -+ origin: OriginFor<T>, |
95 |
| -+ dest: Box<VersionedLocation>, |
96 |
| -+ beneficiary: Box<VersionedLocation>, |
97 |
| -+ assets: Box<VersionedAssets>, |
98 |
| -+ fee_asset_item: u32, |
99 |
| -+ ) -> DispatchResult { |
100 |
| -+ let dest: Location = (*dest).try_into().map_err(|()| Error::<T>::BadVersion)?; |
101 |
| -+ let beneficiary: Location = |
102 |
| -+ (*beneficiary).try_into().map_err(|()| Error::<T>::BadVersion)?; |
103 |
| -+ let assets: Assets = (*assets).try_into().map_err(|()| Error::<T>::BadVersion)?; |
104 |
| -+ // Actually teleport the assets. |
105 |
| -+ Self::do_teleport_assets(origin, dest, beneficiary, assets, fee_asset_item) |
106 |
| -+ } |
107 |
| -+ |
108 |
| -+ /// Transfer some assets from the local chain to the destination chain through their local, |
109 |
| -+ /// destination or remote reserve. |
110 |
| -+ #[pallet::call_index(3)] |
111 |
| -+ #[pallet::weight(Weight::default())] |
112 |
| -+ pub fn reserve_transfer_assets( |
113 |
| -+ origin: OriginFor<T>, |
114 |
| -+ dest: Box<VersionedLocation>, |
115 |
| -+ beneficiary: Box<VersionedLocation>, |
116 |
| -+ assets: Box<VersionedAssets>, |
117 |
| -+ fee_asset_item: u32, |
118 |
| -+ ) -> DispatchResult { |
119 |
| -+ let dest: Location = (*dest).try_into().map_err(|()| Error::<T>::BadVersion)?; |
120 |
| -+ let beneficiary: Location = |
121 |
| -+ (*beneficiary).try_into().map_err(|()| Error::<T>::BadVersion)?; |
122 |
| -+ let assets: Assets = (*assets).try_into().map_err(|()| Error::<T>::BadVersion)?; |
123 |
| -+ // Actually reserve transfer the assets. |
124 |
| -+ Self::do_reserve_transfer_assets(origin, dest, beneficiary, assets, fee_asset_item) |
125 |
| -+ } |
126 |
| -+ } |
127 |
| -+} |
128 |
| -+ |
129 |
| -+impl<T: Config> Pallet<T> { |
130 |
| -+ pub fn do_execute(origin: OriginFor<T>, message: Xcm<()>) -> DispatchResult { |
131 |
| -+ todo!("{:?}", message) |
132 |
| -+ } |
133 |
| -+ |
134 |
| -+ /// Relay an XCM `message` from a given `interior` location in this context to a given `dest` |
135 |
| -+ /// location. |
136 |
| -+ pub fn do_send(origin: OriginFor<T>, dest: Location, mut message: Xcm<()>) -> DispatchResult { |
137 |
| -+ todo!("{:?} {:?}", dest, message) |
138 |
| -+ } |
139 |
| -+ |
140 |
| -+ pub fn do_teleport_assets( |
141 |
| -+ origin: OriginFor<T>, |
142 |
| -+ dest: Location, |
143 |
| -+ beneficiary: Location, |
144 |
| -+ assets: Assets, |
145 |
| -+ // The index into `assets` of the item which should be used to pay fees. |
146 |
| -+ // We don't use this in our naive implementation. |
147 |
| -+ _fee_asset_item: u32, |
148 |
| -+ ) -> DispatchResult { |
149 |
| -+ todo!("{:?} {:?} {:?}", dest, beneficiary, assets) |
150 |
| -+ } |
151 |
| -+ |
152 |
| -+ pub fn do_reserve_transfer_assets( |
153 |
| -+ _origin: OriginFor<T>, |
154 |
| -+ _dest: Location, |
155 |
| -+ _beneficiary: Location, |
156 |
| -+ _assets: Assets, |
157 |
| -+ _fee_asset_item: u32, |
158 |
| -+ ) -> DispatchResult { |
159 |
| -+ // There are 3 different reserve transfer scenarios: |
160 |
| -+ // - A local reserve transfer: reserve-transfer `asset` to `dest`, using local chain as |
161 |
| -+ // reserve. |
162 |
| -+ // - A destination reserve transfer: reserve-transfer `asset` to `dest`, using `dest` as |
163 |
| -+ // reserve. |
164 |
| -+ // - A remote reserve transfer: reserve-transfer `asset` to `dest`, using remote chain |
165 |
| -+ // `Location` as reserve. |
166 |
| -+ // |
167 |
| -+ // This is a lot to do in this workshop, but a welcome challenge for the reader to |
168 |
| -+ // implement. |
169 |
| -+ unimplemented!() |
170 |
| -+ } |
171 |
| -+} |
0 commit comments