2021-07-17 18:00:08 +00:00
|
|
|
import { useState, useEffect } from "react";
|
2021-08-08 22:49:45 +00:00
|
|
|
import { Block, BlockWithTransactions } from "@ethersproject/abstract-provider";
|
|
|
|
import { JsonRpcProvider } from "@ethersproject/providers";
|
|
|
|
import { getAddress } from "@ethersproject/address";
|
|
|
|
import { Contract } from "@ethersproject/contracts";
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
|
|
import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes";
|
2021-07-21 19:06:51 +00:00
|
|
|
import { getInternalOperations } from "./nodeFunctions";
|
2021-08-01 09:59:59 +00:00
|
|
|
import {
|
|
|
|
TokenMetas,
|
|
|
|
TokenTransfer,
|
|
|
|
TransactionData,
|
|
|
|
InternalOperation,
|
2021-08-02 02:53:36 +00:00
|
|
|
ProcessedTransaction,
|
|
|
|
OperationType,
|
2021-08-01 09:59:59 +00:00
|
|
|
} from "./types";
|
|
|
|
import erc20 from "./erc20.json";
|
|
|
|
|
|
|
|
const TRANSFER_TOPIC =
|
|
|
|
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
|
2021-07-17 18:00:08 +00:00
|
|
|
|
2021-08-08 22:49:45 +00:00
|
|
|
export interface ExtendedBlock extends Block {
|
2021-07-26 22:59:07 +00:00
|
|
|
blockReward: BigNumber;
|
|
|
|
unclesReward: BigNumber;
|
|
|
|
feeReward: BigNumber;
|
|
|
|
size: number;
|
|
|
|
sha3Uncles: string;
|
|
|
|
stateRoot: string;
|
|
|
|
totalDifficulty: BigNumber;
|
2021-08-02 18:43:37 +00:00
|
|
|
transactionCount: number;
|
2021-07-26 22:59:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 18:09:28 +00:00
|
|
|
export const readBlock = async (
|
2021-08-08 22:49:45 +00:00
|
|
|
provider: JsonRpcProvider,
|
2021-07-29 18:09:28 +00:00
|
|
|
blockNumberOrHash: string
|
|
|
|
) => {
|
|
|
|
let blockPromise: Promise<any>;
|
2021-08-08 22:49:45 +00:00
|
|
|
if (isHexString(blockNumberOrHash, 32)) {
|
2021-08-02 18:43:37 +00:00
|
|
|
// TODO: fix
|
2021-07-29 18:09:28 +00:00
|
|
|
blockPromise = provider.send("eth_getBlockByHash", [
|
|
|
|
blockNumberOrHash,
|
|
|
|
false,
|
|
|
|
]);
|
|
|
|
} else {
|
2021-08-02 18:43:37 +00:00
|
|
|
blockPromise = provider.send("ots_getBlockDetails", [blockNumberOrHash]);
|
2021-07-29 18:09:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 18:43:37 +00:00
|
|
|
const _rawBlock = await blockPromise;
|
|
|
|
const _block = provider.formatter.block(_rawBlock.block);
|
|
|
|
const _rawIssuance = _rawBlock.issuance;
|
|
|
|
|
2021-07-29 18:09:28 +00:00
|
|
|
const extBlock: ExtendedBlock = {
|
|
|
|
blockReward: provider.formatter.bigNumber(_rawIssuance.blockReward ?? 0),
|
|
|
|
unclesReward: provider.formatter.bigNumber(_rawIssuance.uncleReward ?? 0),
|
2021-08-15 03:45:24 +00:00
|
|
|
feeReward: provider.formatter.bigNumber(_rawBlock.totalFees),
|
2021-08-02 18:43:37 +00:00
|
|
|
size: provider.formatter.number(_rawBlock.block.size),
|
|
|
|
sha3Uncles: _rawBlock.block.sha3Uncles,
|
|
|
|
stateRoot: _rawBlock.block.stateRoot,
|
|
|
|
totalDifficulty: provider.formatter.bigNumber(
|
|
|
|
_rawBlock.block.totalDifficulty
|
|
|
|
),
|
|
|
|
transactionCount: provider.formatter.number(
|
|
|
|
_rawBlock.block.transactionCount
|
|
|
|
),
|
2021-07-29 18:09:28 +00:00
|
|
|
..._block,
|
|
|
|
};
|
|
|
|
return extBlock;
|
|
|
|
};
|
|
|
|
|
2021-08-02 02:53:36 +00:00
|
|
|
export const useBlockTransactions = (
|
2021-08-08 22:49:45 +00:00
|
|
|
provider: JsonRpcProvider | undefined,
|
2021-08-03 04:34:14 +00:00
|
|
|
blockNumber: number,
|
|
|
|
pageNumber: number,
|
|
|
|
pageSize: number
|
2021-08-03 03:45:07 +00:00
|
|
|
): [number | undefined, ProcessedTransaction[] | undefined] => {
|
|
|
|
const [totalTxs, setTotalTxs] = useState<number>();
|
2021-08-02 02:53:36 +00:00
|
|
|
const [txs, setTxs] = useState<ProcessedTransaction[]>();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const readBlock = async () => {
|
2021-08-03 02:07:27 +00:00
|
|
|
const result = await provider.send("ots_getBlockTransactions", [
|
|
|
|
blockNumber,
|
2021-08-03 04:34:14 +00:00
|
|
|
pageNumber,
|
|
|
|
pageSize,
|
2021-08-02 02:53:36 +00:00
|
|
|
]);
|
2021-08-03 02:07:27 +00:00
|
|
|
const _block = provider.formatter.blockWithTransactions(
|
|
|
|
result.fullblock
|
|
|
|
) as unknown as BlockWithTransactions;
|
|
|
|
const _receipts = result.receipts;
|
2021-08-02 02:53:36 +00:00
|
|
|
|
2021-08-03 02:07:27 +00:00
|
|
|
const rawTxs = _block.transactions
|
2021-08-02 02:53:36 +00:00
|
|
|
.map(
|
|
|
|
(t, i): ProcessedTransaction => ({
|
|
|
|
blockNumber: blockNumber,
|
|
|
|
timestamp: _block.timestamp,
|
|
|
|
miner: _block.miner,
|
|
|
|
idx: i,
|
|
|
|
hash: t.hash,
|
|
|
|
from: t.from,
|
|
|
|
to: t.to,
|
|
|
|
createdContractAddress: _receipts[i].contractAddress,
|
|
|
|
value: t.value,
|
|
|
|
fee:
|
|
|
|
t.type !== 2
|
|
|
|
? provider.formatter
|
|
|
|
.bigNumber(_receipts[i].gasUsed)
|
|
|
|
.mul(t.gasPrice!)
|
|
|
|
: provider.formatter
|
|
|
|
.bigNumber(_receipts[i].gasUsed)
|
|
|
|
.mul(t.maxPriorityFeePerGas!.add(_block.baseFeePerGas!)),
|
|
|
|
gasPrice:
|
|
|
|
t.type !== 2
|
|
|
|
? t.gasPrice!
|
|
|
|
: t.maxPriorityFeePerGas!.add(_block.baseFeePerGas!),
|
|
|
|
data: t.data,
|
|
|
|
status: provider.formatter.number(_receipts[i].status),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.reverse();
|
2021-08-03 02:07:27 +00:00
|
|
|
setTxs(rawTxs);
|
2021-08-03 03:45:07 +00:00
|
|
|
setTotalTxs(result.fullblock.transactionCount);
|
2021-08-02 02:53:36 +00:00
|
|
|
|
|
|
|
const checkTouchMinerAddr = await Promise.all(
|
2021-08-03 02:07:27 +00:00
|
|
|
rawTxs.map(async (res) => {
|
2021-08-02 02:53:36 +00:00
|
|
|
const ops = await getInternalOperations(provider, res.hash);
|
|
|
|
return (
|
|
|
|
ops.findIndex(
|
|
|
|
(op) =>
|
|
|
|
op.type === OperationType.TRANSFER &&
|
|
|
|
res.miner !== undefined &&
|
2021-08-08 22:49:45 +00:00
|
|
|
res.miner === getAddress(op.to)
|
2021-08-02 02:53:36 +00:00
|
|
|
) !== -1
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
2021-08-03 02:07:27 +00:00
|
|
|
const processedTxs = rawTxs.map(
|
2021-08-02 02:53:36 +00:00
|
|
|
(r, i): ProcessedTransaction => ({
|
|
|
|
...r,
|
|
|
|
internalMinerInteraction: checkTouchMinerAddr[i],
|
|
|
|
})
|
|
|
|
);
|
2021-08-03 02:07:27 +00:00
|
|
|
setTxs(processedTxs);
|
2021-08-02 02:53:36 +00:00
|
|
|
};
|
|
|
|
readBlock();
|
2021-08-03 04:34:14 +00:00
|
|
|
}, [provider, blockNumber, pageNumber, pageSize]);
|
2021-08-02 02:53:36 +00:00
|
|
|
|
2021-08-03 03:45:07 +00:00
|
|
|
return [totalTxs, txs];
|
2021-08-02 02:53:36 +00:00
|
|
|
};
|
|
|
|
|
2021-07-26 22:59:07 +00:00
|
|
|
export const useBlockData = (
|
2021-08-08 22:49:45 +00:00
|
|
|
provider: JsonRpcProvider | undefined,
|
2021-07-26 22:59:07 +00:00
|
|
|
blockNumberOrHash: string
|
|
|
|
) => {
|
|
|
|
const [block, setBlock] = useState<ExtendedBlock>();
|
|
|
|
useEffect(() => {
|
|
|
|
if (!provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-29 18:09:28 +00:00
|
|
|
const _readBlock = async () => {
|
|
|
|
const extBlock = await readBlock(provider, blockNumberOrHash);
|
2021-07-26 22:59:07 +00:00
|
|
|
setBlock(extBlock);
|
|
|
|
};
|
2021-07-29 18:09:28 +00:00
|
|
|
_readBlock();
|
2021-07-26 22:59:07 +00:00
|
|
|
}, [provider, blockNumberOrHash]);
|
|
|
|
|
|
|
|
return block;
|
|
|
|
};
|
|
|
|
|
2021-08-01 09:59:59 +00:00
|
|
|
export const useTxData = (
|
2021-08-08 22:49:45 +00:00
|
|
|
provider: JsonRpcProvider | undefined,
|
2021-08-01 09:59:59 +00:00
|
|
|
txhash: string
|
2021-09-04 06:19:42 +00:00
|
|
|
): TransactionData | undefined | null => {
|
|
|
|
const [txData, setTxData] = useState<TransactionData | undefined | null>();
|
2021-08-01 09:59:59 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-02 20:06:53 +00:00
|
|
|
const readTxData = async () => {
|
2021-08-01 09:59:59 +00:00
|
|
|
const [_response, _receipt] = await Promise.all([
|
|
|
|
provider.getTransaction(txhash),
|
|
|
|
provider.getTransactionReceipt(txhash),
|
|
|
|
]);
|
2021-09-04 06:19:42 +00:00
|
|
|
if (_response === null) {
|
|
|
|
setTxData(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let _block: ExtendedBlock | undefined;
|
|
|
|
if (_response.blockNumber) {
|
|
|
|
_block = await readBlock(provider, _response.blockNumber.toString());
|
|
|
|
}
|
|
|
|
|
2021-08-01 09:59:59 +00:00
|
|
|
document.title = `Transaction ${_response.hash} | Otterscan`;
|
|
|
|
|
|
|
|
// Extract token transfers
|
|
|
|
const tokenTransfers: TokenTransfer[] = [];
|
2021-09-04 06:19:42 +00:00
|
|
|
if (_receipt) {
|
|
|
|
for (const l of _receipt.logs) {
|
|
|
|
if (l.topics.length !== 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (l.topics[0] !== TRANSFER_TOPIC) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tokenTransfers.push({
|
|
|
|
token: l.address,
|
|
|
|
from: getAddress(hexDataSlice(arrayify(l.topics[1]), 12)),
|
|
|
|
to: getAddress(hexDataSlice(arrayify(l.topics[2]), 12)),
|
|
|
|
value: BigNumber.from(l.data),
|
|
|
|
});
|
2021-08-01 09:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract token meta
|
|
|
|
const tokenMetas: TokenMetas = {};
|
|
|
|
for (const t of tokenTransfers) {
|
|
|
|
if (tokenMetas[t.token]) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-08 22:49:45 +00:00
|
|
|
const erc20Contract = new Contract(t.token, erc20, provider);
|
2021-08-01 09:59:59 +00:00
|
|
|
const [name, symbol, decimals] = await Promise.all([
|
|
|
|
erc20Contract.name(),
|
|
|
|
erc20Contract.symbol(),
|
|
|
|
erc20Contract.decimals(),
|
|
|
|
]);
|
|
|
|
tokenMetas[t.token] = {
|
|
|
|
name,
|
|
|
|
symbol,
|
|
|
|
decimals,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setTxData({
|
2021-09-04 06:19:42 +00:00
|
|
|
transactionHash: _response.hash,
|
|
|
|
from: _response.from,
|
|
|
|
to: _response.to,
|
2021-08-01 09:59:59 +00:00
|
|
|
value: _response.value,
|
|
|
|
tokenTransfers,
|
|
|
|
tokenMetas,
|
|
|
|
type: _response.type ?? 0,
|
|
|
|
maxFeePerGas: _response.maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas: _response.maxPriorityFeePerGas,
|
|
|
|
gasPrice: _response.gasPrice!,
|
|
|
|
gasLimit: _response.gasLimit,
|
|
|
|
nonce: _response.nonce,
|
|
|
|
data: _response.data,
|
2021-09-04 06:19:42 +00:00
|
|
|
confirmedData:
|
|
|
|
_receipt === null
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
status: _receipt.status === 1,
|
|
|
|
blockNumber: _receipt.blockNumber,
|
|
|
|
transactionIndex: _receipt.transactionIndex,
|
|
|
|
blockBaseFeePerGas: _block!.baseFeePerGas,
|
|
|
|
blockTransactionCount: _block!.transactionCount,
|
|
|
|
confirmations: _receipt.confirmations,
|
|
|
|
timestamp: _block!.timestamp,
|
|
|
|
miner: _block!.miner,
|
|
|
|
createdContractAddress: _receipt.contractAddress,
|
|
|
|
fee: _response.gasPrice!.mul(_receipt.gasUsed),
|
|
|
|
gasUsed: _receipt.gasUsed,
|
|
|
|
logs: _receipt.logs,
|
|
|
|
},
|
2021-08-01 09:59:59 +00:00
|
|
|
});
|
|
|
|
};
|
2021-08-02 20:06:53 +00:00
|
|
|
readTxData();
|
2021-08-01 09:59:59 +00:00
|
|
|
}, [provider, txhash]);
|
|
|
|
|
|
|
|
return txData;
|
|
|
|
};
|
|
|
|
|
2021-07-21 19:06:51 +00:00
|
|
|
export const useInternalOperations = (
|
2021-08-08 22:49:45 +00:00
|
|
|
provider: JsonRpcProvider | undefined,
|
2021-09-04 06:19:42 +00:00
|
|
|
txData: TransactionData | undefined | null
|
2021-07-21 19:06:51 +00:00
|
|
|
): InternalOperation[] | undefined => {
|
|
|
|
const [intTransfers, setIntTransfers] = useState<InternalOperation[]>();
|
2021-07-17 18:00:08 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const traceTransfers = async () => {
|
2021-09-04 06:19:42 +00:00
|
|
|
if (!provider || !txData || !txData.confirmedData) {
|
2021-07-17 18:00:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-02 02:10:32 +00:00
|
|
|
const _transfers = await getInternalOperations(
|
|
|
|
provider,
|
|
|
|
txData.transactionHash
|
|
|
|
);
|
2021-07-19 23:49:54 +00:00
|
|
|
for (const t of _transfers) {
|
|
|
|
t.from = provider.formatter.address(t.from);
|
|
|
|
t.to = provider.formatter.address(t.to);
|
|
|
|
t.value = provider.formatter.bigNumber(t.value);
|
2021-07-17 18:58:33 +00:00
|
|
|
}
|
2021-07-19 23:49:54 +00:00
|
|
|
setIntTransfers(_transfers);
|
2021-07-17 18:00:08 +00:00
|
|
|
};
|
|
|
|
traceTransfers();
|
|
|
|
}, [provider, txData]);
|
|
|
|
|
2021-07-17 18:58:33 +00:00
|
|
|
return intTransfers;
|
2021-07-17 18:00:08 +00:00
|
|
|
};
|