Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

Commit

Permalink
fix: handle bigint with maxBy and minBy
Browse files Browse the repository at this point in the history
fixes #439
  • Loading branch information
shanejonas committed Nov 9, 2020
1 parent 27d6f76 commit 2070fe1
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/components/BlockView/BlockGasPrice.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<TableRow>
<TableCell>{t("Average Gas Price")}</TableCell>
<TableCell>
{ meanBy(transactions, (t: any) => BigInt(t.gasPrice)) }
</TableCell>
</TableRow>

<TableRow>
<TableCell>{t("Min Gas Price")}</TableCell>
<TableCell>
{ hexToNumber(minBy(transactions, (t: any) => BigInt(t.gasPrice)).gasPrice) }
{hexToNumber(BigIntMinBy(transactions, (tx: any) => BigInt(tx.gasPrice))?.gasPrice || "")}
</TableCell>
</TableRow>

<TableRow>
<TableCell>{t("Max Gas Price")}</TableCell>
<TableCell>
{ hexToNumber(maxBy(transactions, (t: any) => BigInt(t.gasPrice)).gasPrice) }
{hexToNumber(BigIntMaxBy(transactions, (tx: any) => BigInt(tx.gasPrice))?.gasPrice) || ""}
</TableCell>
</TableRow>
</>
Expand Down

0 comments on commit 2070fe1

Please sign in to comment.