forked from AwolDes/reach-tailwind-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.js
65 lines (58 loc) · 2 KB
/
connector.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 { AuthContext } from './providers/auth';
const reach = loadStdlib('ALGO');
const { standardUnit } = reach;
const Connector = () => {
const { setAuthData, auth } = useContext(AuthContext);
const [funding, setFunding] = useState(false);
const throwError = useAsyncError();
const connectWallet = async () => {
try {
const acc = await reach.getDefaultAccount();
const balAtomic = await reach.balanceOf(acc);
const bal = reach.formatCurrency(balAtomic, 4);
setAuthData({ acc, bal });
// setBal(bal);
} catch (e) {
throwError(e.message);
}
};
const disconnectWallet = () => {
setAuthData(null);
};
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 });
setFunding(false);
} catch (e) {
throwError(e.message);
}
};
return (
<div className="p-2">
{auth.acc ? (
<div>
<button className="p-2 bg-black text-white my-4" onClick={() => disconnectWallet()}>
Disconnect Wallet
</button>
{!funding ? <button className="p-2 bg-black text-white my-4 ml-4" onClick={() => fundAccount(10)}>Fund Wallet</button> : (
<button className="p-2 bg-black text-white my-4 ml-4">Funding 10 {standardUnit}...</button>
)}
<p>Wallet Balance: {auth.bal} {standardUnit}</p>
</div>
) : (
<button className="p-2 bg-black text-white my-4" onClick={() => connectWallet()}>
Connect Wallet
</button>
)}
</div>
);
};
export default Connector;