forked from AwolDes/reach-tailwind-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaucet.js
51 lines (46 loc) · 1.64 KB
/
faucet.js
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
import React, { useState, useContext } from 'react';
import { loadStdlib } from '@reach-sh/stdlib'; // eslint-disable-line import/no-extraneous-dependencies
import { useAsyncError } from './utils/hooks';
import Connector from './connector';
import { AuthContext } from './providers/auth';
const reach = loadStdlib('ALGO');
const Faucet = () => {
const { setAuthData, auth } = useContext(AuthContext);
const throwError = useAsyncError();
const [amount, setAmount] = useState(0);
const [funding, setFunding] = useState(false);
const { standardUnit } = reach;
const fundAccount = async (fundAmount) => {
try {
setFunding(true);
const faucet = await reach.getFaucet();
await reach.transfer(faucet, auth.acc, reach.parseCurrency(fundAmount));
const balAtomic = await reach.balanceOf(auth.acc);
const bal = reach.formatCurrency(balAtomic, 4);
setAuthData({ ...auth, bal });
setAmount(0);
setFunding(false);
} catch (e) {
throwError(e.message);
}
};
return (
<div>
<h1 className="text-2xl">Faucet for Local Testnet</h1>
{auth.acc && (
<div className="p-2">
<h2 className="text-lg">Fund account</h2>
<p>Would you like to fund your account with additional {standardUnit}?</p>
<input
type="number"
placeholder={amount}
onChange={(e) => setAmount(e.currentTarget.value)}
/>
<button className="p-2 mx-2 bg-black text-white" onClick={() => fundAccount(amount)}>Fund Account</button>
{funding && <p>Funding account...</p>}
</div>
)}
</div>
);
};
export default Faucet;