forked from wslyvh/nexth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-message.tsx
76 lines (65 loc) · 2.44 KB
/
custom-message.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
import { Chain, useNetwork, useSwitchNetwork } from 'wagmi'
import { Button, Code, ListItem, Text, UnorderedList } from '@chakra-ui/react'
import { useEffect, useState } from 'react'
import { NextSeo } from 'next-seo'
import { HeadingComponent } from 'components/layout/HeadingComponent'
import { messageAddress, messageConfig, readMessage } from 'abis'
import { ETH_CHAINS } from 'utils/config'
import { LinkComponent } from 'components/layout/LinkComponent'
export default function MessageExample() {
const [message, setMessage] = useState('')
const { chain } = useNetwork()
const { switchNetwork } = useSwitchNetwork()
useEffect(() => {
async function read() {
try {
const data = await readMessage({ functionName: 'message' })
setMessage(data)
return
} catch (e) {
console.log('Unable to read message', e)
}
setMessage('')
}
read()
}, [chain])
async function switchToNetwork(id?: number) {
if (switchNetwork && !!id) {
switchNetwork(id)
}
}
return (
<div>
<NextSeo title="Custom Solidity Contract" />
<HeadingComponent as="h2">Custom Solidity Contract</HeadingComponent>
<Text>
This example shows a custom Solidity smart contract deployed using Hardhat. You can find sample contract under <Code>/contracts</Code>.
Contracts are automatically verified on Etherscan and types and hooks are generated using <Code>wagmi-cli</Code>
</Text>
<HeadingComponent as="h3">Deployments</HeadingComponent>
<Text>Switch to a network where this contract is deployed.</Text>
<UnorderedList>
{Object.keys(messageConfig.address).map((i) => {
const chain = ETH_CHAINS.find((chain: Chain) => String(chain.id) === i)
const address = (messageAddress as any)[i]
const explorerUrl = chain?.blockExplorers?.default.url
return (
<ListItem key={i}>
<Button size="xs" mr={2} onClick={() => switchToNetwork(chain?.id)}>
{chain?.name ?? i}
</Button>
{explorerUrl && <LinkComponent href={`${explorerUrl}/address/${address}`}>{address}</LinkComponent>}
{!explorerUrl && address}
</ListItem>
)
})}
</UnorderedList>
{message && (
<div>
<HeadingComponent as="h3">Current message</HeadingComponent>
<p>{message}</p>
</div>
)}
</div>
)
}