substrate_api_client/api/runtime_api/
transaction_payment.rs

1/*
2   Copyright 2024 Supercomputing Systems AG
3   Licensed under the Apache License, Version 2.0 (the "License");
4   you may not use this file except in compliance with the License.
5   You may obtain a copy of the License at
6	   http://www.apache.org/licenses/LICENSE-2.0
7   Unless required by applicable law or agreed to in writing, software
8   distributed under the License is distributed on an "AS IS" BASIS,
9   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10   See the License for the specific language governing permissions and
11   limitations under the License.
12*/
13
14use super::{RuntimeApi, RuntimeApiClient};
15use crate::{api::Result, rpc::Request};
16use ac_primitives::{config::Config, FeeDetails, RuntimeDispatchInfo, UncheckedExtrinsic, Weight};
17#[cfg(all(not(feature = "sync-api"), not(feature = "std")))]
18use alloc::boxed::Box;
19use alloc::{vec, vec::Vec};
20use sp_core::Encode;
21
22#[maybe_async::maybe_async(?Send)]
23pub trait TransactionPaymentApi: RuntimeApi {
24	type FeeDetails;
25	type RuntimeDispatchInfo;
26	type Balance;
27	type Weight;
28
29	/// Query the transaction fee details.
30	async fn query_fee_details<Address, Call, Signature, TransactionExtension>(
31		&self,
32		extrinsic: UncheckedExtrinsic<Address, Call, Signature, TransactionExtension>,
33		length: u32,
34		at_block: Option<Self::Hash>,
35	) -> Result<Self::FeeDetails>
36	where
37		Address: Encode,
38		Call: Encode,
39		Signature: Encode,
40		TransactionExtension: Encode;
41
42	/// Query the transaction fee details of opaque extrinsic.
43	async fn query_fee_details_opaque(
44		&self,
45		extrinsic: Vec<u8>,
46		length: u32,
47		at_block: Option<Self::Hash>,
48	) -> Result<Self::FeeDetails>;
49
50	/// Query the transaction fee info.
51	async fn query_info<Address, Call, Signature, TransactionExtension>(
52		&self,
53		extrinsic: UncheckedExtrinsic<Address, Call, Signature, TransactionExtension>,
54		length: u32,
55		at_block: Option<Self::Hash>,
56	) -> Result<Self::RuntimeDispatchInfo>
57	where
58		Address: Encode,
59		Call: Encode,
60		Signature: Encode,
61		TransactionExtension: Encode;
62
63	/// Query the transaction info of opaque extrinsic.
64	async fn query_info_opaque(
65		&self,
66		extrinsic: Vec<u8>,
67		length: u32,
68		at_block: Option<Self::Hash>,
69	) -> Result<Self::RuntimeDispatchInfo>;
70
71	/// Query the output of the current LengthToFee given some input.
72	async fn query_length_to_fee(
73		&self,
74		length: u32,
75		at_block: Option<Self::Hash>,
76	) -> Result<Self::Balance>;
77
78	/// Query the output of the current WeightToFee given some input.
79	async fn query_weight_to_fee(
80		&self,
81		weight: Self::Weight,
82		at_block: Option<Self::Hash>,
83	) -> Result<Self::Balance>;
84}
85
86#[maybe_async::maybe_async(?Send)]
87impl<T, Client> TransactionPaymentApi for RuntimeApiClient<T, Client>
88where
89	T: Config,
90	Client: Request,
91{
92	type FeeDetails = FeeDetails<T::Balance>;
93	type RuntimeDispatchInfo = RuntimeDispatchInfo<T::Balance>;
94	type Balance = T::Balance;
95	type Weight = Weight;
96
97	async fn query_fee_details<Address, Call, Signature, TransactionExtension>(
98		&self,
99		extrinsic: UncheckedExtrinsic<Address, Call, Signature, TransactionExtension>,
100		length: u32,
101		at_block: Option<Self::Hash>,
102	) -> Result<Self::FeeDetails>
103	where
104		Address: Encode,
105		Call: Encode,
106		Signature: Encode,
107		TransactionExtension: Encode,
108	{
109		self.query_fee_details_opaque(extrinsic.encode(), length, at_block).await
110	}
111
112	async fn query_fee_details_opaque(
113		&self,
114		extrinsic: Vec<u8>,
115		length: u32,
116		at_block: Option<Self::Hash>,
117	) -> Result<Self::FeeDetails> {
118		self.runtime_call(
119			"TransactionPaymentApi_query_fee_details",
120			vec![extrinsic, length.encode()],
121			at_block,
122		)
123		.await
124	}
125
126	async fn query_info<Address, Call, Signature, TransactionExtension>(
127		&self,
128		extrinsic: UncheckedExtrinsic<Address, Call, Signature, TransactionExtension>,
129		length: u32,
130		at_block: Option<Self::Hash>,
131	) -> Result<Self::RuntimeDispatchInfo>
132	where
133		Address: Encode,
134		Call: Encode,
135		Signature: Encode,
136		TransactionExtension: Encode,
137	{
138		self.query_info_opaque(extrinsic.encode(), length, at_block).await
139	}
140
141	async fn query_info_opaque(
142		&self,
143		extrinsic: Vec<u8>,
144		length: u32,
145		at_block: Option<Self::Hash>,
146	) -> Result<Self::RuntimeDispatchInfo> {
147		self.runtime_call(
148			"TransactionPaymentApi_query_info",
149			vec![extrinsic, length.encode()],
150			at_block,
151		)
152		.await
153	}
154
155	async fn query_length_to_fee(
156		&self,
157		length: u32,
158		at_block: Option<Self::Hash>,
159	) -> Result<Self::Balance> {
160		self.runtime_call(
161			"TransactionPaymentApi_query_length_to_fee",
162			vec![length.encode()],
163			at_block,
164		)
165		.await
166	}
167
168	async fn query_weight_to_fee(
169		&self,
170		weight: Self::Weight,
171		at_block: Option<Self::Hash>,
172	) -> Result<Self::Balance> {
173		self.runtime_call(
174			"TransactionPaymentApi_query_weight_to_fee",
175			vec![weight.encode()],
176			at_block,
177		)
178		.await
179	}
180}