substrate_api_client/extrinsic/
balances.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-balances`.
19//! https://polkadot.js.org/docs/substrate/extrinsics/#balances
20
21use crate::{api::Api, rpc::Request};
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 codec::{Compact, Encode};
30
31pub const BALANCES_MODULE: &str = "Balances";
32pub const TRANSFER_ALLOW_DEATH: &str = "transfer_allow_death";
33pub const TRANSFER_KEEP_ALIVE: &str = "transfer_keep_alive";
34pub const FORCE_SET_BALANCE: &str = "force_set_balance";
35
36/// Call for a balance transfer.
37pub type TransferAllowDeathCall<Address, Balance> = (CallIndex, Address, Compact<Balance>);
38
39/// Call for a balance transfer.
40pub type TransferKeepAliveCall<Address, Balance> = (CallIndex, Address, Compact<Balance>);
41
42/// Call to the balance of an account.
43pub type ForceSetBalanceCall<Address, Balance> = (CallIndex, Address, Compact<Balance>);
44
45#[maybe_async::maybe_async(?Send)]
46pub trait BalancesExtrinsics {
47	type Balance;
48	type Address;
49	type Extrinsic<Call>;
50
51	/// Transfer some liquid free balance to another account.
52	#[allow(clippy::type_complexity)]
53	async fn balance_transfer_allow_death(
54		&self,
55		to: Self::Address,
56		amount: Self::Balance,
57	) -> Option<Self::Extrinsic<TransferAllowDeathCall<Self::Address, Self::Balance>>>;
58
59	/// Transfer some liquid free balance to another account.
60	#[allow(clippy::type_complexity)]
61	async fn balance_transfer_keep_alive(
62		&self,
63		to: Self::Address,
64		amount: Self::Balance,
65	) -> Option<Self::Extrinsic<TransferKeepAliveCall<Self::Address, Self::Balance>>>;
66
67	///  Set the balances of a given account.
68	#[allow(clippy::type_complexity)]
69	async fn balance_force_set_balance(
70		&self,
71		who: Self::Address,
72		free_balance: Self::Balance,
73	) -> Option<Self::Extrinsic<ForceSetBalanceCall<Self::Address, Self::Balance>>>;
74}
75
76#[maybe_async::maybe_async(?Send)]
77impl<T, Client> BalancesExtrinsics for Api<T, Client>
78where
79	T: Config,
80	Client: Request,
81	Compact<T::Balance>: Encode,
82{
83	type Balance = T::Balance;
84	type Address = <T::ExtrinsicSigner as SignExtrinsic<T::AccountId>>::ExtrinsicAddress;
85	type Extrinsic<Call> = UncheckedExtrinsic<
86		Self::Address,
87		Call,
88		<T::ExtrinsicSigner as SignExtrinsic<T::AccountId>>::Signature,
89		<T::ExtrinsicParams as ExtrinsicParams<T::Index, T::Hash>>::TxExtension,
90	>;
91
92	async fn balance_transfer_allow_death(
93		&self,
94		to: Self::Address,
95		amount: Self::Balance,
96	) -> Option<Self::Extrinsic<TransferAllowDeathCall<Self::Address, Self::Balance>>> {
97		compose_extrinsic!(self, BALANCES_MODULE, TRANSFER_ALLOW_DEATH, to, Compact(amount))
98	}
99
100	#[allow(clippy::type_complexity)]
101	async fn balance_transfer_keep_alive(
102		&self,
103		to: Self::Address,
104		amount: Self::Balance,
105	) -> Option<Self::Extrinsic<TransferKeepAliveCall<Self::Address, Self::Balance>>> {
106		compose_extrinsic!(self, BALANCES_MODULE, TRANSFER_KEEP_ALIVE, to, Compact(amount))
107	}
108
109	async fn balance_force_set_balance(
110		&self,
111		who: Self::Address,
112		free_balance: Self::Balance,
113	) -> Option<Self::Extrinsic<ForceSetBalanceCall<Self::Address, Self::Balance>>> {
114		compose_extrinsic!(self, BALANCES_MODULE, FORCE_SET_BALANCE, who, Compact(free_balance))
115	}
116}