subcoin_rpc/
lib.rs

1mod address;
2mod blockchain;
3mod error;
4mod network;
5mod raw_transactions;
6
7use address::{Address, AddressApiServer};
8use bitcoin::Network as BitcoinNetwork;
9use blockchain::{Blockchain, BlockchainApiServer};
10use network::{Network, NetworkApiServer};
11use raw_transactions::{RawTransactions, RawTransactionsApiServer};
12use sc_client_api::{AuxStore, BlockBackend, HeaderBackend};
13use sp_runtime::traits::Block as BlockT;
14use std::sync::Arc;
15use subcoin_indexer::IndexerQuery;
16use subcoin_network::NetworkApi;
17use subcoin_primitives::{BitcoinTransactionAdapter, TransactionIndex};
18
19/// Subcoin RPC.
20pub struct SubcoinRpc<Block, Client, TransactionAdapter> {
21    /// Blockchain RPC.
22    pub blockchain: Blockchain<Block, Client, TransactionAdapter>,
23    /// Raw transactions RPC.
24    pub raw_transactions: RawTransactions<Block, Client>,
25    /// Network RPC.
26    pub network: Network<Block, Client>,
27    /// Address RPC (only available when indexer is enabled).
28    pub address: Option<Address<Block, Client>>,
29}
30
31impl<Block, Client, TransactionAdapter> SubcoinRpc<Block, Client, TransactionAdapter>
32where
33    Block: BlockT + 'static,
34    Client: HeaderBackend<Block> + BlockBackend<Block> + AuxStore + Send + Sync + 'static,
35    TransactionAdapter: BitcoinTransactionAdapter<Block> + Send + Sync + 'static,
36{
37    /// Creates a new instance of [`SubcoinRpc`].
38    pub fn new(
39        client: Arc<Client>,
40        network_api: Arc<dyn NetworkApi>,
41        transaction_indexer: Arc<dyn TransactionIndex + Send + Sync>,
42        indexer_query: Option<Arc<IndexerQuery>>,
43        bitcoin_network: BitcoinNetwork,
44    ) -> Self {
45        let address =
46            indexer_query.map(|query| Address::<Block, Client>::new(client.clone(), query));
47
48        Self {
49            blockchain: Blockchain::<_, _, TransactionAdapter>::new(
50                client.clone(),
51                transaction_indexer,
52                bitcoin_network,
53            ),
54            raw_transactions: RawTransactions::new(client.clone(), network_api.clone()),
55            network: Network::new(client, network_api),
56            address,
57        }
58    }
59
60    /// Merges the Subcoin RPC components into a given RPC method registry.
61    pub fn merge_into(
62        self,
63        module: &mut jsonrpsee::Methods,
64    ) -> Result<(), jsonrpsee::server::RegisterMethodError> {
65        let Self {
66            blockchain,
67            raw_transactions,
68            network,
69            address,
70        } = self;
71
72        module.merge(blockchain.into_rpc())?;
73        module.merge(raw_transactions.into_rpc())?;
74        module.merge(network.into_rpc())?;
75
76        if let Some(address) = address {
77            module.merge(address.into_rpc())?;
78        }
79
80        Ok(())
81    }
82}