Skip to main content
This guide demonstrates how to integrate the CoW Protocol SDK with Wagmi and Viem for a production-ready React application with proper wallet management.

Why Wagmi?

Wagmi provides:
  • Easy wallet connection with multiple providers
  • Automatic account and network management
  • React hooks for blockchain interactions
  • TypeScript support
  • Better developer experience

Installation

Setup Wagmi

Configure Wagmi with your app settings:
wagmi.ts

Setup CoW SDK

Create a Trading SDK instance:
cowSdk.ts

Bind SDK to Wagmi Hook

Create a custom hook to sync the SDK with Wagmi’s wallet state:
useBindCoWSdkToWagmi.ts

Swap Component

Create a swap form that uses Wagmi hooks:
SwapForm.tsx

Main App Component

App.tsx

Main Entry Point

main.tsx

Token Approval with Wagmi

Handle token approvals using the SDK:

Key Benefits

  1. Automatic Network Switching: Wagmi handles network changes automatically
  2. Multi-Wallet Support: Users can connect with various wallets
  3. TypeScript Safety: Full type safety with Viem
  4. React Query Integration: Efficient data fetching and caching
  5. Better UX: Professional wallet connection UI

Connect Wallet Button

Add a proper wallet connection button:

Network Switching

Handle network switching gracefully:

Next Steps