From 2070fe18229e3ea20ccd27bd8df4a5d379d0c315 Mon Sep 17 00:00:00 2001 From: shanejonas Date: Mon, 9 Nov 2020 08:08:10 -0800 Subject: [PATCH] fix: handle bigint with maxBy and minBy fixes #439 --- src/components/BlockView/BlockGasPrice.tsx | 47 ++++++++++++++++------ 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/components/BlockView/BlockGasPrice.tsx b/src/components/BlockView/BlockGasPrice.tsx index 9eaf8951..806bf02a 100644 --- a/src/components/BlockView/BlockGasPrice.tsx +++ b/src/components/BlockView/BlockGasPrice.tsx @@ -1,36 +1,57 @@ import * as React from "react"; import { hexToNumber } from "@etclabscore/eserialize"; import { useTranslation } from "react-i18next"; -import {meanBy, minBy, maxBy} from "lodash"; - import { TableCell, TableRow } from "@material-ui/core"; +const BigIntMinBy = (list: [], func: (item: any) => BigInt): any => { + let min: any; + for (const item of list) { + if (min === undefined) { + min = item; + continue; + } + const r = func(item); + if (r < func(min)) { + min = item; + } + } + return min; +}; + +const BigIntMaxBy = (list: [], func: (item: any) => BigInt): any => { + let max: any; + for (const item of list) { + if (max === undefined) { + max = item; + continue; + } + const r = func(item); + if (r > func(max)) { + max = item; + } + } + return max; +}; + function BlockGasPrice(props: any) { const { t } = useTranslation(); - const {transactions} = props.block; + const { transactions } = props.block; - if (transactions.length === 0) {return <>;} + if (transactions.length === 0) { return null; } return ( <> - - {t("Average Gas Price")} - - { meanBy(transactions, (t: any) => BigInt(t.gasPrice)) } - - - {t("Min Gas Price")} - { hexToNumber(minBy(transactions, (t: any) => BigInt(t.gasPrice)).gasPrice) } + {hexToNumber(BigIntMinBy(transactions, (tx: any) => BigInt(tx.gasPrice))?.gasPrice || "")} {t("Max Gas Price")} - { hexToNumber(maxBy(transactions, (t: any) => BigInt(t.gasPrice)).gasPrice) } + {hexToNumber(BigIntMaxBy(transactions, (tx: any) => BigInt(tx.gasPrice))?.gasPrice) || ""}