Skip to main content

Viem Adapter

The Viem Adapter provides integration with the viem library, enabling you to use all CoW Protocol SDK packages with viem clients and accounts.

Installation

npm install @cowprotocol/sdk-viem-adapter

Constructor

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 })

const adapter = new ViemAdapter({ provider, signer: account })

Parameters

provider
PublicClient
required
A viem PublicClient instance created with createPublicClient.
signer
Account | PrivateKey
Optional viem account instance or private key string. Can be created with privateKeyToAccount or obtained from wagmi hooks.
walletClient
WalletClient
Optional WalletClient instance. Use this instead of signer when working with wagmi’s useWalletClient hook.

Properties

signer

Returns the ViemSignerAdapter instance. Throws an error if no signer was provided.
const signerAdapter = adapter.signer

utils

Utility methods for working with viem types and contracts.
const interface = adapter.utils.createInterface(abi)

TypedDataVersionedSigner

Signer class for EIP-712 typed data with version support.

TypedDataV3Signer

Signer class for EIP-712 typed data version 3 (legacy).

IntChainIdTypedDataV4Signer

Signer class for EIP-712 typed data version 4 with integer chain ID handling.

Methods

getChainId

Returns the chain ID from the connected provider.
const chainId = await adapter.getChainId()
Returns: Promise<number>

getCode

Returns the bytecode at a given address.
const code = await adapter.getCode('0x...')
Parameters:
  • address (string): The contract address
Returns: Promise<string | undefined>

getTransactionReceipt

Returns the transaction receipt for a given transaction hash.
const receipt = await adapter.getTransactionReceipt('0x...')
Parameters:
  • hash (string): The transaction hash
Returns: Promise<TransactionReceipt | null>

getStorageAt

Returns the value from a storage position at a given address.
const value = await adapter.getStorageAt('0x...', '0x0')
Parameters:
  • address (Address): The contract address
  • slot (0x${string}): The storage slot
Returns: Promise<0x${string}>

call

Executes a read-only call to a contract.
const result = await adapter.call({
  to: '0x...',
  data: '0x...'
})
Parameters:
  • txParams (TransactionParams): Transaction parameters
  • provider (PublicClient, optional): Override provider
Returns: Promise<string>

readContract

Reads from a contract function.
const result = await adapter.readContract({
  address: '0x...',
  abi: contractAbi,
  functionName: 'balanceOf',
  args: ['0x...']
})
Parameters:
  • address (string): Contract address
  • abi (Abi): Contract ABI
  • functionName (string): Function name to call
  • args (ContractValue[], optional): Function arguments
  • provider (PublicClient, optional): Override provider
Returns: Promise<unknown>

getBlock

Returns block information for a given block tag.
const block = await adapter.getBlock('latest')
Parameters:
  • blockTag (BlockTag): Block tag (e.g., ‘latest’, ‘pending’, block number)
  • provider (PublicClient, optional): Override provider
Returns: Promise<Block>

getContract

Creates a contract instance with a compatible interface.
const contract = adapter.getContract('0x...', contractAbi)
const result = await contract.read.functionName()
Parameters:
  • address (string): Contract address
  • abi (Abi): Contract ABI
Returns: GenericContract

setSigner

Sets or updates the signer for the adapter.
const newAccount = privateKeyToAccount('0x...')
adapter.setSigner(newAccount)
Parameters:
  • signer (Account | PrivateKey): New signer to use

setProvider

Sets or updates the provider for the adapter.
const newProvider = createPublicClient({ chain: mainnet, transport: http() })
adapter.setProvider(newProvider)
Parameters:
  • provider (PublicClient): New provider to use
Returns: PublicClient

signerOrNull

Returns the signer adapter or null if not set.
const signer = adapter.signerOrNull()
if (signer) {
  // Use signer
}
Returns: ViemSignerAdapter | null

createSigner

Creates a new signer adapter from an account or private key.
const signerAdapter = adapter.createSigner(account)
Parameters:
  • signer (Account | PrivateKey | ViemSignerAdapter): Signer to wrap
Returns: ViemSignerAdapter

Usage Examples

With Wagmi

import { useWalletClient, usePublicClient } from 'wagmi'
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'

function MyComponent() {
  const publicClient = usePublicClient()
  const { data: walletClient } = useWalletClient()
  
  const adapter = new ViemAdapter({
    provider: publicClient,
    walletClient: walletClient
  })
  
  // Use adapter with SDK
}

With Private Key

import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
import { http, createPublicClient, privateKeyToAccount } from 'viem'
import { mainnet } from 'viem/chains'

const account = privateKeyToAccount('0x...')
const provider = createPublicClient({
  chain: mainnet,
  transport: http('https://eth.llamarpc.com')
})

const adapter = new ViemAdapter({ provider, signer: account })

With CoW SDK

import { CowSdk, SupportedChainId } from '@cowprotocol/cow-sdk'
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 })
const adapter = new ViemAdapter({ provider, signer: account })

const sdk = new CowSdk({
  chainId: SupportedChainId.SEPOLIA,
  adapter,
  tradingOptions: {
    traderParams: {
      appCode: 'YOUR_APP_CODE',
    },
    options: {
      chainId: SupportedChainId.SEPOLIA,
    },
  },
})

const orderId = await sdk.trading.postSwapOrder(parameters)

See Also

Build docs developers (and LLMs) love