2021-07-17 18:00:08 +00:00
|
|
|
import React, { useState, useEffect, useMemo, useContext } from "react";
|
2021-07-01 18:21:40 +00:00
|
|
|
import { Route, Switch, useParams } from "react-router-dom";
|
|
|
|
import { BigNumber, ethers } from "ethers";
|
|
|
|
import StandardFrame from "./StandardFrame";
|
|
|
|
import StandardSubtitle from "./StandardSubtitle";
|
|
|
|
import Tab from "./components/Tab";
|
2021-07-14 19:30:28 +00:00
|
|
|
import Details from "./transaction/Details";
|
|
|
|
import Logs from "./transaction/Logs";
|
2021-07-01 18:21:40 +00:00
|
|
|
import erc20 from "./erc20.json";
|
2021-07-17 18:00:08 +00:00
|
|
|
import { TokenMetas, TokenTransfer, TransactionData } from "./types";
|
2021-07-09 05:07:20 +00:00
|
|
|
import { RuntimeContext } from "./useRuntime";
|
2021-07-14 07:43:58 +00:00
|
|
|
import { SelectionContext, useSelection } from "./useSelection";
|
2021-07-17 18:00:08 +00:00
|
|
|
import { useInternalTransfers } from "./useErigonHooks";
|
2021-07-01 18:21:40 +00:00
|
|
|
|
|
|
|
const TRANSFER_TOPIC =
|
|
|
|
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
|
|
|
|
|
|
|
|
type TransactionParams = {
|
|
|
|
txhash: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const Transaction: React.FC = () => {
|
2021-07-09 05:07:20 +00:00
|
|
|
const { provider } = useContext(RuntimeContext);
|
2021-07-01 18:21:40 +00:00
|
|
|
const params = useParams<TransactionParams>();
|
|
|
|
const { txhash } = params;
|
|
|
|
|
|
|
|
const [txData, setTxData] = useState<TransactionData>();
|
|
|
|
useEffect(() => {
|
2021-07-08 19:02:42 +00:00
|
|
|
if (!provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-01 18:21:40 +00:00
|
|
|
const readBlock = async () => {
|
|
|
|
const [_response, _receipt] = await Promise.all([
|
|
|
|
provider.getTransaction(txhash),
|
|
|
|
provider.getTransactionReceipt(txhash),
|
|
|
|
]);
|
|
|
|
const _block = await provider.getBlock(_receipt.blockNumber);
|
|
|
|
document.title = `Transaction ${_response.hash} | Otterscan`;
|
|
|
|
|
|
|
|
// Extract token transfers
|
|
|
|
const tokenTransfers: TokenTransfer[] = [];
|
|
|
|
for (const l of _receipt.logs) {
|
|
|
|
if (l.topics.length !== 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (l.topics[0] !== TRANSFER_TOPIC) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tokenTransfers.push({
|
|
|
|
token: l.address,
|
2021-07-03 20:17:47 +00:00
|
|
|
from: ethers.utils.getAddress(
|
|
|
|
ethers.utils.hexDataSlice(ethers.utils.arrayify(l.topics[1]), 12)
|
|
|
|
),
|
|
|
|
to: ethers.utils.getAddress(
|
|
|
|
ethers.utils.hexDataSlice(ethers.utils.arrayify(l.topics[2]), 12)
|
2021-07-01 18:21:40 +00:00
|
|
|
),
|
|
|
|
value: BigNumber.from(l.data),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract token meta
|
|
|
|
const tokenMetas: TokenMetas = {};
|
|
|
|
for (const t of tokenTransfers) {
|
|
|
|
if (tokenMetas[t.token]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const erc20Contract = new ethers.Contract(t.token, erc20, provider);
|
|
|
|
const [name, symbol, decimals] = await Promise.all([
|
|
|
|
erc20Contract.name(),
|
|
|
|
erc20Contract.symbol(),
|
|
|
|
erc20Contract.decimals(),
|
|
|
|
]);
|
|
|
|
tokenMetas[t.token] = {
|
|
|
|
name,
|
|
|
|
symbol,
|
|
|
|
decimals,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setTxData({
|
|
|
|
transactionHash: _receipt.transactionHash,
|
|
|
|
status: _receipt.status === 1,
|
|
|
|
blockNumber: _receipt.blockNumber,
|
|
|
|
transactionIndex: _receipt.transactionIndex,
|
|
|
|
confirmations: _receipt.confirmations,
|
|
|
|
timestamp: _block.timestamp,
|
2021-07-05 03:03:01 +00:00
|
|
|
miner: _block.miner,
|
2021-07-01 18:21:40 +00:00
|
|
|
from: _receipt.from,
|
|
|
|
to: _receipt.to,
|
|
|
|
value: _response.value,
|
|
|
|
tokenTransfers,
|
|
|
|
tokenMetas,
|
|
|
|
fee: _response.gasPrice!.mul(_receipt.gasUsed),
|
|
|
|
gasPrice: _response.gasPrice!,
|
|
|
|
gasLimit: _response.gasLimit,
|
|
|
|
gasUsed: _receipt.gasUsed,
|
|
|
|
gasUsedPerc:
|
|
|
|
_receipt.gasUsed.toNumber() / _response.gasLimit.toNumber(),
|
|
|
|
nonce: _response.nonce,
|
|
|
|
data: _response.data,
|
|
|
|
logs: _receipt.logs,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
readBlock();
|
2021-07-08 19:02:42 +00:00
|
|
|
}, [provider, txhash]);
|
2021-07-01 18:21:40 +00:00
|
|
|
|
2021-07-17 18:00:08 +00:00
|
|
|
const transfers = useInternalTransfers(provider, txData);
|
2021-07-05 21:08:52 +00:00
|
|
|
const sendsEthToMiner = useMemo(() => {
|
|
|
|
if (!txData || !transfers) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const t of transfers) {
|
|
|
|
if (t.to === txData.miner) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}, [txData, transfers]);
|
2021-07-01 18:21:40 +00:00
|
|
|
|
2021-07-14 07:43:58 +00:00
|
|
|
const selectionCtx = useSelection();
|
|
|
|
|
2021-07-01 18:21:40 +00:00
|
|
|
return (
|
|
|
|
<StandardFrame>
|
|
|
|
<StandardSubtitle>Transaction Details</StandardSubtitle>
|
|
|
|
{txData && (
|
2021-07-14 07:43:58 +00:00
|
|
|
<SelectionContext.Provider value={selectionCtx}>
|
2021-07-01 18:21:40 +00:00
|
|
|
<div className="flex space-x-2 border-l border-r border-t rounded-t-lg bg-white">
|
|
|
|
<Tab href={`/tx/${txhash}`}>Overview</Tab>
|
|
|
|
<Tab href={`/tx/${txhash}/logs`}>
|
|
|
|
Logs{txData && ` (${txData.logs.length})`}
|
|
|
|
</Tab>
|
|
|
|
</div>
|
|
|
|
<Switch>
|
|
|
|
<Route path="/tx/:txhash/" exact>
|
2021-07-14 19:30:28 +00:00
|
|
|
<Details
|
|
|
|
txData={txData}
|
|
|
|
transfers={transfers}
|
|
|
|
sendsEthToMiner={sendsEthToMiner}
|
|
|
|
/>
|
2021-07-01 18:21:40 +00:00
|
|
|
</Route>
|
|
|
|
<Route path="/tx/:txhash/logs/" exact>
|
2021-07-14 19:30:28 +00:00
|
|
|
<Logs txData={txData} />
|
2021-07-01 18:21:40 +00:00
|
|
|
</Route>
|
|
|
|
</Switch>
|
2021-07-14 07:43:58 +00:00
|
|
|
</SelectionContext.Provider>
|
2021-07-01 18:21:40 +00:00
|
|
|
)}
|
|
|
|
</StandardFrame>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(Transaction);
|