Skip to content

Commit

Permalink
chore: remove iota.js and do the conversion manually
Browse files Browse the repository at this point in the history
  • Loading branch information
VmMad committed May 13, 2024
1 parent ad2da37 commit cbaa804
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import StatDisplay from "../../StatDisplay";
import { clamp } from "~/helpers/clamp";
import { Link } from "react-router-dom";
import { getTimeRemaining } from "~/helpers/nova/novaTimeUtils";
import { formatAmountWithMetricUnit } from "~/helpers/nova/formatAmountWithMetricUnit";
import { formatRawAmountWithMetricUnit } from "~/helpers/nova/formatRawAmountWithMetricUnit";
import "./LandingEpochSection.scss";

const EPOCH_DATE_FORMAT = "DD MMM YYYY HH:mm:ss";
Expand Down Expand Up @@ -50,11 +50,11 @@ const LandingEpochSection: React.FC = () => {
subtitle: "Validators",
},
{
title: `${totalCommitteeStake !== undefined ? formatAmountWithMetricUnit(totalCommitteeStake, tokenInfo) : "--"}`,
title: `${totalCommitteeStake !== undefined ? formatRawAmountWithMetricUnit(totalCommitteeStake, tokenInfo) : "--"}`,
subtitle: "Staked in committee",
},
{
title: `${commiiteeDelegatorStake !== undefined ? formatAmountWithMetricUnit(commiiteeDelegatorStake, tokenInfo) : "--"}`,
title: `${commiiteeDelegatorStake !== undefined ? formatRawAmountWithMetricUnit(commiiteeDelegatorStake, tokenInfo) : "--"}`,
subtitle: "Delegated in committee",
},
{
Expand Down
6 changes: 3 additions & 3 deletions client/src/app/routes/nova/landing/Landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useNetworkStats } from "~/helpers/nova/hooks/useNetworkStats";
import Hero from "~/app/components/Hero";
import { IStatDisplay } from "~/app/lib/interfaces";
import { StatDisplaySize } from "~/app/lib/enums";
import { formatAmountWithMetricUnit } from "~/helpers/nova/formatAmountWithMetricUnit";
import { formatRawAmountWithMetricUnit } from "~/helpers/nova/formatRawAmountWithMetricUnit";
import "./Landing.scss";

const Landing: React.FC = () => {
Expand Down Expand Up @@ -52,12 +52,12 @@ const Landing: React.FC = () => {
size: StatDisplaySize.Small,
},
{
title: `${totalValidatorsStake !== undefined ? formatAmountWithMetricUnit(totalValidatorsStake, tokenInfo) : "--"}`,
title: `${totalValidatorsStake !== undefined ? formatRawAmountWithMetricUnit(totalValidatorsStake, tokenInfo) : "--"}`,
subtitle: "Total Staked",
size: StatDisplaySize.Small,
},
{
title: `${totalDelegatedStake !== null ? formatAmountWithMetricUnit(totalDelegatedStake, tokenInfo) : "--"}`,
title: `${totalDelegatedStake !== null ? formatRawAmountWithMetricUnit(totalDelegatedStake, tokenInfo) : "--"}`,
subtitle: "Total Delegated",
size: StatDisplaySize.Small,
},
Expand Down
27 changes: 0 additions & 27 deletions client/src/helpers/nova/formatAmountWithMetricUnit.ts

This file was deleted.

52 changes: 52 additions & 0 deletions client/src/helpers/nova/formatRawAmountWithMetricUnit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { INodeInfoBaseToken } from "@iota/sdk-wasm-stardust/web";
import { formatAmount } from "~/helpers/stardust/valueFormatHelper";

const UNIT_MAP: { threshold: number; unit: string }[] = [
{ unit: "", threshold: 1 },
{ unit: "K", threshold: 1_000 },
{ unit: "M", threshold: 1_000_000 },
{ unit: "G", threshold: 1_000_000_000 },
{ unit: "T", threshold: 1_000_000_000_000 },
{ unit: "P", threshold: 1_000_000_000_000_000 },
];

const sortedUnits = UNIT_MAP.sort((a, b) => b.threshold - a.threshold);

/**
* Format the best amount with metric unit.
* @param value The value to format.
* @returns The formatted string with the metric unit.
*/
function formatBestAmountWithMetric(value: number): string {
for (const { threshold, unit } of sortedUnits) {
if (value >= threshold) {
return `${Math.floor(value / threshold)}${unit}`;
}
}

return `${Math.floor(value)}`;
}

/**
* Format token amount to metric unit.
* @param value The raw amount to format.
* @param tokenInfo The token info configuration to use.
* @returns The formatted string with the metric unit.
*/
export function formatRawAmountWithMetricUnit(value: number | bigint | string, tokenInfo: INodeInfoBaseToken): string {
if (Number.isNaN(value)) {
return "";
}

const tokenAmount = parseInt(formatAmount(value, tokenInfo, false, 0).replace(tokenInfo.unit, ""));
const formattedValue = formatBestAmountWithMetric(tokenAmount);

// Remove iota unit if it exists
const lastIotaIndex = formattedValue.lastIndexOf("i");
if (lastIotaIndex === -1) return `${formattedValue} ${tokenInfo.unit}`;

const formattedAmountWithToken =
formattedValue.substring(0, lastIotaIndex) + formattedValue.substring(lastIotaIndex + 1) + ` ${tokenInfo.unit}`;

return formattedAmountWithToken;
}

0 comments on commit cbaa804

Please sign in to comment.