-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo-5.tsx
82 lines (68 loc) · 2.45 KB
/
demo-5.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
import { useEffect } from 'react'
import type { NextPage } from 'next'
import { useAccount, useConnect, useDisconnect, useWaitForTransaction } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'
import Nav from '../components/Nav'
import Heading from '../components/Heading'
import DaiBox from '../components/DaiBox'
import CdaiBox from '../components/CdaiBox'
import Footer from '../components/Footer'
import AuthorizeCompoundOverlay from '../components/AuthorizeCompoundOverlay'
import ConnectOverlay from '../components/ConnectOverlay'
import useCdaiAllowance from '../hooks/useCdaiAllowance'
import useCdaiApprove from '../hooks/useCdaiApprove'
import useDaiBalance from '../hooks/useDaiBalance'
import useCdaiUnderlyingBalance from '../hooks/useCdaiUnderlyingBalance'
import styles from '../styles/Home.module.css'
/**
* Paso 5: obtenemos info de la tx para así informar al usuario del
* progreso y recargar las queries relacionadas
*/
const Home: NextPage = () => {
// Connection
const { connect } = useConnect({
connector: new InjectedConnector(),
})
const { disconnect } = useDisconnect()
const { address, isConnected } = useAccount()
// Read
const daiBalance = useDaiBalance(address)
const cdaiBalance = useCdaiUnderlyingBalance(address)
const cdaiAllowance = useCdaiAllowance(address)
// Write
const authorize = useCdaiApprove()
// Tx info
const authTx = useWaitForTransaction({ hash: authorize.data?.hash })
// Effects
useEffect(() => {
if (authTx.isSuccess) {
cdaiAllowance.refetch()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [authTx.isSuccess])
return (
<div>
<Nav address={address} onConnectPress={connect} onDisconnectPress={disconnect} />
<main className={styles.main}>
<Heading />
<div className={styles.content}>
<DaiBox balance={daiBalance.formatted} isLoading={daiBalance.isLoading} />
< >
<CdaiBox balance={cdaiBalance.formatted} isLoading={cdaiBalance.isLoading} />
{isConnected && !cdaiAllowance.isAuthorized && (
<AuthorizeCompoundOverlay
isLoading={cdaiAllowance.isLoading || authTx.isLoading}
onAuthorizePress={authorize.write}
/>
)}
{!isConnected && <ConnectOverlay onConnectPress={connect} />}
</div>
</main>
<Footer />
</div>
)
}
/**
* Exports
*/
export default Home