1use alloy::primitives::{Address, B256, Bytes, U256};
17use nautilus_model::defi::{Block, DexType, PoolIdentifier, rpc::RpcLog};
18use serde::Deserialize;
19use serde_json::Value;
20
21use crate::events::{
22 burn::BurnEvent, collect::CollectEvent, fee_protocol_collect::FeeProtocolCollectEvent,
23 fee_protocol_update::FeeProtocolUpdateEvent, flash::FlashEvent, mint::MintEvent,
24 swap::SwapEvent,
25};
26
27#[derive(Debug, Clone)]
29pub enum BlockchainMessage {
30 Block(Block),
31 SwapEvent(SwapEvent),
32 MintEvent(MintEvent),
33 BurnEvent(BurnEvent),
34 CollectEvent(CollectEvent),
35 FlashEvent(FlashEvent),
36 FeeProtocolUpdateEvent(FeeProtocolUpdateEvent),
37 FeeProtocolCollectEvent(FeeProtocolCollectEvent),
38 PoolLogRemoved {
39 pool_identifier: PoolIdentifier,
40 transaction_hash: String,
41 log_index: u32,
42 },
43}
44
45#[derive(Debug, Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq)]
50pub enum RpcEventType {
51 NewBlock,
52 PoolSwap(DexType),
53 PoolMint(DexType),
54 PoolBurn(DexType),
55 PoolCollect(DexType),
56 PoolFlash(DexType),
57 PoolFeeProtocolUpdate(DexType),
58 PoolFeeProtocolCollect(DexType),
59}
60
61#[cfg(feature = "hypersync")]
63#[derive(Debug, Clone, PartialEq, Eq)]
64pub(crate) enum RpcCallResult {
65 Success(Bytes),
66 Reverted,
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
71#[serde(rename_all = "camelCase")]
72pub struct RpcBlock {
73 #[serde(deserialize_with = "deserialize_hex_u64")]
75 pub number: u64,
76 pub hash: B256,
78 pub parent_hash: B256,
80 #[serde(deserialize_with = "deserialize_hex_u64")]
82 pub timestamp: u64,
83 #[serde(default, deserialize_with = "deserialize_hex_u128_opt")]
85 pub base_fee_per_gas: Option<u128>,
86 #[serde(skip)]
88 pub transactions: Vec<RpcTransaction>,
89}
90
91#[derive(Debug, Deserialize)]
92pub(crate) struct RpcBlockResponse {
93 #[serde(flatten)]
94 pub block: RpcBlock,
95 #[serde(default)]
96 pub transactions: Vec<Value>,
97}
98
99#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
101#[serde(rename_all = "camelCase")]
102pub struct RpcTransaction {
103 pub hash: B256,
105 pub from: Address,
107 #[serde(deserialize_with = "deserialize_hex_u64")]
109 pub nonce: u64,
110 #[serde(default, deserialize_with = "deserialize_hex_u64_opt")]
112 pub chain_id: Option<u64>,
113 #[serde(default, rename = "type", deserialize_with = "deserialize_hex_u8_opt")]
115 pub transaction_type: Option<u8>,
116 pub to: Option<Address>,
118 pub input: Bytes,
120 pub value: U256,
122 #[serde(default, deserialize_with = "deserialize_hex_u64_opt")]
124 pub gas: Option<u64>,
125 pub max_fee_per_gas: Option<U256>,
127 pub max_priority_fee_per_gas: Option<U256>,
129}
130
131#[derive(Debug, Clone, Deserialize)]
133#[serde(rename_all = "camelCase")]
134pub struct RpcTransactionReceipt {
135 pub transaction_hash: B256,
137 pub block_hash: B256,
139 #[serde(deserialize_with = "deserialize_hex_u64")]
141 pub block_number: u64,
142 #[serde(deserialize_with = "deserialize_hex_u64")]
144 pub gas_used: u64,
145 pub effective_gas_price: U256,
147 #[serde(deserialize_with = "deserialize_hex_u64")]
149 pub transaction_index: u64,
150 #[serde(deserialize_with = "deserialize_hex_bool")]
152 pub status: bool,
153 #[serde(default)]
155 pub logs: Vec<RpcLog>,
156}
157
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
160#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
161pub enum RpcCallType {
162 Call,
163 Callcode,
164 Delegatecall,
165 Staticcall,
166 Create,
167 Create2,
168 Selfdestruct,
169}
170
171#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
173#[serde(rename_all = "camelCase")]
174pub struct RpcCallTrace {
175 #[serde(rename = "type")]
177 pub call_type: RpcCallType,
178 pub from: Address,
180 pub to: Option<Address>,
182 #[serde(default)]
184 pub value: U256,
185 #[serde(deserialize_with = "deserialize_hex_u64")]
187 pub gas: u64,
188 #[serde(deserialize_with = "deserialize_hex_u64")]
190 pub gas_used: u64,
191 #[serde(default)]
193 pub input: Bytes,
194 #[serde(default)]
196 pub output: Bytes,
197 #[serde(default)]
199 pub error: Option<String>,
200 #[serde(default)]
202 pub calls: Vec<Self>,
203}
204
205fn deserialize_hex_u64<'de, D>(deserializer: D) -> Result<u64, D::Error>
206where
207 D: serde::Deserializer<'de>,
208{
209 let s = String::deserialize(deserializer)?;
210 let value = parse_hex_quantity(&s).map_err(serde::de::Error::custom)?;
211 u64::try_from(value).map_err(serde::de::Error::custom)
212}
213
214fn deserialize_hex_u64_opt<'de, D>(deserializer: D) -> Result<Option<u64>, D::Error>
215where
216 D: serde::Deserializer<'de>,
217{
218 let value = Option::<String>::deserialize(deserializer)?
219 .map(|value| parse_hex_quantity(&value))
220 .transpose()
221 .map_err(serde::de::Error::custom)?;
222 value
223 .map(u64::try_from)
224 .transpose()
225 .map_err(serde::de::Error::custom)
226}
227
228fn deserialize_hex_u8_opt<'de, D>(deserializer: D) -> Result<Option<u8>, D::Error>
229where
230 D: serde::Deserializer<'de>,
231{
232 let value = Option::<String>::deserialize(deserializer)?
233 .map(|value| parse_hex_quantity(&value))
234 .transpose()
235 .map_err(serde::de::Error::custom)?;
236 value
237 .map(u8::try_from)
238 .transpose()
239 .map_err(serde::de::Error::custom)
240}
241
242fn deserialize_hex_u128_opt<'de, D>(deserializer: D) -> Result<Option<u128>, D::Error>
243where
244 D: serde::Deserializer<'de>,
245{
246 let s: Option<String> = Option::deserialize(deserializer)?;
247 s.map(|s| parse_hex_quantity(&s).map_err(serde::de::Error::custom))
248 .transpose()
249}
250
251fn deserialize_hex_bool<'de, D>(deserializer: D) -> Result<bool, D::Error>
252where
253 D: serde::Deserializer<'de>,
254{
255 let s = String::deserialize(deserializer)?;
256 match s.as_str() {
257 "0x0" => Ok(false),
258 "0x1" => Ok(true),
259 _ => Err(serde::de::Error::custom(
260 "invalid transaction receipt status; expected 0x0 or 0x1",
261 )),
262 }
263}
264
265fn parse_hex_quantity(s: &str) -> anyhow::Result<u128> {
266 let stripped = s.strip_prefix("0x").unwrap_or(s);
267 u128::from_str_radix(stripped, 16)
268 .map_err(|e| anyhow::anyhow!("Failed to parse hex quantity '{s}': {e}"))
269}