forked from LIT-Protocol/js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx-handler.ts
39 lines (35 loc) · 939 Bytes
/
tx-handler.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
const ENDPOINT = 'https://lit-general-worker.getlit.dev/';
// check if endpoint is up
export const checkEndpoint = async () => {
try {
const res = await fetch(ENDPOINT + 'status');
return res.status === 200;
} catch (e) {
return false;
}
};
// create a api call to endpoints at localhost:3031
const handleTx = async (
action: 'process' | 'resolve' | 'wait-until-empty',
data: '' | any = ''
) => {
await fetch(ENDPOINT + action, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data,
}),
});
};
export const processTx = async (description: string, callback: any) => {
if (!(await checkEndpoint())) {
throw new Error('Endpoint is not running');
}
handleTx('process', description);
await handleTx('wait-until-empty', description);
const result = await callback;
await handleTx('resolve', description);
return result;
};