-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathWeb3ModalButton.tsx
94 lines (85 loc) · 2.44 KB
/
Web3ModalButton.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
import React, { useEffect, useState } from 'react'
import { useEthers, shortenAddress, useLookupAddress } from '@usedapp/core'
import { Button } from '../base/Button'
import { Colors } from '../../global/styles'
import styled from 'styled-components'
import Web3Modal from 'web3modal'
import { AccountModal } from './AccountModal'
import WalletConnectProvider from '@walletconnect/web3-provider'
export const Web3ModalButton = () => {
const { account, activate, deactivate } = useEthers()
const { ens } = useLookupAddress(account)
const [showModal, setShowModal] = useState(false)
const [activateError, setActivateError] = useState('')
const { error } = useEthers()
useEffect(() => {
if (error && account) {
setActivateError(error.message)
return
}
setActivateError('')
}, [error, account])
const activateProvider = async () => {
const providerOptions = {
injected: {
display: {
name: 'Metamask',
description: 'Connect with the provider in your Browser',
},
package: null,
},
walletconnect: {
package: WalletConnectProvider,
options: {
bridge: 'https://bridge.walletconnect.org',
infuraId: 'd8df2cb7844e4a54ab0a782f608749dd',
},
},
}
const web3Modal = new Web3Modal({
providerOptions,
})
try {
const provider = await web3Modal.connect()
await activate(provider)
setActivateError('')
} catch (error: any) {
setActivateError(error.message)
}
}
return (
<Account>
<ErrorWrapper>{activateError}</ErrorWrapper>
{showModal && <AccountModal setShowModal={setShowModal} />}
{account ? (
<>
<AccountLabel onClick={() => setShowModal(!showModal)}>{ens ?? shortenAddress(account)}</AccountLabel>
<LoginButton onClick={() => deactivate()}>Disconnect</LoginButton>
</>
) : (
<LoginButton onClick={activateProvider}>Connect</LoginButton>
)}
</Account>
)
}
const ErrorWrapper = styled.div`
color: #ff3960;
margin-right: 40px;
margin-left: 40px;
overflow: auto;
`
const Account = styled.div`
display: flex;
align-items: center;
`
const LoginButton = styled(Button)`
background-color: ${Colors.Yellow[100]};
`
const AccountLabel = styled(Button)`
height: 32px;
margin-right: -40px;
padding-right: 40px;
padding-left: 8px;
background-color: ${Colors.Yellow[100]};
font-size: 12px;
`