You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Always check for MetaMask installation before attempting to use Snap functionality.
108
-
2. Handle cases where MetaMask is not installed or doesn't support Snaps gracefully.
109
-
3. Provide clear feedback to users if MetaMask or Snap support is missing.
110
-
4. Consider implementing a retry mechanism for provider detection to account for potential timing issues.
111
-
112
-
By following these steps and using the provided functions, you can reliably detect MetaMask and verify Snap support in your application.
113
-
114
-
115
-
This can be used as follow to check for Metamask Snap Support. This will check for MetaMask presence, and the support of Snap in the installed MetaMask version if any. If there is no MetaMask installed or the version of MetaMask does not support snap
116
-
117
-
```typescript
118
-
let isMetaMaskInstallRequired = false;
119
-
let provider = null;
120
-
try {
121
-
provider = await detectMetamaskSupport(window);
122
-
// Use the new detection method
123
-
124
-
if (provider && (await isSupportSnap(provider))) {
125
-
isMetaMaskInstallRequired = provider === null;
126
-
} else {
127
-
isMetaMaskInstallRequired = true;
128
-
}
129
-
} catch (err) {
130
-
isMetaMaskInstallRequired = true;
131
-
}
132
-
```
133
-
134
-
In case MetaMask is not installed (e.g. `isMetaMaskInstallRequired=true` we can prompt the user to install MetaMask.
135
-
136
-
In case MetaMask is installed, we can prompt the user to install the Snap
137
-
138
-
```typescript
139
-
provider
140
-
.request({
141
-
method: 'wallet_requestSnaps',
142
-
params: {
143
-
[snapId]: { version: snapVersion },
144
-
},
145
-
})
146
-
.then(() => {
147
-
// The snap has been installed proceed accordingly.
148
-
})
149
-
.catch(() => {
150
-
// The snap has not been installed (user rejected the installation)
151
-
});
152
-
```
153
-
## Snap permissions
154
-
155
-
### Snap is not properly approved.
156
-
157
-
Guide users through the process of approving the Snap in MetaMask. If they deny the connection, provide a retry option.
158
-
159
-
### Snap ID error
160
-
161
-
Ensure you're using the correct Snap ID for the StarkNet Snap. Incorrect IDs will result in failed connections.
162
-
The Starknet Snap ID is the name of the npm package of the snap: `@consensys/starknet-snap`.
163
-
164
-
165
-
## Best practices
166
-
167
-
### 1. Graceful error handling
168
-
169
-
Provide clear feedback to the user, such as offering a retry option or showing a message that StarkNet Snap needs to be installed.
170
-
171
-
```javascript
172
-
consthandleConnect=async () => {
173
-
try {
174
-
constres=awaitconnect();
175
-
if (!res) {
176
-
console.log('No wallet connected');
177
-
}
178
-
} catch (error) {
179
-
console.error('Failed to connect to wallet:', error);
180
-
alert('An error occurred while connecting to the wallet. Please try again.');
181
-
}
182
-
};
183
-
```
184
-
185
-
### 2. Detect and Handle account changes
186
-
187
-
Ensure that you detect when the user switches accounts or changes networks. MetaMask emits events like `accountsChanged` and `chainChanged` that you can listen for and handle appropriately.
`get-starknet` supports connecting to multiple wallets. Provide users with options to select the wallet they want to connect, if applicable. You can specify wallet options when calling connect().
204
-
205
-
```javascript
206
-
consthandleConnect=async () => {
207
-
constoptions= {
208
-
modalMode:'alwaysAsk', // Force the wallet selection modal to appear
0 commit comments