-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
158 lines (152 loc) · 4.88 KB
/
App.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { useEffect, useState } from "react";
import {
VStack,
useDisclosure,
Button,
Text,
HStack,
Select,
Input,
Box
} from "@chakra-ui/react";
import SelectWalletModal from "./Modal";
import { CheckCircleIcon, WarningIcon } from "@chakra-ui/icons";
import { Tooltip } from "@chakra-ui/react";
import { networkParams } from "./networks";
import { toHex, truncateAddress } from "./utils";
import { useConnect, useAccount, useNetwork, useSignMessage } from "wagmi";
export default function Home() {
const { isOpen, onOpen, onClose } = useDisclosure();
const [{ data: connectData }] = useConnect();
const [{ data: accountData }, disconnect] = useAccount();
const [{ data: networkData }, switchNetwork] = useNetwork();
const [message, setMessage] = useState("");
const [{ data: signData }, signMessage] = useSignMessage({
message
});
const [error, setError] = useState("");
const [network, setNetwork] = useState(undefined);
const handleNetwork = (e) => {
const id = e.target.value;
setNetwork(Number(id));
};
const handleInput = (e) => {
const msg = e.target.value;
setMessage(msg);
};
return (
<>
<Text position="absolute" top={0} right="15px">
If you're in the sandbox, first "Open in New Window" ⬆️
</Text>
<VStack justifyContent="center" alignItems="center" h="100vh">
<HStack marginBottom="10px">
<Text
margin="0"
lineHeight="1.15"
fontSize={["1.5em", "2em", "3em", "4em"]}
fontWeight="600"
>
Let's connect with
</Text>
<Text
margin="0"
lineHeight="1.15"
fontSize={["1.5em", "2em", "3em", "4em"]}
fontWeight="600"
sx={{
background: "linear-gradient(90deg, #1652f0 0%, #b9cbfb 70.35%)",
WebkitBackgroundClip: "text",
WebkitTextFillColor: "transparent"
}}
>
Wagmi
</Text>
</HStack>
<HStack>
{!connectData.connected ? (
<Button onClick={onOpen}>Connect Wallet</Button>
) : (
<Button onClick={disconnect}>Disconnect</Button>
)}
</HStack>
<VStack justifyContent="center" alignItems="center" padding="10px 0">
<HStack>
<Text>{`Connection Status: `}</Text>
{connectData.connected ? (
<CheckCircleIcon color="green" />
) : (
<WarningIcon color="#cd5700" />
)}
</HStack>
{!accountData ? (
<Text>Account: No Account</Text>
) : (
<Tooltip label={accountData.address} placement="right">
<Text>{`Account: ${truncateAddress(accountData.address)}`}</Text>
</Tooltip>
)}
<Text>{`Network ID: ${
networkData.chain ? networkData.chain.id : "No Network"
}`}</Text>
</VStack>
{connectData.connected && (
<HStack justifyContent="flex-start" alignItems="flex-start">
<Box
maxW="sm"
borderWidth="1px"
borderRadius="lg"
overflow="hidden"
padding="10px"
>
<VStack>
<Button
onClick={() => switchNetwork(network)}
isDisabled={!network}
>
Switch Network
</Button>
<Select placeholder="Select network" onChange={handleNetwork}>
<option value="3">Ropsten</option>
<option value="4">Rinkeby</option>
<option value="42">Kovan</option>
{/* <option value="1666600000">Harmony</option> */}
{/* <option value="42220">Celo</option> */}
</Select>
</VStack>
</Box>
<Box
maxW="sm"
borderWidth="1px"
borderRadius="lg"
overflow="hidden"
padding="10px"
>
<VStack>
<Button
onClick={async () => await signMessage()}
isDisabled={!message}
>
Sign Message
</Button>
<Input
placeholder="Set Message"
maxLength={20}
onChange={handleInput}
w="140px"
/>
{signData ? (
<Tooltip label={signData} placement="bottom">
<Text>{`Signature: ${truncateAddress(signData)}`}</Text>
</Tooltip>
) : null}
</VStack>
</Box>
</HStack>
)}
<Text>{error ? error.message : null}</Text>
</VStack>
<SelectWalletModal isOpen={isOpen} closeModal={onClose} />
</>
);
}