2020-04-29 05:48:38 -07:00
|
|
|
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
|
2020-08-01 07:05:58 -07:00
|
|
|
import {
|
|
|
|
HumanizeDuration,
|
|
|
|
HumanizeDurationLanguage,
|
|
|
|
} from "humanize-duration-ts";
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-09 02:49:47 -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
|
|
|
|
): string {
|
|
|
|
const sol = Math.abs(lamports) / LAMPORTS_PER_SOL;
|
|
|
|
return (
|
|
|
|
"◎" + new Intl.NumberFormat("en-US", { maximumFractionDigits }).format(sol)
|
|
|
|
);
|
2020-04-29 05:48:38 -07:00
|
|
|
}
|
2020-08-01 07:05:58 -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);
|
|
|
|
}
|