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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2015-2022 Parity Technologies (UK) Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Eth rpc interface.

use ethereum_types::{H160, H256, H64, U256, U64};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use std::collections::BTreeMap;

use crate::types::*;

/// Eth rpc interface.
#[rpc(server)]
#[async_trait]
pub trait EthApi {
	// ########################################################################
	// Client
	// ########################################################################

	/// Returns protocol version encoded as a string (quotes are necessary).
	#[method(name = "eth_protocolVersion")]
	fn protocol_version(&self) -> RpcResult<u64>;

	/// Returns an object with data about the sync status or false. (wtf?)
	#[method(name = "eth_syncing")]
	async fn syncing(&self) -> RpcResult<SyncStatus>;

	/// Returns block author.
	#[method(name = "eth_coinbase")]
	fn author(&self) -> RpcResult<H160>;

	/// Returns accounts list.
	#[method(name = "eth_accounts")]
	fn accounts(&self) -> RpcResult<Vec<H160>>;

	/// Returns highest block number.
	#[method(name = "eth_blockNumber")]
	fn block_number(&self) -> RpcResult<U256>;

	/// Returns the chain ID used for transaction signing at the
	/// current best block. None is returned if not
	/// available.
	#[method(name = "eth_chainId")]
	fn chain_id(&self) -> RpcResult<Option<U64>>;

	// ########################################################################
	// Block
	// ########################################################################

	/// Returns block with given hash.
	#[method(name = "eth_getBlockByHash")]
	async fn block_by_hash(&self, hash: H256, full: bool) -> RpcResult<Option<RichBlock>>;

	/// Returns block with given number.
	#[method(name = "eth_getBlockByNumber")]
	async fn block_by_number(
		&self,
		number_or_hash: BlockNumberOrHash,
		full: bool,
	) -> RpcResult<Option<RichBlock>>;

	/// Returns the number of transactions in a block with given hash.
	#[method(name = "eth_getBlockTransactionCountByHash")]
	async fn block_transaction_count_by_hash(&self, hash: H256) -> RpcResult<Option<U256>>;

	/// Returns the number of transactions in a block with given block number.
	#[method(name = "eth_getBlockTransactionCountByNumber")]
	async fn block_transaction_count_by_number(
		&self,
		number_or_hash: BlockNumberOrHash,
	) -> RpcResult<Option<U256>>;

	/// Returns the receipts of a block by number or hash.
	#[method(name = "eth_getBlockReceipts")]
	async fn block_transaction_receipts(
		&self,
		number_or_hash: BlockNumberOrHash,
	) -> RpcResult<Option<Vec<Receipt>>>;

	/// Returns the number of uncles in a block with given hash.
	#[method(name = "eth_getUncleCountByBlockHash")]
	fn block_uncles_count_by_hash(&self, hash: H256) -> RpcResult<U256>;

	/// Returns the number of uncles in a block with given block number.
	#[method(name = "eth_getUncleCountByBlockNumber")]
	fn block_uncles_count_by_number(&self, number_or_hash: BlockNumberOrHash) -> RpcResult<U256>;

	/// Returns an uncles at given block and index.
	#[method(name = "eth_getUncleByBlockHashAndIndex")]
	fn uncle_by_block_hash_and_index(
		&self,
		hash: H256,
		index: Index,
	) -> RpcResult<Option<RichBlock>>;

	/// Returns an uncles at given block and index.
	#[method(name = "eth_getUncleByBlockNumberAndIndex")]
	fn uncle_by_block_number_and_index(
		&self,
		number_or_hash: BlockNumberOrHash,
		index: Index,
	) -> RpcResult<Option<RichBlock>>;

	// ########################################################################
	// Transaction
	// ########################################################################

	/// Get transaction by its hash.
	#[method(name = "eth_getTransactionByHash")]
	async fn transaction_by_hash(&self, hash: H256) -> RpcResult<Option<Transaction>>;

	/// Returns transaction at given block hash and index.
	#[method(name = "eth_getTransactionByBlockHashAndIndex")]
	async fn transaction_by_block_hash_and_index(
		&self,
		hash: H256,
		index: Index,
	) -> RpcResult<Option<Transaction>>;

	/// Returns transaction by given block number and index.
	#[method(name = "eth_getTransactionByBlockNumberAndIndex")]
	async fn transaction_by_block_number_and_index(
		&self,
		number_or_hash: BlockNumberOrHash,
		index: Index,
	) -> RpcResult<Option<Transaction>>;

	/// Returns transaction receipt by transaction hash.
	#[method(name = "eth_getTransactionReceipt")]
	async fn transaction_receipt(&self, hash: H256) -> RpcResult<Option<Receipt>>;

	// ########################################################################
	// State
	// ########################################################################

	/// Returns balance of the given account.
	#[method(name = "eth_getBalance")]
	async fn balance(
		&self,
		address: H160,
		number_or_hash: Option<BlockNumberOrHash>,
	) -> RpcResult<U256>;

	/// Returns content of the storage at given address.
	#[method(name = "eth_getStorageAt")]
	async fn storage_at(
		&self,
		address: H160,
		index: U256,
		number_or_hash: Option<BlockNumberOrHash>,
	) -> RpcResult<H256>;

	/// Returns the number of transactions sent from given address at given time (block number).
	#[method(name = "eth_getTransactionCount")]
	async fn transaction_count(
		&self,
		address: H160,
		number_or_hash: Option<BlockNumberOrHash>,
	) -> RpcResult<U256>;

	/// Returns the code at given address at given time (block number).
	#[method(name = "eth_getCode")]
	async fn code_at(
		&self,
		address: H160,
		number_or_hash: Option<BlockNumberOrHash>,
	) -> RpcResult<Bytes>;

	// ########################################################################
	// Execute
	// ########################################################################

	/// Call contract, returning the output data.
	#[method(name = "eth_call")]
	async fn call(
		&self,
		request: TransactionRequest,
		number_or_hash: Option<BlockNumberOrHash>,
		state_overrides: Option<BTreeMap<H160, CallStateOverride>>,
	) -> RpcResult<Bytes>;

	/// Estimate gas needed for execution of given contract.
	#[method(name = "eth_estimateGas")]
	async fn estimate_gas(
		&self,
		request: TransactionRequest,
		number_or_hash: Option<BlockNumberOrHash>,
	) -> RpcResult<U256>;

	// ########################################################################
	// Fee
	// ########################################################################

	/// Returns current gas_price.
	#[method(name = "eth_gasPrice")]
	fn gas_price(&self) -> RpcResult<U256>;

	/// Introduced in EIP-1159 for getting information on the appropriate priority fee to use.
	#[method(name = "eth_feeHistory")]
	async fn fee_history(
		&self,
		block_count: U256,
		newest_block: BlockNumberOrHash,
		reward_percentiles: Option<Vec<f64>>,
	) -> RpcResult<FeeHistory>;

	/// Introduced in EIP-1159, a Geth-specific and simplified priority fee oracle.
	/// Leverages the already existing fee history cache.
	#[method(name = "eth_maxPriorityFeePerGas")]
	fn max_priority_fee_per_gas(&self) -> RpcResult<U256>;

	// ########################################################################
	// Mining
	// ########################################################################

	/// Returns true if client is actively mining new blocks.
	#[method(name = "eth_mining")]
	fn is_mining(&self) -> RpcResult<bool>;

	/// Returns the number of hashes per second that the node is mining with.
	#[method(name = "eth_hashrate")]
	fn hashrate(&self) -> RpcResult<U256>;

	/// Returns the hash of the current block, the seedHash, and the boundary condition to be met.
	#[method(name = "eth_getWork")]
	fn work(&self) -> RpcResult<Work>;

	/// Used for submitting mining hashrate.
	#[method(name = "eth_submitHashrate")]
	fn submit_hashrate(&self, hashrate: U256, id: H256) -> RpcResult<bool>;

	/// Used for submitting a proof-of-work solution.
	#[method(name = "eth_submitWork")]
	fn submit_work(&self, nonce: H64, pow_hash: H256, mix_digest: H256) -> RpcResult<bool>;

	// ########################################################################
	// Submit
	// ########################################################################

	/// Sends transaction; will block waiting for signer to return the
	/// transaction hash.
	#[method(name = "eth_sendTransaction")]
	async fn send_transaction(&self, request: TransactionRequest) -> RpcResult<H256>;

	/// Sends signed transaction, returning its hash.
	#[method(name = "eth_sendRawTransaction")]
	async fn send_raw_transaction(&self, bytes: Bytes) -> RpcResult<H256>;
}

/// Eth filters rpc api (polling).
#[rpc(server)]
pub trait EthFilterApi {
	/// Returns id of new filter.
	#[method(name = "eth_newFilter")]
	fn new_filter(&self, filter: Filter) -> RpcResult<U256>;

	/// Returns id of new block filter.
	#[method(name = "eth_newBlockFilter")]
	fn new_block_filter(&self) -> RpcResult<U256>;

	/// Returns id of new block filter.
	#[method(name = "eth_newPendingTransactionFilter")]
	fn new_pending_transaction_filter(&self) -> RpcResult<U256>;

	/// Returns filter changes since last poll.
	#[method(name = "eth_getFilterChanges")]
	async fn filter_changes(&self, index: Index) -> RpcResult<FilterChanges>;

	/// Returns all logs matching given filter (in a range 'from' - 'to').
	#[method(name = "eth_getFilterLogs")]
	async fn filter_logs(&self, index: Index) -> RpcResult<Vec<Log>>;

	/// Uninstalls filter.
	#[method(name = "eth_uninstallFilter")]
	fn uninstall_filter(&self, index: Index) -> RpcResult<bool>;

	/// Returns logs matching given filter object.
	#[method(name = "eth_getLogs")]
	async fn logs(&self, filter: Filter) -> RpcResult<Vec<Log>>;
}