diff --git a/.changeset/heavy-clocks-boil.md b/.changeset/heavy-clocks-boil.md new file mode 100644 index 000000000..a1f1fa64f --- /dev/null +++ b/.changeset/heavy-clocks-boil.md @@ -0,0 +1,5 @@ +--- +"@blobscan/web": patch +--- + +Standarized y-axis units and displayed full amounts on tooltips on charts diff --git a/.changeset/pretty-pigs-type.md b/.changeset/pretty-pigs-type.md new file mode 100644 index 000000000..a216571e5 --- /dev/null +++ b/.changeset/pretty-pigs-type.md @@ -0,0 +1,5 @@ +--- +"@blobscan/eth-units": minor +--- + +Made unit display optional when formatting wei amounts diff --git a/.changeset/thick-trains-cry.md b/.changeset/thick-trains-cry.md new file mode 100644 index 000000000..3cf67159b --- /dev/null +++ b/.changeset/thick-trains-cry.md @@ -0,0 +1,5 @@ +--- +"@blobscan/web": patch +--- + +Displayed full amounts on chart tooltips diff --git a/apps/web/package.json b/apps/web/package.json index 1aa0f7ad9..8366b85b2 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -49,7 +49,6 @@ "next": "^14.2.5", "next-themes": "^0.2.1", "posthog-js": "^1.166.1", - "pretty-bytes": "^6.1.1", "react": "18.2.0", "react-blockies": "^1.4.1", "react-dom": "18.2.0", diff --git a/apps/web/src/components/Cards/MetricCard.tsx b/apps/web/src/components/Cards/MetricCard.tsx index 4643b5666..1f2d1e785 100644 --- a/apps/web/src/components/Cards/MetricCard.tsx +++ b/apps/web/src/components/Cards/MetricCard.tsx @@ -5,9 +5,14 @@ import cn from "classnames"; import "react-loading-skeleton/dist/skeleton.css"; import Skeleton from "react-loading-skeleton"; -import { findBestUnit, prettyFormatWei } from "@blobscan/eth-units"; - -import { formatBytes, formatNumber } from "~/utils"; +import { prettyFormatWei } from "@blobscan/eth-units"; + +import { + formatBytes, + formatNumber, + parseDecimalNumber, + parseSuffixedNumber, +} from "~/utils"; import { Card } from "./Card"; export type MetricType = "standard" | "bytes" | "ethereum" | "percentage"; @@ -24,10 +29,6 @@ export type MetricCardProps = Partial<{ secondaryMetric?: MetricProp; }>; -function isInteger(value: bigint | number) { - return Number.isInteger(value) || typeof value === "bigint"; -} - /** * Creates a placeholder string that takes up the maximum amount of space the input could take with * that amount of characters @@ -41,7 +42,7 @@ function createPlaceholder(input: string): string { return replacedString; } -function formatMetric( +function parseMetricValue( value: number | bigint, { type, @@ -66,7 +67,7 @@ function formatMetric( formattedValue = formatBytes(value); break; case "ethereum": - formattedValue = prettyFormatWei(value, findBestUnit(value)); + formattedValue = prettyFormatWei(value); break; case "percentage": formattedValue = `${formatNumber(value, mode, { @@ -83,9 +84,17 @@ function formatMetric( } const [value_ = "0", unit = ""] = formattedValue.split(" "); + const [numericPart, suffix] = parseSuffixedNumber(value_); + const [integerPart, decimalPart] = parseDecimalNumber( + numericPart ? numericPart.toString() : "" + ); return { value: value_, + numericPart, + integerPart, + decimalPart, + suffix, unit, }; } @@ -124,16 +133,18 @@ function Metric({ compact, isSecondary = false, }: MetricProp & { compact?: boolean; isSecondary?: boolean }) { - const isValueInteger = value && isInteger(value); + const parsedMetric = parseMetricValue(value ?? 0, { + type, + compact, + numberFormatOpts, + }); + const valueProps = useSpring({ - value: Number(value), + value: parsedMetric.numericPart, from: { value: 0 }, cancel: !value, }); const isValueSet = value !== undefined; - const formattedMetric = isValueSet - ? formatMetric(value, { type, compact, numberFormatOpts }) - : undefined; return (
@@ -142,15 +153,13 @@ function Metric({ {isValueSet ? ( - {valueProps.value.to((x) => { - const x_ = isValueInteger ? Math.trunc(x) : x; - const { value: formattedX } = formatMetric(x_, { - type, - compact, - numberFormatOpts, - }); - - return formattedX; + {valueProps.value.to((v) => { + const formattedValue = formatNumber( + v.toFixed(parsedMetric.decimalPart?.length ?? 0) + ); + const suffix = parsedMetric.suffix ?? ""; + + return `${formattedValue}${suffix}`; })} ) : ( @@ -164,7 +173,7 @@ function Metric({ */}
- {createPlaceholder(formattedMetric?.value ?? "")} + {createPlaceholder(parsedMetric.value ?? "")}
- {formattedMetric?.unit} + {parsedMetric?.unit}
diff --git a/apps/web/src/components/Charts/Blob/DailyBlobSizeChart.tsx b/apps/web/src/components/Charts/Blob/DailyBlobSizeChart.tsx index 2a58c135b..a1cdd86c5 100644 --- a/apps/web/src/components/Charts/Blob/DailyBlobSizeChart.tsx +++ b/apps/web/src/components/Charts/Blob/DailyBlobSizeChart.tsx @@ -21,15 +21,15 @@ export const DailyBlobSizeChart: FC> = function ({ ...buildTimeSeriesOptions({ dates: days, axisFormatters: { - yAxisLabel: (value: number) => - formatBytes(value, { maximumFractionDigits: 0 }), - yAxisTooltip: (value: number) => formatBytes(value), + yAxisLabel: (value) => + formatBytes(Number(value), { + unit: "GiB", + hideUnit: true, + }), + yAxisTooltip: (value) => + formatBytes(Number(value), { unit: "GiB", displayAllDecimals: true }), }, - yUnit: "bytes", }), - grid: { - containLabel: true, - }, series: [ { name: "Blob Size", @@ -40,5 +40,7 @@ export const DailyBlobSizeChart: FC> = function ({ ], }; - return ; + return ( + + ); }; diff --git a/apps/web/src/components/Charts/Blob/DailyBlobsChart.tsx b/apps/web/src/components/Charts/Blob/DailyBlobsChart.tsx index 37ad74c95..58c4eae19 100644 --- a/apps/web/src/components/Charts/Blob/DailyBlobsChart.tsx +++ b/apps/web/src/components/Charts/Blob/DailyBlobsChart.tsx @@ -20,12 +20,9 @@ export const DailyBlobsChart: FC> = function ({ ...buildTimeSeriesOptions({ dates: days, axisFormatters: { - yAxisTooltip: (value) => formatNumber(value, "compact"), + yAxisTooltip: (value) => formatNumber(value), }, }), - grid: { - containLabel: true, - }, series: [ { name: "Total Blobs", diff --git a/apps/web/src/components/Charts/Block/DailyAvgBlobFeeChart.tsx b/apps/web/src/components/Charts/Block/DailyAvgBlobFeeChart.tsx index 9765c4369..c85fb6688 100644 --- a/apps/web/src/components/Charts/Block/DailyAvgBlobFeeChart.tsx +++ b/apps/web/src/components/Charts/Block/DailyAvgBlobFeeChart.tsx @@ -1,7 +1,7 @@ import type { FC } from "react"; import type { EChartOption } from "echarts"; -import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units"; +import { formatWei, prettyFormatWei } from "@blobscan/eth-units"; import { ChartCard } from "~/components/Cards/ChartCard"; import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts"; @@ -21,14 +21,11 @@ export const DailyAvgBlobFeeChart: FC> = ...buildTimeSeriesOptions({ dates: days, axisFormatters: { - yAxisTooltip: (value) => formatWei(value, findBestUnit(value)), - yAxisLabel: (value) => prettyFormatWei(value, unit), + yAxisTooltip: (value) => formatWei(value, { toUnit: unit }), + yAxisLabel: (value) => + prettyFormatWei(value, { toUnit: unit, hideUnit: true }), }, - yUnit: "ethereum", }), - grid: { - containLabel: true, - }, series: [ { name: "Avg. Blob Fees", @@ -40,6 +37,10 @@ export const DailyAvgBlobFeeChart: FC> = }; return ( - + ); }; diff --git a/apps/web/src/components/Charts/Block/DailyAvgBlobGasPriceChart.tsx b/apps/web/src/components/Charts/Block/DailyAvgBlobGasPriceChart.tsx index d5266f78d..52fe5266a 100644 --- a/apps/web/src/components/Charts/Block/DailyAvgBlobGasPriceChart.tsx +++ b/apps/web/src/components/Charts/Block/DailyAvgBlobGasPriceChart.tsx @@ -1,7 +1,7 @@ import type { FC } from "react"; import type { EChartOption } from "echarts"; -import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units"; +import { formatWei, prettyFormatWei } from "@blobscan/eth-units"; import { ChartCard } from "~/components/Cards/ChartCard"; import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts"; @@ -22,14 +22,11 @@ export const DailyAvgBlobGasPriceChart: FC< ...buildTimeSeriesOptions({ dates: days, axisFormatters: { - yAxisTooltip: (value) => formatWei(value, findBestUnit(value)), - yAxisLabel: (value) => prettyFormatWei(value, unit), + yAxisTooltip: (value) => formatWei(value, { toUnit: unit }), + yAxisLabel: (value) => + prettyFormatWei(value, { toUnit: unit, hideUnit: true }), }, - yUnit: "ethereum", }), - grid: { - containLabel: true, - }, series: [ { name: "Avg. Blob Gas Prices", @@ -41,6 +38,10 @@ export const DailyAvgBlobGasPriceChart: FC< }; return ( - + ); }; diff --git a/apps/web/src/components/Charts/Block/DailyBlobFeeChart.tsx b/apps/web/src/components/Charts/Block/DailyBlobFeeChart.tsx index 10379db35..184cf1a8a 100644 --- a/apps/web/src/components/Charts/Block/DailyBlobFeeChart.tsx +++ b/apps/web/src/components/Charts/Block/DailyBlobFeeChart.tsx @@ -1,7 +1,7 @@ import type { FC } from "react"; import type { EChartOption } from "echarts"; -import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units"; +import { formatWei, prettyFormatWei } from "@blobscan/eth-units"; import { ChartCard } from "~/components/Cards/ChartCard"; import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts"; @@ -22,14 +22,11 @@ export const DailyBlobFeeChart: FC> = ...buildTimeSeriesOptions({ dates: days, axisFormatters: { - yAxisTooltip: (value) => formatWei(value, findBestUnit(value)), - yAxisLabel: (value) => prettyFormatWei(value, unit), + yAxisTooltip: (value) => formatWei(value, { toUnit: unit }), + yAxisLabel: (value) => + prettyFormatWei(value, { toUnit: unit, hideUnit: true }), }, - yUnit: "ethereum", }), - grid: { - containLabel: true, - }, series: [ { name: "Blob Fees", @@ -40,5 +37,11 @@ export const DailyBlobFeeChart: FC> = animationEasing: "cubicOut", }; - return ; + return ( + + ); }; diff --git a/apps/web/src/components/Charts/Block/DailyBlobGasComparisonChart.tsx b/apps/web/src/components/Charts/Block/DailyBlobGasComparisonChart.tsx index ac382f64e..ede8bbcb5 100644 --- a/apps/web/src/components/Charts/Block/DailyBlobGasComparisonChart.tsx +++ b/apps/web/src/components/Charts/Block/DailyBlobGasComparisonChart.tsx @@ -3,12 +3,9 @@ import * as echarts from "echarts"; import type { EChartOption } from "echarts"; import { useTheme } from "next-themes"; -import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units"; - import { ChartCard } from "~/components/Cards/ChartCard"; -import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts"; import type { DailyBlockStats } from "~/types"; -import { buildTimeSeriesOptions } from "~/utils"; +import { buildTimeSeriesOptions, formatNumber } from "~/utils"; export type DailyBlobGasComparisonChartProps = Partial<{ days: DailyBlockStats["days"]; @@ -20,24 +17,18 @@ export type DailyBlobGasComparisonChartProps = Partial<{ export const DailyBlobGasComparisonChart: FC = function ({ blobAsCalldataGasUsed, blobGasUsed, days, opts = {} }) { const { resolvedTheme } = useTheme(); - const data = blobGasUsed?.map((x) => Number(x)); - const { unit } = useScaledWeiAmounts(data); const options: EChartOption = { ...buildTimeSeriesOptions({ dates: days, axisFormatters: { - yAxisTooltip: (value) => formatWei(value, findBestUnit(value)), - yAxisLabel: (value) => prettyFormatWei(value, unit), + yAxisTooltip: (value) => formatNumber(value, "standard"), }, }), - grid: { - containLabel: true, - }, series: [ { name: "Blob Gas Used", - data: data, + data: blobGasUsed, stack: "gas", type: "bar", diff --git a/apps/web/src/components/Charts/Block/DailyBlobGasUsedChart.tsx b/apps/web/src/components/Charts/Block/DailyBlobGasUsedChart.tsx index da364f44a..3ef2f4c6c 100644 --- a/apps/web/src/components/Charts/Block/DailyBlobGasUsedChart.tsx +++ b/apps/web/src/components/Charts/Block/DailyBlobGasUsedChart.tsx @@ -1,12 +1,9 @@ import type { FC } from "react"; import type { EChartOption } from "echarts"; -import { findBestUnit, formatWei, prettyFormatWei } from "@blobscan/eth-units"; - import { ChartCard } from "~/components/Cards/ChartCard"; -import { useScaledWeiAmounts } from "~/hooks/useScaledWeiAmounts"; import type { DailyBlockStats } from "~/types"; -import { buildTimeSeriesOptions } from "~/utils"; +import { buildTimeSeriesOptions, formatNumber } from "~/utils"; export type DailyBlobGasUsedChartProps = Partial<{ days: DailyBlockStats["days"]; @@ -15,23 +12,17 @@ export type DailyBlobGasUsedChartProps = Partial<{ const BaseChart: FC = function ({ days, blobGasUsed, title }) { - const data = blobGasUsed?.map((x) => Number(x)); - const { unit } = useScaledWeiAmounts(data); const options: EChartOption = { ...buildTimeSeriesOptions({ dates: days, axisFormatters: { - yAxisTooltip: (value) => formatWei(value, findBestUnit(value)), - yAxisLabel: (value) => prettyFormatWei(value, unit), + yAxisTooltip: (value) => formatNumber(value, "standard"), }, }), - grid: { - containLabel: true, - }, series: [ { name: "Blob Gas Used", - data: data, + data: blobGasUsed, stack: "gas", type: "bar", }, diff --git a/apps/web/src/components/Charts/Block/DailyBlocksChart.tsx b/apps/web/src/components/Charts/Block/DailyBlocksChart.tsx index 61e69736d..3f6983e90 100644 --- a/apps/web/src/components/Charts/Block/DailyBlocksChart.tsx +++ b/apps/web/src/components/Charts/Block/DailyBlocksChart.tsx @@ -18,12 +18,9 @@ export const DailyBlocksChart: FC> = function ({ ...buildTimeSeriesOptions({ dates: days, axisFormatters: { - yAxisTooltip: (value) => formatNumber(value, "compact"), + yAxisTooltip: (value) => formatNumber(value), }, }), - grid: { - containLabel: true, - }, series: [ { name: "Total Blocks", diff --git a/apps/web/src/components/Charts/ChartBase.tsx b/apps/web/src/components/Charts/ChartBase.tsx index 4f9e8e8cb..ea38a7d0d 100644 --- a/apps/web/src/components/Charts/ChartBase.tsx +++ b/apps/web/src/components/Charts/ChartBase.tsx @@ -13,7 +13,7 @@ type ChartBaseProps = { }; const COMMON_OPTIONS: EChartOption = { - grid: { top: 27, right: 10, bottom: 22, left: 45 }, + grid: { top: 27, right: 10, bottom: 22, left: 55 }, toolbox: { show: true, feature: { @@ -119,7 +119,6 @@ export const ChartBase: FC = function ({ return ( formatWei(value, findBestUnit(value)), - yAxisLabel: (value) => prettyFormatWei(value, unit), + yAxisTooltip: (value) => formatWei(value, { toUnit: unit }), + yAxisLabel: (value) => + prettyFormatWei(value, { toUnit: unit, hideUnit: true }), }, }), - grid: { - containLabel: true, - }, series: [ { name: "Avg. Max Blob Gas Fees", @@ -44,7 +42,7 @@ export const DailyAvgMaxBlobGasFeeChart: FC< return ( diff --git a/apps/web/src/components/Charts/Transaction/DailyTransactionsChart.tsx b/apps/web/src/components/Charts/Transaction/DailyTransactionsChart.tsx index 93e978c98..bd8efd8a0 100644 --- a/apps/web/src/components/Charts/Transaction/DailyTransactionsChart.tsx +++ b/apps/web/src/components/Charts/Transaction/DailyTransactionsChart.tsx @@ -28,7 +28,6 @@ export const DailyTransactionsChart: FC> = name: "Total Transactions", data: transactions, type: compact ? "line" : "bar", - smooth: true, }, ], ...opts, diff --git a/apps/web/src/components/Displays/EtherUnitDisplay.tsx b/apps/web/src/components/Displays/EtherUnitDisplay.tsx index cb78b26a9..9ef84688c 100644 --- a/apps/web/src/components/Displays/EtherUnitDisplay.tsx +++ b/apps/web/src/components/Displays/EtherUnitDisplay.tsx @@ -1,14 +1,21 @@ import type { FC } from "react"; -import { formatWei, findBestUnit } from "@blobscan/eth-units"; -import type { EtherUnit } from "@blobscan/eth-units"; +import { formatWei } from "@blobscan/eth-units"; +import type { EtherUnit, FormatOptions } from "@blobscan/eth-units"; type Props = { amount: bigint | number | string; toUnit?: EtherUnit; + opts?: FormatOptions; }; -export const EtherUnitDisplay: FC = ({ amount, toUnit }) => { - toUnit = toUnit ? toUnit : findBestUnit(amount); - return
{formatWei(amount, toUnit)}
; +export const EtherUnitDisplay: FC = ({ amount, toUnit, opts = {} }) => { + return ( +
+ {formatWei(amount, { + toUnit, + ...opts, + })} +
+ ); }; diff --git a/apps/web/src/components/Displays/StandardEtherUnitDisplay.tsx b/apps/web/src/components/Displays/StandardEtherUnitDisplay.tsx index 3137b2776..283f23a6f 100644 --- a/apps/web/src/components/Displays/StandardEtherUnitDisplay.tsx +++ b/apps/web/src/components/Displays/StandardEtherUnitDisplay.tsx @@ -1,13 +1,27 @@ +import type { FormatOptions } from "@blobscan/eth-units"; import { formatWei } from "@blobscan/eth-units"; import { EtherUnitDisplay } from "./EtherUnitDisplay"; -export const StandardEtherUnitDisplay = ({ amount }: { amount: bigint }) => { +type StandardEtherUnitDisplayProps = { + amount: bigint; + opts?: FormatOptions; +}; + +export const StandardEtherUnitDisplay = ({ + amount, + opts = {}, +}: StandardEtherUnitDisplayProps) => { return (
- + - ({formatWei(amount, "Gwei")}) + ( + {formatWei(amount, { + toUnit: "Gwei", + ...opts, + })} + )
); diff --git a/apps/web/src/pages/stats/index.tsx b/apps/web/src/pages/stats/index.tsx index f156e6746..f186f852d 100644 --- a/apps/web/src/pages/stats/index.tsx +++ b/apps/web/src/pages/stats/index.tsx @@ -69,7 +69,7 @@ function OverallStats() { @@ -77,7 +77,6 @@ function OverallStats() { name="Total Blob Gas Used" metric={{ value: overall ? BigInt(overall.block.totalBlobGasUsed) : undefined, - type: "ethereum", }} /> @@ -137,7 +136,6 @@ function OverallStats() { ? BigInt(overall.block.totalBlobAsCalldataGasUsed) - BigInt(overall.block.totalBlobGasUsed) : undefined, - type: "ethereum", }} // secondaryMetric={ // overallStats && diff --git a/apps/web/src/utils/bytes.ts b/apps/web/src/utils/bytes.ts new file mode 100644 index 000000000..73e507a95 --- /dev/null +++ b/apps/web/src/utils/bytes.ts @@ -0,0 +1,46 @@ +export const BYTE_UNITS = [ + "B", + "KiB", + "MiB", + "GiB", + "TiB", + "PiB", + "EiB", + "ZiB", + "YiB", +] as const; + +export type BytesOptions = { + displayAllDecimals?: boolean; + decimals?: number; + hideUnit?: boolean; + unit?: (typeof BYTE_UNITS)[number]; +}; + +export function formatBytes( + value: number | bigint, + opts?: BytesOptions +): string { + const { decimals = 2, displayAllDecimals, hideUnit, unit } = opts ?? {}; + + const bytes_ = typeof value !== "number" ? Number(value) : value; + + if (bytes_ === 0) return hideUnit ? "0" : "0 B"; + + const k = 1024; // Base for binary prefixes + const decimals_ = decimals < 0 ? 0 : decimals; + + // Determine the index of the appropriate unit + const i = unit + ? BYTE_UNITS.indexOf(unit) + : Math.floor(Math.log(bytes_) / Math.log(k)); + + const convertedValue = bytes_ / Math.pow(k, i); + const formattedValue = parseFloat( + displayAllDecimals + ? convertedValue.toString() + : convertedValue.toFixed(decimals_) + ).toString(); + + return hideUnit ? formattedValue : `${formattedValue} ${BYTE_UNITS[i]}`; +} diff --git a/apps/web/src/utils/charts.ts b/apps/web/src/utils/charts.ts index 341d1c821..592b71051 100644 --- a/apps/web/src/utils/charts.ts +++ b/apps/web/src/utils/charts.ts @@ -53,8 +53,6 @@ export function createTooltip( }; } -type YUnit = "ethereum" | "bytes"; - export type TimeSeriesInput = Partial<{ dates: string[]; axisFormatters: Partial<{ @@ -63,22 +61,11 @@ export type TimeSeriesInput = Partial<{ yAxisLabel: ValueFormatter; yAxisTooltip: ValueFormatter; }>; - yUnit: YUnit; }>; -const YUINT_TO_GRID: Record = { - bytes: { - left: 55, - }, - ethereum: { - left: 70, - }, -}; - export function buildTimeSeriesOptions({ dates, axisFormatters, - yUnit, }: TimeSeriesInput): EChartOption { const { xAxisLabel = dateAxisFormatter, @@ -88,7 +75,6 @@ export function buildTimeSeriesOptions({ } = axisFormatters || {}; return { xAxis: { - name: "Date", type: "category", data: dates, axisLabel: { @@ -102,6 +88,5 @@ export function buildTimeSeriesOptions({ }, }, tooltip: createTooltip(xAxisTooltip, yAxisTooltip), - grid: yUnit ? YUINT_TO_GRID[yUnit] : undefined, }; } diff --git a/apps/web/src/utils/index.ts b/apps/web/src/utils/index.ts index be0d4d6b1..28c6a2fd8 100644 --- a/apps/web/src/utils/index.ts +++ b/apps/web/src/utils/index.ts @@ -1,3 +1,4 @@ +export * from "./bytes"; export * from "./charts"; export * from "./blob-decoders"; export * from "./deserializers"; diff --git a/apps/web/src/utils/number.ts b/apps/web/src/utils/number.ts index 5b13cbc78..90020eb87 100644 --- a/apps/web/src/utils/number.ts +++ b/apps/web/src/utils/number.ts @@ -1,6 +1,3 @@ -import type { Options } from "pretty-bytes"; -import prettyBytes from "pretty-bytes"; - type FormatMode = "compact" | "standard"; export function numberToBigInt(value: number): bigint { @@ -23,16 +20,6 @@ const NUMBER_FORMAT: Record = { }, }; -export function formatBytes(bytes: number | bigint, opts: Options = {}) { - const bytes_ = typeof bytes === "bigint" ? Number(bytes) : bytes; - - return prettyBytes(bytes_, { - maximumFractionDigits: 3, - binary: true, - ...opts, - }); -} - export function abbreviateNumber(value: number | string): string { return Intl.NumberFormat("en-US", { notation: "compact", @@ -61,6 +48,32 @@ export function formatNumber( ); } +export function removeCommas(formattedNumber: string): string { + return formattedNumber.trim().replace(/,/g, ""); +} + +export function parseSuffixedNumber(value: string): [number, string?] { + // Remove any leading/trailing whitespace + value = removeCommas(value); + + // Regular expression to match the numerical part and optional suffix + const regex = /^(-?\d+(?:\.\d+)?)([a-zA-Z]+)?$/; + const match = value.match(regex); + + if (!match || !match[1]) { + return [parseFloat(value)]; + } + + const numberPart = parseFloat(match[1]); + const suffixPart = match[2] ? match[2].toUpperCase() : undefined; + + return [numberPart, suffixPart]; +} + +export function parseDecimalNumber(value: string) { + return removeCommas(value).split("."); +} + export function calculatePercentage( numerator: bigint, denominator: bigint, diff --git a/packages/eth-format/index.ts b/packages/eth-format/index.ts index f1ca6665a..bedb9fde3 100644 --- a/packages/eth-format/index.ts +++ b/packages/eth-format/index.ts @@ -2,6 +2,10 @@ const ETH_UNITS = { wei: 0, Gwei: 9, ether: 18 }; export type EthAmount = string | number | bigint; export type EtherUnit = keyof typeof ETH_UNITS; +export type FormatOptions = Partial<{ + hideUnit: boolean; + toUnit: EtherUnit; +}>; const compactFormatter = Intl.NumberFormat("en-US", { notation: "compact", @@ -20,9 +24,15 @@ const fullwideFormatter = Intl.NumberFormat("fullwide", { * This function never converts the provided value to a Number * ensuring that the full precision of the input is preserved. */ -export function formatWei(wei: EthAmount, toUnit: EtherUnit = "Gwei"): string { +export function formatWei(wei: EthAmount, opts?: FormatOptions): string { + const toUnit = opts?.toUnit || findBestUnit(wei); const converted = convertWei(wei, toUnit); const formatted = insertCommas(converted); + + if (opts?.hideUnit) { + return formatted; + } + return `${formatted} ${toUnit}`; } @@ -32,12 +42,15 @@ export function formatWei(wei: EthAmount, toUnit: EtherUnit = "Gwei"): string { * is preserved. Instead, this function provides a more human-readable * representation of the value. */ -export function prettyFormatWei( - wei: string | number | bigint, - toUnit: EtherUnit = "Gwei" -) { +export function prettyFormatWei(wei: EthAmount, opts?: FormatOptions) { + const toUnit = opts?.toUnit || findBestUnit(wei); const converted = convertWei(wei, toUnit) as Intl.StringNumericLiteral; const formatted = compactFormatter.format(converted); + + if (opts?.hideUnit) { + return formatted; + } + return `${formatted} ${toUnit}`; } @@ -51,7 +64,7 @@ export function convertWei(wei: EthAmount, toUnit: EtherUnit = "Gwei"): string { /** * This function finds the best unit to display the value of `wei`. */ -export function findBestUnit(wei: bigint | string | number): EtherUnit { +export function findBestUnit(wei: EthAmount): EtherUnit { const length = countIntegerDigits(wei); if (length >= ETH_UNITS.ether) { @@ -68,7 +81,7 @@ export function findBestUnit(wei: bigint | string | number): EtherUnit { /** * Returns the number of integer digits in the value. */ -export function countIntegerDigits(value: string | number | bigint): number { +export function countIntegerDigits(value: EthAmount): number { if (typeof value === "number" && !Number.isFinite(value)) { return 0; // Return 0 for Infinity, -Infinity, and NaN } diff --git a/packages/eth-format/test/eth-units.test.ts b/packages/eth-format/test/eth-units.test.ts index dbd4ea24d..d1b472818 100644 --- a/packages/eth-format/test/eth-units.test.ts +++ b/packages/eth-format/test/eth-units.test.ts @@ -114,227 +114,293 @@ test("negative values", () => { }); test("can format wei", () => { - expect(formatWei(BigInt("1"), "wei")).toBe("1 wei"); - expect(formatWei(BigInt("2"), "wei")).toBe("2 wei"); - expect(formatWei(BigInt("123"), "wei")).toBe("123 wei"); - expect(formatWei(BigInt("1234"), "wei")).toBe("1,234 wei"); - expect(formatWei(BigInt("123456"), "wei")).toBe("123,456 wei"); - expect(formatWei(BigInt("1234567"), "wei")).toBe("1,234,567 wei"); - expect(formatWei(BigInt("12345678"), "wei")).toBe("12,345,678 wei"); - expect(formatWei(BigInt("123456789"), "wei")).toBe("123,456,789 wei"); - expect(formatWei(BigInt("1234567890"), "wei")).toBe("1,234,567,890 wei"); + expect(formatWei(BigInt("1"), { toUnit: "wei" })).toBe("1 wei"); + expect(formatWei(BigInt("2"), { toUnit: "wei" })).toBe("2 wei"); + expect(formatWei(BigInt("123"), { toUnit: "wei" })).toBe("123 wei"); + expect(formatWei(BigInt("1234"), { toUnit: "wei" })).toBe("1,234 wei"); + expect(formatWei(BigInt("123456"), { toUnit: "wei" })).toBe("123,456 wei"); + expect(formatWei(BigInt("1234567"), { toUnit: "wei" })).toBe("1,234,567 wei"); + expect(formatWei(BigInt("12345678"), { toUnit: "wei" })).toBe( + "12,345,678 wei" + ); + expect(formatWei(BigInt("123456789"), { toUnit: "wei" })).toBe( + "123,456,789 wei" + ); + expect(formatWei(BigInt("1234567890"), { toUnit: "wei" })).toBe( + "1,234,567,890 wei" + ); }); test("can format negative wei", () => { - expect(formatWei(BigInt("-1"), "wei")).toBe("-1 wei"); - expect(formatWei(BigInt("-2"), "wei")).toBe("-2 wei"); - expect(formatWei(BigInt("-123"), "wei")).toBe("-123 wei"); - expect(formatWei(BigInt("-1234"), "wei")).toBe("-1,234 wei"); - expect(formatWei(BigInt("-123456"), "wei")).toBe("-123,456 wei"); - expect(formatWei(BigInt("-1234567"), "wei")).toBe("-1,234,567 wei"); - expect(formatWei(BigInt("-12345678"), "wei")).toBe("-12,345,678 wei"); - expect(formatWei(BigInt("-123456789"), "wei")).toBe("-123,456,789 wei"); - expect(formatWei(BigInt("-1234567890"), "wei")).toBe("-1,234,567,890 wei"); + expect(formatWei(BigInt("-1"), { toUnit: "wei" })).toBe("-1 wei"); + expect(formatWei(BigInt("-2"), { toUnit: "wei" })).toBe("-2 wei"); + expect(formatWei(BigInt("-123"), { toUnit: "wei" })).toBe("-123 wei"); + expect(formatWei(BigInt("-1234"), { toUnit: "wei" })).toBe("-1,234 wei"); + expect(formatWei(BigInt("-123456"), { toUnit: "wei" })).toBe("-123,456 wei"); + expect(formatWei(BigInt("-1234567"), { toUnit: "wei" })).toBe( + "-1,234,567 wei" + ); + expect(formatWei(BigInt("-12345678"), { toUnit: "wei" })).toBe( + "-12,345,678 wei" + ); + expect(formatWei(BigInt("-123456789"), { toUnit: "wei" })).toBe( + "-123,456,789 wei" + ); + expect(formatWei(BigInt("-1234567890"), { toUnit: "wei" })).toBe( + "-1,234,567,890 wei" + ); }); test("can format gwei", () => { - expect(formatWei(BigInt("1"), "Gwei")).toBe("0.000000001 Gwei"); - expect(formatWei(BigInt("2"), "Gwei")).toBe("0.000000002 Gwei"); - expect(formatWei(BigInt("123"), "Gwei")).toBe("0.000000123 Gwei"); - expect(formatWei(BigInt("1234"), "Gwei")).toBe("0.000001234 Gwei"); - expect(formatWei(BigInt("12345"), "Gwei")).toBe("0.000012345 Gwei"); - expect(formatWei(BigInt("123456"), "Gwei")).toBe("0.000123456 Gwei"); - expect(formatWei(BigInt("1234567"), "Gwei")).toBe("0.001234567 Gwei"); - expect(formatWei(BigInt("12345678"), "Gwei")).toBe("0.012345678 Gwei"); - expect(formatWei(BigInt("123456789"), "Gwei")).toBe("0.123456789 Gwei"); - expect(formatWei(BigInt("1234567890"), "Gwei")).toBe("1.23456789 Gwei"); - expect(formatWei(BigInt("12345678901"), "Gwei")).toBe("12.345678901 Gwei"); - expect(formatWei(BigInt("123456789012"), "Gwei")).toBe("123.456789012 Gwei"); - expect(formatWei(BigInt("1234567890123"), "Gwei")).toBe( + expect(formatWei(BigInt("1"), { toUnit: "Gwei" })).toBe("0.000000001 Gwei"); + expect(formatWei(BigInt("2"), { toUnit: "Gwei" })).toBe("0.000000002 Gwei"); + expect(formatWei(BigInt("123"), { toUnit: "Gwei" })).toBe("0.000000123 Gwei"); + expect(formatWei(BigInt("1234"), { toUnit: "Gwei" })).toBe( + "0.000001234 Gwei" + ); + expect(formatWei(BigInt("12345"), { toUnit: "Gwei" })).toBe( + "0.000012345 Gwei" + ); + expect(formatWei(BigInt("123456"), { toUnit: "Gwei" })).toBe( + "0.000123456 Gwei" + ); + expect(formatWei(BigInt("1234567"), { toUnit: "Gwei" })).toBe( + "0.001234567 Gwei" + ); + expect(formatWei(BigInt("12345678"), { toUnit: "Gwei" })).toBe( + "0.012345678 Gwei" + ); + expect(formatWei(BigInt("123456789"), { toUnit: "Gwei" })).toBe( + "0.123456789 Gwei" + ); + expect(formatWei(BigInt("1234567890"), { toUnit: "Gwei" })).toBe( + "1.23456789 Gwei" + ); + expect(formatWei(BigInt("12345678901"), { toUnit: "Gwei" })).toBe( + "12.345678901 Gwei" + ); + expect(formatWei(BigInt("123456789012"), { toUnit: "Gwei" })).toBe( + "123.456789012 Gwei" + ); + expect(formatWei(BigInt("1234567890123"), { toUnit: "Gwei" })).toBe( "1,234.567890123 Gwei" ); - expect(formatWei(BigInt("12345678901234"), "Gwei")).toBe( + expect(formatWei(BigInt("12345678901234"), { toUnit: "Gwei" })).toBe( "12,345.678901234 Gwei" ); - expect(formatWei(BigInt("123456789012345"), "Gwei")).toBe( + expect(formatWei(BigInt("123456789012345"), { toUnit: "Gwei" })).toBe( "123,456.789012345 Gwei" ); - expect(formatWei(BigInt("1234567890123456"), "Gwei")).toBe( + expect(formatWei(BigInt("1234567890123456"), { toUnit: "Gwei" })).toBe( "1,234,567.890123456 Gwei" ); }); test("can format negative gwei", () => { - expect(formatWei(BigInt("-1"), "Gwei")).toBe("-0.000000001 Gwei"); - expect(formatWei(BigInt("-2"), "Gwei")).toBe("-0.000000002 Gwei"); - expect(formatWei(BigInt("-123"), "Gwei")).toBe("-0.000000123 Gwei"); - expect(formatWei(BigInt("-1234"), "Gwei")).toBe("-0.000001234 Gwei"); - expect(formatWei(BigInt("-12345"), "Gwei")).toBe("-0.000012345 Gwei"); - expect(formatWei(BigInt("-123456"), "Gwei")).toBe("-0.000123456 Gwei"); - expect(formatWei(BigInt("-1234567"), "Gwei")).toBe("-0.001234567 Gwei"); - expect(formatWei(BigInt("-12345678"), "Gwei")).toBe("-0.012345678 Gwei"); - expect(formatWei(BigInt("-123456789"), "Gwei")).toBe("-0.123456789 Gwei"); - expect(formatWei(BigInt("-1234567890"), "Gwei")).toBe("-1.23456789 Gwei"); - expect(formatWei(BigInt("-12345678901"), "Gwei")).toBe("-12.345678901 Gwei"); - expect(formatWei(BigInt("-123456789012"), "Gwei")).toBe( + expect(formatWei(BigInt("-1"), { toUnit: "Gwei" })).toBe("-0.000000001 Gwei"); + expect(formatWei(BigInt("-2"), { toUnit: "Gwei" })).toBe("-0.000000002 Gwei"); + expect(formatWei(BigInt("-123"), { toUnit: "Gwei" })).toBe( + "-0.000000123 Gwei" + ); + expect(formatWei(BigInt("-1234"), { toUnit: "Gwei" })).toBe( + "-0.000001234 Gwei" + ); + expect(formatWei(BigInt("-12345"), { toUnit: "Gwei" })).toBe( + "-0.000012345 Gwei" + ); + expect(formatWei(BigInt("-123456"), { toUnit: "Gwei" })).toBe( + "-0.000123456 Gwei" + ); + expect(formatWei(BigInt("-1234567"), { toUnit: "Gwei" })).toBe( + "-0.001234567 Gwei" + ); + expect(formatWei(BigInt("-12345678"), { toUnit: "Gwei" })).toBe( + "-0.012345678 Gwei" + ); + expect(formatWei(BigInt("-123456789"), { toUnit: "Gwei" })).toBe( + "-0.123456789 Gwei" + ); + expect(formatWei(BigInt("-1234567890"), { toUnit: "Gwei" })).toBe( + "-1.23456789 Gwei" + ); + expect(formatWei(BigInt("-12345678901"), { toUnit: "Gwei" })).toBe( + "-12.345678901 Gwei" + ); + expect(formatWei(BigInt("-123456789012"), { toUnit: "Gwei" })).toBe( "-123.456789012 Gwei" ); - expect(formatWei(BigInt("-1234567890123"), "Gwei")).toBe( + expect(formatWei(BigInt("-1234567890123"), { toUnit: "Gwei" })).toBe( "-1,234.567890123 Gwei" ); - expect(formatWei(BigInt("-12345678901234"), "Gwei")).toBe( + expect(formatWei(BigInt("-12345678901234"), { toUnit: "Gwei" })).toBe( "-12,345.678901234 Gwei" ); - expect(formatWei(BigInt("-123456789012345"), "Gwei")).toBe( + expect(formatWei(BigInt("-123456789012345"), { toUnit: "Gwei" })).toBe( "-123,456.789012345 Gwei" ); - expect(formatWei(BigInt("-1234567890123456"), "Gwei")).toBe( + expect(formatWei(BigInt("-1234567890123456"), { toUnit: "Gwei" })).toBe( "-1,234,567.890123456 Gwei" ); }); test("can format ether", () => { - expect(formatWei(BigInt("1"), "ether")).toBe("0.000000000000000001 ether"); - expect(formatWei(BigInt("2"), "ether")).toBe("0.000000000000000002 ether"); - expect(formatWei(BigInt("123"), "ether")).toBe("0.000000000000000123 ether"); - expect(formatWei(BigInt("1234"), "ether")).toBe("0.000000000000001234 ether"); - expect(formatWei(BigInt("12345"), "ether")).toBe( + expect(formatWei(BigInt("1"), { toUnit: "ether" })).toBe( + "0.000000000000000001 ether" + ); + expect(formatWei(BigInt("2"), { toUnit: "ether" })).toBe( + "0.000000000000000002 ether" + ); + expect(formatWei(BigInt("123"), { toUnit: "ether" })).toBe( + "0.000000000000000123 ether" + ); + expect(formatWei(BigInt("1234"), { toUnit: "ether" })).toBe( + "0.000000000000001234 ether" + ); + expect(formatWei(BigInt("12345"), { toUnit: "ether" })).toBe( "0.000000000000012345 ether" ); - expect(formatWei(BigInt("123456"), "ether")).toBe( + expect(formatWei(BigInt("123456"), { toUnit: "ether" })).toBe( "0.000000000000123456 ether" ); - expect(formatWei(BigInt("1234567"), "ether")).toBe( + expect(formatWei(BigInt("1234567"), { toUnit: "ether" })).toBe( "0.000000000001234567 ether" ); - expect(formatWei(BigInt("12345678"), "ether")).toBe( + expect(formatWei(BigInt("12345678"), { toUnit: "ether" })).toBe( "0.000000000012345678 ether" ); - expect(formatWei(BigInt("123456789"), "ether")).toBe( + expect(formatWei(BigInt("123456789"), { toUnit: "ether" })).toBe( "0.000000000123456789 ether" ); - expect(formatWei(BigInt("1234567890"), "ether")).toBe( + expect(formatWei(BigInt("1234567890"), { toUnit: "ether" })).toBe( "0.00000000123456789 ether" ); - expect(formatWei(BigInt("12345678901"), "ether")).toBe( + expect(formatWei(BigInt("12345678901"), { toUnit: "ether" })).toBe( "0.000000012345678901 ether" ); - expect(formatWei(BigInt("123456789012"), "ether")).toBe( + expect(formatWei(BigInt("123456789012"), { toUnit: "ether" })).toBe( "0.000000123456789012 ether" ); - expect(formatWei(BigInt("1234567890123"), "ether")).toBe( + expect(formatWei(BigInt("1234567890123"), { toUnit: "ether" })).toBe( "0.000001234567890123 ether" ); - expect(formatWei(BigInt("12345678901234"), "ether")).toBe( + expect(formatWei(BigInt("12345678901234"), { toUnit: "ether" })).toBe( "0.000012345678901234 ether" ); - expect(formatWei(BigInt("123456789012345"), "ether")).toBe( + expect(formatWei(BigInt("123456789012345"), { toUnit: "ether" })).toBe( "0.000123456789012345 ether" ); - expect(formatWei(BigInt("1234567890123456"), "ether")).toBe( + expect(formatWei(BigInt("1234567890123456"), { toUnit: "ether" })).toBe( "0.001234567890123456 ether" ); - expect(formatWei(BigInt("12345678901234567"), "ether")).toBe( + expect(formatWei(BigInt("12345678901234567"), { toUnit: "ether" })).toBe( "0.012345678901234567 ether" ); - expect(formatWei(BigInt("123456789012345678"), "ether")).toBe( + expect(formatWei(BigInt("123456789012345678"), { toUnit: "ether" })).toBe( "0.123456789012345678 ether" ); - expect(formatWei(BigInt("1234567890123456789"), "ether")).toBe( + expect(formatWei(BigInt("1234567890123456789"), { toUnit: "ether" })).toBe( "1.234567890123456789 ether" ); - expect(formatWei(BigInt("12345678901234567890"), "ether")).toBe( + expect(formatWei(BigInt("12345678901234567890"), { toUnit: "ether" })).toBe( "12.34567890123456789 ether" ); - expect(formatWei(BigInt("123456789012345678901"), "ether")).toBe( + expect(formatWei(BigInt("123456789012345678901"), { toUnit: "ether" })).toBe( "123.456789012345678901 ether" ); - expect(formatWei(BigInt("1234567890123456789012"), "ether")).toBe( + expect(formatWei(BigInt("1234567890123456789012"), { toUnit: "ether" })).toBe( "1,234.567890123456789012 ether" ); - expect(formatWei(BigInt("12345678901234567890123"), "ether")).toBe( - "12,345.678901234567890123 ether" - ); - expect(formatWei(BigInt("123456789012345678901234"), "ether")).toBe( - "123,456.789012345678901234 ether" - ); - expect(formatWei(BigInt("1234567890123456789012345"), "ether")).toBe( - "1,234,567.890123456789012345 ether" - ); - expect(formatWei(BigInt("12345678901234567890123456"), "ether")).toBe( - "12,345,678.901234567890123456 ether" - ); - expect(formatWei(BigInt("123456789012345678901234567"), "ether")).toBe( - "123,456,789.012345678901234567 ether" - ); - expect(formatWei(BigInt("1234567890123456789012345678"), "ether")).toBe( - "1,234,567,890.123456789012345678 ether" - ); + expect( + formatWei(BigInt("12345678901234567890123"), { toUnit: "ether" }) + ).toBe("12,345.678901234567890123 ether"); + expect( + formatWei(BigInt("123456789012345678901234"), { toUnit: "ether" }) + ).toBe("123,456.789012345678901234 ether"); + expect( + formatWei(BigInt("1234567890123456789012345"), { toUnit: "ether" }) + ).toBe("1,234,567.890123456789012345 ether"); + expect( + formatWei(BigInt("12345678901234567890123456"), { toUnit: "ether" }) + ).toBe("12,345,678.901234567890123456 ether"); + expect( + formatWei(BigInt("123456789012345678901234567"), { toUnit: "ether" }) + ).toBe("123,456,789.012345678901234567 ether"); + expect( + formatWei(BigInt("1234567890123456789012345678"), { toUnit: "ether" }) + ).toBe("1,234,567,890.123456789012345678 ether"); }); test("can format negative ether", () => { - expect(formatWei(BigInt("-1"), "ether")).toBe("-0.000000000000000001 ether"); - expect(formatWei(BigInt("-2"), "ether")).toBe("-0.000000000000000002 ether"); - expect(formatWei(BigInt("-123"), "ether")).toBe( + expect(formatWei(BigInt("-1"), { toUnit: "ether" })).toBe( + "-0.000000000000000001 ether" + ); + expect(formatWei(BigInt("-2"), { toUnit: "ether" })).toBe( + "-0.000000000000000002 ether" + ); + expect(formatWei(BigInt("-123"), { toUnit: "ether" })).toBe( "-0.000000000000000123 ether" ); - expect(formatWei(BigInt("-1234"), "ether")).toBe( + expect(formatWei(BigInt("-1234"), { toUnit: "ether" })).toBe( "-0.000000000000001234 ether" ); - expect(formatWei(BigInt("-12345"), "ether")).toBe( + expect(formatWei(BigInt("-12345"), { toUnit: "ether" })).toBe( "-0.000000000000012345 ether" ); - expect(formatWei(BigInt("-123456"), "ether")).toBe( + expect(formatWei(BigInt("-123456"), { toUnit: "ether" })).toBe( "-0.000000000000123456 ether" ); - expect(formatWei(BigInt("-1234567"), "ether")).toBe( + expect(formatWei(BigInt("-1234567"), { toUnit: "ether" })).toBe( "-0.000000000001234567 ether" ); - expect(formatWei(BigInt("-12345678"), "ether")).toBe( + expect(formatWei(BigInt("-12345678"), { toUnit: "ether" })).toBe( "-0.000000000012345678 ether" ); - expect(formatWei(BigInt("-123456789"), "ether")).toBe( + expect(formatWei(BigInt("-123456789"), { toUnit: "ether" })).toBe( "-0.000000000123456789 ether" ); - expect(formatWei(BigInt("-1234567890"), "ether")).toBe( + expect(formatWei(BigInt("-1234567890"), { toUnit: "ether" })).toBe( "-0.00000000123456789 ether" ); - expect(formatWei(BigInt("-12345678901"), "ether")).toBe( + expect(formatWei(BigInt("-12345678901"), { toUnit: "ether" })).toBe( "-0.000000012345678901 ether" ); - expect(formatWei(BigInt("-123456789012"), "ether")).toBe( + expect(formatWei(BigInt("-123456789012"), { toUnit: "ether" })).toBe( "-0.000000123456789012 ether" ); - expect(formatWei(BigInt("-1234567890123"), "ether")).toBe( + expect(formatWei(BigInt("-1234567890123"), { toUnit: "ether" })).toBe( "-0.000001234567890123 ether" ); - expect(formatWei(BigInt("-12345678901234"), "ether")).toBe( + expect(formatWei(BigInt("-12345678901234"), { toUnit: "ether" })).toBe( "-0.000012345678901234 ether" ); - expect(formatWei(BigInt("-123456789012345"), "ether")).toBe( + expect(formatWei(BigInt("-123456789012345"), { toUnit: "ether" })).toBe( "-0.000123456789012345 ether" ); - expect(formatWei(BigInt("-1234567890123456"), "ether")).toBe( + expect(formatWei(BigInt("-1234567890123456"), { toUnit: "ether" })).toBe( "-0.001234567890123456 ether" ); - expect(formatWei(BigInt("-12345678901234567"), "ether")).toBe( + expect(formatWei(BigInt("-12345678901234567"), { toUnit: "ether" })).toBe( "-0.012345678901234567 ether" ); - expect(formatWei(BigInt("-123456789012345678"), "ether")).toBe( + expect(formatWei(BigInt("-123456789012345678"), { toUnit: "ether" })).toBe( "-0.123456789012345678 ether" ); - expect(formatWei(BigInt("-1234567890123456789"), "ether")).toBe( + expect(formatWei(BigInt("-1234567890123456789"), { toUnit: "ether" })).toBe( "-1.234567890123456789 ether" ); - expect(formatWei(BigInt("-12345678901234567890"), "ether")).toBe( + expect(formatWei(BigInt("-12345678901234567890"), { toUnit: "ether" })).toBe( "-12.34567890123456789 ether" ); - expect(formatWei(BigInt("-123456789012345678901"), "ether")).toBe( + expect(formatWei(BigInt("-123456789012345678901"), { toUnit: "ether" })).toBe( "-123.456789012345678901 ether" ); - expect(formatWei(BigInt("-1234567890123456789012"), "ether")).toBe( - "-1,234.567890123456789012 ether" - ); + expect( + formatWei(BigInt("-1234567890123456789012"), { toUnit: "ether" }) + ).toBe("-1,234.567890123456789012 ether"); +}); + +test("can format wei and hide unit", () => { + expect(formatWei(BigInt("1"), { hideUnit: true })).toBe("1"); }); test("can shift string values", () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0f77e0bdc..2bfbab66d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -334,9 +334,6 @@ importers: posthog-js: specifier: ^1.166.1 version: 1.166.1 - pretty-bytes: - specifier: ^6.1.1 - version: 6.1.1 react: specifier: 18.2.0 version: 18.2.0 @@ -5960,10 +5957,6 @@ packages: engines: {node: '>=10.13.0'} hasBin: true - pretty-bytes@6.1.1: - resolution: {integrity: sha512-mQUvGU6aUFQ+rNvTIAcZuWGRT9a6f6Yrg9bHs4ImKF+HZCEK+plBvnAZYSIQztknZF2qnzNtr6F8s0+IuptdlQ==} - engines: {node: ^14.13.1 || >=16.0.0} - pretty-format@29.7.0: resolution: {integrity: sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -13187,8 +13180,6 @@ snapshots: prettier@2.8.8: {} - pretty-bytes@6.1.1: {} - pretty-format@29.7.0: dependencies: '@jest/schemas': 29.6.3