1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use subxt::ext::sp_runtime::MultiAddress;
use crate::{
aleph_zero::{self, api, api::runtime_types::pallet_balances::BalanceLock},
connections::TxInfo,
pallet_balances::pallet::Call::transfer,
pallets::utility::UtilityApi,
AccountId, Balance, BlockHash,
Call::Balances,
ConnectionApi, ParamsBuilder, SignedConnectionApi, TxStatus,
};
/// Pallet balances read-only API.
#[async_trait::async_trait]
pub trait BalanceApi {
/// API for [`locks`](https://paritytech.github.io/substrate/master/pallet_balances/pallet/struct.Pallet.html#method.locks) call.
/// * `account` - an account to query locked balance for
/// * `at` - optional hash of a block to query state from
async fn locks_for_account(
&self,
account: AccountId,
at: Option<BlockHash>,
) -> Vec<BalanceLock<Balance>>;
/// API for [`locks`](https://paritytech.github.io/substrate/master/pallet_balances/pallet/struct.Pallet.html#method.locks) call.
/// * `accounts` - a list of accounts to query locked balance for
/// * `at` - optional hash of a block to query state from
async fn locks(
&self,
accounts: &[AccountId],
at: Option<BlockHash>,
) -> Vec<Vec<BalanceLock<Balance>>>;
/// Returns [`total_issuance`](https://paritytech.github.io/substrate/master/pallet_balances/pallet/type.TotalIssuance.html).
async fn total_issuance(&self, at: Option<BlockHash>) -> Balance;
}
/// Pallet balances API
#[async_trait::async_trait]
pub trait BalanceUserApi {
/// API for [`transfer`](https://paritytech.github.io/substrate/master/pallet_balances/pallet/struct.Pallet.html#method.transfer) call.
async fn transfer(
&self,
dest: AccountId,
amount: Balance,
status: TxStatus,
) -> anyhow::Result<TxInfo>;
/// API for [`transfer`](https://paritytech.github.io/substrate/master/pallet_balances/pallet/struct.Pallet.html#method.transfer) call.
/// Include tip in the tx.
async fn transfer_with_tip(
&self,
dest: AccountId,
amount: Balance,
tip: Balance,
status: TxStatus,
) -> anyhow::Result<TxInfo>;
}
/// Pallet balances logic not directly related to any pallet call.
#[async_trait::async_trait]
pub trait BalanceUserBatchExtApi {
/// Performs batch of `balances.transfer` calls.
/// * `dest` - a list of accounts to send tokens to
/// * `amount` - an amount to transfer
/// * `status` - a [`TxStatus`] for a tx to wait for
///
/// # Examples
/// ```ignore
/// for chunk in stash_accounts.chunks(1024) {
/// connection
/// .batch_transfer(chunk, 1_000_000_000_000u128, TxStatus::InBlock)
/// .await
/// .unwrap();
/// }
/// ```
async fn batch_transfer(
&self,
dest: &[AccountId],
amount: Balance,
status: TxStatus,
) -> anyhow::Result<TxInfo>;
}
#[async_trait::async_trait]
impl<C: ConnectionApi> BalanceApi for C {
async fn locks_for_account(
&self,
account: AccountId,
at: Option<BlockHash>,
) -> Vec<BalanceLock<Balance>> {
let address = aleph_zero::api::storage().balances().locks(&account);
self.get_storage_entry(&address, at).await.0
}
async fn locks(
&self,
accounts: &[AccountId],
at: Option<BlockHash>,
) -> Vec<Vec<BalanceLock<Balance>>> {
let mut locks = vec![];
for account in accounts {
locks.push(self.locks_for_account(account.clone(), at).await);
}
locks
}
async fn total_issuance(&self, at: Option<BlockHash>) -> Balance {
let address = api::storage().balances().total_issuance();
self.get_storage_entry(&address, at).await
}
}
#[async_trait::async_trait]
impl<S: SignedConnectionApi> BalanceUserApi for S {
async fn transfer(
&self,
dest: AccountId,
amount: Balance,
status: TxStatus,
) -> anyhow::Result<TxInfo> {
let tx = api::tx()
.balances()
.transfer(MultiAddress::Id(dest), amount);
self.send_tx(tx, status).await
}
async fn transfer_with_tip(
&self,
dest: AccountId,
amount: Balance,
tip: Balance,
status: TxStatus,
) -> anyhow::Result<TxInfo> {
let tx = api::tx()
.balances()
.transfer(MultiAddress::Id(dest), amount);
self.send_tx_with_params(tx, ParamsBuilder::new().tip(tip), status)
.await
}
}
#[async_trait::async_trait]
impl<S: SignedConnectionApi> BalanceUserBatchExtApi for S {
async fn batch_transfer(
&self,
dests: &[AccountId],
amount: Balance,
status: TxStatus,
) -> anyhow::Result<TxInfo> {
let calls = dests
.iter()
.map(|dest| {
Balances(transfer {
dest: MultiAddress::Id(dest.clone()),
value: amount,
})
})
.collect();
self.batch_call(calls, status).await
}
}