otterscan/src/transaction/Logs.tsx

83 lines
2.2 KiB
TypeScript
Raw Normal View History

import React, { useContext, useMemo } from "react";
2021-09-17 22:42:19 +00:00
import { Interface } from "@ethersproject/abi";
2021-07-14 19:30:28 +00:00
import ContentFrame from "../ContentFrame";
2021-09-17 22:42:19 +00:00
import LogEntry from "./LogEntry";
2021-07-14 19:30:28 +00:00
import { TransactionData } from "../types";
2021-12-02 19:19:03 +00:00
import { Metadata } from "../sourcify/useSourcify";
import { RuntimeContext } from "../useRuntime";
2021-12-02 19:19:03 +00:00
import { useContractsMetadata } from "../hooks";
2021-07-14 19:30:28 +00:00
type LogsProps = {
txData: TransactionData;
2021-09-17 22:42:19 +00:00
metadata: Metadata | null | undefined;
2021-07-14 19:30:28 +00:00
};
const Logs: React.FC<LogsProps> = ({ txData, metadata }) => {
const baseMetadatas = useMemo((): Record<string, Metadata | null> => {
if (!txData.to || metadata === undefined) {
return {};
}
const md: Record<string, Metadata | null> = {};
md[txData.to] = metadata;
return md;
}, [txData.to, metadata]);
const logAddresses = useMemo(
() => txData.confirmedData?.logs.map((l) => l.address) ?? [],
[txData]
);
const { provider } = useContext(RuntimeContext);
2021-12-02 19:19:03 +00:00
const metadatas = useContractsMetadata(logAddresses, provider, baseMetadatas);
const logDescs = useMemo(() => {
2021-09-22 18:11:13 +00:00
if (!txData) {
2021-09-17 22:42:19 +00:00
return undefined;
}
return txData.confirmedData?.logs.map((l) => {
const mt = metadatas[l.address];
if (!mt) {
return mt;
}
const abi = mt.output.abi;
const intf = new Interface(abi as any);
2021-09-22 18:11:13 +00:00
try {
return intf.parseLog({
topics: l.topics,
data: l.data,
});
} catch (err) {
console.warn("Couldn't find function signature", err);
return null;
}
});
2021-09-22 18:11:13 +00:00
}, [metadatas, txData]);
2021-09-17 22:42:19 +00:00
return (
<ContentFrame tabs>
2021-09-23 21:33:20 +00:00
{txData.confirmedData && (
<>
{txData.confirmedData.logs.length > 0 ? (
<>
{txData.confirmedData.logs.map((l, i) => (
<LogEntry
key={i}
log={l}
logDesc={logDescs?.[i]}
metadatas={metadatas}
2021-09-23 21:33:20 +00:00
/>
))}
</>
) : (
<div className="text-sm py-4">Transaction didn't emit any logs</div>
)}
</>
)}
2021-09-17 22:42:19 +00:00
</ContentFrame>
);
};
2021-07-14 19:30:28 +00:00
export default React.memo(Logs);