@@ -2,131 +2,9 @@ import Callout from 'nextra-theme-docs/callout'
2
2
3
3
# Wallet connections
4
4
5
- <Callout >
6
- The content on this site is based off of version ` 0.2.28 ` of ` wagmi ` . The
7
- ` 0.3.0 ` version release brought in tons of breaking API changes. The content
8
- on this site has not been updated to be compatible with it yet.
9
- </Callout >
10
-
11
5
[ create-web3-frontend] ( https://github.com/dhaiwat10/create-web3-frontend ) already does this for you.
12
6
13
- 1 . First, setup the ` wagmi ` Provider at the root of your app.
14
-
15
- If you're using Next.js, this would be done in ` pages/_app.tsx ` . If you're using create-react-app, this would be done in ` src/index.tsx ` .
16
-
17
- ``` tsx
18
- import ' ../styles/globals.css'
19
- import type { AppProps } from ' next/app'
20
- import { Connector , Provider , chain , defaultChains } from ' wagmi'
21
- import { InjectedConnector } from ' wagmi/connectors/injected'
22
- import { WalletConnectConnector } from ' wagmi/connectors/walletConnect'
23
- import { WalletLinkConnector } from ' wagmi/connectors/walletLink'
24
- import { providers } from ' ethers'
25
-
26
- // Get environment variables
27
- const infuraId = process .env .NEXT_PUBLIC_INFURA_ID as string
28
-
29
- // Pick chains
30
- const chains = defaultChains
31
- const defaultChain = chain .mainnet
32
-
33
- // Set up connectors
34
- type ConnectorsConfig = { chainId? : number }
35
- const connectors = ({ chainId }: ConnectorsConfig ) => {
36
- const rpcUrl =
37
- chains .find ((x ) => x .id === chainId )?.rpcUrls ?.[0 ] ??
38
- defaultChain .rpcUrls [0 ]
39
- return [
40
- new InjectedConnector ({ chains }),
41
- new WalletConnectConnector ({
42
- chains ,
43
- options: {
44
- infuraId ,
45
- qrcode: true ,
46
- },
47
- }),
48
- new WalletLinkConnector ({
49
- chains ,
50
- options: {
51
- appName: ' create-web3-frontend' ,
52
- jsonRpcUrl: ` ${rpcUrl }/${infuraId } ` ,
53
- },
54
- }),
55
- ]
56
- }
57
-
58
- // Set up providers
59
- type ProviderConfig = { chainId? : number ; connector? : Connector }
60
- const isChainSupported = (chainId ? : number ) =>
61
- chains .some ((x ) => x .id === chainId )
62
-
63
- const provider = ({ chainId }: ProviderConfig ) =>
64
- providers .getDefaultProvider (
65
- isChainSupported (chainId ) ? chainId : defaultChain .id ,
66
- {
67
- infuraId ,
68
- }
69
- )
70
- const webSocketProvider = ({ chainId }: ProviderConfig ) =>
71
- isChainSupported (chainId )
72
- ? new providers .InfuraWebSocketProvider (chainId , infuraId )
73
- : undefined
74
-
75
- function MyApp({ Component , pageProps }: AppProps ) {
76
- return (
77
- <Provider
78
- autoConnect
79
- connectors = { connectors }
80
- provider = { provider }
81
- webSocketProvider = { webSocketProvider }
82
- >
83
- <Component { ... pageProps } />
84
- </Provider >
85
- )
86
- }
87
-
88
- export default MyApp
89
- ```
90
-
91
- 2 . Now, setup the wallet connection screen wherever you wish.
92
-
93
- ``` tsx
94
- import type { NextPage } from ' next'
95
- import { FC } from ' react'
96
- import { useConnect , useAccount } from ' wagmi'
97
-
98
- const Home: NextPage = () => {
99
- const [{ data : connectData, error : connectError }, connect] = useConnect ()
100
- const { connected } = connectData
101
- const [{ data : accountData }, disconnect] = useAccount ()
102
-
103
- if (connected ) {
104
- return (
105
- <div >
106
- <p >Welcome { accountData ?.address } </p >
107
- <button onClick = { disconnect } >Disconnect</button >
108
- <InfoSection />
109
- </div >
110
- )
111
- }
112
-
113
- return (
114
- <div >
115
- <p >Connect your wallet:</p >
116
- <div >
117
- { /* connectData.connectors contains the list of available 'connectors' like Metamask, WalletConnect, etc */ }
118
- { connectData .connectors .map ((x ) => (
119
- <button key = { x .id } onClick = { () => connect (x )} >
120
- { x .name }
121
- { ! x .ready && ' (unsupported)' }
122
- </button >
123
- ))}
124
- </div >
125
-
126
- { connectError && <p >{ connectError ?.message ?? ' Failed to connect' } </p >}
127
- </div >
128
- )
129
- }
7
+ You have two options.
130
8
131
- export default Home
132
- ```
9
+ 1 . Use [ Rainbowkit ] ( https://www.rainbowkit.com ) (recommended, fastest and prettiest), or
10
+ 2 . If you want to make a 'custom' wallet connection interface, refer the [ wallet connection guide in the wagmi docs ] ( https://wagmi.sh/examples/connect-wallet )
0 commit comments