1
// Copyright (C) Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
3

            
4
// Substrate 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
// Substrate 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 getting XCM fees.
18

            
19
use alloc::vec::Vec;
20
use codec::{Decode, Encode};
21
use frame_support::pallet_prelude::TypeInfo;
22
use sp_weights::Weight;
23
use xcm::{Version, VersionedAssetId, VersionedAssets, VersionedLocation, VersionedXcm};
24

            
25
sp_api::decl_runtime_apis! {
26
	/// A trait of XCM payment API.
27
	///
28
	/// API provides functionality for obtaining:
29
	///
30
	/// * the weight required to execute an XCM message,
31
	/// * a list of acceptable `AssetId`s for message execution payment,
32
	/// * the cost of the weight in the specified acceptable `AssetId`.
33
	/// * the fees for an XCM message delivery.
34
	///
35
	/// To determine the execution weight of the calls required for
36
	/// [`xcm::latest::Instruction::Transact`] instruction, `TransactionPaymentCallApi` can be used.
37
	pub trait XcmPaymentApi {
38
		/// Returns a list of acceptable payment assets.
39
		///
40
		/// # Arguments
41
		///
42
		/// * `xcm_version`: Version.
43
		fn query_acceptable_payment_assets(xcm_version: Version) -> Result<Vec<VersionedAssetId>, Error>;
44

            
45
		/// Returns a weight needed to execute a XCM.
46
		///
47
		/// # Arguments
48
		///
49
		/// * `message`: `VersionedXcm`.
50
		fn query_xcm_weight(message: VersionedXcm<()>) -> Result<Weight, Error>;
51

            
52
		/// Converts a weight into a fee for the specified `AssetId`.
53
		///
54
		/// # Arguments
55
		///
56
		/// * `weight`: convertible `Weight`.
57
		/// * `asset`: `VersionedAssetId`.
58
		fn query_weight_to_asset_fee(weight: Weight, asset: VersionedAssetId) -> Result<u128, Error>;
59

            
60
		/// Get delivery fees for sending a specific `message` to a `destination`.
61
		/// These always come in a specific asset, defined by the chain.
62
		///
63
		/// # Arguments
64
		/// * `message`: The message that'll be sent, necessary because most delivery fees are based on the
65
		///   size of the message.
66
		/// * `destination`: The destination to send the message to. Different destinations may use
67
		///   different senders that charge different fees.
68
		fn query_delivery_fees(destination: VersionedLocation, message: VersionedXcm<()>) -> Result<VersionedAssets, Error>;
69
	}
70
}
71

            
72
#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo)]
73
pub enum Error {
74
	/// An API part is unsupported.
75
	#[codec(index = 0)]
76
	Unimplemented,
77

            
78
	/// Converting a versioned data structure from one version to another failed.
79
	#[codec(index = 1)]
80
	VersionedConversionFailed,
81

            
82
	/// XCM message weight calculation failed.
83
	#[codec(index = 2)]
84
	WeightNotComputable,
85

            
86
	/// XCM version not able to be handled.
87
	#[codec(index = 3)]
88
	UnhandledXcmVersion,
89

            
90
	/// The given asset is not handled as a fee asset.
91
	#[codec(index = 4)]
92
	AssetNotFound,
93

            
94
	/// Destination is known to be unroutable.
95
	#[codec(index = 5)]
96
	Unroutable,
97
}