@@ -21,117 +21,57 @@ the MetaMask browser extension and MetaMask Mobile.
2121- Set up a project with [ Wagmi] ( https://wagmi.sh/react/getting-started ) .
2222- Create an Infura API key and allowlist to [ make read-only requests] ( ../../how-to/make-read-only-requests.md ) .
2323
24- ## Steps
24+ ## Configure the MetaMask connector
2525
26- ### 1. Configure MetaMask SDK
27-
28- In your Wagmi project, configure MetaMask SDK with the proper [ SDK options] ( ../../reference/sdk-js-options.md ) .
29-
30- ``` javascript
31- const MetaMaskOptions = {
32- dappMetadata: {
33- name: " Example Wagmi dapp" ,
34- },
35- infuraAPIKey: " YOUR-API-KEY" ,
36- // Other options.
37- }
38- ```
39-
40- #### Dapp metadata
41-
42- Specify the [ ` dappMetadata ` ] ( ../../reference/sdk-js-options.md#dappmetadata ) option to help
43- identify your dapp within the MetaMask ecosystem.
44- This option is required when configuring the MetaMask connector with Wagmi.
45-
46- #### Infura API key
47-
48- We recommend specifying 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- Read more about the [ benefits of using the Infura API with Wagmi] ( #benefits-of-using-the-infura-api-with-wagmi ) .
51-
52- #### Universal links
53-
54- We recommend using universal links instead of deeplinks to avoid issues on iOS.
55- Thus, do not enable the [ ` useDeeplink ` ] ( ../../reference/sdk-js-options.md#usedeeplink ) option.
56- Using universal links ensures a smoother transition for users accessing your dapp from mobile
57- devices, providing a better user experience compared to traditional deeplinking methods.
58-
59- ### 2. Configure Wagmi with the MetaMask connector
60-
61- Configure Wagmi to include MetaMask as a connector and specify the Ethereum chains your dapp will support.
62- Use the ` MetaMaskOptions ` you created in the previous step when adding the ` metaMask ` connector.
63- For example:
26+ In your Wagmi project, configure the MetaMask connector:
6427
6528``` javascript
66- import { createConfig , http } from " wagmi"
67- import { mainnet , sepolia } from " wagmi/chains"
6829import { metaMask } from " wagmi/connectors"
6930
70- const MetaMaskOptions = {
71- dappMetadata: {
72- name: " Example Wagmi dapp" ,
73- },
74- infuraAPIKey: " YOUR-API-KEY" ,
75- // Other options.
76- }
77-
7831export const config = createConfig ({
7932 chains: [mainnet, sepolia],
8033 connectors: [
81- metaMask (MetaMaskOptions),
82- // Other connectors
34+ metaMask ({
35+ infuraAPIKey: import .meta.env.VITE_INFURA_API_KEY,
36+ }),
8337 ],
8438 transports: {
8539 [mainnet .id ]: http (),
40+ // You can also configure the transcripts to use INFURA_API_KEY directly into the Wagmi config
41+ // to share with other providers.
42+ // [mainnet.id]: http("https://mainnet.infura.io/v3/...")
8643 [sepolia .id ]: http (),
8744 },
8845})
8946```
9047
91- ### 3. Implement contract interaction using ` usePrepareContractWrite `
92-
93- Due to a known issue in Safari where a 500ms timeout can interrupt smart contract interactions, we
94- recommend using the [ ` usePrepareContractWrite ` ] ( https://1.x.wagmi.sh/react/prepare-hooks/usePrepareContractWrite )
95- hook from Wagmi.
96- This approach ensures smooth transactions by preparing the contract write operation ahead of the
97- actual execution.
98-
99- ``` javascript
100- import { usePrepareContractWrite , useContractWrite } from " wagmi"
101-
102- const { config } = usePrepareContractWrite ({
103- address: " 0xContractAddress" ,
104- abi: contractABI,
105- functionName: " functionToCall" ,
106- args: [arg1, arg2],
107- })
48+ Make sure to configure the MetaMask connector with the proper [ SDK options] ( ../../reference/sdk-js-options.md ) .
10849
109- const { write } = useContractWrite (config)
50+ To provide a better mobile user experience, specify the [ ` infuraAPIKey ` ] ( ../../reference/sdk-js-options.md#infuraapikey )
51+ option to [ make read-only requests] ( ../../how-to/make-read-only-requests.md ) using the Infura API.
52+ You can set your Infura API key in environment variables:
11053
111- write ()
54+ ``` env
55+ VITE_INFURA_API_KEY=<YOUR-API-KEY>
11256```
11357
11458## Benefits of using the Infura API with Wagmi
11559
11660Read-only requests are blockchain requests that do not require user wallet interaction.
117- Mobile dapps can lose their continuous connection with MetaMask, causing read-only requests to fail.
61+ Mobile dapps can lose their continuous connection with MetaMask, causing read-only requests to fail.
62+ When the mobile wallet is disconnected, the dapp must deeplink into the wallet to "wake up" the connection.
11863
119- These are some errors that might occur in mobile environments:
64+ Without setting the ` infuraAPIKey ` , the dapp might experience issues in mobile environments:
12065
12166![ Wagmi errors] ( ../../assets/wagmi-errors.png )
12267
12368To overcome this limitation in mobile dapps that rely on a continuous connection with MetaMask,
12469use the Infura API to make read-only requests.
125- You can do this by [ configuring the SDK with an Infura API key] ( #2-configure-wagmi-with-the-metamask-connector ) .
12670This approach offloads the read operations to Infura's nodes, reducing the load on your own
12771infrastructure and ensuring high availability and reliability, independent of the user's wallet connection.
12872
12973By using the Infura API, you can ensure:
13074
131- - ** Uninterrupted access:** Continuous network access for read-only requests, regardless of MetaMask's state.
132-
133- - ** Enhanced stability:** Stabilized dapp functionality by relying on Infura's robust infrastructure
75+ - ** Uninterrupted access** - Continuous network access for read-only requests, regardless of MetaMask's state.
76+ - ** Enhanced stability** - Stabilized dapp functionality by relying on Infura's robust infrastructure
13477 rather than the mobile environment's variable connectivity and background processing constraints.
135-
136- In summary, using the Infura API compensates for Wagmi's lack of mobile optimization by providing a
137- stable network backend for read-only operations.
0 commit comments