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
#![allow(dead_code)]

use ckb_jsonrpc_types::{
    BlockNumber, BlockView, HeaderView, JsonBytes, TransactionView, TxStatus, Uint32,
};
use ckb_types::H256;
use reqwest::{Client, Url};
use serde::{Deserialize, Serialize};
use async_trait::async_trait;

use std::{
    future::Future,
    io,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
};

use crate::{types::{Cell, CellsCapacity, IndexerTip, Order, Pagination, SearchKey, Tx},
    Rpc,
};

macro_rules! jsonrpc {
    ($method:expr, $self:ident, $return:ty$(, $params:ident$(,)?)*) => {{
        let old = $self.id.fetch_add(1, Ordering::AcqRel);
        let data = format!(
            r#"{{"id": {}, "jsonrpc": "2.0", "method": "{}", "params": {}}}"#,
            old,
            $method,
            serde_json::to_value(($($params,)*)).unwrap()
        );

        let req_json: serde_json::Value = serde_json::from_str(&data).unwrap();

        let c = $self.raw.post($self.ckb_uri.clone()).json(&req_json);
        async {
            let resp = c
                .send()
                .await
                .map_err::<io::Error, _>(|e| io::Error::new(io::ErrorKind::ConnectionAborted, format!("{:?}", e)))?;
            let output = resp
                .json::<jsonrpc_core::response::Output>()
                .await
                .map_err::<io::Error, _>(|e| io::Error::new(io::ErrorKind::InvalidData, format!("{:?}", e)))?;

            match output {
                jsonrpc_core::response::Output::Success(success) => {
                    Ok(serde_json::from_value::<$return>(success.result).unwrap())
                }
                jsonrpc_core::response::Output::Failure(e) => {
                    Err(io::Error::new(io::ErrorKind::InvalidData, format!("{:?}", e)))
                }
            }
        }
    }}
}

// Default implementation of ckb Rpc client
#[derive(Clone)]
pub struct RpcClient {
    raw: Client,
    ckb_uri: Url,
    id: Arc<AtomicU64>,
}

impl RpcClient {
    pub fn new(ckb_uri: &str) -> Self {
        let ckb_uri = Url::parse(ckb_uri).expect("ckb uri, e.g. \"http://127.0.0.1:8114\"");

        RpcClient {
            raw: Client::new(),
            ckb_uri,
            id: Arc::new(AtomicU64::new(0)),
        }
    }

    pub fn new_with_client(ckb_uri: &str, raw: Client) -> Self {
        let ckb_uri = Url::parse(ckb_uri).expect("ckb uri, e.g. \"http://127.0.0.1:8114\"");

        RpcClient {
            raw,
            ckb_uri,
            id: Arc::new(AtomicU64::new(0)),
        }
    }

    pub fn get_transaction(
        &self,
        hash: &H256,
    ) -> impl Future<Output = Result<Option<TransactionView>, io::Error>> {
        #[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Debug)]
        struct TransactionWithStatusResponse {
            /// The transaction.
            pub transaction: Option<TransactionView>,
            /// The Transaction status.
            pub tx_status: TxStatus,
        }
        let task = jsonrpc!("get_transaction", self, TransactionWithStatusResponse, hash);
        async {
            let res = task.await?;
            Ok(res.transaction)
        }
    }

    pub fn get_header_by_number(
        &self,
        number: BlockNumber,
    ) -> impl Future<Output = Result<HeaderView, io::Error>> {
        jsonrpc!("get_header_by_number", self, HeaderView, number)
    }

    pub fn get_block_by_number(
        &self,
        number: BlockNumber,
    ) -> impl Future<Output = Result<BlockView, io::Error>> {
        jsonrpc!("get_block_by_number", self, BlockView, number)
    }

    pub fn get_header(
        &self,
        hash: H256,
    ) -> impl Future<Output = Result<Option<HeaderView>, io::Error>> {
        jsonrpc!("get_header", self, Option<HeaderView>, hash)
    }

    pub fn get_indexer_tip(&self) -> impl Future<Output = Result<IndexerTip, io::Error>> {
        jsonrpc!("get_indexer_tip", self, IndexerTip)
    }

    pub fn get_transactions(
        &self,
        search_key: SearchKey,
        order: Order,
        limit: Uint32,
        after: Option<JsonBytes>,
    ) -> impl Future<Output = Result<Pagination<Tx>, io::Error>> {
        jsonrpc!(
            "get_transactions",
            self,
            Pagination<Tx>,
            search_key,
            order,
            limit,
            after
        )
    }

    pub fn get_cells(
        &self,
        search_key: SearchKey,
        order: Order,
        limit: Uint32,
        after: Option<JsonBytes>,
    ) -> impl Future<Output = Result<Pagination<Cell>, io::Error>> {
        jsonrpc!(
            "get_cells",
            self,
            Pagination<Cell>,
            search_key,
            order,
            limit,
            after
        )
    }

    pub fn get_cells_capacity(
        &self,
        search_key: SearchKey,
    ) -> impl Future<Output = Result<Option<CellsCapacity>, io::Error>> {
        jsonrpc!(
            "get_cells_capacity",
            self,
            Option<CellsCapacity>,
            search_key,
        )
    }
}

#[async_trait]
impl Rpc for RpcClient {
    async fn get_transactions(
        &self,
        search_key: SearchKey,
        order: Order,
        limit: Uint32,
        after: Option<JsonBytes>,
    ) -> Result<Pagination<Tx>, io::Error> {
        self.get_transactions(search_key, order, limit, after).await
    }

    async fn get_transaction(&self, hash: &H256) -> Result<Option<TransactionView>, io::Error> {
        self.get_transaction(hash).await
    }

    async fn get_header_by_number(&self, number: BlockNumber) -> Result<HeaderView, io::Error> {
        self.get_header_by_number(number).await
    }

    async fn get_indexer_tip(&self) -> Result<IndexerTip, io::Error> {
        self.get_indexer_tip().await
    }

    async fn get_block_by_number(&self, number: BlockNumber) -> Result<BlockView, std::io::Error> {
        self.get_block_by_number(number).await
    }
}