ac_primitives/
rpc_numbers.rs

1// This file is part of Substrate.
2
3// Copyright (C) 2017-2022 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
21// Copied the whole file from substrate, as sp_rpc is not no_std compatible.
22// https://github.com/paritytech/substrate/blob/cd2fdcf85eb96c53ce2a5d418d4338eb92f5d4f5/primitives/rpc/src/number.rs
23
24use core::fmt::Debug;
25use primitive_types::U256;
26use serde::{Deserialize, Serialize};
27
28/// A number type that can be serialized both as a number or a string that encodes a number in a
29/// string.
30///
31/// We allow two representations of the block number as input. Either we deserialize to the type
32/// that is specified in the block type or we attempt to parse given hex value.
33///
34/// The primary motivation for having this type is to avoid overflows when using big integers in
35/// JavaScript (which we consider as an important RPC API consumer).
36#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
37#[serde(untagged)]
38pub enum NumberOrHex {
39	/// The number represented directly.
40	Number(u64),
41	/// Hex representation of the number.
42	Hex(U256),
43}
44
45impl Default for NumberOrHex {
46	fn default() -> Self {
47		Self::Number(Default::default())
48	}
49}
50
51impl NumberOrHex {
52	/// Converts this number into an U256.
53	pub fn into_u256(self) -> U256 {
54		match self {
55			NumberOrHex::Number(n) => n.into(),
56			NumberOrHex::Hex(h) => h,
57		}
58	}
59}
60
61impl From<u32> for NumberOrHex {
62	fn from(n: u32) -> Self {
63		NumberOrHex::Number(n.into())
64	}
65}
66
67impl From<u64> for NumberOrHex {
68	fn from(n: u64) -> Self {
69		NumberOrHex::Number(n)
70	}
71}
72
73impl From<u128> for NumberOrHex {
74	fn from(n: u128) -> Self {
75		NumberOrHex::Hex(n.into())
76	}
77}
78
79impl From<U256> for NumberOrHex {
80	fn from(n: U256) -> Self {
81		NumberOrHex::Hex(n)
82	}
83}
84
85/// An error type that signals an out-of-range conversion attempt.
86pub struct TryFromIntError(pub(crate) ());
87
88impl TryFrom<NumberOrHex> for u32 {
89	type Error = TryFromIntError;
90	fn try_from(num_or_hex: NumberOrHex) -> Result<u32, Self::Error> {
91		num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
92	}
93}
94
95impl TryFrom<NumberOrHex> for u64 {
96	type Error = TryFromIntError;
97	fn try_from(num_or_hex: NumberOrHex) -> Result<u64, Self::Error> {
98		num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
99	}
100}
101
102impl TryFrom<NumberOrHex> for u128 {
103	type Error = TryFromIntError;
104	fn try_from(num_or_hex: NumberOrHex) -> Result<u128, Self::Error> {
105		num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
106	}
107}
108
109impl From<NumberOrHex> for U256 {
110	fn from(num_or_hex: NumberOrHex) -> U256 {
111		num_or_hex.into_u256()
112	}
113}