otterscan/src/transaction/Logs.tsx

88 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-09-17 22:42:19 +00:00
import React, { useMemo } from "react";
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";
import { useAppConfigContext } from "../useAppConfig";
import { Metadata, useMultipleMetadata } from "../useSourcify";
import { ResolvedAddresses } from "../api/address-resolver";
2021-07-14 19:30:28 +00:00
type LogsProps = {
txData: TransactionData;
2021-09-17 22:42:19 +00:00
metadata: Metadata | null | undefined;
resolvedAddresses: ResolvedAddresses | undefined;
2021-07-14 19:30:28 +00:00
};
const Logs: React.FC<LogsProps> = ({ txData, metadata, resolvedAddresses }) => {
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 { sourcifySource } = useAppConfigContext();
const logAddresses = useMemo(
() => txData.confirmedData?.logs.map((l) => l.address) ?? [],
[txData]
);
const metadatas = useMultipleMetadata(
baseMetadatas,
logAddresses,
1,
sourcifySource
);
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]}
resolvedAddresses={resolvedAddresses}
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);