use alloy::primitives::U256;
use hypersync_client::format::Hex;
use nautilus_core::{UnixNanos, datetime::NANOSECONDS_IN_SECOND};
use nautilus_model::defi::{Block, Blockchain, hex::from_str_hex_to_u64};
use ustr::Ustr;
pub fn transform_hypersync_block(
chain: Blockchain,
received_block: hypersync_client::simple_types::Block,
) -> Result<Block, anyhow::Error> {
let number = received_block
.number
.ok_or_else(|| anyhow::anyhow!("Missing block number"))?;
let gas_limit = from_str_hex_to_u64(
received_block
.gas_limit
.ok_or_else(|| anyhow::anyhow!("Missing gas limit"))?
.encode_hex()
.as_str(),
)?;
let gas_used = from_str_hex_to_u64(
received_block
.gas_used
.ok_or_else(|| anyhow::anyhow!("Missing gas used"))?
.encode_hex()
.as_str(),
)?;
let timestamp = from_str_hex_to_u64(
received_block
.timestamp
.ok_or_else(|| anyhow::anyhow!("Missing timestamp"))?
.encode_hex()
.as_str(),
)?;
let mut block = Block::new(
received_block
.hash
.ok_or_else(|| anyhow::anyhow!("Missing hash"))?
.to_string(),
received_block
.parent_hash
.ok_or_else(|| anyhow::anyhow!("Missing parent hash"))?
.to_string(),
number,
Ustr::from(
received_block
.miner
.ok_or_else(|| anyhow::anyhow!("Missing miner"))?
.to_string()
.as_str(),
),
gas_limit,
gas_used,
UnixNanos::new(timestamp * NANOSECONDS_IN_SECOND),
Some(chain),
);
if let Some(base_fee_hex) = received_block.base_fee_per_gas {
let s = base_fee_hex.encode_hex();
let val = U256::from_str_radix(s.trim_start_matches("0x"), 16)?;
block = block.with_base_fee(val);
}
if let (Some(used_hex), Some(excess_hex)) =
(received_block.blob_gas_used, received_block.excess_blob_gas)
{
let used = U256::from_str_radix(used_hex.encode_hex().trim_start_matches("0x"), 16)?;
let excess = U256::from_str_radix(excess_hex.encode_hex().trim_start_matches("0x"), 16)?;
block = block.with_blob_gas(used, excess);
}
Ok(block)
}
#[cfg(test)]
mod tests {}