-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
45 lines (41 loc) · 1.08 KB
/
util.js
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
import { formatUnits } from "@ethersproject/units";
export function shortenHex(hex, 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.",
};
/**
*
* @param {("Account"|"Transaction")} type
* @param {[number, string]} data
*/
export function formatEtherscanLink(type, data) {
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}`;
}
}
}
/**
* @name parseBalance
*
* @param {import("@ethersproject/bignumber").BigNumberish} balance
* @param {number} decimals
* @param {number} decimalsToDisplay
*
* @returns {string}
*/
export const parseBalance = (balance, decimals = 18, decimalsToDisplay = 3) =>
Number(formatUnits(balance, decimals)).toFixed(decimalsToDisplay);