import React, {
useState,
useRef,
useCallback,
useMemo,
useContext,
} from "react";
import { Tooltip, OverlayTrigger, Spinner } from "react-bootstrap";
import { Row } from "react-table";
import ToAddrDisp from "src/components/Misc/Disp/ToAddrDisp/ToAddrDisp";
import ViewAllTable from "src/components/ViewAllPages/ViewAllTable/ViewAllTable";
import {
NetworkContext,
QueryPreservingLink,
} from "src/services/network/networkProvider";
import { TransactionDetails } from "src/typings/api";
import { hexAddrToZilAddr, qaToZil } from "src/utils/Utils";
import { TxList } from "@zilliqa-js/core/src/types";
import { Transaction } from "@zilliqa-js/account/src/transaction";
import { faExclamationCircle } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
const TxnsPage: React.FC = () => {
const networkContext = useContext(NetworkContext);
if (!networkContext) {
return (
);
}
const { dataService } = networkContext;
const fetchIdRef = useRef(0);
const [isLoading, setIsLoading] = useState(false);
const [pageCount, setPageCount] = useState(0);
const [data, setData] = useState(null);
const [recentTxnHashes, setRecentTxnHashes] = useState(null);
const columns = useMemo(
() => [
{
id: "from-col",
Header: "From",
accessor: "txn.senderAddress",
Cell: ({ value }: { value: string }) => (
{hexAddrToZilAddr(value)}
),
},
{
id: "to-col",
Header: "To",
Cell: ({ row }: { row: Row }) => {
return ;
},
},
{
id: "hash-col",
Header: "Hash",
accessor: "hash",
Cell: ({ row }: { row: Row }) => {
console.log(row);
return (
{row.original.txn.txParams.receipt &&
!row.original.txn.txParams.receipt.success && (
)}
{"0x" + row.original.hash}
);
},
},
{
id: "amount-col",
Header: "Amount",
accessor: "txn.amount",
Cell: ({ value }: { value: string }) => (
{qaToZil(value)}}
>
{qaToZil(value, 12)}
),
},
{
id: "fee-col",
Header: "Fee",
accessor: "txn",
Cell: ({ value }: { value: Transaction }) => {
let fee = 0;
if (value.txParams.receipt) {
fee =
Number(value.txParams.gasPrice) *
value.txParams.receipt.cumulative_gas;
}
return (
{qaToZil(fee)}}
>
{qaToZil(fee, 4)}
);
},
},
],
[]
);
const fetchData = useCallback(
({ pageIndex }: { pageIndex: number }) => {
if (!dataService) return;
const fetchId = ++fetchIdRef.current;
let txnHashes: string[] | null;
let txnList: TxList;
let txnBodies: TransactionDetails[];
const getData = async () => {
try {
setIsLoading(true);
txnHashes = recentTxnHashes;
if (!txnHashes) {
txnList = await dataService.getRecentTransactions();
if (!txnList) return;
txnHashes = txnList.TxnHashes;
setPageCount(Math.ceil(txnList.number / 10));
setRecentTxnHashes(txnHashes);
}
const slicedTxnHashes = txnHashes.slice(
pageIndex * 10,
pageIndex * 10 + 10
);
if (slicedTxnHashes) {
txnBodies = await dataService.getTransactionsDetails(
slicedTxnHashes
);
if (txnBodies) setData(txnBodies);
}
} catch (e) {
console.log(e);
} finally {
setIsLoading(false);
}
};
if (fetchId === fetchIdRef.current) getData();
},
[dataService]
);
return (
<>
{
Recent Transactions
}
>
);
};
export default TxnsPage;