Skip to content

Commit 54f2132

Browse files
committed
Migrate alerter to alloy
1 parent 91299bd commit 54f2132

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/alerter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ edition = "2024"
66
license = "MIT OR Apache-2.0"
77

88
[dependencies]
9+
alloy = { workspace = true }
910
anyhow = { workspace = true }
1011
clap = { workspace = true }
1112
humantime = { workspace = true }
1213
observe = { workspace = true }
1314
mimalloc = { workspace = true }
1415
model = { workspace = true }
1516
number = { workspace = true }
16-
primitive-types = { workspace = true }
1717
prometheus = { workspace = true }
1818
reqwest = { workspace = true, features = ["json"] }
1919
serde_with = { workspace = true }

crates/alerter/src/lib.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
// price api (0x). If this is the case it alerts.
55

66
use {
7+
alloy::primitives::{Address, U256, address},
78
anyhow::{Context, Result},
89
clap::Parser,
910
model::order::{BUY_ETH_ADDRESS, OrderClass, OrderKind, OrderStatus, OrderUid},
1011
number::serialization::HexOrDecimalU256,
11-
primitive_types::{H160, U256},
1212
prometheus::IntGauge,
1313
reqwest::Client,
1414
serde_with::serde_as,
@@ -24,10 +24,10 @@ use {
2424
#[serde(rename_all = "camelCase")]
2525
struct Order {
2626
kind: OrderKind,
27-
buy_token: H160,
27+
buy_token: Address,
2828
#[serde_as(as = "HexOrDecimalU256")]
2929
buy_amount: U256,
30-
sell_token: H160,
30+
sell_token: Address,
3131
#[serde_as(as = "HexOrDecimalU256")]
3232
sell_amount: U256,
3333
uid: OrderUid,
@@ -90,12 +90,10 @@ impl OrderBookApi {
9090

9191
// Converts the eth placeholder address to weth. Leaves other addresses
9292
// untouched.
93-
fn convert_eth_to_weth(token: H160) -> H160 {
94-
let weth: H160 = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
95-
.parse()
96-
.unwrap();
97-
if token == BUY_ETH_ADDRESS {
98-
weth
93+
fn convert_eth_to_weth(token: Address) -> Address {
94+
const WETH: Address = address!("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
95+
if token == Address::from_slice(&BUY_ETH_ADDRESS.0) {
96+
WETH
9997
} else {
10098
token
10199
}

crates/number/src/serialization.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,43 @@ use {
55
std::fmt,
66
};
77

8+
/// (De)serialization structure able to deserialize decimal and hexadecimal
9+
/// numbers, serializes as decimal.
810
pub struct HexOrDecimalU256;
911

10-
impl<'de> DeserializeAs<'de, U256> for HexOrDecimalU256 {
11-
fn deserialize_as<D>(deserializer: D) -> Result<U256, D::Error>
12+
impl<'de> DeserializeAs<'de, alloy::primitives::U256> for HexOrDecimalU256 {
13+
fn deserialize_as<D>(deserializer: D) -> Result<alloy::primitives::U256, D::Error>
1214
where
1315
D: Deserializer<'de>,
1416
{
15-
deserialize(deserializer)
17+
struct Visitor {}
18+
impl de::Visitor<'_> for Visitor {
19+
type Value = alloy::primitives::U256;
20+
21+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
22+
write!(
23+
formatter,
24+
"a u256 encoded either as 0x hex prefixed or decimal encoded string"
25+
)
26+
}
27+
28+
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
29+
where
30+
E: de::Error,
31+
{
32+
if s.trim().starts_with("0x") {
33+
alloy::primitives::U256::from_str_radix(s, 16).map_err(|err| {
34+
de::Error::custom(format!("failed to decode {s:?} as hex u256: {err}"))
35+
})
36+
} else {
37+
alloy::primitives::U256::from_str_radix(s, 10).map_err(|err| {
38+
de::Error::custom(format!("failed to decode {s:?} as decimal u256: {err}"))
39+
})
40+
}
41+
}
42+
}
43+
44+
deserializer.deserialize_str(Visitor {})
1645
}
1746
}
1847

@@ -25,6 +54,24 @@ impl SerializeAs<U256> for HexOrDecimalU256 {
2554
}
2655
}
2756

57+
impl<'de> DeserializeAs<'de, U256> for HexOrDecimalU256 {
58+
fn deserialize_as<D>(deserializer: D) -> Result<U256, D::Error>
59+
where
60+
D: Deserializer<'de>,
61+
{
62+
deserialize(deserializer)
63+
}
64+
}
65+
66+
impl SerializeAs<alloy::primitives::U256> for HexOrDecimalU256 {
67+
fn serialize_as<S>(source: &alloy::primitives::U256, serializer: S) -> Result<S::Ok, S::Error>
68+
where
69+
S: Serializer,
70+
{
71+
serializer.serialize_str(&source.to_string())
72+
}
73+
}
74+
2875
pub fn serialize<S>(value: &U256, serializer: S) -> Result<S::Ok, S::Error>
2976
where
3077
S: Serializer,

0 commit comments

Comments
 (0)