solana/explorer/src/utils/index.tsx

58 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-08-01 08:31:39 -07:00
import React from "react";
2020-04-29 05:48:38 -07:00
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import {
HumanizeDuration,
HumanizeDurationLanguage,
} from "humanize-duration-ts";
2020-08-01 08:31:39 -07:00
import { ReactNode } from "react";
export const NUM_TICKS_PER_SECOND = 160;
export const DEFAULT_TICKS_PER_SLOT = 64;
export const NUM_SLOTS_PER_SECOND =
NUM_TICKS_PER_SECOND / DEFAULT_TICKS_PER_SLOT;
export const MS_PER_SLOT = 1000 / NUM_SLOTS_PER_SECOND;
2020-04-29 05:48:38 -07:00
export function assertUnreachable(x: never): never {
throw new Error("Unreachable!");
}
2020-04-29 05:48:38 -07:00
2020-05-22 12:09:28 -07:00
export function lamportsToSolString(
lamports: number,
maximumFractionDigits: number = 9
2020-08-01 08:31:39 -07:00
): ReactNode {
2020-05-22 12:09:28 -07:00
const sol = Math.abs(lamports) / LAMPORTS_PER_SOL;
return (
2020-08-01 08:31:39 -07:00
<>
<span className="text-monospace">
{new Intl.NumberFormat("en-US", { maximumFractionDigits }).format(sol)}
</span>
</>
2020-05-22 12:09:28 -07:00
);
2020-04-29 05:48:38 -07:00
}
const HUMANIZER = new HumanizeDuration(new HumanizeDurationLanguage());
HUMANIZER.setOptions({
language: "short",
spacer: "",
delimiter: " ",
round: true,
units: ["d", "h", "m", "s"],
largest: 3,
});
HUMANIZER.addLanguage("short", {
y: () => "y",
mo: () => "mo",
w: () => "w",
d: () => "d",
h: () => "h",
m: () => "m",
s: () => "s",
ms: () => "ms",
decimal: ".",
});
export function slotsToHumanString(slots: number): string {
return HUMANIZER.humanize(slots * MS_PER_SLOT);
}