Skip to content

Commit 2b70757

Browse files
Edit MM Connect EVM guides and styles (#2880)
* Edit MM Connect EVM guides and styles * fix ilnk * add evm headless mode * additions and edits
1 parent 3048eac commit 2b70757

21 files changed

Lines changed: 420 additions & 172 deletions

File tree

embedded-wallets/build-with-ai.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ For self-hosted instances, add it to your `openclaw.json`:
9494
For any LLM tool with a system prompt or custom instructions field, paste the skill content below directly.
9595

9696
<details>
97-
<summary><strong>View `SKILL.md` file</strong></summary>
97+
<summary>View `SKILL.md` file</summary>
9898

9999
<SkillContent />
100100

metamask-connect/evm/guides/best-practices/display.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ keywords:
1818

1919
# Display in MetaMask
2020

21+
Follow these best practices when displaying icons or method names in MetaMask.
22+
2123
## Display icons
2224

2325
When your dapp makes a sign-in request to a MetaMask user, MetaMask may render a modal that displays
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
title: 'Use Headless Mode - MetaMask Connect EVM'
3+
sidebar_label: Use headless mode
4+
description: Render a custom QR code or connection UI by using headless mode with MetaMask Connect EVM.
5+
keywords:
6+
[headless, QR code, custom UI, display_uri, evm, MetaMask, Connect, connection, modal, deeplink]
7+
---
8+
9+
# Use headless mode
10+
11+
By default, MetaMask Connect renders its own QR code modal when connecting to MetaMask Mobile
12+
via MetaMask Wallet Protocol (MWP).
13+
Headless mode suppresses this built-in modal so you can render your own connection UI.
14+
15+
Use headless mode when you want to:
16+
17+
- Display a custom-styled QR code that matches your dapp's design.
18+
- Show the connection URI in a different format (for example, a deeplink button instead of a QR code).
19+
- Integrate the connection flow into an existing modal or onboarding wizard.
20+
21+
## Prerequisites
22+
23+
Follow Step 1 of the [quickstart](../quickstart/javascript.md) to install the EVM client.
24+
25+
## Steps
26+
27+
### 1. Initialize the client with headless mode
28+
29+
Initialize an EVM client using [`createEVMClient`](../reference/methods.md#createevmclient), and set `ui.headless` to `true`:
30+
31+
```javascript
32+
import { createEVMClient, getInfuraRpcUrls } from '@metamask/connect-evm'
33+
34+
const evmClient = await createEVMClient({
35+
dapp: {
36+
name: 'My Dapp',
37+
url: window.location.href,
38+
},
39+
api: {
40+
supportedNetworks: {
41+
...getInfuraRpcUrls({ infuraApiKey: '<YOUR_INFURA_API_KEY>' }),
42+
},
43+
},
44+
ui: { headless: true },
45+
})
46+
```
47+
48+
### 2. Register a `display_uri` listener before connecting
49+
50+
The `display_uri` event fires on the EIP-1193 [provider](../reference/provider-api.md) during the connecting phase with a one-time-use pairing URI.
51+
You **must** register the listener before calling `connect`, or you may miss the event:
52+
53+
```javascript
54+
const provider = evmClient.getProvider()
55+
56+
provider.on('display_uri', uri => {
57+
showCustomQrModal(uri)
58+
})
59+
```
60+
61+
As an alternative to `provider.on`, you can pass a `displayUri` callback when initializing the client via [`eventHandlers`](../reference/methods.md#createevmclient) (see the [migration guide](migrate-from-sdk.md#6-update-event-handling)).
62+
63+
### 3. Connect and handle the result
64+
65+
Call [`connect`](../reference/methods.md#connect) to connect to MetaMask, and handle the result:
66+
67+
```javascript
68+
try {
69+
await evmClient.connect({ chainIds: ['0x1'] })
70+
hideCustomQrModal()
71+
} catch (err) {
72+
hideCustomQrModal()
73+
if (err.code === 4001) {
74+
// User rejected — show retry UI
75+
} else {
76+
console.error('Connection failed:', err)
77+
}
78+
}
79+
```
80+
81+
## Important considerations
82+
83+
### URI is one-time-use
84+
85+
The pairing URI delivered by `display_uri` is a one-time-use token.
86+
Once used or expired, it cannot be reused.
87+
If the connection fails, call `connect` again to generate a fresh URI.
88+
89+
### `display_uri` only fires during connecting
90+
91+
The event fires only while the client status is `'connecting'`.
92+
After the connection resolves (success or error), `display_uri` stops firing.
93+
94+
### Extension connections skip QR
95+
96+
When the MetaMask browser extension is installed and `ui.preferExtension` is `true` (the default),
97+
the SDK connects directly through the extension.
98+
No `display_uri` event fires because no QR code is needed.
99+
100+
To display the QR connection option even when the extension is available, set `ui.preferExtension` to `false`:
101+
102+
```javascript
103+
const evmClient = await createEVMClient({
104+
dapp: { name: 'My Dapp', url: window.location.href },
105+
api: {
106+
supportedNetworks: {
107+
...getInfuraRpcUrls({ infuraApiKey: '<YOUR_INFURA_API_KEY>' }),
108+
},
109+
},
110+
ui: {
111+
headless: true,
112+
preferExtension: false,
113+
},
114+
})
115+
```
116+
117+
### Monitor connection status
118+
119+
The EVM client exposes a [`status`](../reference/methods.md#properties) property
120+
(`'loaded' | 'pending' | 'connecting' | 'connected' | 'disconnected'`) you can read when updating UI.
121+
122+
## Next steps
123+
124+
- [Send transactions](send-transactions/index.md) in your JavaScript or Wagmi dapp.
125+
- [Sign data](sign-data/index.md) with the connected session.
126+
- See the [EVM methods reference](../reference/methods.md) for the full API.

metamask-connect/evm/guides/interact-with-contracts.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,35 @@ With MetaMask Connect EVM:
4343

4444
The following examples demonstrate how to use MetaMask Connect EVM with viem, web3.js, ethers.js, Ethereum APIs, or Wagmi to interact with Solidity smart contracts.
4545

46-
This simple Hello World contract allows anyone to read and write a message to it.
46+
## Prerequisites
4747

48-
```tsx
49-
// SPDX-License-Identifier: GPL-3.0
50-
pragma solidity >=0.7.0 <0.9.0;
48+
- Follow the [JavaScript quickstart](../quickstart/javascript.md) or [Wagmi quickstart](../quickstart/wagmi.md) to install, initialize, and connect the EVM client.
49+
- Create and deploy a simple smart contract. For example, the following contract allows anyone to read and write a message to it.
5150

52-
contract HelloWorld {
51+
<details>
52+
<summary>Hello World contract</summary>
53+
<div>
5354

54-
string public message;
55+
```tsx
56+
// SPDX-License-Identifier: GPL-3.0
57+
pragma solidity >=0.7.0 <0.9.0;
5558

56-
constructor(string memory initMessage) {
57-
message = initMessage;
58-
}
59+
contract HelloWorld {
60+
61+
string public message;
62+
63+
constructor(string memory initMessage) {
64+
message = initMessage;
65+
}
5966

60-
function update(string memory newMessage) public {
61-
message = newMessage;
67+
function update(string memory newMessage) public {
68+
message = newMessage;
69+
}
6270
}
63-
}
64-
```
71+
```
72+
73+
</div>
74+
</details>
6575

6676
## Read from contracts
6777

metamask-connect/evm/guides/manage-networks.md

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import TabItem from '@theme/TabItem';
2828

2929
# Manage networks
3030

31-
Use MetaMask Connect EVM to detect, switch, and add EVM networks in your dapp. MetaMask Connect EVM supports `wallet_switchEthereumChain` for switching between networks, `wallet_addEthereumChain` for adding custom networks, and the `chainChanged` event for monitoring network changes in real time.
31+
Use MetaMask Connect EVM to detect, switch, and add EVM networks in your dapp. MetaMask Connect EVM provides `getChainId` for detecting the current network, `switchChain` for switching between networks, and the `chainChanged` event for monitoring network changes in real time.
3232

3333
With MetaMask Connect EVM:
3434

@@ -43,11 +43,15 @@ With MetaMask Connect EVM:
4343
</a>
4444
</p>
4545

46+
## Prerequisites
47+
48+
Follow the [JavaScript quickstart](../quickstart/javascript.md) or [Wagmi quickstart](../quickstart/wagmi.md) to install, initialize, and connect the EVM client.
49+
4650
## Detect and switch networks
4751

48-
With Vanilla JavaScript, implement network management directly using the
49-
[`eth_chainId`](../reference/json-rpc-api/eth_chainId.mdx) RPC method and
50-
[`chainChanged`](../reference/provider-api.md#chainchanged) provider event.
52+
With Vanilla JavaScript, implement network management using
53+
[`getChainId`](../reference/methods.md#getchainid) to get the current chain ID, and
54+
[`chainChanged`](../reference/provider-api.md#chainchanged) on the provider to track network switches.
5155

5256
With Wagmi, use the provided hooks for several network-related operations.
5357

@@ -72,82 +76,49 @@ const evmClient = await createEVMClient({
7276
},
7377
},
7478
})
75-
const provider = evmClient.getProvider()
7679

77-
// Get current chain ID
78-
async function getCurrentChain() {
79-
try {
80-
const chainId = await provider.request({
81-
method: 'eth_chainId',
82-
})
83-
console.log('Current chain ID:', chainId)
84-
return chainId
85-
} catch (err) {
86-
console.error('Error getting chain:', err)
87-
}
80+
// Get the current chain ID
81+
function getCurrentChain() {
82+
const chainId = evmClient.getChainId()
83+
console.log('Current chain ID:', chainId)
84+
return chainId
8885
}
8986

9087
// Listen for network changes
88+
const provider = evmClient.getProvider()
9189
provider.on('chainChanged', chainId => {
9290
console.log('Network changed to:', chainId)
9391
// We recommend reloading the page
9492
window.location.reload()
9593
})
9694
```
9795

98-
Switch networks using the
99-
[`wallet_switchEthereumChain`](../reference/json-rpc-api/wallet_switchEthereumChain.mdx)
100-
and [`wallet_addEthereumChain`](../reference/json-rpc-api/wallet_addEthereumChain.mdx)
101-
RPC methods:
96+
Switch networks using [`switchChain`](../reference/methods.md#switchchain). Pass the optional
97+
`chainConfiguration` so unknown chains are added to MetaMask in the same step:
10298

10399
```javascript
104100
// Network configurations
105101
const networks = {
106102
mainnet: {
107103
chainId: '0x1',
108-
name: 'Ethereum Mainnet',
109104
},
110105
optimism: {
111106
chainId: '0xA',
112-
name: 'Optimism',
113-
rpcUrls: ['https://mainnet.optimism.io'],
114-
nativeCurrency: {
115-
name: 'Ethereum',
116-
symbol: 'ETH',
117-
decimals: 18,
107+
chainConfiguration: {
108+
chainName: 'Optimism',
109+
nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
110+
rpcUrls: ['https://mainnet.optimism.io'],
111+
blockExplorerUrls: ['https://optimistic.etherscan.io'],
118112
},
119-
blockExplorerUrls: ['https://optimistic.etherscan.io'],
120113
},
121114
}
122115

123116
async function switchNetwork(networkKey) {
124-
const network = networks[networkKey]
125-
126117
try {
127-
// Try to switch to the network
128-
await provider.request({
129-
method: 'wallet_switchEthereumChain',
130-
params: [{ chainId: network.chainId }],
131-
})
118+
await evmClient.switchChain(networks[networkKey])
132119
} catch (err) {
133-
// If the error code is 4902, the network needs to be added
134-
if (err.code === 4902) {
135-
try {
136-
await provider.request({
137-
method: 'wallet_addEthereumChain',
138-
params: [
139-
{
140-
chainId: network.chainId,
141-
chainName: network.name,
142-
rpcUrls: network.rpcUrls,
143-
nativeCurrency: network.nativeCurrency,
144-
blockExplorerUrls: network.blockExplorerUrls,
145-
},
146-
],
147-
})
148-
} catch (addError) {
149-
console.error('Error adding network:', addError)
150-
}
120+
if (err.code === 4001) {
121+
console.log('User rejected network switch')
151122
} else {
152123
console.error('Error switching network:', err)
153124
}

0 commit comments

Comments
 (0)