sp_rpc/
number.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! A number type that can be serialized both as a number or a string that encodes a number in a
19//! string.
20
21use serde::{Deserialize, Serialize};
22use sp_core::U256;
23use std::fmt::Debug;
24
25/// A number type that can be serialized both as a number or a string that encodes a number in a
26/// string.
27///
28/// We allow two representations of the block number as input. Either we deserialize to the type
29/// that is specified in the block type or we attempt to parse given hex value.
30///
31/// The primary motivation for having this type is to avoid overflows when using big integers in
32/// JavaScript (which we consider as an important RPC API consumer).
33#[derive(Copy, Clone, Serialize, Deserialize, Debug, PartialEq)]
34#[serde(untagged)]
35pub enum NumberOrHex {
36	/// The number represented directly.
37	Number(u64),
38	/// Hex representation of the number.
39	Hex(U256),
40}
41
42impl Default for NumberOrHex {
43	fn default() -> Self {
44		Self::Number(Default::default())
45	}
46}
47
48impl NumberOrHex {
49	/// Converts this number into an U256.
50	pub fn into_u256(self) -> U256 {
51		match self {
52			NumberOrHex::Number(n) => n.into(),
53			NumberOrHex::Hex(h) => h,
54		}
55	}
56}
57
58impl From<u32> for NumberOrHex {
59	fn from(n: u32) -> Self {
60		NumberOrHex::Number(n.into())
61	}
62}
63
64impl From<u64> for NumberOrHex {
65	fn from(n: u64) -> Self {
66		NumberOrHex::Number(n)
67	}
68}
69
70impl From<u128> for NumberOrHex {
71	fn from(n: u128) -> Self {
72		NumberOrHex::Hex(n.into())
73	}
74}
75
76impl From<U256> for NumberOrHex {
77	fn from(n: U256) -> Self {
78		NumberOrHex::Hex(n)
79	}
80}
81
82/// An error type that signals an out-of-range conversion attempt.
83pub struct TryFromIntError(pub(crate) ());
84
85impl TryFrom<NumberOrHex> for u32 {
86	type Error = TryFromIntError;
87	fn try_from(num_or_hex: NumberOrHex) -> Result<u32, Self::Error> {
88		num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
89	}
90}
91
92impl TryFrom<NumberOrHex> for u64 {
93	type Error = TryFromIntError;
94	fn try_from(num_or_hex: NumberOrHex) -> Result<u64, Self::Error> {
95		num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
96	}
97}
98
99impl TryFrom<NumberOrHex> for u128 {
100	type Error = TryFromIntError;
101	fn try_from(num_or_hex: NumberOrHex) -> Result<u128, Self::Error> {
102		num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
103	}
104}
105
106impl From<NumberOrHex> for U256 {
107	fn from(num_or_hex: NumberOrHex) -> U256 {
108		num_or_hex.into_u256()
109	}
110}
111
112#[cfg(test)]
113mod tests {
114	use super::*;
115	use crate::assert_deser;
116
117	#[test]
118	fn should_serialize_and_deserialize() {
119		assert_deser(r#""0x1234""#, NumberOrHex::Hex(0x1234.into()));
120		assert_deser(r#""0x0""#, NumberOrHex::Hex(0.into()));
121		assert_deser(r#"5"#, NumberOrHex::Number(5));
122		assert_deser(r#"10000"#, NumberOrHex::Number(10000));
123		assert_deser(r#"0"#, NumberOrHex::Number(0));
124		assert_deser(r#"1000000000000"#, NumberOrHex::Number(1000000000000));
125	}
126}