ethrpc 0.0.8

Ethereum transport-agnositic JSON RPC implementation
Documentation
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Module containing serializable JSON RPC data types.

pub mod batch;
mod value;

pub use self::value::{JsonError, Value};
use serde::{
    de::{self, Deserializer},
    Deserialize, Serialize, Serializer,
};
use std::{
    borrow::Cow,
    fmt::{self, Debug, Formatter},
    future::Future,
    sync::atomic::{self, AtomicU32},
};
use thiserror::Error;

/// Executes a JSON RPC call with the provided roundtrip implementation.
pub fn call<M, F, E>(method: M, params: M::Params, roundtrip: F) -> Result<M::Result, E>
where
    M: crate::method::Method + Serialize,
    F: FnOnce(Request) -> Result<Response, E>,
    E: From<Error> + From<JsonError>,
{
    let request = Request::new(method, params)?;
    let response = roundtrip(request)?;
    Ok(response.result::<M>()??)
}

/// Executes a JSON RPC call with the provided `async` roundtrip implementation.
pub async fn call_async<M, F, Fut, E>(
    method: M,
    params: M::Params,
    roundtrip: F,
) -> Result<M::Result, E>
where
    M: crate::method::Method + Serialize,
    F: FnOnce(Request) -> Fut,
    Fut: Future<Output = Result<Response, E>>,
    E: From<Error> + From<JsonError>,
{
    let request = Request::new(method, params)?;
    let response = roundtrip(request).await?;
    Ok(response.result::<M>()??)
}

/// JSON RPC supported version.
#[derive(Debug, Deserialize, Serialize)]
pub enum Version {
    /// Version 2.0 of the JSON RPC specification.
    #[serde(rename = "2.0")]
    V2,
}

/// Request and response ID.
///
/// Note that `u32` is used. This is so it always fits in a `f64` and obeys the
/// "SHOULD NOT have fractional parts" rule from the specification.  Since the
/// ID is set by the client, we shouldn't run into issues where a numerical ID
/// does not fit into this value or a string ID is used.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)]
#[serde(transparent)]
pub struct Id(pub u32);

impl Id {
    fn next() -> Self {
        static ID: AtomicU32 = AtomicU32::new(0);
        Self(ID.fetch_add(1, atomic::Ordering::Relaxed))
    }
}

/// A JSON RPC method name.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(transparent)]
pub struct Method(pub Cow<'static, str>);

impl Method {
    /// Creates a new method name from a string.
    pub fn new(name: impl Into<String>) -> Self {
        Self(Cow::Owned(name.into()))
    }

    /// Creates a new method name from a static string reference.
    pub fn from_static(name: &'static str) -> Self {
        Self(Cow::Borrowed(name))
    }

    /// Returns the method as a string reference.
    pub fn as_str(&self) -> &str {
        self.0.as_ref()
    }
}

/// A request object.
#[derive(Debug, Deserialize, Serialize)]
pub struct Request {
    pub jsonrpc: Version,
    pub method: Method,
    pub params: Value,
    pub id: Id,
}

impl Request {
    /// Creates a new request for the specified method and paramters.
    pub fn new<M>(method: M, params: M::Params) -> Result<Self, JsonError>
    where
        M: crate::method::Method,
    {
        Ok(Self {
            jsonrpc: Version::V2,
            method: Method(method.name()),
            params: Value::new(params, M::serialize_params)?,
            id: Id::next(),
        })
    }
}

/// Notification object.
#[derive(Debug, Deserialize, Serialize)]
pub struct Notification {
    pub jsonrpc: Version,
    pub method: Method,
    pub params: Value,
}

impl Notification {
    /// Creates a new notification for the specified method and paramters.
    pub fn new<M>(method: M, params: M::Params) -> Result<Self, JsonError>
    where
        M: crate::method::Method,
    {
        Ok(Self {
            jsonrpc: Version::V2,
            method: Method(method.name()),
            params: Value::new(params, M::serialize_params)?,
        })
    }
}

/// Response object.
#[derive(Debug)]
pub struct Response {
    pub jsonrpc: Version,
    pub result: Result<Value, Error>,
    pub id: Option<Id>,
}

impl Response {
    /// Extracts the response result for a method.
    pub fn result<M>(self) -> Result<Result<M::Result, Error>, JsonError>
    where
        M: crate::method::Method,
    {
        // TODO(nlordell): Might be some standard library function for this.
        match self.result {
            Ok(value) => value.result::<M>().map(Ok),
            Err(err) => Ok(Err(err)),
        }
    }
}

impl<'de> Deserialize<'de> for Response {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        #[serde(rename_all = "lowercase")]
        enum Key {
            JsonRpc,
            Result,
            Error,
            Id,
        }

        struct Visitor;

        impl<'de> de::Visitor<'de> for Visitor {
            type Value = Response;

            fn expecting(&self, f: &mut Formatter) -> fmt::Result {
                f.write_str("JSON RPC response")
            }

            fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
            where
                V: de::MapAccess<'de>,
            {
                let mut jsonrpc = None;
                let mut result = None;
                let mut error = None;
                let mut id = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        Key::JsonRpc => {
                            if jsonrpc.is_some() {
                                return Err(de::Error::duplicate_field("jsonrpc"));
                            }
                            jsonrpc = Some(map.next_value()?);
                        }
                        Key::Result => {
                            if result.is_some() {
                                return Err(de::Error::duplicate_field("result"));
                            }
                            result = Some(map.next_value()?);
                        }
                        Key::Error => {
                            if error.is_some() {
                                return Err(de::Error::duplicate_field("error"));
                            }
                            error = Some(map.next_value()?);
                        }
                        Key::Id => {
                            if id.is_some() {
                                return Err(de::Error::duplicate_field("id"));
                            }
                            id = Some(map.next_value()?);
                        }
                    }
                }

                // Note that some RPC servers return **both** `result` and
                // `error` fields in some conditions (such as auto-mine nodes
                // returning both when mining a transaction that reverts). In
                // these cases, the `result` is what is expected by the Ethereum
                // RPC standard, so prefer it (even if it not strictly JSON RPC
                // standard compatible).
                Ok(Response {
                    jsonrpc: jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?,
                    result: match (result, error) {
                        (Some(result), _) => Ok(result),
                        (None, Some(error)) => Err(error),
                        (None, None) => {
                            return Err(de::Error::custom("missing 'result' or 'error' field"))
                        }
                    },
                    id,
                })
            }
        }

        deserializer.deserialize_struct("Response", &["jsonrpc", "result", "error", "id"], Visitor)
    }
}

impl Serialize for Response {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        #[derive(Serialize)]
        struct Response<'a> {
            jsonrpc: Version,
            #[serde(skip_serializing_if = "Option::is_none")]
            result: Option<&'a Value>,
            #[serde(skip_serializing_if = "Option::is_none")]
            error: Option<&'a Error>,
            #[serde(skip_serializing_if = "Option::is_none")]
            id: Option<Id>,
        }

        let (result, error) = match &self.result {
            Ok(result) => (Some(result), None),
            Err(error) => (None, Some(error)),
        };
        Response {
            jsonrpc: Version::V2,
            result,
            error,
            id: self.id,
        }
        .serialize(serializer)
    }
}

/// An RPC error that may be produced on a response.
#[derive(Clone, Debug, Deserialize, Error, Serialize)]
#[error("{code}: {message}")]
#[serde(deny_unknown_fields)]
pub struct Error {
    pub code: ErrorCode,
    pub message: String,
    #[serde(default)]
    pub data: Value,
}

impl Error {
    /// Creates an error with a custom error message.
    pub fn custom(message: impl Into<String>) -> Self {
        Self {
            code: ErrorCode::from(-32000),
            message: message.into(),
            data: Value::default(),
        }
    }
}

/// An error code.
#[derive(Clone, Copy, Debug, Deserialize, Error, Serialize)]
#[serde(from = "i32", into = "i32")]
pub enum ErrorCode {
    #[error("parse error")]
    ParseError,
    #[error("invalid request")]
    InvalidRequest,
    #[error("method not found")]
    MethodNotFound,
    #[error("invalid params")]
    InvalidParams,
    #[error("internal error")]
    InternalError,
    #[error("server error ({0})")]
    ServerError(i32),
    #[error("reserved ({0})")]
    Reserved(i32),
    #[error("{0}")]
    Other(i32),
}

impl From<i32> for ErrorCode {
    fn from(code: i32) -> Self {
        #[allow(clippy::match_overlapping_arm)]
        match code {
            -32700 => Self::ParseError,
            -32600 => Self::InvalidRequest,
            -32601 => Self::MethodNotFound,
            -32602 => Self::InvalidParams,
            -32603 => Self::InternalError,
            -32099..=-32000 => Self::ServerError(code),
            -32768..=-32100 => Self::Reserved(code),
            _ => Self::Other(code),
        }
    }
}

impl From<ErrorCode> for i32 {
    fn from(code: ErrorCode) -> Self {
        match code {
            ErrorCode::ParseError => -32700,
            ErrorCode::InvalidRequest => -32600,
            ErrorCode::MethodNotFound => -32601,
            ErrorCode::InvalidParams => -32602,
            ErrorCode::InternalError => -32603,
            ErrorCode::ServerError(code) => code,
            ErrorCode::Reserved(code) => code,
            ErrorCode::Other(code) => code,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        eth,
        types::{BlockId, Empty, TransactionCall},
        web3,
    };
    use ethprim::address;
    use hex_literal::hex;
    use serde_json::json;

    fn roundtrip(
        call: serde_json::Value,
        result: serde_json::Value,
    ) -> impl FnOnce(Request) -> Result<Response, Box<dyn std::error::Error>> {
        move |request| {
            assert_eq!(
                call,
                json!({
                    "method": request.method,
                    "params": request.params,
                }),
            );
            Ok(Response {
                jsonrpc: Version::V2,
                result: Ok(Value(result)),
                id: Some(request.id),
            })
        }
    }

    #[test]
    fn calls() {
        let version = call(
            web3::ClientVersion,
            Empty,
            roundtrip(
                json!({
                    "method": "web3_clientVersion",
                    "params": [],
                }),
                json!("geth"),
            ),
        )
        .unwrap();
        assert_eq!(version, "geth");

        let output = call(
            eth::Call,
            (
                TransactionCall {
                    to: Some(address!("0x9008D19f58AAbD9eD0D60971565AA8510560ab41")),
                    input: Some(hex!("f698da25").to_vec()),
                    ..Default::default()
                },
                BlockId::default(),
            ),
            roundtrip(
                json!({
                    "method": "eth_call",
                    "params": [
                        {
                            "to": "0x9008D19f58AAbD9eD0D60971565AA8510560ab41",
                            "input": "0xf698da25",
                        },
                        "latest",
                    ],
                }),
                json!("0xc078f884a2676e1345748b1feace7b0abee5d00ecadb6e574dcdd109a63e8943"),
            ),
        )
        .unwrap();
        assert_eq!(
            output,
            hex!("c078f884a2676e1345748b1feace7b0abee5d00ecadb6e574dcdd109a63e8943")
        );
    }
}