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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// This file is part of Frontier.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// 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/>.

/// (Non-standard) Ethereum pubsub interface.
pub mod pubsub;

use ethereum_types::{Address, H256, U256, U64};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

pub use self::pubsub::*;
use crate::types::{
	access_list::AccessListResult,
	block::Block,
	block_id::{BlockNumberOrTag, BlockNumberOrTagOrHash},
	bytes::Bytes,
	fee::FeeHistoryResult,
	filter::{Filter, FilterChanges},
	index::Index,
	log::Log,
	proof::AccountProof,
	state::StateOverrides,
	sync::SyncingStatus,
	transaction::{Transaction, TransactionReceipt, TransactionRequest},
};

/// Ethereum RPC client interfaces.
pub trait EthApiClient:
	EthBlockApiClient
	+ EthClientApiClient
	+ EthExecuteApiClient
	+ EthFeeMarketApiClient
	+ EthFilterApiClient
	+ EthSignApiClient
	+ EthStateApiClient
	+ EthSubmitApiClient
	+ EthTransactionApiClient
	+ EthPubSubApiClient
{
}

impl<T> EthApiClient for T where
	T: EthBlockApiClient
		+ EthClientApiClient
		+ EthExecuteApiClient
		+ EthFeeMarketApiClient
		+ EthFilterApiClient
		+ EthSignApiClient
		+ EthStateApiClient
		+ EthSubmitApiClient
		+ EthTransactionApiClient
		+ EthPubSubApiClient
{
}

/// Ethereum RPC server interfaces.
pub trait EthApiServer:
	EthBlockApiServer
	+ EthClientApiServer
	+ EthExecuteApiServer
	+ EthFeeMarketApiServer
	+ EthFilterApiServer
	+ EthSignApiServer
	+ EthStateApiServer
	+ EthSubmitApiServer
	+ EthTransactionApiServer
	+ EthPubSubApiServer
{
}

impl<T> EthApiServer for T where
	T: EthBlockApiServer
		+ EthClientApiServer
		+ EthExecuteApiServer
		+ EthFeeMarketApiServer
		+ EthFilterApiServer
		+ EthSignApiServer
		+ EthStateApiServer
		+ EthSubmitApiServer
		+ EthTransactionApiServer
		+ EthPubSubApiServer
{
}

/// Ethereum (block) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthBlockApi {
	/// Returns information about a block by hash.
	#[method(name = "getBlockByHash")]
	async fn block_by_hash(&self, hash: H256, full: bool) -> RpcResult<Option<Block>>;

	/// Returns information about a block by number.
	#[method(name = "getBlockByNumber")]
	async fn block_by_number(
		&self,
		block: BlockNumberOrTag,
		full: bool,
	) -> RpcResult<Option<Block>>;

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

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

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

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

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

/// Ethereum (client) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthClientApi {
	/// Returns the chain ID of the current network.
	#[method(name = "chainId")]
	async fn chain_id(&self) -> RpcResult<U64>;

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

	/// Returns the client coinbase address.
	#[method(name = "coinbase")]
	async fn author(&self) -> RpcResult<Address>;

	/// Returns a list of addresses owned by client.
	#[method(name = "accounts")]
	async fn accounts(&self) -> RpcResult<Vec<Address>>;

	/// Returns the number of most recent block.
	#[method(name = "blockNumber")]
	async fn block_number(&self) -> RpcResult<U64>;
}

/// Ethereum (execute) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthExecuteApi {
	/// Executes a new message call immediately without creating a transaction on the blockchain.
	#[method(name = "call")]
	async fn call(
		&self,
		request: TransactionRequest,
		number_or_hash: Option<BlockNumberOrTagOrHash>,
		state_overrides: Option<StateOverrides>,
		// block_overrides: Option<BlockOverrides>,
	) -> RpcResult<Bytes>;

	/// Generates and returns an estimate of hou much gas is necessary to allow the transaction to complete.
	#[method(name = "estimateGas")]
	async fn estimate_gas(
		&self,
		request: TransactionRequest,
		number_or_hash: Option<BlockNumberOrTag>,
		state_overrides: Option<StateOverrides>,
	) -> RpcResult<U256>;

	/// Generates an access list for a transaction.
	#[method(name = "createAccessList")]
	async fn create_access_list(
		&self,
		request: TransactionRequest,
		number_or_hash: Option<BlockNumberOrTag>,
	) -> RpcResult<AccessListResult>;
}

/// Ethereum (fee market) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthFeeMarketApi {
	/// Returns the current price per gas in wei.
	#[method(name = "gasPrice")]
	async fn gas_price(&self) -> RpcResult<U256>;

	/// Returns the current maxPriorityFeePerGas per gas in wei, which introduced in EIP-1159.
	#[method(name = "maxPriorityFeePerGas")]
	async fn max_priority_fee_per_gas(&self) -> RpcResult<U256>;

	/// Returns transaction base fee per gas and effective priority fee per gas for the requested/supported block range.
	///
	/// Transaction fee history, which is introduced in EIP-1159.
	#[method(name = "feeHistory")]
	async fn fee_history(
		&self,
		block_count: U256,
		newest_block: BlockNumberOrTag,
		reward_percentiles: Option<Vec<f64>>,
	) -> RpcResult<FeeHistoryResult>;
}

/// Ethereum (filter) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthFilterApi {
	/// Creates a filter object, based on filter options, to notify when the state changes (logs).
	#[method(name = "newFilter")]
	async fn new_filter(&self, filter: Filter) -> RpcResult<U256>;

	/// Creates a filter in the node, to notify when a new block arrives.
	#[method(name = "newBlockFilter")]
	async fn new_block_filter(&self) -> RpcResult<U256>;

	/// Creates a filter in the node, to notify when new pending transactions arrive.
	#[method(name = "newPendingTransactionFilter")]
	async fn new_pending_transaction_filter(&self, full: Option<bool>) -> RpcResult<U256>;

	/// Uninstalls a filter with given id.
	#[method(name = "uninstallFilter")]
	async fn uninstall_filter(&self, filter_id: Index) -> RpcResult<bool>;

	/// Polling method for a filter, which returns an array of logs which occurred since last poll.
	#[method(name = "getFilterChanges")]
	async fn filter_changes(&self, filter_id: Index) -> RpcResult<FilterChanges>;

	/// Returns an array of all logs matching filter with given id.
	#[method(name = "getFilterLogs")]
	async fn filter_logs(&self, filter_id: Index) -> RpcResult<Vec<Log>>;

	/// Returns an array of all logs matching filter with given id.
	#[method(name = "getLogs")]
	async fn logs(&self, filter: Filter) -> RpcResult<Vec<Log>>;
}

/// Ethereum (sign) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthSignApi {
	/// Returns an EIP-191 signature over the provided data.
	#[method(name = "sign")]
	async fn sign(&self, address: Address, message: Bytes) -> RpcResult<Bytes>;

	/// Returns an RLP encoded transaction signed by the specified account.
	#[method(name = "signTransaction")]
	async fn sign_transaction(&self, request: TransactionRequest) -> RpcResult<Bytes>;
}

/// Ethereum (state) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthStateApi {
	/// Returns the balance of the account of given address.
	#[method(name = "getBalance")]
	async fn balance(
		&self,
		address: Address,
		block: Option<BlockNumberOrTagOrHash>,
	) -> RpcResult<U256>;

	/// Returns the value from a storage position at a given address.
	#[method(name = "getStorageAt")]
	async fn storage_at(
		&self,
		address: Address,
		slot: U256,
		block: Option<BlockNumberOrTagOrHash>,
	) -> RpcResult<H256>;

	/// Returns the number of transactions sent from an address.
	#[method(name = "getTransactionCount")]
	async fn transaction_count(
		&self,
		address: Address,
		block: Option<BlockNumberOrTagOrHash>,
	) -> RpcResult<U256>;

	/// Returns the code at a given address.
	#[method(name = "getCode")]
	async fn code(
		&self,
		address: Address,
		block: Option<BlockNumberOrTagOrHash>,
	) -> RpcResult<Bytes>;

	/// Returns the merkle proof for a given account and optionally some storage keys.
	#[method(name = "getProof")]
	async fn proof(
		&self,
		address: Address,
		storage_keys: H256,
		block: Option<BlockNumberOrTagOrHash>,
	) -> RpcResult<AccountProof>;
}

/// Ethereum (submit) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthSubmitApi {
	/// Signs and submits a transaction; will block waiting for signer to return the transaction hash.
	#[method(name = "eth_sendTransaction")]
	async fn send_transaction(&self, request: TransactionRequest) -> RpcResult<H256>;

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

/// Ethereum (transaction) RPC interface.
#[rpc(client, server, namespace = "eth")]
#[async_trait]
pub trait EthTransactionApi {
	/// Returns the information about a transaction requested by transaction hash.
	#[method(name = "getTransactionByHash")]
	async fn transaction_by_hash(&self, transaction_hash: H256) -> RpcResult<Option<Transaction>>;

	/// Returns information about a transaction by block hash and transaction index position.
	#[method(name = "getTransactionByBlockHashAndIndex")]
	async fn transaction_by_block_hash_and_index(
		&self,
		block_hash: H256,
		transaction_index: Index,
	) -> RpcResult<Option<Transaction>>;

	/// Returns information about a transaction by block number and transaction index position.
	#[method(name = "getTransactionByBlockNumberAndIndex")]
	async fn transaction_by_block_number_and_index(
		&self,
		block: BlockNumberOrTag,
		transaction_index: Index,
	) -> RpcResult<Option<Transaction>>;

	/// Returns the receipt of a transaction by transaction hash.
	#[method(name = "getTransactionReceipt")]
	async fn transaction_receipt(
		&self,
		transaction_hash: H256,
	) -> RpcResult<Option<TransactionReceipt>>;
}