-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
74 lines (57 loc) · 2.16 KB
/
utils.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
import {
FormSubmission,
FormIdPreImage,
ArweaveTx,
ArweveGraphQLResult
} from "./types";
const { NEXT_PUBLIC_CHAIN_ID } = process.env;
import { sha256 } from "ethers/lib/utils";
import axios from "axios";
export const notEmpty = <T>(value: T): boolean =>
value === null || value === undefined ? false : true;
export const formSubmissionSchemeValid = (
formSubmission: FormSubmission
): boolean =>
notEmpty(formSubmission.formId) &&
notEmpty(formSubmission.unixTime) &&
typeof formSubmission.answers !== "string";
export const getTxArweaveExplorerUrl = (txId: string) =>
`https://viewblock.io/arweave/tx/${txId}`;
export const getShortenId = (id: string) =>
`${id.slice(0, 3)}...${id.slice(6, 9)}`;
export const getCurrentUnixTime = () => Math.floor(new Date().getTime() / 1000);
export const getArweaveTxTagValue = (tx, tagName): string =>
tx.tags.find(({ name }) => tagName === name).value;
export const getArweaveTxUnixTime = (tx): number =>
parseInt(getArweaveTxTagValue(tx, "Unix-Time"));
export const getNetworkNameFromChainId = (chainId: number): string => {
switch (chainId) {
case 5:
return "goerli";
default:
return "";
}
};
export const getEtherscanUrl = (id: string) =>
NEXT_PUBLIC_CHAIN_ID &&
`https://${getNetworkNameFromChainId(
parseInt(NEXT_PUBLIC_CHAIN_ID)
)}.etherscan.io/search?q=${id}`;
export const getFormIdFromForm = (form: FormIdPreImage): string =>
sha256(new TextEncoder().encode(JSON.stringify(form)));
export const sleep = ms => {
return new Promise(resolve => setTimeout(resolve, ms));
};
export const getFormUrl = (formId: string) =>
`${window.location.origin}/forms/${formId}`;
export const getFormResponsesUrl = (formId: string) =>
`${window.location.origin}/forms/${formId}/submissions`;
export const getArweaveTxData = async (txId: string) => {
const result = await axios.get(`https://arweave.net/${txId}`);
const data = result.data;
return data;
};
export const getNodesFromArweaveGraphQLResult = (
result: ArweveGraphQLResult
): ArweaveTx[] => result.data.transactions.edges.map(({ node }) => node).flat();
export const removeDuplicates = <T>(array: T[]) => [...new Set(array)];