1use std::fmt::Display;
17
18use alloy_primitives::U256;
19use nautilus_core::UnixNanos;
20use serde::{Deserialize, Serialize};
21use ustr::Ustr;
22
23use crate::defi::{
24 Blockchain,
25 hex::{
26 deserialize_hex_number, deserialize_hex_timestamp, deserialize_opt_hex_u64,
27 deserialize_opt_hex_u256,
28 },
29};
30
31#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
33pub struct BlockPosition {
34 pub number: u64,
36 pub transaction_hash: String,
38 pub transaction_index: u32,
40 pub log_index: u32,
42}
43
44impl BlockPosition {
45 pub fn new(number: u64, transaction_hash: String, index: u32, log_index: u32) -> Self {
47 Self {
48 number,
49 transaction_hash,
50 transaction_index: index,
51 log_index,
52 }
53 }
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
58#[serde(rename_all = "camelCase")]
59#[cfg_attr(
60 feature = "python",
61 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
62)]
63pub struct Block {
64 #[serde(skip)]
66 pub chain: Option<Blockchain>, pub hash: String,
69 #[serde(deserialize_with = "deserialize_hex_number")]
71 pub number: u64,
72 pub parent_hash: String,
74 pub miner: Ustr,
76 #[serde(deserialize_with = "deserialize_hex_number")]
78 pub gas_limit: u64,
79 #[serde(deserialize_with = "deserialize_hex_number")]
81 pub gas_used: u64,
82 #[serde(default, deserialize_with = "deserialize_opt_hex_u256")]
84 pub base_fee_per_gas: Option<U256>,
85 #[serde(default, deserialize_with = "deserialize_opt_hex_u256")]
87 pub blob_gas_used: Option<U256>,
88 #[serde(default, deserialize_with = "deserialize_opt_hex_u256")]
90 pub excess_blob_gas: Option<U256>,
91 #[serde(default, deserialize_with = "deserialize_opt_hex_u256")]
93 pub l1_gas_price: Option<U256>,
94 #[serde(default, deserialize_with = "deserialize_opt_hex_u64")]
96 pub l1_gas_used: Option<u64>,
97 #[serde(default, deserialize_with = "deserialize_opt_hex_u64")]
99 pub l1_fee_scalar: Option<u64>,
100 #[serde(deserialize_with = "deserialize_hex_timestamp")]
102 pub timestamp: UnixNanos,
103}
104
105impl Block {
106 #[allow(clippy::too_many_arguments)]
108 pub fn new(
109 hash: String,
110 parent_hash: String,
111 number: u64,
112 miner: Ustr,
113 gas_limit: u64,
114 gas_used: u64,
115 timestamp: UnixNanos,
116 chain: Option<Blockchain>,
117 ) -> Self {
118 Self {
119 chain,
120 hash,
121 parent_hash,
122 number,
123 miner,
124 gas_used,
125 gas_limit,
126 timestamp,
127 base_fee_per_gas: None,
128 blob_gas_used: None,
129 excess_blob_gas: None,
130 l1_gas_price: None,
131 l1_gas_used: None,
132 l1_fee_scalar: None,
133 }
134 }
135
136 pub fn chain(&self) -> Blockchain {
142 if let Some(chain) = self.chain {
143 chain
144 } else {
145 panic!("Must have the `chain` field set")
146 }
147 }
148
149 pub fn set_chain(&mut self, chain: Blockchain) {
150 self.chain = Some(chain)
151 }
152
153 #[must_use]
155 pub fn with_base_fee(mut self, fee: U256) -> Self {
156 self.base_fee_per_gas = Some(fee);
157 self
158 }
159
160 #[must_use]
162 pub fn with_blob_gas(mut self, used: U256, excess: U256) -> Self {
163 self.blob_gas_used = Some(used);
164 self.excess_blob_gas = Some(excess);
165 self
166 }
167
168 #[must_use]
170 pub fn with_l1_fee_components(mut self, price: U256, gas_used: u64, scalar: u64) -> Self {
171 self.l1_gas_price = Some(price);
172 self.l1_gas_used = Some(gas_used);
173 self.l1_fee_scalar = Some(scalar);
174 self
175 }
176}
177
178impl PartialEq for Block {
179 fn eq(&self, other: &Self) -> bool {
180 self.hash == other.hash
181 }
182}
183
184impl Eq for Block {}
185
186impl Display for Block {
187 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
188 write!(
189 f,
190 "Block(chain={}, number={}, timestamp={}, hash={})",
191 self.chain(),
192 self.number,
193 self.timestamp.to_rfc3339(),
194 self.hash
195 )
196 }
197}
198
199#[cfg(test)]
200mod tests {
201 use alloy_primitives::U256;
202 use chrono::{TimeZone, Utc};
203 use nautilus_core::UnixNanos;
204 use rstest::{fixture, rstest};
205 use ustr::Ustr;
206
207 use super::Block;
208 use crate::defi::{Blockchain, chain::chains, rpc::RpcNodeWssResponse};
209
210 #[fixture]
211 fn eth_rpc_block_response() -> String {
212 r#"{
214 "jsonrpc":"2.0",
215 "method":"eth_subscription",
216 "params":{
217 "subscription":"0xe06a2375238a4daa8ec823f585a0ef1e",
218 "result":{
219 "baseFeePerGas":"0x1862a795",
220 "blobGasUsed":"0xc0000",
221 "difficulty":"0x0",
222 "excessBlobGas":"0x4840000",
223 "extraData":"0x546974616e2028746974616e6275696c6465722e78797a29",
224 "gasLimit":"0x223b4a1",
225 "gasUsed":"0xde3909",
226 "hash":"0x71ece187051700b814592f62774e6ebd8ebdf5efbb54c90859a7d1522ce38e0a",
227 "miner":"0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97",
228 "mixHash":"0x43adbd4692459c8820b0913b0bc70e8a87bed2d40c395cc41059aa108a7cbe84",
229 "nonce":"0x0000000000000000",
230 "number":"0x1542e9f",
231 "parentBeaconBlockRoot":"0x58673bf001b31af805fb7634fbf3257dde41fbb6ae05c71799b09632d126b5c7",
232 "parentHash":"0x2abcce1ac985ebea2a2d6878a78387158f46de8d6db2cefca00ea36df4030a40",
233 "receiptsRoot":"0x35fead0b79338d4acbbc361014521d227874a1e02d24342ed3e84460df91f271",
234 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
235 "stateRoot":"0x99f29ee8ed6622c6a1520dca86e361029605f76d2e09aa7d3b1f9fc8b0268b13",
236 "timestamp":"0x6801f4bb",
237 "transactionsRoot":"0x9484b18d38886f25a44b465ad0136c792ef67dd5863b102cab2ab7a76bfb707d",
238 "withdrawalsRoot":"0x152f0040f4328639397494ef0d9c02d36c38b73f09588f304084e9f29662e9cb"
239 }
240 }
241 }"#.to_string()
242 }
243
244 #[fixture]
245 fn polygon_rpc_block_response() -> String {
246 r#"{
248 "jsonrpc": "2.0",
249 "method": "eth_subscription",
250 "params": {
251 "subscription": "0x20f7c54c468149ed99648fd09268c903",
252 "result": {
253 "baseFeePerGas": "0x19e",
254 "difficulty": "0x18",
255 "gasLimit": "0x1c9c380",
256 "gasUsed": "0x1270f14",
257 "hash": "0x38ca655a2009e1748097f5559a0c20de7966243b804efeb53183614e4bebe199",
258 "miner": "0x0000000000000000000000000000000000000000",
259 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
260 "nonce": "0x0000000000000000",
261 "number": "0x43309ed",
262 "parentHash": "0xf25e108267e3d6e1e4aaf4e329872273f2b1ad6186a4a22e370623aa8d021c50",
263 "receiptsRoot": "0xfffb93a991d15b9689536e59f20564cc49c254ec41a222d988abe58d2869968c",
264 "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
265 "stateRoot": "0xe66a9bc516bde8fc7b8c1ba0b95bfea0f4574fc6cfe95c68b7f8ab3d3158278d",
266 "timestamp": "0x680250d5",
267 "totalDifficulty": "0x505bd180",
268 "transactionsRoot": "0xd9ebc2fd5c7ce6f69ab2e427da495b0b0dff14386723b8c07b347449fd6293a6"
269 }
270 }
271 }"#.to_string()
272 }
273
274 #[fixture]
275 fn base_rpc_block_response() -> String {
276 r#"{
277 "jsonrpc":"2.0",
278 "method":"eth_subscription",
279 "params":{
280 "subscription":"0xeb7d715d93964e22b2d99192791ca984",
281 "result":{
282 "baseFeePerGas":"0xaae54",
283 "blobGasUsed":"0x0",
284 "difficulty":"0x0",
285 "excessBlobGas":"0x0",
286 "extraData":"0x00000000fa00000002",
287 "gasLimit":"0x7270e00",
288 "gasUsed":"0x56fce26",
289 "hash":"0x14575c65070d455e6d20d5ee17be124917a33ce4437dd8615a56d29e8279b7ad",
290 "logsBloom":"0x02bcf67d7b87f2d884b8d56bbe3965f6becc9ed8f9637ffc67efdffcef446cf435ffec7e7ce8e4544fe782bb06ef37afc97687cbf3c7ee7e26dd12a8f1fd836bc17dd2fd64fce3ef03bc74d8faedb07dddafe6f2cedff3e6f5d8683cc2ef26f763dee76e7b6fdeeade8c8a7cec7a5fdca237be97be2efe67dc908df7ce3f94a3ce150b2a9f07776fa577d5c52dbffe5bfc38bbdfeefc305f0efaf37fba3a4cdabf366b17fcb3b881badbe571dfb2fd652e879fbf37e88dbedb6a6f9f4bb7aef528e81c1f3cda38f777cb0a2d6f0ddb8abcb3dda5d976541fa062dba6255a7b328b5fdf47e8d6fac2fc43d8bee5936e6e8f2bff33526fdf6637f3f2216d950fef",
291 "miner":"0x4200000000000000000000000000000000000011",
292 "mixHash":"0xeacd829463c5d21df523005d55f25a0ca20474f1310c5c7eb29ff2c479789e98",
293 "nonce":"0x0000000000000000",
294 "number":"0x1bca2ac",
295 "parentBeaconBlockRoot":"0xfe4c48425a274a6716c569dfa9c238551330fc39d295123b12bc2461e6f41834",
296 "parentHash":"0x9a6ad4ffb258faa47ecd5eea9e7a9d8fa1772aa6232bc7cb4bbad5bc30786258",
297 "receiptsRoot":"0x5fc932dd358c33f9327a704585c83aafbe0d25d12b62c1cd8282df8b328aac16",
298 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
299 "stateRoot":"0xd2d3a6a219fb155bfc5afbde11f3161f1051d931432ccf32c33affe54176bb18",
300 "timestamp":"0x6803a23b",
301 "transactionsRoot":"0x59726fb9afc101cd49199c70bbdbc28385f4defa02949cb6e20493e16035a59d",
302 "withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
303 }
304 }
305 }"#.to_string()
306 }
307
308 #[fixture]
309 fn arbitrum_rpc_block_response() -> String {
310 r#"{
312 "jsonrpc":"2.0",
313 "method":"eth_subscription",
314 "params":{
315 "subscription":"0x0c5a0b38096440ef9a30a84837cf2012",
316 "result":{
317 "baseFeePerGas":"0x989680",
318 "difficulty":"0x1",
319 "extraData":"0xc66cd959dcdc1baf028efb61140d4461629c53c9643296cbda1c40723e97283b",
320 "gasLimit":"0x4000000000000",
321 "gasUsed":"0x17af4",
322 "hash":"0x724a0af4720fd7624976f71b16163de25f8532e87d0e7058eb0c1d3f6da3c1f8",
323 "miner":"0xa4b000000000000000000073657175656e636572",
324 "mixHash":"0x0000000000023106000000000154528900000000000000200000000000000000",
325 "nonce":"0x00000000001daa7c",
326 "number":"0x138d1ab4",
327 "parentHash":"0xe7176e201c2db109be479770074ad11b979de90ac850432ed38ed335803861b6",
328 "receiptsRoot":"0xefb382e3a4e3169e57920fa2367fc81c98bbfbd13611f57767dee07d3b3f96d4",
329 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
330 "stateRoot":"0x57e5475675abf1ec4c763369342e327a04321d17eeaa730a4ca20a9cafeee380",
331 "timestamp":"0x6803a606",
332 "totalDifficulty":"0x123a3d6c",
333 "transactionsRoot":"0x710b520177ecb31fa9092d16ee593b692070912b99ddd9fcf73eb4e9dd15193d"
334 }
335 }
336 }"#.to_string()
337 }
338
339 #[rstest]
340 fn test_block_set_chain() {
341 let mut block = Block::new(
342 "0x1234567890abcdef".to_string(),
343 "0xabcdef1234567890".to_string(),
344 12345,
345 Ustr::from("0x742E4422b21FB8B4dF463F28689AC98bD56c39e0"),
346 21000,
347 20000,
348 UnixNanos::from(1_640_995_200_000_000_000u64),
349 None,
350 );
351
352 assert!(block.chain.is_none());
353
354 let chain = Blockchain::Ethereum;
355 block.set_chain(chain);
356
357 assert_eq!(block.chain, Some(chain));
358 }
359
360 #[rstest]
361 fn test_ethereum_block_parsing(eth_rpc_block_response: String) {
362 let mut block =
363 match serde_json::from_str::<RpcNodeWssResponse<Block>>(ð_rpc_block_response) {
364 Ok(rpc_response) => rpc_response.params.result,
365 Err(e) => panic!("Failed to deserialize block response with error {e}"),
366 };
367 block.set_chain(Blockchain::Ethereum);
368
369 assert_eq!(
370 block.to_string(),
371 "Block(chain=Ethereum, number=22294175, timestamp=2025-04-18T06:44:11+00:00, hash=0x71ece187051700b814592f62774e6ebd8ebdf5efbb54c90859a7d1522ce38e0a)".to_string(),
372 );
373 assert_eq!(
374 block.hash,
375 "0x71ece187051700b814592f62774e6ebd8ebdf5efbb54c90859a7d1522ce38e0a"
376 );
377 assert_eq!(
378 block.parent_hash,
379 "0x2abcce1ac985ebea2a2d6878a78387158f46de8d6db2cefca00ea36df4030a40"
380 );
381 assert_eq!(block.number, 22294175);
382 assert_eq!(block.miner, "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97");
383 assert_eq!(
385 block.timestamp,
386 UnixNanos::from(Utc.with_ymd_and_hms(2025, 4, 18, 6, 44, 11).unwrap())
387 );
388 assert_eq!(block.gas_used, 14563593);
389 assert_eq!(block.gas_limit, 35894433);
390
391 assert_eq!(block.base_fee_per_gas, Some(U256::from(0x1862a795u64)));
392 assert_eq!(block.blob_gas_used, Some(U256::from(0xc0000u64)));
393 assert_eq!(block.excess_blob_gas, Some(U256::from(0x4840000u64)));
394 }
395
396 #[rstest]
397 fn test_polygon_block_parsing(polygon_rpc_block_response: String) {
398 let mut block =
399 match serde_json::from_str::<RpcNodeWssResponse<Block>>(&polygon_rpc_block_response) {
400 Ok(rpc_response) => rpc_response.params.result,
401 Err(e) => panic!("Failed to deserialize block response with error {e}"),
402 };
403 block.set_chain(Blockchain::Polygon);
404
405 assert_eq!(
406 block.to_string(),
407 "Block(chain=Polygon, number=70453741, timestamp=2025-04-18T13:17:09+00:00, hash=0x38ca655a2009e1748097f5559a0c20de7966243b804efeb53183614e4bebe199)".to_string(),
408 );
409 assert_eq!(
410 block.hash,
411 "0x38ca655a2009e1748097f5559a0c20de7966243b804efeb53183614e4bebe199"
412 );
413 assert_eq!(
414 block.parent_hash,
415 "0xf25e108267e3d6e1e4aaf4e329872273f2b1ad6186a4a22e370623aa8d021c50"
416 );
417 assert_eq!(block.number, 70453741);
418 assert_eq!(block.miner, "0x0000000000000000000000000000000000000000");
419 assert_eq!(
421 block.timestamp,
422 UnixNanos::from(Utc.with_ymd_and_hms(2025, 4, 18, 13, 17, 9).unwrap())
423 );
424 assert_eq!(block.gas_used, 19336980);
425 assert_eq!(block.gas_limit, 30000000);
426 assert_eq!(block.base_fee_per_gas, Some(U256::from(0x19eu64)));
427 assert!(block.blob_gas_used.is_none()); assert!(block.excess_blob_gas.is_none()); }
430
431 #[rstest]
432 fn test_base_block_parsing(base_rpc_block_response: String) {
433 let mut block =
434 match serde_json::from_str::<RpcNodeWssResponse<Block>>(&base_rpc_block_response) {
435 Ok(rpc_response) => rpc_response.params.result,
436 Err(e) => panic!("Failed to deserialize block response with error {e}"),
437 };
438 block.set_chain(Blockchain::Base);
439
440 assert_eq!(
441 block.to_string(),
442 "Block(chain=Base, number=29139628, timestamp=2025-04-19T13:16:43+00:00, hash=0x14575c65070d455e6d20d5ee17be124917a33ce4437dd8615a56d29e8279b7ad)".to_string(),
443 );
444 assert_eq!(
445 block.hash,
446 "0x14575c65070d455e6d20d5ee17be124917a33ce4437dd8615a56d29e8279b7ad"
447 );
448 assert_eq!(
449 block.parent_hash,
450 "0x9a6ad4ffb258faa47ecd5eea9e7a9d8fa1772aa6232bc7cb4bbad5bc30786258"
451 );
452 assert_eq!(block.number, 29139628);
453 assert_eq!(block.miner, "0x4200000000000000000000000000000000000011");
454 assert_eq!(
456 block.timestamp,
457 UnixNanos::from(Utc.with_ymd_and_hms(2025, 4, 19, 13, 16, 43).unwrap())
458 );
459 assert_eq!(block.gas_used, 91213350);
460 assert_eq!(block.gas_limit, 120000000);
461
462 assert_eq!(block.base_fee_per_gas, Some(U256::from(0xaae54u64)));
463 assert_eq!(block.blob_gas_used, Some(U256::ZERO));
464 assert_eq!(block.excess_blob_gas, Some(U256::ZERO));
465 }
466
467 #[rstest]
468 fn test_arbitrum_block_parsing(arbitrum_rpc_block_response: String) {
469 let mut block =
470 match serde_json::from_str::<RpcNodeWssResponse<Block>>(&arbitrum_rpc_block_response) {
471 Ok(rpc_response) => rpc_response.params.result,
472 Err(e) => panic!("Failed to deserialize block response with error {e}"),
473 };
474 block.set_chain(Blockchain::Arbitrum);
475
476 assert_eq!(
477 block.to_string(),
478 "Block(chain=Arbitrum, number=328014516, timestamp=2025-04-19T13:32:54+00:00, hash=0x724a0af4720fd7624976f71b16163de25f8532e87d0e7058eb0c1d3f6da3c1f8)".to_string(),
479 );
480 assert_eq!(
481 block.hash,
482 "0x724a0af4720fd7624976f71b16163de25f8532e87d0e7058eb0c1d3f6da3c1f8"
483 );
484 assert_eq!(
485 block.parent_hash,
486 "0xe7176e201c2db109be479770074ad11b979de90ac850432ed38ed335803861b6"
487 );
488 assert_eq!(block.number, 328014516);
489 assert_eq!(block.miner, "0xa4b000000000000000000073657175656e636572");
490 assert_eq!(
492 block.timestamp,
493 UnixNanos::from(Utc.with_ymd_and_hms(2025, 4, 19, 13, 32, 54).unwrap())
494 );
495 assert_eq!(block.gas_used, 97012);
496 assert_eq!(block.gas_limit, 1125899906842624);
497
498 assert_eq!(block.base_fee_per_gas, Some(U256::from(0x989680u64)));
499 assert!(block.blob_gas_used.is_none());
500 assert!(block.excess_blob_gas.is_none());
501 }
502
503 #[rstest]
504 fn test_block_builder_helpers() {
505 let block = Block::new(
506 "0xabc".into(),
507 "0xdef".into(),
508 1,
509 Ustr::from("0x0000000000000000000000000000000000000000"),
510 100_000,
511 50_000,
512 UnixNanos::from(1_700_000_000u64),
513 Some(Blockchain::Arbitrum),
514 );
515
516 let block = block
517 .with_base_fee(U256::from(1_000u64))
518 .with_blob_gas(U256::from(0x10u8), U256::from(0x20u8))
519 .with_l1_fee_components(U256::from(30_000u64), 1_234, 1_000_000);
520
521 assert_eq!(block.chain, Some(chains::ARBITRUM.name));
522 assert_eq!(block.base_fee_per_gas, Some(U256::from(1_000u64)));
523 assert_eq!(block.blob_gas_used, Some(U256::from(0x10u8)));
524 assert_eq!(block.excess_blob_gas, Some(U256::from(0x20u8)));
525 assert_eq!(block.l1_gas_price, Some(U256::from(30_000u64)));
526 assert_eq!(block.l1_gas_used, Some(1_234));
527 assert_eq!(block.l1_fee_scalar, Some(1_000_000));
528 }
529}