1// This file is part of Frontier.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
56// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
1011// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
1516// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
1819use ethereum_types::H256;
20use serde::{de, Deserialize, Serialize};
2122use crate::{block::Header, filter::Filter, log::Log, transaction::Transaction};
2324/// Subscription kind.
25#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub enum PubSubKind {
28/// New block headers subscription.
29NewHeads,
30/// Logs subscription.
31Logs,
32/// New Pending Transactions subscription.
33NewPendingTransactions,
34}
3536/// Any additional parameters for a subscription.
37#[derive(Clone, Debug, Eq, PartialEq, Default)]
38pub enum PubSubParams {
39/// No parameters passed.
40#[default]
41None,
42/// Log parameters.
43Logs(Box<Filter>),
44/// Boolean parameter for new pending transactions.
45Bool(bool),
46}
4748impl serde::Serialize for PubSubParams {
49fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
50where
51S: serde::Serializer,
52 {
53match self {
54Self::None => serializer.serialize_none(),
55Self::Logs(logs) => logs.serialize(serializer),
56Self::Bool(full) => full.serialize(serializer),
57 }
58 }
59}
6061impl<'de> serde::Deserialize<'de> for PubSubParams {
62fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
63where
64D: serde::Deserializer<'de>,
65 {
66let v = serde_json::Value::deserialize(deserializer)?;
6768if v.is_null() {
69return Ok(Self::None);
70 }
7172if let Some(val) = v.as_bool() {
73return Ok(Self::Bool(val));
74 }
7576 serde_json::from_value(v)
77 .map(|f| Self::Logs(Box::new(f)))
78 .map_err(|e| de::Error::custom(format!("Invalid Pub-Sub parameters: {e}")))
79 }
80}
8182/// Subscription result.
83#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
84#[serde(untagged)]
85pub enum PubSubResult {
86/// New block header.
87Header(Box<Header>),
88/// Log.
89Log(Box<Log>),
90/// Transaction hash.
91TransactionHash(H256),
92/// Transaction.
93FullTransaction(Box<Transaction>),
94}
9596#[cfg(test)]
97mod tests {
98use super::*;
99100#[test]
101fn pubsub_params_serde_impl() {
102let cases = [
103 ("null", PubSubParams::None),
104 ("true", PubSubParams::Bool(true)),
105 ("false", PubSubParams::Bool(false)),
106 ];
107for (raw, typed) in cases {
108let deserialized = serde_json::from_str::<PubSubParams>(raw).unwrap();
109assert_eq!(deserialized, typed);
110111let serialized = serde_json::to_string(&typed).unwrap();
112assert_eq!(serialized, raw);
113 }
114 }
115}