If you are interested in using WAGMI in your brand new react application, follow the instructions here to initialize your app with the CLI. Otherwise, follow the instructions below.
Next you will need to create a wagmi config file:
In this example you can find it under wagmi-config.ts
Here you need to create the default WAGMI config, and under connector, add a
Magic Eden Wallet Injector.
This is necessary to have WAGMI pick up the Magic Eden Wallet option,
and add it to the Wallet Options a user can choose from.
import { http, createConfig } from"wagmi";import { mainnet, sepolia } from"wagmi/chains";import { coinbaseWallet, injected } from"wagmi/connectors";// Extend the global Window interface directly in this filedeclare global {interfaceWindow { magicEden?: { ethereum?: {request: (args: { method:string; params?:any[] }) =>Promise<any>;on: (event:string,handler: (...args:any[]) =>void) =>void;removeAllListeners: () =>void;// Add other methods and properties as needed }; }; }}// Define the Magic Eden Wallet ConnectorconstME_CONNECT=injected({target() {constprovider=typeof window !=="undefined"?window.magicEden?.ethereum :undefined;return { id:"Magic Eden", name:"Magic Eden", provider: provider asany,// We still use `any` here to bypass strict type checking for the provider from WAGMI }; },});// Create the configurationexportconstconfig=createConfig({ chains: [mainnet, sepolia], connectors: [ME_CONNECT,coinbaseWallet({ appName:"Create Wagmi" })], ssr:true, transports: { [mainnet.id]:http(), [sepolia.id]:http(), },});// Declare the module to register the config type with wagmideclaremodule"wagmi" {interfaceRegister { config:typeof config; }}
Setup WAGMI Provider and QueryClient
Once you have the config setup, you need to wrap your page or app with
WagmiProvider using the config object we previously created, and QueryClient.
If using NextJS 14 with App Router, make sure you add use client; at the top of the file.
Next create a component that detects and displays all wallets available.
This code creates a WalletOptions component that uses the useConnect hook from the wagmi library to get a list of available wallet connectors. It then maps over these connectors to create a WalletOption component for each one.
The WalletOption component takes a connector and an onClick function as props. It uses the useState and useEffect hooks from React to check if the provider for the given connector is ready. If the provider is ready, it enables a button that displays the name of the connector and calls the onClick function when clicked.
This allows users to see all available wallet options and connect to their chosen wallet.
Now create a component that handles a wallet being connected and displays the account
This Account component handles the display of the connected wallet's account information. It uses the useAccount hook from the wagmi library to get the address of the connected wallet. It also uses the useDisconnect hook to provide a function that can be called to disconnect the wallet.
The useEnsName and useEnsAvatar hooks are used to get the ENS name and avatar for the connected wallet, if they exist. The ENS name and avatar are then displayed along with the wallet's address.
A button is then provided to allow the user to disconnect their wallet. When this button is clicked, the disconnect function provided by the useDisconnect hook is called.
This component is designed to be used once a wallet has been connected. If no wallet is connected, it will not display anything.
Now, create a component that detects when a wallet is present, and if none are connected
displays all available wallet options.
This ConnectWallet component checks if a wallet is already connected using the isConnected property from the useAccount hook. If a wallet is connected, it returns the Account component which displays the connected wallet's account information and provides a disconnect button. If no wallet is connected, it returns the WalletOptions component which displays all available wallet options for the user to connect to.
Finally, add your ConnectWallet component to your page, and you should now be able to see all wallet options. Select Magic Eden Wallet and use it to transact.