Skip to main content

Overview

The CDP SDK is compatible with viem in a couple ways:
  1. viem Local Accounts can be set as the owner of a CDP Smart Account. See the Signer Example below.
  2. CDP accounts can be wrapped into viem Custom Accounts using the toAccount function. See the Wallet Client Example below.

Signer Example

You can create a viem Local Account and set it as the owner of a CDP Smart Account.
main.ts

Wallet Client Example

You can pass a CDP account to the toAccount function and get back a viem Custom Account, which can be used wherever viem expects an account. For example, you can create a viem Wallet Client from this account and use the Wallet Client to send transactions. The following code snippet demonstrates how to use a viem Wallet Client to send a transaction. If you followed the quickstart, you will have a project that you can use to follow along with the example below.

1. Install viem

Install viem in your project:

2. Create a Wallet Client from a CDP account

Here we use toAccount from viem/accounts to wrap a CDP account in a viem Custom Account, which is immediately passed to the createWalletClient function to create a viem Wallet Client.
main.ts
  1. Send a transaction with the Wallet Client
Next, use the Wallet Client to send a transaction, after requesting some testnet ETH from the faucet.
main.ts
  1. Putting it all together
Here’s the complete code for the example above.
main.ts
  • v2 Wallet Accounts: Read more about the types of accounts and networks we support.
  • viem: Learn from the official docs on how to get started developing with viem.