Skip to content

Commit

Permalink
Parse token transfers from received logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sealer3 committed Apr 16, 2024
1 parent 5bfb976 commit 0e1497f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/execution/address/AddressERC20Results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "../../ots2/usePrototypeTransferHooks";
import { usePageNumber } from "../../ots2/useUIHooks";
import { PAGE_SIZE } from "../../params";
import { findTokenTransfersInLogs } from "../../useErigonHooks";
import { RuntimeContext } from "../../useRuntime";
import { usePageTitle } from "../../useTitle";
import { AddressAwareComponentProps } from "../types";
Expand Down Expand Up @@ -53,6 +54,7 @@ const AddressERC20Results: FC<AddressAwareComponentProps> = ({ address }) => {
to: m.receipt.to,
value: m.transaction.value,
type: m.transaction.type,
tokenTransfers: findTokenTransfersInLogs(m.receipt.logs),
}),
),
[results],
Expand Down
2 changes: 2 additions & 0 deletions src/execution/address/AddressERC721Results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const AddressERC721Results: FC<AddressAwareComponentProps> = ({ address }) => {
to: m.receipt.to,
value: m.transaction.value,
type: m.transaction.type,
// TODO: Token transfers for ERC-721 tokens
tokenTransfers: [],
}),
),
[results],
Expand Down
5 changes: 2 additions & 3 deletions src/execution/address/ERC20Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import TransactionDirection from "../../components/TransactionDirection";
import TransactionLink from "../../components/TransactionLink";
import { TokenTransfer } from "../../types";
import { BlockNumberContext } from "../../useBlockTagContext";
import { useTokenTransfers, useTxData } from "../../useErigonHooks";
import { RuntimeContext } from "../../useRuntime";
import TransactionAddress from "../components/TransactionAddress";
import { AddressAwareComponentProps } from "../types";
Expand All @@ -23,6 +22,7 @@ export type ERC20ItemProps = AddressAwareComponentProps & {
to: string | null;
value: bigint;
type: number;
tokenTransfers: TokenTransfer[];
};

const ERC20Item: FC<ERC20ItemProps> = ({
Expand All @@ -36,10 +36,9 @@ const ERC20Item: FC<ERC20ItemProps> = ({
to,
value,
type,
tokenTransfers,
}) => {
const { provider } = useContext(RuntimeContext);
const txData = useTxData(provider, hash);
const tokenTransfers = useTokenTransfers(txData);
return (
<BlockNumberContext.Provider value={blockNumber}>
<tr>
Expand Down
23 changes: 15 additions & 8 deletions src/useErigonHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BlockTag,
Contract,
JsonRpcApiProvider,
Log,
TransactionReceiptParams,
TransactionResponseParams,
ZeroAddress,
Expand Down Expand Up @@ -327,6 +328,19 @@ export const useTxData = (
return txData;
};

export const findTokenTransfersInLogs = (
logs: readonly Log[],
): TokenTransfer[] => {
return logs
.filter((l) => l.topics.length === 3 && l.topics[0] === TRANSFER_TOPIC)
.map((l) => ({
token: l.address,
from: getAddress(dataSlice(getBytes(l.topics[1]), 12)),
to: getAddress(dataSlice(getBytes(l.topics[2]), 12)),
value: BigInt(l.data),
}));
};

export const useTokenTransfers = (
txData?: TransactionData | null,
): TokenTransfer[] | undefined => {
Expand All @@ -338,14 +352,7 @@ export const useTokenTransfers = (
return undefined;
}

return txData.confirmedData.logs
.filter((l) => l.topics.length === 3 && l.topics[0] === TRANSFER_TOPIC)
.map((l) => ({
token: l.address,
from: getAddress(dataSlice(getBytes(l.topics[1]), 12)),
to: getAddress(dataSlice(getBytes(l.topics[2]), 12)),
value: BigInt(l.data),
}));
return findTokenTransfersInLogs(txData.confirmedData.logs);
}, [txData]);

return transfers;
Expand Down

0 comments on commit 0e1497f

Please sign in to comment.