otterscan/src/search/TransactionItem.tsx

118 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-07-01 18:21:40 +00:00
import React from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faExclamationCircle,
faCoins,
} from "@fortawesome/free-solid-svg-icons";
2021-07-01 18:21:40 +00:00
import MethodName from "../components/MethodName";
import BlockLink from "../components/BlockLink";
import TransactionLink from "../components/TransactionLink";
import AddressOrENSName from "../components/AddressOrENSName";
2021-07-01 18:21:40 +00:00
import TimestampAge from "../components/TimestampAge";
import TransactionDirection, {
Direction,
Flags,
2021-07-01 18:21:40 +00:00
} from "../components/TransactionDirection";
import TransactionValue from "../components/TransactionValue";
import { ENSReverseCache, ProcessedTransaction } from "../types";
2021-07-01 18:21:40 +00:00
import { FeeDisplay } from "./useFeeToggler";
import { formatValue } from "../components/formatter";
type TransactionItemProps = {
tx: ProcessedTransaction;
ensCache?: ENSReverseCache;
2021-07-01 18:21:40 +00:00
selectedAddress?: string;
feeDisplay: FeeDisplay;
};
const TransactionItem: React.FC<TransactionItemProps> = ({
tx,
ensCache,
2021-07-01 18:21:40 +00:00
selectedAddress,
feeDisplay,
}) => {
let direction: Direction | undefined;
if (selectedAddress) {
if (tx.from === selectedAddress && tx.to === selectedAddress) {
direction = Direction.SELF;
} else if (tx.from === selectedAddress) {
direction = Direction.OUT;
} else if (tx.to === selectedAddress) {
direction = Direction.IN;
} else {
direction = Direction.INTERNAL;
}
}
const ensFrom = ensCache && tx.from && ensCache[tx.from];
const ensTo = ensCache && tx.to && ensCache[tx.to];
const flash = tx.gasPrice.isZero() && tx.internalMinerInteraction;
2021-07-01 18:21:40 +00:00
return (
<div
className={`grid grid-cols-12 gap-x-1 items-baseline text-sm border-t border-gray-200 hover:bg-gray-100 ${
flash ? "bg-yellow-100" : ""
} px-2 py-3`}
>
2021-07-01 18:21:40 +00:00
<div className="col-span-2 flex space-x-1 items-baseline">
{tx.status === 0 && (
<span className="text-red-600" title="Transaction reverted">
<FontAwesomeIcon icon={faExclamationCircle} />
</span>
)}
<span className="truncate">
<TransactionLink txHash={tx.hash} />
</span>
</div>
<MethodName data={tx.data} />
<span>
<BlockLink blockTag={tx.blockNumber} />
</span>
<TimestampAge timestamp={tx.timestamp} />
<span className="col-span-2 flex justify-between items-baseline space-x-2 pr-2">
<span className="truncate" title={tx.from}>
{tx.from && (
<div className="flex items-baseline space-x-1">
{tx.miner && tx.miner === tx.from && (
<span className="text-yellow-400" title="Miner address">
<FontAwesomeIcon icon={faCoins} size="1x" />
</span>
)}
<AddressOrENSName
address={tx.from}
ensName={ensFrom}
selectedAddress={selectedAddress}
/>
</div>
)}
2021-07-01 18:21:40 +00:00
</span>
<span>
<TransactionDirection
direction={direction}
flags={tx.internalMinerInteraction ? Flags.MINER : undefined}
/>
2021-07-01 18:21:40 +00:00
</span>
</span>
<span className="col-span-2 truncate" title={tx.to}>
{tx.to && (
<AddressOrENSName
address={tx.to}
ensName={ensTo}
selectedAddress={selectedAddress}
/>
)}
2021-07-01 18:21:40 +00:00
</span>
<span className="col-span-2 truncate">
<TransactionValue value={tx.value} />
</span>
<span className="font-balance text-xs text-gray-500 truncate">
{feeDisplay === FeeDisplay.TX_FEE
? formatValue(tx.fee, 18)
: formatValue(tx.gasPrice, 9)}
</span>
</div>
);
};
export default React.memo(TransactionItem);