1use alloc::vec::Vec;
22use gear_core_errors::ReplyCode;
23use parity_scale_codec::{Decode, Encode};
24use scale_decode::DecodeAsType;
25use scale_encode::EncodeAsType;
26use scale_info::TypeInfo;
27
28#[derive(
32 Clone, Debug, Default, PartialEq, Eq, Encode, EncodeAsType, Decode, DecodeAsType, TypeInfo,
33)]
34#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
35pub struct GasInfo {
36 pub min_limit: u64,
38 pub reserved: u64,
40 pub burned: u64,
42 pub may_be_returned: u64,
44 pub waited: bool,
48}
49
50#[derive(
54 Clone, Debug, PartialEq, Eq, Encode, EncodeAsType, Decode, DecodeAsType, TypeInfo, Hash,
55)]
56#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
57pub struct ReplyInfo {
58 #[cfg_attr(feature = "std", serde(with = "impl_serde::serialize"))]
60 pub payload: Vec<u8>,
61 pub value: u128,
63 pub code: ReplyCode,
65}
66
67#[derive(
69 Clone,
70 Copy,
71 Debug,
72 Default,
73 PartialEq,
74 Eq,
75 Encode,
76 EncodeAsType,
77 Decode,
78 DecodeAsType,
79 TypeInfo,
80 derive_more::From,
81 derive_more::Into,
82)]
83pub struct RpcValue(pub u128);
84
85#[cfg(feature = "std")]
86impl<'de> serde::Deserialize<'de> for RpcValue {
87 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
88 where
89 D: serde::Deserializer<'de>,
90 {
91 use alloc::format;
92 use core::fmt;
93 use serde::de::{self, Visitor};
94
95 struct RpcValueVisitor;
96
97 impl<'de> Visitor<'de> for RpcValueVisitor {
98 type Value = RpcValue;
99
100 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
101 f.write_str("a numeric literal, a 0x-prefixed string with big-endian bytes, or a numeric string representing a u128; for large integer literals, consider using string options for clarity and to avoid potential parsing issues")
102 }
103
104 fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E> {
105 Ok(RpcValue(v))
106 }
107
108 fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> {
109 Ok(RpcValue(v as u128))
110 }
111
112 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
113 where
114 E: de::Error,
115 {
116 if let Some(hex) = v.strip_prefix("0x") {
117 let bytes = hex::decode(hex)
118 .map_err(|e| E::custom(format!("invalid hex string: {e}")))?;
119
120 if bytes.len() > 16 {
121 return Err(E::custom("invalid hex string: too long for u128"));
122 }
123
124 let mut padded = [0u8; 16];
126 padded[16 - bytes.len()..].copy_from_slice(&bytes);
127
128 Ok(RpcValue(u128::from_be_bytes(padded)))
129 } else {
130 v.parse::<u128>()
131 .map(RpcValue)
132 .map_err(|e| E::custom(format!("invalid numeric string: {e}")))
133 }
134 }
135 }
136
137 deserializer.deserialize_any(RpcValueVisitor)
138 }
139}