ckb_client/
lib.rs

1
2pub mod address;
3pub mod constant;
4pub mod network_type;
5pub mod rpc_client;
6pub mod types;
7
8use async_trait::async_trait;
9use ckb_types::H256;
10use serde::{Deserialize, Serialize};
11use types::{HeaderViewWithExtension, IndexerTip, Order, Pagination, SearchKey, Tx};
12
13use ckb_jsonrpc_types::{
14    BlockNumber, BlockView, CellInfo, HeaderView, JsonBytes, OutPoint, TransactionView, Uint32
15};
16// Cell changes on a single block
17#[derive(Serialize, Deserialize)]
18pub struct Submit {
19    pub header: HeaderView,
20    pub inputs: Vec<OutPoint>,
21    pub outputs: Vec<(OutPoint, CellInfo)>,
22}
23
24pub trait TipState {
25    fn load(&self) -> &IndexerTip;
26    fn update(&mut self, current: IndexerTip);
27}
28
29#[async_trait]
30pub trait SubmitProcess {
31    fn is_closed(&self) -> bool;
32    // if false return, it means this cell process should be shutdown
33    async fn submit_cells(&mut self, cells: Vec<Submit>) -> bool;
34    async fn submit_headers(&mut self, headers: Vec<HeaderViewWithExtension>) -> bool;
35}
36
37#[async_trait]
38pub trait Rpc {
39    // ckb indexer `get_transactions`
40    async fn get_transactions(
41        &self,
42        search_key: SearchKey,
43        order: Order,
44        limit: Uint32,
45        after: Option<JsonBytes>,
46    ) -> Result<Pagination<Tx>, std::io::Error>;
47
48    // ckb rpc `get_transaction`
49    async fn get_transaction(&self, hash: &H256)
50        -> Result<Option<TransactionView>, std::io::Error>;
51
52    // ckb rpc `get_header_by_number`
53    async fn get_header_by_number(&self, number: BlockNumber)
54        -> Result<HeaderView, std::io::Error>;
55    // ckb indexer `get_indexer_tip`
56    async fn get_indexer_tip(&self) -> Result<IndexerTip, std::io::Error>;
57
58    // ckb rpc `get_block_by_number`
59    async fn get_block_by_number(&self, number: BlockNumber) -> Result<BlockView, std::io::Error>;
60}
61
62impl TipState for IndexerTip {
63    fn load(&self) -> &IndexerTip {
64        self
65    }
66
67    fn update(&mut self, current: IndexerTip) {
68        *self = current
69    }
70}