This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBalances.js
97 lines (83 loc) · 2.84 KB
/
Balances.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
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
import { useState, useEffect } from 'react';
import useMaker from '../hooks/useMaker';
import { Text, Grid, Box, Checkbox, Button } from 'theme-ui';
const MatchingMarket = '0xe325acB9765b02b8b418199bf9650972299235F4';
const Balances = () => {
const { maker, fetchTokenBalance, web3Connected } = useMaker();
const [ethBalance, setEthBalance] = useState(null);
const [wethBalance, setWethBalance] = useState(null);
const [daiBalance, setDaiBalance] = useState(null);
const [wethAuth, setWethAuth] = useState(false);
const [daiAuth, setDaiAuth] = useState(false);
const approveUnlimited = (token) => {
maker.service('token').getToken(token).approveUnlimited(MatchingMarket);
};
useEffect(() => {
const fetchBalances = async () => {
const ethBal = await fetchTokenBalance('ETH');
const wethBal = await fetchTokenBalance('WETH');
const daiBal = await fetchTokenBalance('DAI');
setEthBalance(ethBal.toString());
setDaiBalance(daiBal.toString());
setWethBalance(wethBal.toString());
};
const checkAuth = () => {
maker.service('token').getToken('WETH').allowance(maker.currentAddress(),MatchingMarket).then(allow=>setWethAuth(allow.toNumber()>10));
maker.service('token').getToken('DAI').allowance(maker.currentAddress(),MatchingMarket).then(allow=>setDaiAuth(allow.toNumber()>10));
};
if (web3Connected) {
fetchBalances();
checkAuth();
}
const interval = setInterval(() => {
if (web3Connected) {
fetchBalances();
checkAuth();
}
}, 4000);
return () => clearInterval(interval);
},[maker,web3Connected,fetchTokenBalance]);
return (
<div>
<Grid
columns={4}
sx={{
borderBottom: '1px solid',
borderTop: '1px solid',
borderColor: 'muted',
px: 2,
py: 1
}}
>
{['Token','Balance', 'Authorization', 'Approve Unlimited'].map((h, key) => (
<Text sx={{ fontWeight: 'bold' }} key={key}>
{h}
</Text>
))}
</Grid>
<Box>
<Grid columns={4} key='eth'>
<Text>ETH</Text>
<Text>{ethBalance}</Text>
</Grid>
<Grid columns={4} key='weth'>
<Text>WETH</Text>
<Text>{wethBalance}</Text>
<Checkbox checked={wethAuth}/>
<Button sx={{ width: 6 , mr: 0, lineHeight:1, p:0, mb: 1 }} onClick={() => approveUnlimited('WETH')}>
Approve
</Button>
</Grid>
<Grid columns={4} key='dai'>
<Text>DAI</Text>
<Text>{daiBalance}</Text>
<Checkbox checked={daiAuth} />
<Button sx={{ width: 6 , mr: 0, lineHeight: 1, p:0, mb: 1}} onClick={() => approveUnlimited('DAI')}>
Approve
</Button>
</Grid>
</Box>
</div>
);
};
export default Balances;