Skip to content

Commit 55afd8e

Browse files
abretonc7schristopherferreira9alexandratran
authored
docs: first fixes (#1714)
* docs: first fixes * docs: add headless option * docs: wip * docs: cleanup * fix: removes logger; standerdizes the provider variable name; add connectAndSign feature * docs: cleanup * edits --------- Co-authored-by: Christopher Ferreira <[email protected]> Co-authored-by: Alexandra Tran <[email protected]> Co-authored-by: Alexandra Carrillo <[email protected]>
1 parent 8893047 commit 55afd8e

File tree

11 files changed

+176
-352
lines changed

11 files changed

+176
-352
lines changed

wallet/assets/custom-modal.gif

-201 KB
Loading

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

Lines changed: 19 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
6829
import { 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-
7831
export 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

11660
Read-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

12368
To overcome this limitation in mobile dapps that rely on a continuous connection with MetaMask,
12469
use 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).
12670
This approach offloads the read operations to Infura's nodes, reducing the load on your own
12771
infrastructure and ensuring high availability and reliability, independent of the user's wallet connection.
12872

12973
By 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.

wallet/connect/3rd-party-libraries/web3-onboard.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ example, [`dappMetadata`](../../reference/sdk-js-options.md#dappmetadata):
4747
```javascript
4848
const metamaskSDKWallet = metamaskSDK({
4949
options: {
50-
extensionOnly: false,
5150
dappMetadata: {
5251
name: "Example Web3-Onboard Dapp",
5352
},

wallet/connect/metamask-sdk/javascript/index.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,28 @@ Instantiate the SDK using any [options](../../../reference/sdk-js-options.md):
5959
const MMSDK = new MetaMaskSDK({
6060
dappMetadata: {
6161
name: "JavaScript example dapp",
62-
url: window.location.href,
6362
},
6463
infuraAPIKey: process.env.INFURA_API_KEY,
6564
// Other options.
6665
})
67-
68-
// You can also access via window.ethereum.
69-
const ethereum = MMSDK.getProvider()
7066
```
7167

7268
- Use [`dappMetadata`](../../../reference/sdk-js-options.md#dappmetadata) to display information
7369
about your dapp in the MetaMask connection modal.
7470
- Use [`infuraAPIKey`](../../../reference/sdk-js-options.md#infuraapikey) to
7571
[make read-only RPC requests](../../../how-to/make-read-only-requests.md) from your dapp.
76-
- Use [`modals`](../../../reference/sdk-js-options.md#modals) to [customize the logic and UI of
72+
- Use [`headless`](../../../reference/sdk-js-options.md#headless) to [customize the logic and UI of
7773
the displayed modals](../../../how-to/display/custom-modals.md).
7874

7975
### 4. Use the SDK
8076

81-
Use the SDK by calling any [provider API methods](../../../reference/provider-api.md).
82-
Always call [`eth_requestAccounts`](/wallet/reference/json-rpc-methods/eth_requestaccounts) using
83-
[`request()`](../../../reference/provider-api.md#request) first, since it
84-
prompts the installation or connection popup to appear.
77+
We recommend calling `connect()` first. You can also call [`eth_requestAccounts`](/wallet/reference/json-rpc-methods/eth_requestaccounts) using
78+
[`request()`](../../../reference/provider-api.md#request) first, which will prompt the installation or connection popup to appear.
8579

8680
```javascript
87-
ethereum.request({ method: "eth_requestAccounts", params: [] })
81+
const accounts = await MMSDK.connect()
82+
const provider = MMSDK.getProvider()
83+
provider.request({ method: "eth_accounts", params: [] })
8884
```
8985

9086
You can also call the SDK's [`connectAndSign`](../../../how-to/sign-data/connect-and-sign.md) method, and
@@ -106,10 +102,10 @@ const MMSDK = new MetaMaskSDK({
106102
// Other options.
107103
})
108104

109-
// You can also access via window.ethereum.
105+
const accounts = await MMSDK.connect()
106+
// You can also access the Ethereum provider object but we recommend using the SDK's methods.
110107
const ethereum = MMSDK.getProvider()
111-
112-
ethereum.request({ method: "eth_requestAccounts", params: [] })
108+
ethereum.request({ method: "eth_accounts", params: [] })
113109
```
114110

115111
See the [example JavaScript dapps](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples)

wallet/connect/metamask-sdk/javascript/nodejs.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,17 @@ Instantiate the SDK using any [options](../../../reference/sdk-js-options.md):
4343
```javascript title="index.js"
4444
const MMSDK = new MetaMaskSDK({
4545
dappMetadata: {
46-
name: "Example Node.js Dapp",
47-
url: window.location.href,
46+
name: "Node.js dapp",
4847
},
4948
infuraAPIKey: process.env.INFURA_API_KEY,
5049
// Other options.
5150
})
52-
53-
// You can also access via window.ethereum
54-
const ethereum = MMSDK.getProvider()
5551
```
5652

5753
- Use [`dappMetadata`](../../../reference/sdk-js-options.md#dappmetadata) to display information
5854
about your dapp in the MetaMask connection modal.
5955
- Use [`infuraAPIKey`](../../../reference/sdk-js-options.md#infuraapikey) to
6056
[make read-only RPC requests](../../../how-to/make-read-only-requests.md) from your dapp.
61-
- Use [`modals`](../../../reference/sdk-js-options.md#modals) to [customize the logic and UI of
62-
the displayed modals](../../../how-to/display/custom-modals.md).
6357

6458
### 4. Use the SDK
6559

@@ -69,7 +63,12 @@ Always call [`eth_requestAccounts`](/wallet/reference/json-rpc-methods/eth_reque
6963
prompts the installation or connection popup to appear.
7064

7165
```javascript
72-
ethereum.request({ method: "eth_requestAccounts", params: [] })
66+
const accounts = await sdk.connect();
67+
console.log("connect request accounts", accounts);
68+
69+
// You can also access the Ethereum provider object.
70+
const provider = MMSDK.getProvider()
71+
provider.request({ method: "eth_requestAccounts", params: [] })
7372
```
7473

7574
You can also call the SDK's [`connectAndSign`](../../../how-to/sign-data/connect-and-sign.md) method, and
@@ -85,16 +84,14 @@ import { MetaMaskSDK } from "@metamask/sdk"
8584
const MMSDK = new MetaMaskSDK({
8685
dappMetadata: {
8786
name: "Example Node.js Dapp",
88-
url: window.location.href,
8987
},
9088
infuraAPIKey: process.env.INFURA_API_KEY,
9189
// Other options.
9290
})
9391

94-
// You can also access via window.ethereum
95-
const ethereum = MMSDK.getProvider()
96-
97-
ethereum.request({ method: "eth_requestAccounts", params: [] })
92+
const accounts = await MMSDK.connect()
93+
const provider = MMSDK.getProvider()
94+
provider.request({ method: "eth_accounts", params: [] })
9895
```
9996

10097
See the [example Node.js dapp](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples/nodejs)

wallet/connect/metamask-sdk/javascript/other-web-frameworks.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,27 @@ Instantiate the SDK using any [options](../../../reference/sdk-js-options.md):
4545
const MMSDK = new MetaMaskSDK({
4646
dappMetadata: {
4747
name: "Example JavaScript Dapp",
48-
url: window.location.href,
4948
},
5049
infuraAPIKey: process.env.INFURA_API_KEY,
5150
// Other options
5251
});
5352

54-
// You can also access via window.ethereum
55-
const ethereum = MMSDK.getProvider();
5653
```
5754

5855
- Use [`dappMetadata`](../../../reference/sdk-js-options.md#dappmetadata) to display information
5956
about your dapp in the MetaMask connection modal.
6057
- Use [`infuraAPIKey`](../../../reference/sdk-js-options.md#infuraapikey) to
6158
[make read-only RPC requests](../../../how-to/make-read-only-requests.md) from your dapp.
62-
- Use [`modals`](../../../reference/sdk-js-options.md#modals) to [customize the logic and UI of
59+
- Use [`headless`](../../../reference/sdk-js-options.md#headless) to [customize the logic and UI of
6360
the displayed modals](../../../how-to/display/custom-modals.md).
6461

6562
### 4. Use the SDK
6663

67-
Use the SDK by calling any [provider API methods](../../../reference/provider-api.md).
68-
Always call [`eth_requestAccounts`](/wallet/reference/json-rpc-methods/eth_requestaccounts) using
69-
[`request()`](../../../reference/provider-api.md#request) first, since it
70-
prompts the installation or connection popup to appear.
64+
We recommend calling `connect()` first. You can also call [`eth_requestAccounts`](/wallet/reference/json-rpc-methods/eth_requestaccounts) using
65+
[`request()`](../../../reference/provider-api.md#request) first, which will prompt the installation or connection popup to appear.
7166

7267
```javascript
73-
ethereum.request({ method: "eth_requestAccounts", params: [] })
68+
const accounts = await MMSDK.connect()
7469
```
7570

7671
You can also call the SDK's [`connectAndSign`](../../../how-to/sign-data/connect-and-sign.md) method, and
@@ -92,10 +87,11 @@ const MMSDK = new MetaMaskSDK({
9287
// Other options
9388
});
9489

95-
// You can also access via window.ethereum
96-
const ethereum = MMSDK.getProvider();
90+
// You can also connect MMSDK.init() first to get access to the provider.
91+
await MMSDK.connect();
9792

98-
ethereum.request({ method: "eth_requestAccounts", params: [] });
93+
const ethereum = MMSDK.getProvider();
94+
ethereum.request({ method: "eth_accounts", params: [] });
9995
```
10096

10197
See the [example JavaScript dapps](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples)

wallet/connect/metamask-sdk/javascript/pure-js.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,11 @@ To import, instantiate, and use the SDK, you can insert a script in the head sec
2222
const MMSDK = new MetaMaskSDK.MetaMaskSDK({
2323
dappMetadata: {
2424
name: "Example Pure JS Dapp",
25-
url: window.location.href,
2625
},
2726
infuraAPIKey: process.env.INFURA_API_KEY,
2827
// Other options.
2928
});
30-
// Because the init process of MetaMask SDK is async.
31-
setTimeout(() => {
32-
// You can also access via window.ethereum.
33-
const ethereum = MMSDK.getProvider();
34-
35-
ethereum.request({ method: 'eth_requestAccounts' });
36-
}, 0);
29+
MMSDK.connect()
3730
</script>
3831
...
3932
</head>
@@ -45,9 +38,9 @@ You can configure the SDK using any [options](../../../reference/sdk-js-options.
4538
about your dapp in the MetaMask connection modal.
4639
- Use [`infuraAPIKey`](../../../reference/sdk-js-options.md#infuraapikey) to
4740
[make read-only RPC requests](../../../how-to/make-read-only-requests.md) from your dapp.
48-
- Use [`modals`](../../../reference/sdk-js-options.md#modals) to [customize the logic and UI of
41+
- Use [`headless`](../../../reference/sdk-js-options.md#headless) to [customize the logic and UI of
4942
the displayed modals](../../../how-to/display/custom-modals.md).
50-
43+
5144
You can call any [provider API methods](../../../reference/provider-api.md) using the SDK.
5245
Always call [`eth_requestAccounts`](/wallet/reference/json-rpc-methods/eth_requestaccounts) using
5346
[`request()`](../../../reference/provider-api.md#request) first, since it prompts the installation

wallet/connect/metamask-sdk/javascript/react/index.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ const root = ReactDOM.createRoot(
6363
root.render(
6464
<React.StrictMode>
6565
<MetaMaskProvider
66-
debug={false}
6766
sdkOptions={{
6867
dappMetadata: {
6968
name: "Example React Dapp",
@@ -79,7 +78,6 @@ root.render(
7978
);
8079
```
8180

82-
When initializing `MetaMaskProvider`, set `debug` to `true` to activate debug mode.
8381
For the full list of options you can set for `sdkOptions`, see the
8482
[JavaScript SDK options reference](../../../../reference/sdk-js-options.md).
8583
Important options include:
@@ -88,7 +86,7 @@ Important options include:
8886
about your dapp in the MetaMask connection modal.
8987
- [`infuraAPIKey`](../../../../reference/sdk-js-options.md#infuraapikey) - Use this to
9088
[make read-only RPC requests](../../../../how-to/make-read-only-requests.md) from your dapp.
91-
- [`modals`](../../../../reference/sdk-js-options.md#modals) - Use this to [customize the logic and UI of
89+
- Use [`headless`](../../../../reference/sdk-js-options.md#headless) to [customize the logic and UI of
9290
the displayed modals](../../../../how-to/display/custom-modals.md).
9391

9492
### 4. Use the SDK

wallet/connect/metamask-sdk/javascript/react/react-ui.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ root.render(
6868
sdkOptions={{
6969
dappMetadata: {
7070
name: "Example React UI Dapp",
71-
url: window.location.href,
7271
},
7372
infuraAPIKey: process.env.INFURA_API_KEY,
7473
// Other options.
@@ -88,7 +87,7 @@ Important options include:
8887
about your dapp in the MetaMask connection modal.
8988
- [`infuraAPIKey`](../../../../reference/sdk-js-options.md#infuraapikey) - Use this to
9089
[make read-only RPC requests](../../../../how-to/make-read-only-requests.md) from your dapp.
91-
- [`modals`](../../../../reference/sdk-js-options.md#modals) - Use this to [customize the logic and UI of
90+
- Use [`headless`](../../../../reference/sdk-js-options.md#headless) to [customize the logic and UI of
9291
the displayed modals](../../../../how-to/display/custom-modals.md).
9392

9493
### 4. Use the SDK

0 commit comments

Comments
 (0)