-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatters.ts
111 lines (84 loc) · 2.83 KB
/
formatters.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import type { Coin } from "@cosmjs/stargate";
import BigNumber from "bignumber.js";
import { minDisplayedXion, minDisplayedXionDecs, xionToUSD } from "@/constants";
import { getEmptyXionCoin, normaliseCoin } from "./core/coins";
export const formatCoin = (
coin: Coin,
compact?: boolean,
noDenom?: boolean,
) => {
const resolved = normaliseCoin(coin);
const amount = new BigNumber(resolved.amount);
const denomSuffix = noDenom ? "" : ` ${resolved.denom}`;
if (amount.eq(0)) {
return `${amount.toFormat()}${denomSuffix}`;
}
if (amount.lt(minDisplayedXion)) {
return `<${minDisplayedXion}${denomSuffix}`;
}
if (compact) {
const formatter = Intl.NumberFormat("en", {
notation: "compact",
});
return `${formatter.format(amount.toNumber())}${denomSuffix}`;
}
return `${amount.toFormat(Math.min(minDisplayedXionDecs, amount.decimalPlaces() || Infinity))}${denomSuffix}`;
};
export const formatVotingPowerPerc = (perc: null | number) => {
if (typeof perc !== "number" || Number.isNaN(perc)) {
return null;
}
const percNum = perc < 0.0001 ? "<0.1" : (perc * 100).toFixed(1);
return `${percNum}%`;
};
export const formatToSmallDisplay = (
num: BigNumber,
minNumber?: number,
maxDecimals?: number,
) => {
if (minNumber && num.lt(minNumber)) {
return `<${minNumber}`;
}
if (maxDecimals) {
return num.toFormat(Math.min(maxDecimals, num.decimalPlaces() || Infinity));
}
return Intl.NumberFormat("en", { notation: "compact" }).format(
num.toNumber(),
);
};
export const formatCommission = (commissionRate: string, decimals: number) => {
const comission = new BigNumber(commissionRate)
.div(new BigNumber(10).pow(18))
.toNumber();
return `${(comission * 100).toFixed(decimals)}%`;
};
export const formatXionToUSD = (coin: Coin | null, compact?: boolean) => {
const normalised = normaliseCoin(coin || getEmptyXionCoin());
const value = coin ? new BigNumber(normalised.amount) : new BigNumber(0);
const usd = value.times(xionToUSD);
if (usd.eq(0)) {
return "$0";
}
if (!compact && usd.lt(0.01)) {
return "<$0.01";
}
return `$${compact ? formatToSmallDisplay(usd) : usd.toFormat(2)}`;
};
export const formatUnbondingCompletionTime = (completionTime: number) => {
const completionTimestamp = completionTime * 1000;
const remainingDays = Math.floor(
(completionTimestamp - Date.now()) / (1000 * 60 * 60 * 24),
);
const month = new Date(completionTimestamp).toLocaleString("en-US", {
month: "short",
});
const day = new Date(completionTimestamp).getDate();
const year = new Date(completionTimestamp).getFullYear();
return `in ${remainingDays} day${remainingDays === 1 ? "" : "s"}, ${month} ${day} ${year}`;
};
export const formatAPR = (apr: BigNumber | null) => {
if (!apr) {
return "-";
}
return `${apr.times(100).toFixed(2)}%`;
};