-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
59 lines (53 loc) · 1.58 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { BigNumberish } from "@ethersproject/bignumber";
import { formatUnits } from "@ethersproject/units";
import { LIME_ICON } from "./constants";
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);
export const getCurrentDateTimeWithOffset = (offset = 0) => {
let currentDateTime = new Date();
currentDateTime.setMinutes(currentDateTime.getMinutes() - offset);
let formattedDateTime = currentDateTime.toISOString().slice(0, 16);
return formattedDateTime;
};
export const showNotification = (header: string, body: string) => {
Notification.requestPermission().then((result) => {
if (result === "granted") {
navigator.serviceWorker.ready.then((registration) => {
registration.showNotification(header, {
body,
icon: LIME_ICON,
});
});
}
});
};