solana/explorer/src/utils/index.tsx
2020-08-02 20:06:51 +08:00

58 lines
1.3 KiB
TypeScript

import React from "react";
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import {
HumanizeDuration,
HumanizeDurationLanguage,
} from "humanize-duration-ts";
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;
export function assertUnreachable(x: never): never {
throw new Error("Unreachable!");
}
export function lamportsToSolString(
lamports: number,
maximumFractionDigits: number = 9
): ReactNode {
const sol = Math.abs(lamports) / LAMPORTS_PER_SOL;
return (
<>
<span className="text-monospace">
{new Intl.NumberFormat("en-US", { maximumFractionDigits }).format(sol)}
</span>
</>
);
}
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);
}