nautilus_blockchain/rpc/
utils.rs1#[must_use]
23pub fn is_subscription_confirmation_response(json: &serde_json::Value) -> bool {
24 json.get("id").is_some() && json.get("result").is_some()
25}
26
27#[must_use]
39pub fn is_subscription_event(json: &serde_json::Value) -> bool {
40 json.get("method")
41 .is_some_and(|value| value.as_str() == Some("eth_subscription"))
42}
43
44#[must_use]
46pub fn extract_rpc_subscription_id(json: &serde_json::Value) -> Option<&str> {
47 json.get("params")
48 .and_then(|params| params.get("subscription"))
49 .and_then(|subscription| subscription.as_str())
50}
51
52#[cfg(test)]
53mod tests {
54 use rstest::{fixture, rstest};
55
56 use super::*;
57
58 #[fixture]
59 fn subscription_confirmation() -> serde_json::Value {
60 serde_json::from_str(
61 r#"{"jsonrpc":"2.0","id":1,"result":"0x4edabdfee3c542878dcc064c12151869"}"#,
62 )
63 .unwrap()
64 }
65
66 #[fixture]
67 fn subscription_event() -> serde_json::Value {
68 serde_json::from_str(r#"{"jsonrpc":"2.0","method":"eth_subscription",
69 "params":{"subscription":"0x4edabdfee3c542878dcc064c12151869",
70 "result":{"baseFeePerGas":"0x989680","difficulty":"0x1",
71 "extraData":"0x5fcd3faec8b0c37571510e87ab402f0b7e6693ec607c880d38343e1884eb6823",
72 "gasLimit":"0x4000000000000","gasUsed":"0x47a6d4",
73 "hash":"0xb1e9f3e327e0686c9a299d9d6dbb6f2a77b60e1b948ddab9055bacbe02b7aee0",
74 "miner":"0xa4b000000000000000000073657175656e636572",
75 "mixHash":"0x00000000000231fe000000000154e0b000000000000000200000000000000000",
76 "nonce":"0x00000000001dc3fe","number":"0x13a7cad4",
77 "parentHash":"0x37356a864e9fd6eca0d4ebdd704739717f70e0e1f733b52317d377107c9b51ca",
78 "receiptsRoot":"0x0604749e4d9c71de05e0a1661fa9b3eafeac1da1e98125dcc48015d9d9c5d0da",
79 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
80 "stateRoot":"0x41e066985a516865c2d29a2fd5672f8de54b0459098a0d134ceb092e1e578e28",
81 "timestamp":"0x680a58bf","totalDifficulty":"0x1254ed8c",
82 "transactionsRoot":"0x1e5209d3a83f6315c74d5e39d59ad85420b51709b695473ee4f321147c356564"}}}"#
83 ).unwrap()
84 }
85
86 #[rstest]
87 fn test_is_subscription_confirmation_response(subscription_confirmation: serde_json::Value) {
88 assert!(is_subscription_confirmation_response(
89 &subscription_confirmation
90 ));
91 }
92
93 #[rstest]
94 fn test_is_subscription_event(subscription_event: serde_json::Value) {
95 assert!(is_subscription_event(&subscription_event));
96 }
97
98 #[rstest]
99 fn test_extract_subscription_id(subscription_event: serde_json::Value) {
100 let id = extract_rpc_subscription_id(&subscription_event);
101 assert_eq!(id, Some("0x4edabdfee3c542878dcc064c12151869"));
102 }
103}