1use ethereum_types::{H160, U256, U64};
20use jsonrpsee::core::RpcResult;
21use sc_client_api::backend::{Backend, StorageProvider};
23use sp_api::ProvideRuntimeApi;
24use sp_blockchain::HeaderBackend;
25use sp_consensus::SyncOracle;
26use sp_runtime::traits::{Block as BlockT, UniqueSaturatedInto};
27use fc_rpc_core::types::*;
29use fp_rpc::EthereumRuntimeRPCApi;
30
31use crate::{eth::Eth, internal_err};
32
33impl<B, C, P, CT, BE, CIDP, EC> Eth<B, C, P, CT, BE, CIDP, EC>
34where
35 B: BlockT,
36 C: ProvideRuntimeApi<B>,
37 C::Api: EthereumRuntimeRPCApi<B>,
38 C: HeaderBackend<B> + StorageProvider<B, BE> + 'static,
39 BE: Backend<B>,
40{
41 pub fn protocol_version(&self) -> RpcResult<u64> {
42 Ok(1)
43 }
44
45 pub async fn syncing(&self) -> RpcResult<SyncStatus> {
46 if self.sync.is_major_syncing() {
47 let current_number = self.client.info().best_number;
48 let highest_number = self
49 .sync
50 .status()
51 .await
52 .map_err(|_| internal_err("fetch best_seen_block failed"))?
53 .best_seen_block
54 .unwrap_or(current_number);
55
56 let current_number = UniqueSaturatedInto::<u128>::unique_saturated_into(current_number);
57 let highest_number = UniqueSaturatedInto::<u128>::unique_saturated_into(highest_number);
58
59 Ok(SyncStatus::Info(SyncInfo {
60 starting_block: U256::zero(),
61 current_block: U256::from(current_number),
62 highest_block: U256::from(highest_number),
63 warp_chunks_amount: None,
64 warp_chunks_processed: None,
65 }))
66 } else {
67 Ok(SyncStatus::None)
68 }
69 }
70
71 pub fn author(&self) -> RpcResult<H160> {
72 let hash = self.client.info().best_hash;
73 let current_block = self
74 .storage_override
75 .current_block(hash)
76 .ok_or_else(|| internal_err("fetching author through override failed"))?;
77 Ok(current_block.header.beneficiary)
78 }
79
80 pub fn accounts(&self) -> RpcResult<Vec<H160>> {
81 Ok(self
82 .signers
83 .iter()
84 .flat_map(|signer| signer.accounts())
85 .collect::<Vec<_>>())
86 }
87
88 pub fn block_number(&self) -> RpcResult<U256> {
89 let best_number = self.client.info().best_number;
90 let best_number = UniqueSaturatedInto::<u128>::unique_saturated_into(best_number);
91 Ok(U256::from(best_number))
92 }
93
94 pub fn chain_id(&self) -> RpcResult<Option<U64>> {
95 let hash = self.client.info().best_hash;
96 let chain_id = self
97 .client
98 .runtime_api()
99 .chain_id(hash)
100 .map_err(|err| internal_err(format!("fetch runtime chain id failed: {err:?}")))?;
101 Ok(Some(U64::from(chain_id)))
102 }
103}