fc_rpc_v2_types/
pubsub.rs

1// This file is part of Frontier.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// 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.
10
11// 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.
15
16// 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/>.
18
19use ethereum_types::H256;
20use serde::{de, Deserialize, Serialize};
21
22use crate::{block::Header, filter::Filter, log::Log, transaction::Transaction};
23
24/// Subscription kind.
25#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub enum PubSubKind {
28	/// New block headers subscription.
29	NewHeads,
30	/// Logs subscription.
31	Logs,
32	/// New Pending Transactions subscription.
33	NewPendingTransactions,
34}
35
36/// Any additional parameters for a subscription.
37#[derive(Clone, Debug, Eq, PartialEq, Default)]
38pub enum PubSubParams {
39	/// No parameters passed.
40	#[default]
41	None,
42	/// Log parameters.
43	Logs(Box<Filter>),
44	/// Boolean parameter for new pending transactions.
45	Bool(bool),
46}
47
48impl serde::Serialize for PubSubParams {
49	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
50	where
51		S: serde::Serializer,
52	{
53		match self {
54			Self::None => serializer.serialize_none(),
55			Self::Logs(logs) => logs.serialize(serializer),
56			Self::Bool(full) => full.serialize(serializer),
57		}
58	}
59}
60
61impl<'de> serde::Deserialize<'de> for PubSubParams {
62	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
63	where
64		D: serde::Deserializer<'de>,
65	{
66		let v = serde_json::Value::deserialize(deserializer)?;
67
68		if v.is_null() {
69			return Ok(Self::None);
70		}
71
72		if let Some(val) = v.as_bool() {
73			return Ok(Self::Bool(val));
74		}
75
76		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}
81
82/// Subscription result.
83#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
84#[serde(untagged)]
85pub enum PubSubResult {
86	/// New block header.
87	Header(Box<Header>),
88	/// Log.
89	Log(Box<Log>),
90	/// Transaction hash.
91	TransactionHash(H256),
92	/// Transaction.
93	FullTransaction(Box<Transaction>),
94}
95
96#[cfg(test)]
97mod tests {
98	use super::*;
99
100	#[test]
101	fn pubsub_params_serde_impl() {
102		let cases = [
103			("null", PubSubParams::None),
104			("true", PubSubParams::Bool(true)),
105			("false", PubSubParams::Bool(false)),
106		];
107		for (raw, typed) in cases {
108			let deserialized = serde_json::from_str::<PubSubParams>(raw).unwrap();
109			assert_eq!(deserialized, typed);
110
111			let serialized = serde_json::to_string(&typed).unwrap();
112			assert_eq!(serialized, raw);
113		}
114	}
115}