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
A viem createPublicClient instance
A viem account from privateKeyToAccount or similar
A viem WalletClient, useful 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 iface = 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...')
address (string) - Contract address
Returns: Promise<string | undefined>
getTransactionReceipt
Returns the transaction receipt for a given transaction hash.
const receipt = await adapter.getTransactionReceipt('0x...')
hash (string) - 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')
address (string) - Contract address
slot (`0x${string}`) - Storage slot
Returns: Promise<`0x${string}`>
call
Executes a read-only call to a contract.
const result = await adapter.call({
to: '0x...',
data: '0x...'
})
txParams (CallParameters) - Transaction parameters
provider (PublicClient) - Optional provider override
Returns: Promise<string>
readContract
Reads from a contract function.
const result = await adapter.readContract({
address: '0x...',
abi: contractAbi,
functionName: 'balanceOf',
args: ['0x...']
})
address (string) - Contract address
abi (Abi) - Contract ABI
functionName (string) - Function to call
args (unknown[]) - Function arguments
provider (PublicClient) - Optional provider override
Returns: Promise<unknown>
getBlock
Returns block information for a given block tag.
const block = await adapter.getBlock('latest')
blockTag (BlockTag) - Block identifier
provider (PublicClient) - Optional provider override
Returns: Promise<Block>
getContract
Creates a contract instance with a compatible interface.
const contract = adapter.getContract('0x...', contractAbi)
const result = await contract.read.functionName()
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)
signer (Account | PrivateKey | WalletClient) - New signer
setProvider
Sets or updates the provider for the adapter.
const newProvider = createPublicClient({ chain: mainnet, transport: http() })
adapter.setProvider(newProvider)
provider (PublicClient) - New provider
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)
signer (Account | PrivateKey | WalletClient) - Signer source
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(/* ... */)
See Also