|
1 |
| -use anyhow::Result; |
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use ethers::types::{ |
| 3 | + Address, Block as EthersBlock, Bytes, Transaction as EthersTransaction, H256, U256, U64, |
| 4 | +}; |
2 | 5 | use serde::{Deserialize, Serialize};
|
3 | 6 |
|
4 |
| -use crate::types::{BlobEntity, BlockEntity, TransactionEntity}; |
| 7 | +use crate::{beacon_client::types::BlobData, utils::web3::calculate_versioned_hash}; |
| 8 | + |
| 9 | +#[derive(Serialize, Deserialize, Debug)] |
| 10 | +pub struct BlockEntity { |
| 11 | + pub number: U64, |
| 12 | + pub hash: H256, |
| 13 | + pub timestamp: U256, |
| 14 | + pub slot: u32, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Serialize, Deserialize, Debug)] |
| 18 | +pub struct TransactionEntity { |
| 19 | + pub hash: H256, |
| 20 | + pub from: Address, |
| 21 | + pub to: Address, |
| 22 | + #[serde(rename = "blockNumber")] |
| 23 | + pub block_number: U64, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Serialize, Deserialize, Debug)] |
| 27 | +pub struct BlobEntity { |
| 28 | + #[serde(rename = "versionedHash")] |
| 29 | + pub versioned_hash: H256, |
| 30 | + pub commitment: String, |
| 31 | + pub data: Bytes, |
| 32 | + #[serde(rename = "txHash")] |
| 33 | + pub tx_hash: H256, |
| 34 | + pub index: u32, |
| 35 | +} |
5 | 36 |
|
6 | 37 | #[derive(Serialize, Deserialize, Debug)]
|
7 | 38 | pub struct SlotResponse {
|
@@ -33,3 +64,77 @@ pub enum BlobscanClientError {
|
33 | 64 | }
|
34 | 65 |
|
35 | 66 | pub type BlobscanClientResult<T> = Result<T, BlobscanClientError>;
|
| 67 | + |
| 68 | +impl<'a> TryFrom<(&'a EthersBlock<EthersTransaction>, u32)> for BlockEntity { |
| 69 | + type Error = anyhow::Error; |
| 70 | + |
| 71 | + fn try_from( |
| 72 | + (ethers_block, slot): (&'a EthersBlock<EthersTransaction>, u32), |
| 73 | + ) -> Result<Self, Self::Error> { |
| 74 | + let number = ethers_block |
| 75 | + .number |
| 76 | + .with_context(|| format!("Missing block number field in execution block"))?; |
| 77 | + |
| 78 | + Ok(Self { |
| 79 | + number, |
| 80 | + hash: ethers_block |
| 81 | + .hash |
| 82 | + .with_context(|| format!("Missing block hash field in execution block {number}"))?, |
| 83 | + timestamp: ethers_block.timestamp, |
| 84 | + slot, |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl<'a> TryFrom<(&'a EthersTransaction, &'a EthersBlock<EthersTransaction>)> |
| 90 | + for TransactionEntity |
| 91 | +{ |
| 92 | + type Error = anyhow::Error; |
| 93 | + |
| 94 | + fn try_from( |
| 95 | + (ethers_tx, ethers_block): (&'a EthersTransaction, &'a EthersBlock<EthersTransaction>), |
| 96 | + ) -> Result<Self, Self::Error> { |
| 97 | + let hash = ethers_tx.hash; |
| 98 | + |
| 99 | + Ok(Self { |
| 100 | + block_number: ethers_block |
| 101 | + .number |
| 102 | + .with_context(|| format!("Missing block number field in execution block"))?, |
| 103 | + hash, |
| 104 | + from: ethers_tx.from, |
| 105 | + to: ethers_tx |
| 106 | + .to |
| 107 | + .with_context(|| format!("Missing to field in transaction {hash}"))?, |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl<'a> TryFrom<(&'a BlobData, u32, H256)> for BlobEntity { |
| 113 | + type Error = anyhow::Error; |
| 114 | + |
| 115 | + fn try_from( |
| 116 | + (blob_data, index, tx_hash): (&'a BlobData, u32, H256), |
| 117 | + ) -> Result<Self, Self::Error> { |
| 118 | + Ok(Self { |
| 119 | + tx_hash, |
| 120 | + index, |
| 121 | + commitment: blob_data.kzg_commitment.clone(), |
| 122 | + data: blob_data.blob.clone(), |
| 123 | + versioned_hash: calculate_versioned_hash(&blob_data.kzg_commitment)?, |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl<'a> From<(&'a BlobData, &'a H256, usize, &'a H256)> for BlobEntity { |
| 129 | + fn from( |
| 130 | + (blob_data, versioned_hash, index, tx_hash): (&'a BlobData, &'a H256, usize, &'a H256), |
| 131 | + ) -> Self { |
| 132 | + Self { |
| 133 | + tx_hash: tx_hash.clone(), |
| 134 | + index: index as u32, |
| 135 | + commitment: blob_data.kzg_commitment.clone(), |
| 136 | + data: blob_data.blob.clone(), |
| 137 | + versioned_hash: versioned_hash.clone(), |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments