xcm_runtime_apis/dry_run.rs
1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
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}