xcm_runtime_apis/dry_run.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Runtime API definition for dry-running XCM-related extrinsics.
18//! This API can be used to simulate XCMs and, for example, find the fees
19//! that need to be paid.
20
21use alloc::vec::Vec;
22use codec::{Decode, Encode};
23use frame_support::pallet_prelude::{DispatchResultWithPostInfo, TypeInfo};
24use xcm::prelude::*;
25
26/// Effects of dry-running an extrinsic.
27#[derive(Encode, Decode, Debug, TypeInfo)]
28pub struct CallDryRunEffects<Event> {
29 /// The result of executing the extrinsic.
30 pub execution_result: DispatchResultWithPostInfo,
31 /// The list of events fired by the extrinsic.
32 pub emitted_events: Vec<Event>,
33 /// The local XCM that was attempted to be executed, if any.
34 pub local_xcm: Option<VersionedXcm<()>>,
35 /// The list of XCMs that were queued for sending.
36 pub forwarded_xcms: Vec<(VersionedLocation, Vec<VersionedXcm<()>>)>,
37}
38
39/// Effects of dry-running an XCM program.
40#[derive(Encode, Decode, Debug, TypeInfo)]
41pub struct XcmDryRunEffects<Event> {
42 /// The outcome of the XCM program execution.
43 pub execution_result: Outcome,
44 /// List of events fired by the XCM program execution.
45 pub emitted_events: Vec<Event>,
46 /// List of queued messages for sending.
47 pub forwarded_xcms: Vec<(VersionedLocation, Vec<VersionedXcm<()>>)>,
48}
49
50sp_api::decl_runtime_apis! {
51 /// API for dry-running extrinsics and XCM programs to get the programs that need to be passed to the fees API.
52 ///
53 /// All calls return a vector of tuples (location, xcm) where each "xcm" is executed in "location".
54 /// If there's local execution, the location will be "Here".
55 /// This vector can be used to calculate both execution and delivery fees.
56 ///
57 /// Calls or XCMs might fail when executed, this doesn't mean the result of these calls will be an `Err`.
58 /// In those cases, there might still be a valid result, with the execution error inside it.
59 /// The only reasons why these calls might return an error are listed in the [`Error`] enum.
60 #[api_version(2)]
61 pub trait DryRunApi<Call, Event, OriginCaller>
62 where
63 Call: Encode,
64 Event: Decode,
65 OriginCaller: Encode
66 {
67 /// Dry run call V2.
68 fn dry_run_call(origin: OriginCaller, call: Call, result_xcms_version: XcmVersion) -> Result<CallDryRunEffects<Event>, Error>;
69
70 /// Dry run call V1.
71 #[changed_in(2)]
72 fn dry_run_call(origin: OriginCaller, call: Call) -> Result<CallDryRunEffects<Event>, Error>;
73
74 /// Dry run XCM program
75 fn dry_run_xcm(origin_location: VersionedLocation, xcm: VersionedXcm<Call>) -> Result<XcmDryRunEffects<Event>, Error>;
76 }
77}
78
79#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo)]
80pub enum Error {
81 /// An API call is unsupported.
82 #[codec(index = 0)]
83 Unimplemented,
84
85 /// Converting a versioned data structure from one version to another failed.
86 #[codec(index = 1)]
87 VersionedConversionFailed,
88}