CoW SDK v7 is 99% backward compatible with v6. The primary change is the introduction of adapters that allow the SDK to work with different Web3 libraries (viem, ethers v6, and ethers v5).
In v7, you must set an adapter corresponding to the library you use: ViemAdapter, EthersV6Adapter, or EthersV5Adapter.
The main difference is creating and passing an adapter when instantiating SDK classes.
v6
v7
import { SupportedChainId, TradingSdk } from '@cowprotocol/cow-sdk'const options = {}const sdk = new TradingSdk({ chainId: SupportedChainId.SEPOLIA, appCode: 'YOUR_APP_CODE',}, options)
import { SupportedChainId, TradingSdk } from '@cowprotocol/cow-sdk'import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'import { createPublicClient, http, privateKeyToAccount } from 'viem'import { sepolia } from 'viem/chains'// NEW: Instantiate and set adapterconst adapter = new ViemAdapter({ provider: createPublicClient({ chain: sepolia, transport: http('YOUR_RPC_URL') }), // You can also set `walletClient` instead of `signer` using `useWalletClient` from wagmi signer: privateKeyToAccount('YOUR_PRIVATE_KEY' as `0x${string}`)})const options = {}const sdk = new TradingSdk({ chainId: SupportedChainId.SEPOLIA, appCode: 'YOUR_APP_CODE',}, options, adapter)
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'import { http, createPublicClient, privateKeyToAccount } from 'viem'import { sepolia } from 'viem/chains'const account = privateKeyToAccount('YOUR_PRIVATE_KEY' as `0x${string}`)const transport = http('YOUR_RPC_URL')const provider = createPublicClient({ chain: sepolia, transport })// You can also set `walletClient` instead of `signer` using `useWalletClient` from wagmiconst adapter = new ViemAdapter({ provider, signer: account })
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'import { JsonRpcProvider, Wallet } from 'ethers'const provider = new JsonRpcProvider('YOUR_RPC_URL')const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)const adapter = new EthersV6Adapter({ provider, signer: wallet })
import { EthersV5Adapter } from '@cowprotocol/sdk-ethers-v5-adapter'import { ethers } from 'ethers'const provider = new ethers.providers.JsonRpcProvider('YOUR_RPC_URL')const wallet = new ethers.Wallet('YOUR_PRIVATE_KEY', provider)const adapter = new EthersV5Adapter({ provider, signer: wallet })
Most SDK packages (e.g., MetadataApi, OrderBookApi, BridgingSdk) accept an adapter parameter. However, you can also set a global adapter that will be used by all SDK instances:
import { setGlobalAdapter, CowShedSdk, TradingSdk } from '@cowprotocol/cow-sdk'import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'const adapter = new ViemAdapter({ // ... adapter configuration})// Set global adaptersetGlobalAdapter(adapter)// Now all SDK instances will use this adapterconst cowShedSdk = new CowShedSdk()const tradingSdk = new TradingSdk({ chainId: SupportedChainId.MAINNET, appCode: 'YOUR_APP_CODE',})
When you provide an adapter directly to an SDK constructor, it takes precedence over the global adapter.
When integrating with wagmi, you need to bind the SDK to your app’s account state. When the account or network changes in the wallet, update the SDK accordingly.
// cowSdk.tsimport { SupportedChainId, TradingSdk } from '@cowprotocol/cow-sdk'export const tradingSdk = new TradingSdk({ chainId: SupportedChainId.MAINNET, // Default chain, can be changed JIT appCode: 'CoWSdkReactExampleWagmi',})
// useBindCoWSdkToWagmi.tsimport { useAccount, usePublicClient, useWalletClient } from 'wagmi'import { useEffect, useState } from 'react'import { ViemAdapter, ViemAdapterOptions } from '@cowprotocol/sdk-viem-adapter'import { tradingSdk } from '../cowSdk.ts'import { setGlobalAdapter } from '@cowprotocol/cow-sdk'export function useBindCoWSdkToWagmi(): boolean { const account = useAccount() const { chainId } = account const { data: walletClient } = useWalletClient() const publicClient = usePublicClient() const [isSdkReady, setIsSdkReady] = useState(false) /** * Sync Trading SDK with wagmi account state (chainId and signer) */ useEffect(() => { if (!walletClient || !chainId) { setIsSdkReady(false) return } setGlobalAdapter( new ViemAdapter({ provider: publicClient, walletClient, } as unknown as ViemAdapterOptions), ) tradingSdk.setTraderParams({ chainId }) setIsSdkReady(true) return () => { setIsSdkReady(false) } }, [publicClient, walletClient, chainId]) return isSdkReady}
Make sure to check that isSdkReady is true before using the SDK in your components. This ensures the adapter is properly configured with the user’s wallet and network.