-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathxcm_api.rs
289 lines (249 loc) · 9.92 KB
/
xcm_api.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// This file is part of Astar.
// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.
use crate::setup::*;
use cumulus_primitives_core::Unlimited;
use sp_runtime::traits::{BlakeTwo256, Hash, Zero};
use xcm::{
v4::{
Asset as XcmAsset, AssetId as XcmAssetId, Fungibility, Junction, Junctions::*, Location,
Xcm, VERSION as V_4,
},
VersionedLocation, VersionedXcm,
};
use xcm_runtime_apis::dry_run::runtime_decl_for_dry_run_api::DryRunApiV1;
use xcm_runtime_apis::fees::runtime_decl_for_xcm_payment_api::XcmPaymentApiV1;
/// Register an asset into `pallet-assets` instance, and register as as cross-chain asset.
///
/// If specified, also set _units-per-second_ to make the asset _payable_.
fn prepare_asset(asset_id: u128, location: VersionedLocation, units_per_second: Option<u128>) {
// 1. Create an asset representation
assert_ok!(Assets::force_create(
RuntimeOrigin::root(),
asset_id.into(),
MultiAddress::Id(ALICE),
true,
1
));
// 2. Register its location & bind it to the registered asset representation
assert_ok!(XcAssetConfig::register_asset_location(
RuntimeOrigin::root(),
Box::new(location.clone()),
asset_id.into(),
));
// 3. Maybe set the units per second
if let Some(units_per_second) = units_per_second {
assert_ok!(XcAssetConfig::set_asset_units_per_second(
RuntimeOrigin::root(),
Box::new(location),
units_per_second.into()
));
}
}
#[test]
fn query_acceptable_payment_assets_is_ok() {
new_test_ext().execute_with(|| {
// 0. Sanity check for unsupported version
{
assert!(Runtime::query_acceptable_payment_assets(2).is_err());
}
// 1. First check the return values without any foreign asset registered.
{
let assets = Runtime::query_acceptable_payment_assets(V_4)
.expect("Must return at least native currency.");
assert_eq!(assets, vec![XcmAssetId(Location::here()).into()]);
}
// 2. Register two foreign assets - one payable, one not.
// Expect native asset & payable asset to be returned.
{
let payable_location = Location::new(1, Here);
let non_payable_location = Location::new(1, Junction::Parachain(2));
prepare_asset(1, payable_location.clone().into_versioned(), Some(1000));
prepare_asset(2, non_payable_location.clone().into_versioned(), None);
let assets = Runtime::query_acceptable_payment_assets(V_4)
.expect("Must return at least native currency.");
assert_eq!(assets.len(), 2);
assert!(assets.contains(&XcmAssetId(Location::here()).into()));
assert!(assets.contains(&XcmAssetId(payable_location).into()));
}
})
}
#[test]
fn query_weight_to_asset_fee_is_ok() {
new_test_ext().execute_with(|| {
// 0. Sanity check for unsupported asset
{
let non_payable_location = Location::new(1, Here);
assert!(Runtime::query_weight_to_asset_fee(
Weight::from_parts(1000, 1000),
XcmAssetId(non_payable_location.clone()).into(),
)
.is_err());
prepare_asset(1, non_payable_location.clone().into_versioned(), None);
assert!(Runtime::query_weight_to_asset_fee(
Weight::from_parts(1000, 1000),
XcmAssetId(non_payable_location).into(),
)
.is_err());
}
// 1. Native asset payment
{
let weight = Weight::from_parts(1000, 1000);
let expected_fee = WeightToFee::weight_to_fee(&weight);
let fee =
Runtime::query_weight_to_asset_fee(weight, XcmAssetId(Location::here()).into())
.expect("Must return fee for native asset.");
assert_eq!(
fee, expected_fee,
"Fee must match the expected weight-to-fee conversion."
);
}
// 2. Foreign asset payment
{
let payable_location = Location::new(2, Here);
let units_per_second = 1_000_000_000_000;
prepare_asset(
2,
payable_location.clone().into_versioned(),
Some(units_per_second),
);
let weight = Weight::from_parts(1_000_000_000, 1_000_000);
let expected_fee = XcAssetConfig::weight_to_fee(weight, units_per_second);
let fee =
Runtime::query_weight_to_asset_fee(weight, XcmAssetId(payable_location).into())
.expect("Must return fee for payable asset.");
assert_eq!(
fee, expected_fee,
"Fee must match the expected weight-to-fee conversion."
);
}
})
}
#[test]
fn query_xcm_weight_is_ok() {
new_test_ext().execute_with(|| {
let native_asset: XcmAsset =
XcmAssetId(Location::here()).into_asset(Fungibility::Fungible(1_000_000_000));
// Prepare an xcm sequence
let xcm_sequence = Xcm::<()>::builder_unsafe()
.withdraw_asset(native_asset.clone())
.deposit_asset(
native_asset,
Junction::AccountId32 {
network: None,
id: BOB.clone().into(),
},
)
.build();
let weight =
Runtime::query_xcm_weight(VersionedXcm::V4(xcm_sequence)).expect("Must return weight.");
assert!(
!weight.is_zero(),
"Weight must be non-zero since we're performing asset withdraw & deposit."
);
})
}
#[test]
fn query_delivery_fees_is_ok() {
new_test_ext().execute_with(|| {
let location = Location::new(1, Here).into_versioned();
// Prepare a dummy xcm sequence
let xcm_sequence = Xcm::<()>::builder_unsafe()
.clear_error()
.unsubscribe_version()
.build();
// TODO: this is something we should revisit
assert!(
Runtime::query_delivery_fees(location, VersionedXcm::V4(xcm_sequence)).is_err(),
"At the moment, `PriceForMessageDelivery` is not implemented."
);
})
}
#[test]
fn dry_run_call_is_ok() {
new_test_ext().execute_with(|| {
let origin = OriginCaller::system(frame_system::RawOrigin::Signed(ALICE.clone()).into());
// TODO: Improve this test using an XCM call with more side effects and compare local_xcm with recorded one to get ride of `xcm_recorder_configuration_is_ok` test
let call = RuntimeCall::System(frame_system::Call::remark_with_event {
remark: vec![0u8; 32],
});
let result = Runtime::dry_run_call(origin, call).expect("Must return some effects.");
assert_eq!(result.forwarded_xcms, vec![]);
assert_eq!(
result.emitted_events[0],
RuntimeEvent::System(frame_system::Event::Remarked {
sender: ALICE.into(),
hash: BlakeTwo256::hash_of(&[0u8; 32]).into(),
}),
);
})
}
#[test]
fn dry_run_xcm_is_ok() {
new_test_ext().execute_with(|| {
let transfer_amount = 10_000 * UNIT;
let native_asset: XcmAsset =
XcmAssetId(Location::here()).into_asset(Fungibility::Fungible(transfer_amount.clone()));
// Prepare an xcm sequence
let xcm_sequence = Xcm::<()>::builder_unsafe()
.withdraw_asset(native_asset.clone())
.clear_origin()
.buy_execution((Here, 1 * UNIT), Unlimited) // TODO: This can be improved by estimating real execution fees
.deposit_asset(
native_asset,
Junction::AccountId32 {
network: None,
id: BOB.clone().into(),
},
)
.build();
// ALICE location origin
let origin_location = VersionedLocation::V4(
Junction::AccountId32 {
id: ALICE.into(),
network: None,
}
.into(),
);
let dummy_message =
Xcm::<RuntimeCall>::from(VersionedXcm::V4(xcm_sequence).try_into().unwrap());
let versioned_xcm = VersionedXcm::V4(dummy_message);
let result = Runtime::dry_run_xcm(origin_location, versioned_xcm)
.expect("Must return some effects.");
assert_eq!(result.forwarded_xcms, vec![]);
assert_eq!(
result.emitted_events[0],
RuntimeEvent::Balances(pallet_balances::Event::Burned {
who: ALICE.into(),
amount: transfer_amount
}),
);
})
}
#[test]
fn xcm_recorder_configuration_is_ok() {
use xcm_executor::RecordXcm;
new_test_ext().execute_with(|| {
let result = <xcm_config::XcmConfig as xcm_executor::Config>::XcmRecorder::should_record();
assert!(
!result,
"XCM recorder should NOT record incoming XCMs by default."
);
<xcm_config::XcmConfig as xcm_executor::Config>::XcmRecorder::set_record_xcm(true);
let result = <xcm_config::XcmConfig as xcm_executor::Config>::XcmRecorder::should_record();
assert!(
result,
"XCM recorder must be ready to record incoming XCMs."
);
})
}