-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.ts
38 lines (34 loc) · 965 Bytes
/
util.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
import type { BigNumberish } from "@ethersproject/bignumber";
import { formatUnits } from "@ethersproject/units";
export function shortenHex(hex: string, length = 4) {
return `${hex.substring(0, length + 2)}…${hex.substring(
hex.length - length
)}`;
}
const ETHERSCAN_PREFIXES = {
1: "",
3: "ropsten.",
4: "rinkeby.",
5: "goerli.",
42: "kovan.",
};
export function formatEtherscanLink(
type: "Account" | "Transaction",
data: [number, string]
) {
switch (type) {
case "Account": {
const [chainId, address] = data;
return `https://${ETHERSCAN_PREFIXES[chainId]}etherscan.io/address/${address}`;
}
case "Transaction": {
const [chainId, hash] = data;
return `https://${ETHERSCAN_PREFIXES[chainId]}etherscan.io/tx/${hash}`;
}
}
}
export const parseBalance = (
value: BigNumberish,
decimals = 18,
decimalsToDisplay = 3
) => parseFloat(formatUnits(value, decimals)).toFixed(decimalsToDisplay);