loom_broadcast_flashbots/client/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::str::FromStr;

use alloy_primitives::{Address, U256, U64};
use serde::{de, Deserialize};
use serde_json::Value;

pub fn deserialize_u64<'de, D>(deserializer: D) -> Result<U64, D::Error>
where
    D: de::Deserializer<'de>,
{
    Ok(match Value::deserialize(deserializer)? {
        Value::String(s) => {
            if s.as_str() == "0x" {
                return Ok(U64::ZERO);
            }

            if s.as_str().starts_with("0x") {
                U64::from_str_radix(s.as_str(), 16).map_err(de::Error::custom)?
            } else {
                U64::from_str_radix(s.as_str(), 10).map_err(de::Error::custom)?
            }
        }
        Value::Number(num) => U64::from(num.as_u64().ok_or_else(|| de::Error::custom("Invalid number"))?),
        _ => return Err(de::Error::custom("wrong type")),
    })
}

pub fn deserialize_u256<'de, D>(deserializer: D) -> Result<U256, D::Error>
where
    D: de::Deserializer<'de>,
{
    Ok(match Value::deserialize(deserializer)? {
        Value::String(s) => {
            if s.as_str() == "0x" {
                return Ok(U256::ZERO);
            }

            if s.as_str().starts_with("0x") {
                U256::from_str_radix(s.as_str(), 16).map_err(de::Error::custom)?
            } else {
                U256::from_str_radix(s.as_str(), 10).map_err(de::Error::custom)?
            }
        }
        Value::Number(num) => U256::from(num.as_u64().ok_or_else(|| de::Error::custom("Invalid number"))?),
        _ => return Err(de::Error::custom("wrong type")),
    })
}

pub fn deserialize_optional_h160<'de, D>(deserializer: D) -> Result<Option<Address>, D::Error>
where
    D: de::Deserializer<'de>,
{
    Ok(match Value::deserialize(deserializer)? {
        Value::String(s) => {
            if s.as_str() == "0x" {
                return Ok(None);
            }

            Some(Address::from_str(s.as_str()).map_err(de::Error::custom)?)
        }
        _ => return Err(de::Error::custom("expected a hexadecimal string")),
    })
}