-
Notifications
You must be signed in to change notification settings - Fork 0
/
faucet.html
127 lines (106 loc) · 3.11 KB
/
faucet.html
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!doctype html>
<h1>🚰 $OMID Faucet</h1>
<p>Get 5 <a href="https://amoy.polygonscan.com/token/0x21cfe003997fb7c2b3cfe5cf71e7833b7b2ece10">$OMID</a> every 24 hours
<p id="user-info">
<p id="drip-info">
<p><button id="drip">Drip</button>
<p id="tx-info"></p>
<script type="module">
import { createWalletClient, createPublicClient, http, custom, formatEther } from 'https://esm.sh/viem';
import { polygonAmoy } from 'https://esm.sh/viem/chains';
export const publicClient = createPublicClient({
chain: polygonAmoy,
transport: http()
})
const tokenAddr = "0x21cFE003997fB7c2B3cfe5cf71e7833B7B2eCe10";
const faucetAddr = "0x1A8fb91b8204596605b312c23E62BD61Fc7c9Eb5";
export const walletClient = createWalletClient({
chain: polygonAmoy,
transport: custom(window.ethereum),
});
await walletClient.switchChain({ id: polygonAmoy.id })
const [account] = await walletClient.requestAddresses()
console.log("Wallet", account);
const tokenAbi = [{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}]
const faucetAbi = [{
"type": "function",
"name": "drip",
"inputs": [],
"outputs": [],
"stateMutability": "nonpayable"
},
{
"type": "function",
"name": "lastDrip",
"inputs": [
{
"name": "to",
"type": "address",
"internalType": "address"
}
],
"outputs": [
{
"name": "",
"type": "uint256",
"internalType": "uint256"
}
],
"stateMutability": "view"
}
];
const lastDrip = await publicClient.readContract({
address: faucetAddr,
abi: faucetAbi,
functionName: 'lastDrip',
args: [account],
});
const balance = await publicClient.readContract({
address: tokenAddr,
abi: tokenAbi,
functionName: 'balanceOf',
args: [account],
});
const now = BigInt(Math.floor(Date.now() / 1000));
document.getElementById("user-info").innerText = `You are ${account}, you have ${formatEther(balance)} $OMID`;
const oneDay = BigInt(86400);
if (now - lastDrip < oneDay) {
document.getElementById("drip-info").innerText = `Wait ${lastDrip + oneDay - now} seconds`;
} else {
document.getElementById("drip-info").innerText = `Drip away`;
}
async function callback() {
if (now - lastDrip < 86400) {
alert("Too soon");
return;
}
const { request } = await publicClient.simulateContract({
address: faucetAddr,
abi: faucetAbi,
functionName: 'drip',
args: [],
account
})
const txHash = await walletClient.writeContract(request);
document.getElementById("tx-info").innerHTML = `Dripped: <a href="https://amoy.polygonscan.com/tx/${txHash}">transaction <code>${txHash}</code></a>`;
}
document.getElementById("drip").onclick=callback;
</script>