moonbeam_client_evm_tracing/types/
serialization.rs

1// Copyright 2019-2025 PureStake Inc.
2// This file is part of Moonbeam.
3
4// Moonbeam is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Moonbeam is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Provide serialization functions for various types and formats.
18
19use ethereum_types::{H256, U256};
20use serde::{
21	ser::{Error, SerializeSeq},
22	Serializer,
23};
24use sp_runtime::traits::UniqueSaturatedInto;
25
26pub fn seq_h256_serialize<S>(data: &Option<Vec<H256>>, serializer: S) -> Result<S::Ok, S::Error>
27where
28	S: Serializer,
29{
30	if let Some(vec) = data {
31		let mut seq = serializer.serialize_seq(Some(vec.len()))?;
32		for hash in vec {
33			seq.serialize_element(&format!("{:x}", hash))?;
34		}
35		seq.end()
36	} else {
37		let seq = serializer.serialize_seq(Some(0))?;
38		seq.end()
39	}
40}
41
42pub fn bytes_0x_serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
43where
44	S: Serializer,
45{
46	serializer.serialize_str(&format!("0x{}", hex::encode(bytes)))
47}
48
49pub fn option_bytes_0x_serialize<S>(
50	bytes: &Option<Vec<u8>>,
51	serializer: S,
52) -> Result<S::Ok, S::Error>
53where
54	S: Serializer,
55{
56	if let Some(bytes) = bytes.as_ref() {
57		return serializer.serialize_str(&format!("0x{}", hex::encode(&bytes[..])));
58	}
59	Err(S::Error::custom("String serialize error."))
60}
61
62pub fn opcode_serialize<S>(opcode: &[u8], serializer: S) -> Result<S::Ok, S::Error>
63where
64	S: Serializer,
65{
66	let d = std::str::from_utf8(opcode)
67		.map_err(|_| S::Error::custom("Opcode serialize error."))?
68		.to_uppercase();
69	serializer.serialize_str(&d)
70}
71
72pub fn string_serialize<S>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error>
73where
74	S: Serializer,
75{
76	let d = std::str::from_utf8(value)
77		.map_err(|_| S::Error::custom("String serialize error."))?
78		.to_string();
79	serializer.serialize_str(&d)
80}
81
82pub fn option_string_serialize<S>(value: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
83where
84	S: Serializer,
85{
86	if let Some(value) = value.as_ref() {
87		let d = std::str::from_utf8(&value[..])
88			.map_err(|_| S::Error::custom("String serialize error."))?
89			.to_string();
90		return serializer.serialize_str(&d);
91	}
92	Err(S::Error::custom("String serialize error."))
93}
94
95pub fn u256_serialize<S>(data: &U256, serializer: S) -> Result<S::Ok, S::Error>
96where
97	S: Serializer,
98{
99	serializer.serialize_u64(UniqueSaturatedInto::<u64>::unique_saturated_into(*data))
100}
101
102pub fn h256_serialize<S>(data: &H256, serializer: S) -> Result<S::Ok, S::Error>
103where
104	S: Serializer,
105{
106	serializer.serialize_str(&format!("{:x}", data))
107}
108
109pub fn h256_0x_serialize<S>(data: &H256, serializer: S) -> Result<S::Ok, S::Error>
110where
111	S: Serializer,
112{
113	serializer.serialize_str(&format!("0x{:x}", data))
114}