Connect to MetaMask using Wagmi
Wagmi is a powerful and efficient React Hooks library designed to streamline dapp development by simplifying Ethereum interactions.
You can integrate MetaMask SDK into your dapp alongside Wagmi, using the MetaMask connector with Wagmi, to enable your users to seamlessly and securely connect to the MetaMask browser extension and MetaMask Mobile.
Prerequisites
- Ensure you have a basic understanding of Ethereum smart contracts and React Hooks.
- Set up a project with Wagmi.
- Create an Infura API key and allowlist to make read-only requests.
Steps
1. Configure MetaMask SDK
In your Wagmi project, configure MetaMask SDK with the proper SDK options.
const MetaMaskOptions = {
dappMetadata: {
name: "Example Wagmi dapp",
},
infuraAPIKey: "YOUR-API-KEY",
// Other options.
}
Dapp metadata
Specify the dappMetadata
option to help
identify your dapp within the MetaMask ecosystem.
This option is required when configuring the MetaMask connector with Wagmi.
Infura API key
We recommend specifying the infuraAPIKey
option to make read-only requests using the Infura API.
Read more about the benefits of using the Infura API with Wagmi.
Universal links
We recommend using universal links instead of deeplinks to avoid issues on iOS.
Thus, do not enable the useDeeplink
option.
Using universal links ensures a smoother transition for users accessing your dapp from mobile
devices, providing a better user experience compared to traditional deeplinking methods.
2. Configure Wagmi with the MetaMask connector
Configure Wagmi to include MetaMask as a connector and specify the Ethereum chains your dapp will support.
Use the MetaMaskOptions
you created in the previous step when adding the metaMask
connector.
For example:
import { createConfig, http } from "wagmi"
import { mainnet, sepolia } from "wagmi/chains"
import { metaMask } from "wagmi/connectors"
const MetaMaskOptions = {
dappMetadata: {
name: "Example Wagmi dapp",
},
infuraAPIKey: "YOUR-API-KEY",
// Other options.
}
export const config = createConfig({
chains: [mainnet, sepolia],
connectors: [
metaMask(MetaMaskOptions),
// Other connectors
],
transports: {
[mainnet.id]: http(),
[sepolia.id]: http(),
},
})