otterscan/src/transaction/LogEntry.tsx

149 lines
5.5 KiB
TypeScript
Raw Normal View History

import React, { useMemo } from "react";
2021-09-17 22:42:19 +00:00
import { Log } from "@ethersproject/abstract-provider";
import { Fragment, Interface, LogDescription } from "@ethersproject/abi";
2021-09-18 18:25:30 +00:00
import { Tab } from "@headlessui/react";
import AddressHighlighter from "../components/AddressHighlighter";
2021-09-17 22:42:19 +00:00
import DecoratedAddressLink from "../components/DecoratedAddressLink";
import Copy from "../components/Copy";
2021-09-18 18:25:30 +00:00
import ModeTab from "../components/ModeTab";
2021-09-25 20:19:49 +00:00
import DecodedParamsTable from "./decoder/DecodedParamsTable";
import DecodedLogSignature from "./decoder/DecodedLogSignature";
2021-09-17 22:42:19 +00:00
import { TransactionData } from "../types";
import { useTopic0 } from "../useTopic0";
2021-09-17 22:42:19 +00:00
type LogEntryProps = {
txData: TransactionData;
log: Log;
logDesc: LogDescription | null | undefined;
};
const LogEntry: React.FC<LogEntryProps> = ({ txData, log, logDesc }) => {
const rawTopic0 = log.topics[0];
const topic0 = useTopic0(rawTopic0);
const topic0LogDesc = useMemo(() => {
2021-10-19 08:20:14 +00:00
if (!topic0) {
return topic0;
}
if (!topic0.signatures) {
return undefined;
}
const sigs = topic0.signatures;
for (const sig of sigs) {
const logFragment = Fragment.fromString(`event ${sig}`);
const intf = new Interface([logFragment]);
try {
return intf.parseLog(log);
} catch (err) {
// Ignore on purpose; try to match other sigs
}
}
return undefined;
}, [topic0, log]);
const resolvedLogDesc = logDesc ?? topic0LogDesc;
return (
<div className="flex space-x-10 py-5">
<div>
<span className="rounded-full w-12 h-12 flex items-center justify-center bg-green-50 text-green-500">
{log.logIndex}
</span>
2021-09-17 22:42:19 +00:00
</div>
<div className="w-full space-y-2">
<div className="grid grid-cols-12 gap-x-3 gap-y-5 text-sm">
<div className="font-bold text-right">Address</div>
<div className="col-span-11 mr-auto">
<div className="flex items-baseline space-x-2 -ml-1 mr-3">
<AddressHighlighter address={log.address}>
<DecoratedAddressLink
address={log.address}
miner={log.address === txData.confirmedData?.miner}
txFrom={log.address === txData.from}
txTo={log.address === txData.to}
/>
</AddressHighlighter>
<Copy value={log.address} />
</div>
2021-09-17 22:42:19 +00:00
</div>
</div>
<Tab.Group>
<Tab.List className="grid grid-cols-12 gap-x-3 gap-y-5 text-sm">
<div className="text-right">Parameters</div>
<div className="col-span-11 flex space-x-1 mb-1">
<ModeTab>Decoded</ModeTab>
<ModeTab>Raw</ModeTab>
</div>
</Tab.List>
<Tab.Panels as={React.Fragment}>
<Tab.Panel className="space-y-2">
{resolvedLogDesc === undefined ? (
2021-09-18 18:25:30 +00:00
<div className="grid grid-cols-12 gap-x-3 gap-y-5 text-sm">
<div className="col-start-2 flex space-x-2 items-center col-span-11">
Waiting for data...
2021-09-18 18:25:30 +00:00
</div>
</div>
) : resolvedLogDesc === null ? (
2021-09-18 18:25:30 +00:00
<div className="grid grid-cols-12 gap-x-3 gap-y-5 text-sm">
2021-09-18 21:03:54 +00:00
<div className="col-start-2 flex space-x-2 items-center col-span-11">
2021-10-19 08:20:14 +00:00
Can't decode data
2021-09-18 18:25:30 +00:00
</div>
</div>
) : (
<>
<div className="grid grid-cols-12 gap-x-3 gap-y-5 text-sm">
<div className="col-start-2 flex space-x-2 items-center col-span-11 font-mono">
<DecodedLogSignature
event={resolvedLogDesc.eventFragment}
/>
</div>
</div>
<div className="grid grid-cols-12 gap-x-3 gap-y-5 text-sm">
<div className="col-start-2 flex space-x-2 items-center col-span-11">
<DecodedParamsTable
args={resolvedLogDesc.args}
paramTypes={resolvedLogDesc.eventFragment.inputs}
txData={txData}
2021-10-12 08:35:18 +00:00
hasParamNames={resolvedLogDesc === logDesc}
/>
</div>
</div>
</>
)}
</Tab.Panel>
<Tab.Panel className="space-y-2">
{log.topics.map((t, i) => (
<div
className="grid grid-cols-12 gap-x-3 gap-y-5 text-sm"
key={i}
>
<div className="text-right">{i === 0 && "Topics"}</div>
<div className="flex space-x-2 items-center col-span-11 font-mono">
<span className="rounded bg-gray-100 text-gray-500 px-2 py-1 text-xs">
{i}
</span>
<span>{t}</span>
</div>
</div>
))}
<div className="grid grid-cols-12 gap-x-3 gap-y-5 text-sm">
<div className="text-right pt-2">Data</div>
<div className="col-span-11">
<textarea
className="w-full h-40 bg-gray-50 font-mono focus:outline-none border rounded p-2"
value={log.data}
readOnly
/>
2021-09-18 18:25:30 +00:00
</div>
</div>
</Tab.Panel>
</Tab.Panels>
</Tab.Group>
</div>
2021-09-17 22:42:19 +00:00
</div>
);
};
2021-09-17 22:42:19 +00:00
export default React.memo(LogEntry);