substrate_api_client/extrinsic/
utility.rs

1/*
2   Copyright 2019 Supercomputing Systems AG
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8	   http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15
16*/
17
18//! Extrinsics for `pallet-utility`.
19//! https://polkadot.js.org/docs/substrate/extrinsics/#utility
20
21use crate::{rpc::Request, Api};
22use ac_compose_macros::compose_extrinsic;
23use ac_primitives::{
24	config::Config, extrinsic_params::ExtrinsicParams, extrinsics::CallIndex, SignExtrinsic,
25	UncheckedExtrinsic,
26};
27#[cfg(all(not(feature = "sync-api"), not(feature = "std")))]
28use alloc::boxed::Box;
29use alloc::vec::Vec;
30use codec::{Decode, Encode};
31
32const UTILITY_MODULE: &str = "Utility";
33const BATCH: &str = "batch";
34const FORCE_BATCH: &str = "force_batch";
35
36#[derive(Clone, Eq, PartialEq, Encode, Decode, Debug)]
37pub struct Batch<Call> {
38	pub calls: Vec<Call>,
39}
40
41pub type BatchCall<Call> = (CallIndex, Batch<Call>);
42
43#[maybe_async::maybe_async(?Send)]
44pub trait UtilityExtrinsics {
45	type Extrinsic<Call>;
46
47	// Send a batch of dispatch calls.
48	async fn batch<Call: Encode + Clone>(
49		&self,
50		calls: Vec<Call>,
51	) -> Option<Self::Extrinsic<BatchCall<Call>>>;
52
53	// Send a batch of dispatch calls. Unlike batch, it allows errors and won't interrupt.
54	async fn force_batch<Call: Encode + Clone>(
55		&self,
56		calls: Vec<Call>,
57	) -> Option<Self::Extrinsic<BatchCall<Call>>>;
58}
59
60#[maybe_async::maybe_async(?Send)]
61impl<T, Client> UtilityExtrinsics for Api<T, Client>
62where
63	T: Config,
64	Client: Request,
65{
66	type Extrinsic<Call> = UncheckedExtrinsic<
67		<T::ExtrinsicSigner as SignExtrinsic<T::AccountId>>::ExtrinsicAddress,
68		Call,
69		<T::ExtrinsicSigner as SignExtrinsic<T::AccountId>>::Signature,
70		<T::ExtrinsicParams as ExtrinsicParams<T::Index, T::Hash>>::TxExtension,
71	>;
72
73	async fn batch<Call: Encode + Clone>(
74		&self,
75		calls: Vec<Call>,
76	) -> Option<Self::Extrinsic<BatchCall<Call>>> {
77		let calls = Batch { calls };
78		compose_extrinsic!(self, UTILITY_MODULE, BATCH, calls)
79	}
80
81	async fn force_batch<Call: Encode + Clone>(
82		&self,
83		calls: Vec<Call>,
84	) -> Option<Self::Extrinsic<BatchCall<Call>>> {
85		let calls = Batch { calls };
86		compose_extrinsic!(self, UTILITY_MODULE, FORCE_BATCH, calls)
87	}
88}