-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.mjs
41 lines (36 loc) · 1.15 KB
/
utils.mjs
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
import { parseEther } from 'viem';
import abi from './abi.json' assert { type: "json" };
export async function callRPC(url, method, params) {
const resp = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: '2',
method,
params,
}),
});
const respBody = await resp.json();
if (respBody.error) {
throw new Error(respBody.error.message);
}
return respBody.result;
}
export async function callClaimContract(walletClient, contractAddress, functionName, args) {
const { request } = await walletClient.simulateContract({
address: contractAddress,
abi,
functionName,
value: parseEther('0'),
args,
});
const hash = await walletClient.writeContract(request);
console.log(`Sent transaction: ${hash}. Waiting for confirmation...`);
const receipt = await walletClient.waitForTransactionReceipt(
{ hash }
);
console.log(receipt.status === 'success' ? 'Transaction confirmed' : 'Transaction failed');
}