substrate_api_client/api/rpc_api/
frame_system.rs

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
   Copyright 2019 Supercomputing Systems AG
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
	   http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

//! Interface to common frame system pallet information.

use crate::{
	api::{Api, GetStorage, Result},
	rpc::Request,
};
use ac_compose_macros::rpc_params;
use ac_primitives::{config::Config, AccountInfo};
#[cfg(not(feature = "sync-api"))]
use alloc::boxed::Box;
use alloc::{string::String, vec::Vec};
use log::*;
use sp_storage::StorageKey;

#[maybe_async::maybe_async(?Send)]
pub trait GetAccountInformation {
	type AccountId;
	type Index;
	type AccountData;

	/// Retrieves the next account index as available on the node.
	async fn get_system_account_next_index(
		&self,
		account_id: Self::AccountId,
	) -> Result<Self::Index>;

	async fn get_account_info(
		&self,
		address: &Self::AccountId,
	) -> Result<Option<AccountInfo<Self::Index, Self::AccountData>>>;

	async fn get_account_data(
		&self,
		address: &Self::AccountId,
	) -> Result<Option<Self::AccountData>>;

	/// Get nonce of an account.
	async fn get_account_nonce(&self, account: &Self::AccountId) -> Result<Self::Index>;
}

#[maybe_async::maybe_async(?Send)]
impl<T, Client> GetAccountInformation for Api<T, Client>
where
	T: Config,
	Client: Request,
{
	type AccountId = T::AccountId;
	type Index = T::Index;
	type AccountData = T::AccountData;

	async fn get_system_account_next_index(
		&self,
		account_id: Self::AccountId,
	) -> Result<Self::Index> {
		let next_index = self
			.client()
			.request("system_accountNextIndex", rpc_params![account_id])
			.await?;
		Ok(next_index)
	}

	async fn get_account_info(
		&self,
		address: &Self::AccountId,
	) -> Result<Option<AccountInfo<Self::Index, Self::AccountData>>> {
		let storagekey: StorageKey = self.metadata().storage_map_key::<Self::AccountId>(
			"System",
			"Account",
			address.clone(),
		)?;

		info!("storage key is: 0x{}", hex::encode(&storagekey));
		self.get_storage_by_key(storagekey, None).await
	}

	async fn get_account_data(
		&self,
		address: &Self::AccountId,
	) -> Result<Option<Self::AccountData>> {
		self.get_account_info(address).await.map(|info| info.map(|i| i.data))
	}

	async fn get_account_nonce(&self, account: &Self::AccountId) -> Result<Self::Index> {
		self.get_account_info(account)
			.await
			.map(|acc_opt| acc_opt.map_or_else(|| 0u32.into(), |acc| acc.nonce))
	}
}

/// Helper functions for some common SystemApi function.
#[maybe_async::maybe_async(?Send)]
pub trait SystemApi {
	type ChainType;
	type Properties;
	type Health;

	/// Get the node's implementation name.
	async fn get_system_name(&self) -> Result<String>;

	/// Get the node implementation's version. Should be a semver string.
	async fn get_system_version(&self) -> Result<String>;

	/// Get the chain's name. Given as a string identifier.
	async fn get_system_chain(&self) -> Result<String>;

	/// Get the chain's type.
	async fn get_system_chain_type(&self) -> Result<Self::ChainType>;

	/// Get a custom set of properties as a JSON object, defined in the chain spec.
	async fn get_system_properties(&self) -> Result<Self::Properties>;

	/// Return health status of the node.
	///
	/// Node is considered healthy if it is:
	/// - connected to some peers (unless running in dev mode)
	/// - not performing a major sync
	async fn get_system_health(&self) -> Result<Self::Health>;

	/// Get the base58-encoded PeerId of the node.
	async fn get_system_local_peer_id(&self) -> Result<String>;

	/// Returns the multi-addresses that the local node is listening on.
	///
	/// The addresses include a trailing `/p2p/` with the local PeerId, and are thus suitable to
	/// be passed to `addReservedPeer` or as a bootnode address for example.
	async fn get_system_local_listen_addresses(&self) -> Result<Vec<String>>;
}

#[maybe_async::maybe_async(?Send)]
impl<T, Client> SystemApi for Api<T, Client>
where
	T: Config,
	Client: Request,
{
	type ChainType = ac_primitives::ChainType;
	type Properties = ac_primitives::Properties;
	type Health = ac_primitives::Health;

	async fn get_system_name(&self) -> Result<String> {
		let res = self.client().request("system_name", rpc_params![]).await?;
		Ok(res)
	}

	async fn get_system_version(&self) -> Result<String> {
		let res = self.client().request("system_version", rpc_params![]).await?;
		Ok(res)
	}

	async fn get_system_chain(&self) -> Result<String> {
		let res = self.client().request("system_chain", rpc_params![]).await?;
		Ok(res)
	}

	async fn get_system_chain_type(&self) -> Result<Self::ChainType> {
		let res = self.client().request("system_chainType", rpc_params![]).await?;
		Ok(res)
	}

	async fn get_system_properties(&self) -> Result<Self::Properties> {
		let res = self.client().request("system_properties", rpc_params![]).await?;
		Ok(res)
	}

	async fn get_system_health(&self) -> Result<Self::Health> {
		let res = self.client().request("system_health", rpc_params![]).await?;
		Ok(res)
	}

	async fn get_system_local_peer_id(&self) -> Result<String> {
		let res = self.client().request("system_localPeerId", rpc_params![]).await?;
		Ok(res)
	}

	async fn get_system_local_listen_addresses(&self) -> Result<Vec<String>> {
		let res = self.client().request("system_localListenAddresses", rpc_params![]).await?;
		Ok(res)
	}
}