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
mod blockchain;
mod error;
mod network;
mod raw_transactions;

use blockchain::{Blockchain, BlockchainApiServer};
use network::{Network, NetworkApiServer};
use raw_transactions::{RawTransactions, RawTransactionsApiServer};
use sc_client_api::{AuxStore, BlockBackend, HeaderBackend};
use sp_runtime::traits::Block as BlockT;
use std::sync::Arc;
use subcoin_network::NetworkApi;
use subcoin_primitives::BitcoinTransactionAdapter;

/// Subcoin RPC.
pub struct SubcoinRpc<Block, Client, TransactionAdapter> {
    /// Blockchain RPC.
    pub blockchain: Blockchain<Block, Client, TransactionAdapter>,
    /// Raw transactions RPC.
    pub raw_transactions: RawTransactions<Block, Client>,
    /// Network RPC.
    pub network: Network<Block, Client>,
}

impl<Block, Client, TransactionAdapter> SubcoinRpc<Block, Client, TransactionAdapter>
where
    Block: BlockT + 'static,
    Client: HeaderBackend<Block> + BlockBackend<Block> + AuxStore + 'static,
    TransactionAdapter: BitcoinTransactionAdapter<Block> + Send + Sync + 'static,
{
    /// Creates a new instance of [`SubcoinRpc`].
    pub fn new(client: Arc<Client>, network_api: Arc<dyn NetworkApi>) -> Self {
        Self {
            blockchain: Blockchain::<_, _, TransactionAdapter>::new(client.clone()),
            raw_transactions: RawTransactions::new(client.clone(), network_api.clone()),
            network: Network::new(client, network_api),
        }
    }

    /// Merges the Subcoin RPC components into a given RPC method registry.
    pub fn merge_into(
        self,
        module: &mut jsonrpsee::Methods,
    ) -> Result<(), jsonrpsee::server::RegisterMethodError> {
        let Self {
            blockchain,
            raw_transactions,
            network,
        } = self;

        module.merge(blockchain.into_rpc())?;
        module.merge(raw_transactions.into_rpc())?;
        module.merge(network.into_rpc())?;

        Ok(())
    }
}