nautilus_blockchain/rpc/
utils.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16/// Determines if a JSON message is a subscription response from the blockchain RPC server.
17///
18/// Example response:
19/// ```json
20/// { "id": 1, "jsonrpc": "2.0", "result": "0x9cef478923ff08bf67fde6c64013158d"}
21/// ```
22#[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/// Determines if a JSON message is a subscription event notification from the blockchain RPC server.
28///
29/// Example response:
30/// ```json
31/// {
32///   "jsonrpc": "2.0", "method": "eth_subscription", "params": {
33///     "subscription": "0x9cef478923ff08bf67fde6c64013158d",
34///     "result": ...
35///    }
36/// }
37/// ```
38#[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/// Extracts the subscription ID from a blockchain RPC subscription event notification.
45#[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}