Skip to content

Commit ab69366

Browse files
committed
graph/ethereum: Use the From<> impls to reduce code redundancy
1 parent bbb2910 commit ab69366

File tree

1 file changed

+24
-104
lines changed

1 file changed

+24
-104
lines changed

graph/src/components/ethereum/types.rs

Lines changed: 24 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -382,72 +382,6 @@ pub struct FullEthereumBlockDataWithReceipts {
382382
pub transaction_receipts: Vec<EthereumTransactionReceiptData>,
383383
}
384384

385-
impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockDataWithReceipts {
386-
type Error = String;
387-
388-
fn try_from(
389-
block: &'a EthereumBlockType,
390-
) -> Result<FullEthereumBlockDataWithReceipts, Self::Error> {
391-
let fullblock = match block {
392-
EthereumBlockType::FullWithReceipts(full_block) => full_block,
393-
EthereumBlockType::Full(_) => return Err(format!(
394-
"Failed to convert EthereumBlockType to FullEthereumBlockDataWithReceipts, requires a EthereumBlockType::FullWithReceipts()"
395-
)),
396-
EthereumBlockType::Light(_) => return Err(format!(
397-
"Failed to convert EthereumBlockType to FullEthereumBlockDataWithReceipts, requires a EthereumBlockType::FullWithReceipts()"
398-
))
399-
};
400-
let block = &fullblock.block;
401-
let transaction_receipts_data = block
402-
.transactions
403-
.iter()
404-
.cloned()
405-
.zip(fullblock.transaction_receipts.iter().cloned())
406-
.map(|transaction_and_receipt| {
407-
assert_eq!(
408-
transaction_and_receipt.0.hash,
409-
transaction_and_receipt.1.transaction_hash
410-
);
411-
EthereumTransactionReceiptData {
412-
hash: transaction_and_receipt.0.hash,
413-
index: transaction_and_receipt.1.transaction_index,
414-
cumulative_gas_used: transaction_and_receipt.1.cumulative_gas_used,
415-
gas_used: transaction_and_receipt.1.gas_used,
416-
contract_address: transaction_and_receipt.1.contract_address,
417-
status: transaction_and_receipt.1.status,
418-
root: transaction_and_receipt.1.root,
419-
420-
// from txs
421-
from: transaction_and_receipt.0.from,
422-
to: transaction_and_receipt.0.to,
423-
value: transaction_and_receipt.0.value,
424-
gas_price: transaction_and_receipt.0.gas_price,
425-
gas: transaction_and_receipt.0.gas,
426-
input: transaction_and_receipt.0.input,
427-
}
428-
})
429-
.collect::<Vec<EthereumTransactionReceiptData>>();
430-
431-
Ok(FullEthereumBlockDataWithReceipts {
432-
hash: block.hash.unwrap(),
433-
parent_hash: block.parent_hash,
434-
uncles_hash: block.uncles_hash,
435-
author: block.author,
436-
state_root: block.state_root,
437-
transactions_root: block.transactions_root,
438-
receipts_root: block.receipts_root,
439-
number: block.number.unwrap(),
440-
gas_used: block.gas_used,
441-
gas_limit: block.gas_limit,
442-
timestamp: block.timestamp,
443-
difficulty: block.difficulty,
444-
total_difficulty: block.total_difficulty.unwrap_or_default(),
445-
size: block.size,
446-
transaction_receipts: transaction_receipts_data,
447-
})
448-
}
449-
}
450-
451385
impl<'a> From<&'a EthereumBlock> for FullEthereumBlockDataWithReceipts {
452386
fn from(block: &'a EthereumBlock) -> FullEthereumBlockDataWithReceipts {
453387
let transaction_receipts_data = block
@@ -502,6 +436,25 @@ impl<'a> From<&'a EthereumBlock> for FullEthereumBlockDataWithReceipts {
502436
}
503437
}
504438

439+
impl<'a> TryFrom<&'a EthereumBlockType> for FullEthereumBlockDataWithReceipts {
440+
type Error = anyhow::Error;
441+
442+
fn try_from(
443+
block: &'a EthereumBlockType,
444+
) -> Result<FullEthereumBlockDataWithReceipts, Self::Error> {
445+
let fullblock = match block {
446+
EthereumBlockType::FullWithReceipts(full_block) => full_block,
447+
EthereumBlockType::Full(_) => return Err(anyhow::anyhow!(
448+
"Failed to convert EthereumBlockType to FullEthereumBlockDataWithReceipts, requires an EthereumBlockType::FullWithReceipts()"
449+
)),
450+
EthereumBlockType::Light(_) => return Err(anyhow::anyhow!(
451+
"Failed to convert EthereumBlockType to FullEthereumBlockDataWithReceipts, requires an EthereumBlockType::FullWithReceipts()"
452+
))
453+
};
454+
Ok(fullblock.into())
455+
}
456+
}
457+
505458
/// Ethereum block data.
506459
#[derive(Clone, Debug, Default)]
507460
pub struct FullEthereumBlockData {
@@ -550,33 +503,8 @@ impl<'a> From<&'a LightEthereumBlock> for FullEthereumBlockData {
550503

551504
impl<'a> From<&'a EthereumBlockType> for FullEthereumBlockData {
552505
fn from(block: &'a EthereumBlockType) -> FullEthereumBlockData {
553-
let block = match block {
554-
EthereumBlockType::FullWithReceipts(full_block) => &full_block.block,
555-
EthereumBlockType::Full(block) => &block,
556-
EthereumBlockType::Light(light_block) => light_block,
557-
};
558-
559-
FullEthereumBlockData {
560-
hash: block.hash.unwrap(),
561-
parent_hash: block.parent_hash,
562-
uncles_hash: block.uncles_hash,
563-
author: block.author,
564-
state_root: block.state_root,
565-
transactions_root: block.transactions_root,
566-
receipts_root: block.receipts_root,
567-
number: block.number.unwrap(),
568-
gas_used: block.gas_used,
569-
gas_limit: block.gas_limit,
570-
timestamp: block.timestamp,
571-
difficulty: block.difficulty,
572-
total_difficulty: block.total_difficulty.unwrap_or_default(),
573-
size: block.size,
574-
transactions: block
575-
.transactions
576-
.iter()
577-
.map(|tx| EthereumTransactionData::from(tx))
578-
.collect(),
579-
}
506+
let block = &LightEthereumBlock::from(block);
507+
block.into()
580508
}
581509
}
582510

@@ -622,11 +550,7 @@ impl<'a> From<&'a LightEthereumBlock> for EthereumBlockData {
622550

623551
impl<'a> From<&'a EthereumBlockType> for EthereumBlockData {
624552
fn from(block: &'a EthereumBlockType) -> EthereumBlockData {
625-
let block = match block {
626-
EthereumBlockType::FullWithReceipts(full_block) => &full_block.block,
627-
EthereumBlockType::Full(full_block) => &full_block,
628-
EthereumBlockType::Light(light_block) => light_block,
629-
};
553+
let block: LightEthereumBlock = block.into();
630554

631555
EthereumBlockData {
632556
hash: block.hash.unwrap(),
@@ -816,12 +740,8 @@ impl<'a> From<&'a EthereumBlock> for EthereumBlockPointer {
816740
}
817741

818742
impl<'a> From<&'a EthereumBlockType> for EthereumBlockPointer {
819-
fn from(b: &'a EthereumBlockType) -> EthereumBlockPointer {
820-
match b {
821-
EthereumBlockType::Light(block) => EthereumBlockPointer::from(block),
822-
EthereumBlockType::Full(block) => EthereumBlockPointer::from(block),
823-
EthereumBlockType::FullWithReceipts(block) => EthereumBlockPointer::from(block),
824-
}
743+
fn from(block_type: &'a EthereumBlockType) -> EthereumBlockPointer {
744+
EthereumBlockPointer::from(LightEthereumBlock::from(block_type))
825745
}
826746
}
827747

0 commit comments

Comments
 (0)