Wagmi is a TypeScript library for Ethereum development that provides a collection of React Hooks for interacting with Ethereum-compatible blockchains. It’s built on top of viem and offers a modern, developer-friendly API for building dapps.The @coinbase/cdp-wagmi package is a bridge that connects Coinbase’s CDP (Coinbase Developer Platform) Wallets to the wagmi ecosystem—bringing CDP-powered wallet functionality into React apps using wagmi hooks.You’ll learn how to install the package, set up the provider, and render your first component.
Check out the CDP Web SDK reference for comprehensive method signatures, types, and examples.
Configure your WagmiProvider with the CDPEmbeddedWalletConnector.CDPEmbeddedWalletConnector provides the necessary context for Wagmi to work correctly with the CDP Frontend SDK. The providerConfig must be provided and is responsible for configuring the EIP-1193 provider’s transports which are used to broadcast non-Base transactions.
main.tsx
Report incorrect code
Copy
Ask AI
import React from 'react';import ReactDOM from 'react-dom/client';import { App } from './App'; // Your main App componentimport { Config } from '@coinbase/cdp-core';import { createCDPEmbeddedWalletConnector } from '@coinbase/cdp-wagmi';import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import { http } from "viem";import { baseSepolia, base } from 'viem/chains';import { WagmiProvider, createConfig, http } from 'wagmi';// Your CDP configconst cdpConfig: Config = { projectId: "your-project-id", // Copy your Project ID here. ethereum: { createOnLogin: "eoa" }}const connector = createCDPEmbeddedWalletConnector({ cdpConfig: cdpConfig, providerConfig: { chains: [base, baseSepolia], transports: { [base.id]: http(), [baseSepolia.id]: http() } }});const wagmiConfig = createConfig({ connectors: [connector], chains: [base, baseSepolia], transports: { [base.id]: http(), [baseSepolia.id]: http(), },});const queryClient = new QueryClient(); // For use with react-queryReactDOM.createRoot(document.getElementById('root')!).render( <React.StrictMode> <CDPHooksProvider config={cdpConfig}> <WagmiProvider config={wagmiConfig}> <QueryClientProvider client={queryClient}> <App /> </QueryClientProvider> </WagmiProvider> </CDPHooksProvider> </React.StrictMode>,);
If you’d like to use CDP’s pre-built UI components, install @coinbase/cdp-react and use the CDPReactProvider component instead of CDPHooksProvider.
Before using Wagmi hooks, users need to be signed in using either CDP React components or hooks. When the user verifies their OTP and is signed in, they will automatically be connected with wagmi.You can use either approach:Option 1: Using React Components
Report incorrect code
Copy
Ask AI
import { AuthButton } from "@coinbase/cdp-react/components/AuthButton";<AuthButton /> // Users sign in via this button
Option 2: Using React Hooks
Report incorrect code
Copy
Ask AI
import { useSignInWithEmail, useVerifyEmailOTP } from "@coinbase/cdp-hooks";// Implement your own sign-in flow using these hooks
Once signed in, the user’s wallet is automatically available to wagmi.