Don't have the ERC20 collapse the top-level transfer ID to the transaction ID

Uses the ID of the transfer event associated with the top-level transfer.
This commit is contained in:
Luke Parker 2024-09-20 01:23:26 -04:00
parent ae76749513
commit 294462641e

View file

@ -30,8 +30,8 @@ pub use abi::IERC20::Transfer;
/// A top-level ERC20 transfer /// A top-level ERC20 transfer
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct TopLevelTransfer { pub struct TopLevelTransfer {
/// The transaction ID which effected this transfer. /// The ID of the event for this transfer.
pub id: [u8; 32], pub id: ([u8; 32], u64),
/// The address which made the transfer. /// The address which made the transfer.
pub from: [u8; 20], pub from: [u8; 20],
/// The amount transferred. /// The amount transferred.
@ -40,14 +40,6 @@ pub struct TopLevelTransfer {
pub data: Vec<u8>, pub data: Vec<u8>,
} }
/// A transaction with a top-level transfer, matched to the log index of the transfer.
pub struct MatchedTopLevelTransfer {
/// The transfer.
pub transfer: TopLevelTransfer,
/// The log index of the transfer.
pub log_index: u64,
}
/// A view for an ERC20 contract. /// A view for an ERC20 contract.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Erc20(Arc<RootProvider<SimpleRequest>>, Address); pub struct Erc20(Arc<RootProvider<SimpleRequest>>, Address);
@ -62,7 +54,7 @@ impl Erc20 {
provider: impl AsRef<RootProvider<SimpleRequest>>, provider: impl AsRef<RootProvider<SimpleRequest>>,
transaction_id: B256, transaction_id: B256,
to: Address, to: Address,
) -> Result<Option<MatchedTopLevelTransfer>, RpcError<TransportErrorKind>> { ) -> Result<Option<TopLevelTransfer>, RpcError<TransportErrorKind>> {
// Fetch the transaction // Fetch the transaction
let transaction = let transaction =
provider.as_ref().get_transaction_by_hash(transaction_id).await?.ok_or_else(|| { provider.as_ref().get_transaction_by_hash(transaction_id).await?.ok_or_else(|| {
@ -132,15 +124,11 @@ impl Erc20 {
let encoded = call.abi_encode(); let encoded = call.abi_encode();
let data = transaction.input.as_ref()[encoded.len() ..].to_vec(); let data = transaction.input.as_ref()[encoded.len() ..].to_vec();
return Ok(Some(MatchedTopLevelTransfer { return Ok(Some(TopLevelTransfer {
transfer: TopLevelTransfer { id: (*transaction_id, log_index),
// Since there's only one top-level transfer per TX, set the ID to the TX ID
id: *transaction_id,
from: *log.from.0, from: *log.from.0,
amount: log.value, amount: log.value,
data, data,
},
log_index,
})); }));
} }
} }
@ -193,7 +181,7 @@ impl Erc20 {
// Panicking on a task panic is desired behavior, and we haven't aborted any tasks // Panicking on a task panic is desired behavior, and we haven't aborted any tasks
match top_level_transfer.unwrap() { match top_level_transfer.unwrap() {
// Top-level transfer // Top-level transfer
Ok(Some(top_level_transfer)) => top_level_transfers.push(top_level_transfer.transfer), Ok(Some(top_level_transfer)) => top_level_transfers.push(top_level_transfer),
// Not a top-level transfer // Not a top-level transfer
Ok(None) => continue, Ok(None) => continue,
// Failed to get this transaction's information so abort // Failed to get this transaction's information so abort