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

Constructor

Parameters

PublicClient
required
A viem PublicClient instance created with createPublicClient.
Account | PrivateKey
Optional viem account instance or private key string. Can be created with privateKeyToAccount or obtained from wagmi hooks.
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.

utils

Utility methods for working with viem types and contracts.

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.
Returns: Promise<number>

getCode

Returns the bytecode at a given address.
Parameters:
  • address (string): The contract address
Returns: Promise<string | undefined>

getTransactionReceipt

Returns the transaction receipt for a given transaction hash.
Parameters:
  • hash (string): The transaction hash
Returns: Promise<TransactionReceipt | null>

getStorageAt

Returns the value from a storage position at a given address.
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.
Parameters:
  • txParams (TransactionParams): Transaction parameters
  • provider (PublicClient, optional): Override provider
Returns: Promise<string>

readContract

Reads from a contract function.
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.
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.
Parameters:
  • address (string): Contract address
  • abi (Abi): Contract ABI
Returns: GenericContract

setSigner

Sets or updates the signer for the adapter.
Parameters:
  • signer (Account | PrivateKey): New signer to use

setProvider

Sets or updates the provider for the adapter.
Parameters:
  • provider (PublicClient): New provider to use
Returns: PublicClient

signerOrNull

Returns the signer adapter or null if not set.
Returns: ViemSignerAdapter | null

createSigner

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

Usage Examples

With Wagmi

With Private Key

With CoW SDK

See Also