Skip to content

Commit

Permalink
firehose: Try to be more consistent in parsing to address
Browse files Browse the repository at this point in the history
  • Loading branch information
leoyvens committed Apr 25, 2024
1 parent e08b6d3 commit 428f866
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions chain/ethereum/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,7 @@ impl<'a> TryInto<web3::types::Transaction> for TransactionTraceAt<'a> {
.from
.try_decode_proto("transaction from address")?,
),
to: match self.trace.calls.len() {
0 => Some(self.trace.to.try_decode_proto("transaction to address")?),
_ => {
match CallType::from_i32(self.trace.calls[0].call_type).ok_or_else(|| {
format_err!("invalid call type: {}", self.trace.calls[0].call_type,)
})? {
CallType::Create => {
None // we don't want the 'to' address on a transaction that creates the contract, to align with RPC behavior
}
_ => Some(self.trace.to.try_decode_proto("transaction to")?),
}
}
},
to: get_to_address(self.trace)?,
value: self.trace.value.as_ref().map_or(U256::zero(), |x| x.into()),
gas_price: self.trace.gas_price.as_ref().map(|x| x.into()),
gas: U256::from(self.trace.gas_limit),
Expand Down Expand Up @@ -353,7 +341,7 @@ impl TryInto<EthereumBlockWithCalls> for &Block {
.logs_bloom
.try_decode_proto("transaction logs bloom")?,
from: t.from.try_decode_proto("transaction from")?,
to: Some(t.to.try_decode_proto("transaction to")?),
to: get_to_address(t)?,
transaction_type: None,
effective_gas_price: None,
})
Expand Down Expand Up @@ -540,3 +528,18 @@ mod test {
);
}
}

fn get_to_address(trace: &TransactionTrace) -> Result<Option<H160>, Error> {
// Try to detect contract creation transactions, which have no 'to' address
let is_contract_creation = trace.to.len() == 0
|| trace.calls.get(0).map_or(false, |call| {
CallType::from_i32(call.call_type)
.map_or(false, |call_type| call_type == CallType::Create)
});

if is_contract_creation {
Ok(None)
} else {
Ok(Some(trace.to.try_decode_proto("transaction to address")?))
}
}

0 comments on commit 428f866

Please sign in to comment.