Skip to content

Commit 2ea5f94

Browse files
Update Wagmi guide and tutorial (#1669)
* Update Wagmi guide * remove infuraApiKey prereq * Update guide and tutorial * Update guide and tutorial * edits * address feedback --------- Co-authored-by: Edouard Bougon <15703023+EdouardBougon@users.noreply.github.com>
1 parent f85b580 commit 2ea5f94

File tree

2 files changed

+58
-158
lines changed

2 files changed

+58
-158
lines changed

wallet/connect/3rd-party-libraries/wagmi.md

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,76 @@ the MetaMask browser extension and MetaMask Mobile.
1515

1616
## Prerequisites
1717

18-
- Ensure you have a basic understanding of Ethereum smart contracts and React Hooks.
19-
- Set up a project with [Wagmi](https://wagmi.sh/react/getting-started).
20-
- Create an Infura API key and allowlist to [make read-only requests](../../how-to/make-read-only-requests.md).
18+
- [Node.js](https://nodejs.org/en/) version 20+
19+
- [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm/) version 9+
20+
- A text editor (for example, [VS Code](https://code.visualstudio.com/))
21+
- The [MetaMask extension](https://metamask.io/) or
22+
[MetaMask Flask](/developer-tools/dashboard/get-started/create-api) installed
23+
- Basic knowledge of [React](https://react.dev/)
2124

22-
## Configure the MetaMask connector
25+
## Steps
2326

24-
In your Wagmi project, configure the MetaMask connector:
27+
### 1. Set up the project
2528

26-
```javascript
29+
If you don't have an existing Wagmi project, create a new project directory using Wagmi's `create wagmi`
30+
command with the `vite-react` template:
31+
32+
```bash
33+
npm create wagmi@latest --template vite-react
34+
```
35+
36+
This prompts you for a project name.
37+
For example, use `mmsdk-wagmi`.
38+
Once your project is created, navigate into it and install the node module dependencies:
39+
40+
```bash
41+
cd mmsdk-wagmi && npm install
42+
```
43+
44+
### 2. Configure the MetaMask connector
45+
46+
In `wagmi.ts`, configure the MetaMask connector with any [parameters](https://wagmi.sh/core/api/connectors/metaMask#parameters).
47+
Specify [`dappMetadata`](https://wagmi.sh/core/api/connectors/metaMask#dappmetadata), including the `name`, `url`, and `iconUrl`,
48+
to help identify your dapp within the MetaMask ecosystem.
49+
50+
```typescript title="wagmi.ts"
2751
import { metaMask } from "wagmi/connectors"
2852

2953
export const config = createConfig({
3054
chains: [mainnet, sepolia],
3155
connectors: [
3256
metaMask({
33-
infuraAPIKey: import.meta.env.VITE_INFURA_API_KEY,
57+
dappMetadata: {
58+
name: "Example Wagmi dapp",
59+
url: "https://wagmi.io",
60+
iconUrl: "https://wagmi.io/favicon.ico",
61+
},
3462
}),
3563
],
3664
transports: {
3765
[mainnet.id]: http(),
38-
// You can also configure the transcripts to use INFURA_API_KEY directly into the Wagmi config
39-
// to share with other providers.
40-
// [mainnet.id]: http("https://mainnet.infura.io/v3/...")
4166
[sepolia.id]: http(),
4267
},
4368
})
4469
```
4570

46-
Make sure to configure the MetaMask connector with the proper [SDK options](../../reference/sdk-js-options.md).
47-
48-
To provide a better mobile user experience, specify the [`infuraAPIKey`](../../reference/sdk-js-options.md#infuraapikey)
49-
option to [make read-only requests](../../how-to/make-read-only-requests.md) using the Infura API.
50-
You can set your Infura API key in environment variables:
51-
52-
```env
53-
VITE_INFURA_API_KEY=<YOUR-API-KEY>
54-
```
55-
56-
## Benefits of using the Infura API with Wagmi
71+
:::note
72+
By default, if the [EIP-6963](https://eips.ethereum.org/EIPS/eip-6963) MetaMask injected provider is detected,
73+
this connector will replace it.
74+
See Wagmi's [`rdns`](https://wagmi.sh/dev/creating-connectors#properties) property for more information.
75+
:::
5776

58-
Read-only requests are blockchain requests that do not require user wallet interaction.
59-
Mobile dapps can lose their continuous connection with MetaMask, causing read-only requests to fail.
60-
When the mobile wallet is disconnected, the dapp must deeplink into the wallet to "wake up" the connection.
77+
### 3. Run the dapp
6178

62-
Without setting the `infuraAPIKey`, the dapp might experience issues in mobile environments:
79+
Start the development server:
6380

64-
![Wagmi errors](../../assets/wagmi-errors.png)
81+
```bash
82+
npm run dev
83+
```
6584

66-
To overcome this limitation in mobile dapps that rely on a continuous connection with MetaMask,
67-
use the Infura API to make read-only requests.
68-
This approach offloads the read operations to Infura's nodes, reducing the load on your own
69-
infrastructure and ensuring high availability and reliability, independent of the user's wallet connection.
85+
Navigate to the displayed localhost URL to view and test your dapp.
7086

71-
By using the Infura API, you can ensure:
87+
## Next steps
7288

73-
- **Uninterrupted access** - Continuous network access for read-only requests, regardless of MetaMask's state.
74-
- **Enhanced stability** - Stabilized dapp functionality by relying on Infura's robust infrastructure
75-
rather than the mobile environment's variable connectivity and background processing constraints.
89+
See the [Create a React dapp with the SDK and Wagmi](../../tutorials/react-dapp-sdk-wagmi.md) tutorial
90+
for more information about configuring the SDK with Wagmi, and how the dapp behaves out of the box.

wallet/tutorials/react-dapp-sdk-wagmi.md

Lines changed: 9 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ custom connect button.
4242

4343
### 1. Set up the project
4444

45+
Create a new project directory using Wagmi's `create wagmi` command with the `vite-react` template:
46+
4547
<Tabs>
4648
<TabItem value="pnpm" label="pnpm" default>
4749

@@ -59,9 +61,9 @@ npm create wagmi@latest --template vite-react
5961
</TabItem>
6062
</Tabs>
6163

62-
This command prompts you for a project name.
64+
This prompts you for a project name.
6365
For example, use `mmsdk-wagmi-tutorial`.
64-
Once the CLI is complete, change directories into that project and install the node module dependencies:
66+
Once your project is created, navigate into it and install the node module dependencies:
6567

6668
<Tabs>
6769
<TabItem value="pnpm" label="pnpm" default>
@@ -100,7 +102,7 @@ This displays a localhost URL in your terminal for easy access to launch in your
100102
➜ press h + enter to show help
101103
```
102104

103-
#### 1.1. Review the Viem, Wagmi, and MetaMask SDK configuration
105+
### 2. Review the Wagmi configuration
104106

105107
In `src/main.tsx`, you can find the following `WagmiProvider` which takes a `config` and `QueryClientProvider`.
106108

@@ -176,7 +178,7 @@ Out of the box, they have also set up the `injected` wallet provider, `coinbaseW
176178

177179
You'll replace these with the MetaMask connector for the purpose of this tutorial and testing MetaMask SDK.
178180

179-
### 2. Update the Wagmi configuration
181+
### 3. Update the Wagmi configuration
180182

181183
Modify this configuration to target the Linea testnet and use the MetaMask connector, which is
182184
Wagmi's built-in support for MetaMask SDK:
@@ -187,7 +189,6 @@ import { lineaSepolia } from "wagmi/chains";
187189
import { metaMask } from "wagmi/connectors";
188190

189191
export const config = createConfig({
190-
multiInjectedProviderDiscovery: false,
191192
chains: [lineaSepolia],
192193
connectors: [
193194
metaMask({
@@ -208,8 +209,6 @@ export const config = createConfig({
208209

209210
This configures Wagmi with the following options:
210211

211-
- `multiInjectedProviderDiscovery` disables automatic discovery of multiple injected wallets,
212-
streamlining the wallet selection process.
213212
- `chains` specifies that the dapp will connect to the Linea Sepolia network.
214213
- `connectors` uses MetaMask as the primary wallet, and includes some dapp metadata (name, URL, and
215214
icon) for a branded user experience.
@@ -219,7 +218,7 @@ This configures Wagmi with the following options:
219218

220219
This setup simplifies wallet integration and provides a smooth user experience while working with Linea Sepolia.
221220

222-
### 3. Connect to MetaMask extension or Mobile
221+
### 4. Connect to MetaMask extension or Mobile
223222

224223
At this point, your dapp displays, and you can connect to and disconnect from MetaMask.
225224
Your connection experience will be similar to that of using the injected provider, as most web3
@@ -251,121 +250,7 @@ MetaMask Mobile.
251250
</video>
252251
</p>
253252

254-
### 3. Configure the SDK
255-
256-
You can configure any [MetaMask JavaScript SDK options](../reference/sdk-js-options.md) within the
257-
Wagmi configuration.
258-
259-
In `wagmi.ts`, create a variable named `metaMaskSDKOptions`:
260-
261-
```typescript title="wagmi.ts"
262-
const metaMaskSDKOptions = {
263-
infuraAPIKey: "<YOUR-API-KEY>",
264-
};
265-
```
266-
267-
:::note
268-
You can use the [`infuraAPIKey`](../reference/sdk-js-options.md#infuraapikey) option to
269-
[make direct, read-only JSON-RPC requests](../how-to/make-read-only-requests.md).
270-
:::
271-
272-
Pass `metaMaskSDKOptions` into the `metaMask()` function in `connectors` and spread its values using
273-
the `...` operator:
274-
275-
```typescript title="wagmi.ts"
276-
connectors: [
277-
metaMask({
278-
dappMetadata: {
279-
name: "MetaMask SDK + Wagmi Tutorial",
280-
url: "https://wagmi.io",
281-
iconUrl: "https://wagmi.io/favicon.ico",
282-
},
283-
...metaMaskSDKOptions,
284-
}),
285-
],
286-
```
287-
288-
### 4. Use additional SDK options
289-
290-
You can configure any number of [SDK options](../reference/sdk-js-options.md) in the
291-
`metaMaskSDKOptions` object to customize the behavior of the SDK.
292-
In this section, you'll plug in options one by one to see how it affects the SDK.
293-
294-
#### Immediately check if MetaMask is installed
295-
296-
Use [`checkInstallationImmediately`](../reference/sdk-js-options.md#checkinstallationimmediately) to
297-
enable or disable immediately checking if MetaMask is installed in the user's browser.
298-
The default value is `false`.
299-
Set this to `true`:
300-
301-
```typescript title="wagmi.ts"
302-
const metaMaskSDKOptions = {
303-
checkInstallationImmediately: true,
304-
};
305-
```
306-
307-
Disable the MetaMask extension and re-load your dapp.
308-
When this option is enabled, the SDK immediately checks and notifies the user with a modal that they
309-
can either install the MetaMask extension or connect to MetaMask Mobile using a QR code.
310-
311-
#### Check if MetaMask is installed before RPC requests
312-
313-
Use [`checkInstallationOnAllCalls`](../reference/sdk-js-options.md#checkinstallationonallcalls) to
314-
enable or disable checking if MetaMask is installed before each JSON-RPC request.
315-
The default value is `false`.
316-
Set this to `true`:
317-
318-
```typescript title="wagmi.ts"
319-
const metaMaskSDKOptions = {
320-
checkInstallationOnAllCalls: true
321-
};
322-
```
323-
324-
Disable the MetaMask extension and re-load your dapp.
325-
Make a read-only RPC request without the `InfuraAPIKey`.
326-
You'll see a similar modal as in the previous test, but it will notify the user upon making the RPC request.
327-
328-
#### Send anonymous analytics to MetaMask
329-
330-
Use [`enableDebug`](../reference/sdk-js-options.md#enabledebug) to enable or disable sending
331-
anonymous analytics to MetaMask to help improve the SDK.
332-
The default value is `true`.
333-
334-
```typescript
335-
const metaMaskSDKOptions = {
336-
enableDebug: true
337-
};
338-
```
339-
340-
This setting does not transmit any sensitive data.
341-
It only sends information that allows MetaMask to analyze and improve the behavior and core
342-
functionality of the SDK.
343-
344-
#### Map RPC URLs for read-only requests
345-
346-
Use [`readonlyRPCMap`](../reference/sdk-js-options.md#readonlyrpcmap) to map RPC URLS for
347-
[read-only RPC requests](../how-to/make-read-only-requests.md).
348-
You can use this option in conjunction with [`infuraAPIKey`](../reference/sdk-js-options.md#infuraapikey)
349-
and [`defaultReadOnlyChainId`](../reference/sdk-js-options.md#defaultreadonlychainid).
350-
351-
For example, you want to make read-only requests to Mainnet (chain ID `0x1`) using the Infura API,
352-
and read-only requests to the local testnet (chain ID `0x539`) use a custom node.
353-
354-
`infuraAPIKey` provides access to various networks supported by Infura.
355-
`readonlyRPCMap` allows access to custom nodes and overrides Infura networks in case of a conflict.
356-
You can use both the Infura API and custom nodes to make read-only requests by specifying both options:
357-
358-
```typescript
359-
const metaMaskSDKOptions = {
360-
infuraAPIKey: "<YOUR-API-KEY>",
361-
readonlyRPCMap: {
362-
"0x539": "http://localhost:8545",
363-
}
364-
};
365-
```
366-
367-
### Conclusion
253+
## Conclusion
368254

369255
This tutorial walked you through generating a dapp using Create Wagmi, and configuring MetaMask SDK.
370-
You explored how the SDK works within a React application with Viem and Wagmi, how it behaves out of
371-
the box, and how to use a few key options to customize its behavior.
256+
You explored how the SDK works within a React application with Wagmi, and how it behaves out of the box.

0 commit comments

Comments
 (0)