otterscan/src/transaction/TraceInput.tsx

102 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-11-06 18:54:34 +00:00
import React, { useState } from "react";
import { Switch } from "@headlessui/react";
2021-11-06 05:12:37 +00:00
import AddressHighlighter from "../components/AddressHighlighter";
import DecoratedAddressLink from "../components/DecoratedAddressLink";
import FormattedBalance from "../components/FormattedBalance";
import FunctionSignature from "./FunctionSignature";
2021-11-06 18:54:34 +00:00
import DecodedParamsTable from "./decoder/DecodedParamsTable";
2021-11-06 05:12:37 +00:00
import { TraceEntry } from "../useErigonHooks";
2021-11-07 12:21:02 +00:00
import { TransactionData } from "../types";
2021-11-06 05:12:37 +00:00
import { ResolvedAddresses } from "../api/address-resolver";
2021-11-06 18:54:34 +00:00
import {
extract4Bytes,
FourBytesEntry,
useTransactionDescription,
} from "../use4Bytes";
2021-11-06 05:12:37 +00:00
type TraceInputProps = {
t: TraceEntry;
txData: TransactionData;
fourBytesMap: Record<string, FourBytesEntry | null | undefined>;
resolvedAddresses: ResolvedAddresses | undefined;
};
const TraceInput: React.FC<TraceInputProps> = ({
t,
txData,
fourBytesMap,
resolvedAddresses,
}) => {
const raw4Bytes = extract4Bytes(t.input);
2021-11-06 18:54:34 +00:00
const fourBytes = raw4Bytes !== null ? fourBytesMap[raw4Bytes] : null;
2021-11-06 05:12:37 +00:00
const sigText =
2021-11-06 18:54:34 +00:00
raw4Bytes === null ? "<fallback>" : fourBytes?.name ?? raw4Bytes;
const hasParams = t.input.length > 10;
const fourBytesTxDesc = useTransactionDescription(
fourBytes,
t.input,
t.value
);
const [expanded, setExpanded] = useState<boolean>(false);
2021-11-06 05:12:37 +00:00
return (
2021-11-06 19:14:30 +00:00
<div className="ml-5 border rounded px-1 py-0.5">
2021-11-06 18:54:34 +00:00
<div className="flex items-baseline">
<span className="text-xs text-gray-400 lowercase">{t.type}</span>
<span>
<AddressHighlighter address={t.to}>
<DecoratedAddressLink
address={t.to}
miner={t.to === txData.confirmedData?.miner}
txFrom={t.to === txData.from}
txTo={t.to === txData.to}
resolvedAddresses={resolvedAddresses}
/>
</AddressHighlighter>
</span>
<span>.</span>
<FunctionSignature callType={t.type} sig={sigText} />
{t.value && !t.value.isZero() && (
<span className="text-red-700 whitespace-nowrap">
{"{"}value: <FormattedBalance value={t.value} /> ETH{"}"}
</span>
)}
<span className="whitespace-nowrap">
(
{hasParams && (
2021-11-06 19:14:30 +00:00
<Switch
className="text-xs"
checked={expanded}
onChange={setExpanded}
>
2021-11-06 18:54:34 +00:00
{expanded ? (
<span className="text-gray-400">[-]</span>
) : (
<>[...]</>
)}
</Switch>
)}
{(!hasParams || !expanded) && <>)</>}
2021-11-06 05:12:37 +00:00
</span>
2021-11-06 18:54:34 +00:00
</div>
{hasParams && expanded && fourBytesTxDesc && (
<>
<div className="ml-5 my-5 mr-5">
<DecodedParamsTable
args={fourBytesTxDesc.args}
paramTypes={fourBytesTxDesc.functionFragment.inputs}
hasParamNames={false}
resolvedAddresses={resolvedAddresses}
/>
</div>
<div>)</div>
</>
2021-11-06 05:12:37 +00:00
)}
</div>
);
};
export default TraceInput;