1use std::fmt::Display;
2
3use crypto_bigint::U256;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct AmountValue(pub U256);
8
9impl From<u8> for AmountValue {
10 fn from(value: u8) -> Self {
11 AmountValue(U256::from(value))
12 }
13}
14
15impl From<u16> for AmountValue {
16 fn from(value: u16) -> Self {
17 AmountValue(U256::from(value))
18 }
19}
20
21impl From<u32> for AmountValue {
22 fn from(value: u32) -> Self {
23 AmountValue(U256::from(value))
24 }
25}
26
27impl From<u64> for AmountValue {
28 fn from(value: u64) -> Self {
29 AmountValue(U256::from(value))
30 }
31}
32
33impl From<u128> for AmountValue {
34 fn from(value: u128) -> Self {
35 AmountValue(U256::from(value))
36 }
37}
38
39impl Display for AmountValue {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 write!(f, "{}", self.0)
42 }
43}
44
45impl Serialize for AmountValue {
46 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
47 where
48 S: serde::Serializer,
49 {
50 serializer.serialize_str(&self.to_string())
51 }
52}
53
54impl<'de> Deserialize<'de> for AmountValue {
55 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
56 where
57 D: serde::Deserializer<'de>,
58 {
59 let s = String::deserialize(deserializer)?;
60 let value = U256::from_str_radix_vartime(&s, 10).map_err(serde::de::Error::custom)?;
61 Ok(AmountValue(value))
62 }
63}