neo3/neo_clients/
api_trait.rs

1use std::{collections::HashMap, error::Error, fmt::Debug};
2
3use crate::{
4	builder::{Signer, Transaction, TransactionBuilder, TransactionSendToken},
5	config::NEOCONFIG,
6	neo_clients::{JsonRpcProvider, ProviderError, RpcClient},
7	neo_protocol::{
8		ApplicationLog, Balance, MemPoolDetails, NeoAddress, NeoBlock, NeoNetworkFee, NeoVersion,
9		Nep11Balances, Nep11Transfers, Nep17Balances, Nep17Transfers, Peers, Plugin, RTransaction,
10		RawTransaction, StateHeight, StateRoot, States, SubmitBlock, UnclaimedGas, ValidateAddress,
11		Validator,
12	},
13	ContractManifest, ContractParameter, ContractState, InvocationResult, NativeContractState,
14	NefFile, StackItem,
15};
16use async_trait::async_trait;
17use auto_impl::auto_impl;
18use primitive_types::{H160, H256};
19
20#[async_trait]
21#[auto_impl(&, Box, Arc)]
22pub trait APITrait: Sync + Send + Debug {
23	/// Error type returned by most operations
24	type Error: Error + Send + Sync + 'static;
25
26	/// The JSON-RPC client type at the bottom of the stack
27	type Provider: JsonRpcProvider;
28
29	/// The HTTP or Websocket provider.
30	fn rpc_client(&self) -> &RpcClient<Self::Provider>;
31
32	async fn network(&self) -> Result<u32, ProviderError>;
33
34	fn nns_resolver(&self) -> H160 {
35		H160::from(NEOCONFIG.lock().unwrap().nns_resolver.clone())
36	}
37
38	fn block_interval(&self) -> u32 {
39		NEOCONFIG.lock().unwrap().milliseconds_per_block
40	}
41
42	fn polling_interval(&self) -> u32 {
43		NEOCONFIG.lock().unwrap().milliseconds_per_block
44	}
45
46	fn max_valid_until_block_increment(&self) -> u32 {
47		NEOCONFIG.lock().unwrap().get_max_valid_until_block_increment()
48	}
49
50	// Blockchain methods
51	async fn get_best_block_hash(&self) -> Result<H256, Self::Error>;
52
53	async fn get_block_hash(&self, block_index: u32) -> Result<H256, Self::Error>;
54
55	async fn get_block(&self, block_hash: H256, full_tx: bool) -> Result<NeoBlock, Self::Error>;
56
57	async fn get_raw_block(&self, block_hash: H256) -> Result<String, Self::Error>;
58
59	// Node methods
60	async fn get_block_header_count(&self) -> Result<u32, Self::Error>;
61
62	async fn get_block_count(&self) -> Result<u32, Self::Error>;
63
64	async fn get_block_header(&self, block_hash: H256) -> Result<NeoBlock, Self::Error>;
65
66	async fn get_block_header_by_index(&self, index: u32) -> Result<NeoBlock, Self::Error>;
67
68	// Smart contract methods
69
70	async fn get_raw_block_header(&self, block_hash: H256) -> Result<String, Self::Error>;
71
72	async fn get_raw_block_header_by_index(&self, index: u32) -> Result<String, Self::Error>;
73
74	// Utility methods
75
76	async fn get_native_contracts(&self) -> Result<Vec<NativeContractState>, Self::Error>;
77
78	// Wallet methods
79
80	async fn get_contract_state(&self, hash: H160) -> Result<ContractState, Self::Error>;
81
82	async fn get_contract_state_by_id(&self, id: i64) -> Result<ContractState, Self::Error>;
83
84	async fn get_native_contract_state(&self, name: &str) -> Result<ContractState, Self::Error>;
85
86	async fn get_mem_pool(&self) -> Result<MemPoolDetails, Self::Error>;
87
88	async fn get_raw_mem_pool(&self) -> Result<Vec<H256>, Self::Error>;
89
90	// Application logs
91
92	async fn get_transaction(&self, hash: H256) -> Result<RTransaction, Self::Error>;
93
94	// State service
95
96	async fn get_raw_transaction(&self, tx_hash: H256) -> Result<String, Self::Error>;
97
98	async fn get_storage(&self, contract_hash: H160, key: &str) -> Result<String, Self::Error>;
99
100	async fn find_storage(
101		&self,
102		contract_hash: H160,
103		prefix_hex_string: &str,
104		start_index: u64,
105	) -> Result<String, ProviderError>;
106
107	async fn find_storage_with_id(
108		&self,
109		contract_id: i64,
110		prefix_hex_string: &str,
111		start_index: u64,
112	) -> Result<String, ProviderError>;
113
114	// Blockchain methods
115
116	async fn get_transaction_height(&self, tx_hash: H256) -> Result<u32, Self::Error>;
117
118	async fn get_next_block_validators(&self) -> Result<Vec<Validator>, Self::Error>;
119
120	async fn get_committee(&self) -> Result<Vec<String>, Self::Error>;
121
122	async fn get_connection_count(&self) -> Result<u32, Self::Error>;
123
124	async fn get_peers(&self) -> Result<Peers, Self::Error>;
125
126	// Smart contract method
127	async fn get_version(&self) -> Result<NeoVersion, Self::Error>;
128
129	async fn send_raw_transaction(&self, hex: String) -> Result<RawTransaction, Self::Error>;
130
131	/// Sends a transaction to the network
132	///
133	/// # Arguments
134	///
135	/// * `tx` - The transaction to send
136	///
137	/// # Returns
138	///
139	/// A `Result` containing the transaction hash or a `ProviderError`
140	async fn send_transaction<'a>(
141		&self,
142		tx: Transaction<'a, Self::Provider>,
143	) -> Result<H256, Self::Error>;
144
145	async fn submit_block(&self, hex: String) -> Result<SubmitBlock, Self::Error>;
146
147	// Blockchain methods
148	async fn invoke_function(
149		&self,
150		contract_hash: &H160,
151		method: String,
152		params: Vec<ContractParameter>,
153		signers: Option<Vec<Signer>>,
154	) -> Result<InvocationResult, Self::Error>;
155
156	async fn invoke_script(
157		&self,
158		hex: String,
159		signers: Vec<Signer>,
160	) -> Result<InvocationResult, Self::Error>;
161
162	// More smart contract methods
163
164	async fn get_unclaimed_gas(&self, hash: H160) -> Result<UnclaimedGas, Self::Error>;
165
166	async fn list_plugins(&self) -> Result<Vec<Plugin>, Self::Error>;
167
168	async fn validate_address(&self, address: &str) -> Result<ValidateAddress, Self::Error>;
169
170	// Wallet methods
171	async fn close_wallet(&self) -> Result<bool, Self::Error>;
172
173	async fn dump_priv_key(&self, script_hash: H160) -> Result<String, Self::Error>;
174
175	async fn get_wallet_balance(&self, token_hash: H160) -> Result<Balance, Self::Error>;
176
177	async fn get_new_address(&self) -> Result<String, Self::Error>;
178
179	async fn get_wallet_unclaimed_gas(&self) -> Result<String, Self::Error>;
180
181	async fn import_priv_key(&self, priv_key: String) -> Result<NeoAddress, Self::Error>;
182
183	async fn calculate_network_fee(&self, hex: String) -> Result<NeoNetworkFee, Self::Error>;
184
185	async fn list_address(&self) -> Result<Vec<NeoAddress>, Self::Error>;
186
187	async fn open_wallet(&self, path: String, password: String) -> Result<bool, Self::Error>;
188
189	async fn send_from(
190		&self,
191		token_hash: H160,
192		from: H160,
193		to: H160,
194		amount: u32,
195	) -> Result<RTransaction, Self::Error>;
196
197	// Transaction methods
198
199	async fn send_many(
200		&self,
201		from: Option<H160>,
202		send_tokens: Vec<TransactionSendToken>,
203	) -> Result<RTransaction, Self::Error>;
204
205	async fn send_to_address(
206		&self,
207		token_hash: H160,
208		to: H160,
209		amount: u32,
210	) -> Result<RTransaction, Self::Error>;
211
212	async fn cancel_transaction(
213		&self,
214		txHash: H256,
215		signers: Vec<H160>,
216		extra_fee: Option<u64>,
217	) -> Result<RTransaction, ProviderError>;
218
219	async fn get_application_log(&self, tx_hash: H256) -> Result<ApplicationLog, Self::Error>;
220
221	async fn get_nep17_balances(&self, script_hash: H160) -> Result<Nep17Balances, Self::Error>;
222
223	async fn get_nep17_transfers(&self, script_hash: H160) -> Result<Nep17Transfers, Self::Error>;
224
225	// NEP-17 methods
226
227	async fn get_nep17_transfers_from(
228		&self,
229		script_hash: H160,
230		from: u64,
231	) -> Result<Nep17Transfers, Self::Error>;
232
233	async fn get_nep17_transfers_range(
234		&self,
235		script_hash: H160,
236		from: u64,
237		to: u64,
238	) -> Result<Nep17Transfers, Self::Error>;
239
240	async fn get_nep11_balances(&self, script_hash: H160) -> Result<Nep11Balances, Self::Error>;
241
242	// NEP-11 methods
243
244	async fn get_nep11_transfers(&self, script_hash: H160) -> Result<Nep11Transfers, Self::Error>;
245
246	async fn get_nep11_transfers_from(
247		&self,
248		script_hash: H160,
249		from: u64,
250	) -> Result<Nep11Transfers, Self::Error>;
251
252	async fn get_nep11_transfers_range(
253		&self,
254		script_hash: H160,
255		from: u64,
256		to: u64,
257	) -> Result<Nep11Transfers, Self::Error>;
258
259	async fn get_nep11_properties(
260		&self,
261		script_hash: H160,
262		token_id: &str,
263	) -> Result<HashMap<String, String>, Self::Error>;
264
265	async fn get_state_root(&self, block_index: u32) -> Result<StateRoot, Self::Error>;
266
267	// State service methods
268	async fn get_proof(
269		&self,
270		root_hash: H256,
271		contract_hash: H160,
272		key: &str,
273	) -> Result<String, Self::Error>;
274
275	async fn verify_proof(&self, root_hash: H256, proof: &str) -> Result<String, Self::Error>;
276
277	async fn get_state_height(&self) -> Result<StateHeight, Self::Error>;
278
279	async fn get_state(
280		&self,
281		root_hash: H256,
282		contract_hash: H160,
283		key: &str,
284	) -> Result<String, Self::Error>;
285
286	async fn find_states(
287		&self,
288		root_hash: H256,
289		contract_hash: H160,
290		key_prefix: &str,
291		start_key: Option<&str>,
292		count: Option<u32>,
293	) -> Result<States, Self::Error>;
294
295	async fn get_block_by_hash(&self, hash: &str, full_tx: bool) -> Result<NeoBlock, Self::Error>;
296
297	async fn broadcast_address(&self) -> Result<bool, Self::Error>;
298
299	async fn broadcast_block(&self, block: NeoBlock) -> Result<bool, Self::Error>;
300
301	async fn broadcast_get_blocks(&self, hash: &str, count: u32) -> Result<bool, Self::Error>;
302
303	async fn broadcast_transaction(&self, tx: RTransaction) -> Result<bool, Self::Error>;
304
305	async fn create_contract_deployment_transaction(
306		&self,
307		nef: NefFile,
308		manifest: ContractManifest,
309		signers: Vec<Signer>,
310	) -> Result<TransactionBuilder<Self::Provider>, Self::Error>;
311
312	async fn create_contract_update_transaction(
313		&self,
314		contract_hash: H160,
315		nef: NefFile,
316		manifest: ContractManifest,
317		signers: Vec<Signer>,
318	) -> Result<TransactionBuilder<Self::Provider>, Self::Error>;
319
320	async fn create_invocation_transaction(
321		&self,
322		contract_hash: H160,
323		method: &str,
324		params: Vec<ContractParameter>,
325		signers: Vec<Signer>,
326	) -> Result<TransactionBuilder<Self::Provider>, Self::Error>;
327
328	async fn get_block_by_index(&self, index: u32, full_tx: bool) -> Result<NeoBlock, Self::Error>;
329
330	async fn get_raw_block_by_index(&self, index: u32) -> Result<String, Self::Error>;
331
332	async fn invoke_function_diagnostics(
333		&self,
334		contract_hash: H160,
335		name: String,
336		params: Vec<ContractParameter>,
337		signers: Vec<Signer>,
338	) -> Result<InvocationResult, Self::Error>;
339
340	async fn invoke_script_diagnostics(
341		&self,
342		hex: String,
343		signers: Vec<Signer>,
344	) -> Result<InvocationResult, Self::Error>;
345
346	async fn traverse_iterator(
347		&self,
348		session_id: String,
349		iterator_id: String,
350		count: u32,
351	) -> Result<Vec<StackItem>, Self::Error>;
352
353	async fn terminate_session(&self, session_id: &str) -> Result<bool, Self::Error>;
354
355	async fn invoke_contract_verify(
356		&self,
357		hash: H160,
358		params: Vec<ContractParameter>,
359		signers: Vec<Signer>,
360	) -> Result<InvocationResult, Self::Error>;
361
362	async fn get_raw_mempool(&self) -> Result<MemPoolDetails, Self::Error>;
363
364	async fn import_private_key(&self, wif: String) -> Result<NeoAddress, Self::Error>;
365
366	async fn get_block_header_hash(&self, hash: H256) -> Result<NeoBlock, Self::Error>;
367
368	async fn send_to_address_send_token(
369		&self,
370		send_token: &TransactionSendToken,
371	) -> Result<RTransaction, Self::Error>;
372
373	async fn send_from_send_token(
374		&self,
375		send_token: &TransactionSendToken,
376		from: H160,
377	) -> Result<RTransaction, Self::Error>;
378}