-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFaucet.tsx
223 lines (200 loc) · 7.37 KB
/
Faucet.tsx
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import {
Flex,
Container,
Stack,
HStack,
Heading,
Box,
Button,
Text,
Stat,
StatNumber,
StatLabel,
Link,
ListItem,
UnorderedList,
} from '@chakra-ui/react';
import { useEffect, useState } from 'react';
import NextImage from 'next/image';
import { useMetamask } from 'use-metamask';
import RoosterOriginal from './images/1f413-original.png';
import { fromWei } from '../util/conversions';
import importTokenToWallet from '../util/importTokenToWallet';
import { web3, claimTokensFromFaucet, getTokenBalance, getFaucetClaimEventsCount } from '../util/web3api';
function Web3Wrapper() {
return web3;
}
const Faucet = () => {
const { connect, metaState } = useMetamask();
const connectedAccount = metaState?.account[0];
const [connectedAccountBalance, setConnectedAccountBalance] = useState<string>();
useEffect(() => {
if (connectedAccount) {
getTokenBalance(connectedAccount).then((balance) => setConnectedAccountBalance(balance));
} else {
setConnectedAccountBalance(undefined);
}
}, [connectedAccount]);
const [faucetBalance, setFaucetBalance] = useState<string>();
useEffect(() => {
const faucetAddress = process.env.NEXT_PUBLIC_KIKIRICOIN_FAUCET_ADDRESS;
if (faucetAddress) {
getTokenBalance(faucetAddress).then((balance) => setFaucetBalance(balance));
}
}, []);
const [faucetClaimCount, setFaucetClaimCount] = useState<number>();
useEffect(() => {
getFaucetClaimEventsCount().then((count) => {
setFaucetClaimCount(count);
});
}, []);
const handleConnect = () => {
connect(Web3Wrapper).catch((error) => console.error(error));
};
const handleClaim = () => {
claimTokensFromFaucet(connectedAccount).then(() => {
getFaucetClaimEventsCount().then((count) => {
setFaucetClaimCount(count);
});
// @todo: refresh faucet balance and connected account balance
});
};
return (
<Box as="section" bg="gray.100" py={24}>
<Container maxW="container.md" px={8}>
<Flex
align="center"
justify={{ base: 'center', md: 'space-around', xl: 'space-between' }}
direction={{ base: 'column', md: 'row' }}
pb={16}
>
<Stack w={{ base: '40%', md: '30%' }} mb={{ base: 12, md: 0 }}>
<NextImage src={RoosterOriginal} alt="Logo" />
</Stack>
<Stack w={{ base: '80%', md: '70%' }} ml={[0, 0, 8]}>
<Heading as="h2" size="xl" fontWeight="bold" color="primary.800" mb={4}>
Faucet
</Heading>
<Text>
Free dispenser of token here. Connect your wallet, and click on the rooster for it to give you 1 KIKI.
Note you can't dispense more than 10 times per day.
</Text>
<HStack pt={4}>
<Button
borderRadius="8px"
py="4"
px="4"
lineHeight="1"
size="md"
variant={'solid'}
colorScheme="blackAlpha"
onClick={() => {
importTokenToWallet().catch((error: Error) => console.error(error));
}}
disabled={!metaState.isAvailable}
>
1. Import KIKI token to MetaMask
</Button>
</HStack>
<HStack pt={4}>
<Button
borderRadius="8px"
py="4"
px="4"
lineHeight="1"
size="md"
variant={'solid'}
colorScheme="brand"
onClick={handleConnect}
disabled={metaState.isConnected}
>
2. Connect Wallet
</Button>
{metaState.isConnected && metaState.account?.[0] && (
<Text as="i">
Already connected to{' '}
<Link href={`https://polygonscan.com/address/${metaState.account[0]}`} color="brand" isExternal>
{metaState.account[0].substring(0, 4)}...
{metaState.account[0].substring(metaState.account[0].length - 4)}
</Link>
</Text>
)}
</HStack>
<HStack pt={4}>
<Button
borderRadius="8px"
py="4"
px="4"
lineHeight="1"
size="md"
variant={'solid'}
colorScheme="brand"
onClick={handleClaim}
disabled={!metaState.isConnected}
>
3. Claim KIKI
</Button>
</HStack>
</Stack>
</Flex>
<Stack
align="center"
justify={{ base: 'center', md: 'space-around', xl: 'space-between' }}
direction={{ base: 'column', md: 'row' }}
alignItems={{ base: 'stretch' }}
spacing={8}
mb={16}
>
<Stat shadow="md" borderWidth="1px" borderRadius="md" px={6} py={10} bg="white">
<StatNumber fontSize="4xl">{faucetClaimCount || '-'}</StatNumber>
<StatLabel>Total Times Used</StatLabel>
</Stat>
<Stat shadow="md" borderWidth="1px" borderRadius="md" px={6} py={10} bg="white">
<StatNumber fontSize="4xl">{faucetBalance !== undefined ? fromWei(faucetBalance) : '-'}</StatNumber>
<StatLabel>Unclaimed KIKI Tokens</StatLabel>
</Stat>
<Stat shadow="md" borderWidth="1px" borderRadius="md" px={6} py={10} bg="white">
<StatNumber fontSize="4xl">
{connectedAccountBalance !== undefined ? fromWei(connectedAccountBalance) : '-'}
</StatNumber>
<StatLabel>
KIKI in Your Wallet{' '}
{metaState.account[0] && (
<Link href={`https://polygonscan.com/address/${metaState.account[0]}`} color="brand" isExternal>
{metaState.account[0].substring(0, 4)}...
{metaState.account[0].substring(metaState.account[0].length - 4)}
</Link>
)}
</StatLabel>
</Stat>
</Stack>
<Heading as="h3" size="md" mb={4}>
Detailed Instructions
</Heading>
<UnorderedList pl={6}>
<ListItem fontSize="sm" mb={2}>
If you do not have a crypto wallet, download and install MetaMask.
</ListItem>
<ListItem fontSize="sm" mb={2}>
When you set up your crypto wallet, be sure to keep your private key safe. The private key is a code that
allows you to access your wallet from anywhere. If anybody else, like a hacker, discovers your private key,
they'd have full control to take your tokens.
</ListItem>
<ListItem fontSize="sm" mb={2}>
Once you have MetaMask installed and configured with an account, import KIKI token to it.
</ListItem>
<ListItem fontSize="sm" mb={2}>
Then click Connect to enable this website to send requests to MetaMask.
</ListItem>
<ListItem fontSize="sm" mb={2}>
TODO
</ListItem>
<ListItem fontSize="sm" mb={2}>
How can I trust your smart contracts? You can inspect their verified source code in Polygonscan.
</ListItem>
</UnorderedList>
</Container>
</Box>
);
};
export default Faucet;