Creates a new CDP EVM account.
Optional
options: CreateServerAccountOptions = {}Optional parameters for creating the account.
Options for creating an EVM server account.
A promise that resolves to the newly created account.
Creates a new CDP EVM smart account.
Parameters for creating the smart account.
Options for creating an EVM smart account.
Optional
idempotencyKey?: stringThe idempotency key.
Optional
name?: stringThe name of the account.
The owner of the account.
A promise that resolves to the newly created smart account.
const account = await cdp.evm.createAccount();
const smartAccount = await cdp.evm.createSmartAccount({
owner: account,
});
// See https://viem.sh/docs/accounts/local/privateKeyToAccount
const privateKey = generatePrivateKey();
const account = privateKeyToAccount(privateKey);
const smartAccount = await client.evm.createSmartAccount({
owner: account,
});
const idempotencyKey = uuidv4();
// First call
await cdp.evm.createSmartAccount({
owner: account,
idempotencyKey,
});
// Second call with the same idempotency key will return the same smart account
await cdp.evm.createSmartAccount({
owner: account,
idempotencyKey,
Creates a quote for a swap between two tokens on an EVM network.
The options for creating a swap quote.
A promise that resolves to the swap quote result or a response indicating that liquidity is unavailable.
const swapQuote = await cdp.evm.createSwapQuote({
network: "ethereum",
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
fromAmount: BigInt("1000000000000000000"), // 1 WETH
taker: "0x1234567890123456789012345678901234567890"
});
Exports a CDP EVM account's private key. It is important to store the private key in a secure place after it's exported.
Parameters for exporting the account.
Options for exporting an EVM server account.
A promise that resolves to the exported account’s 32-byte private key as a hex string, without the "0x" prefix.
Gets a CDP EVM account.
Parameters for getting the account.
Either address
or name
must be provided.
If both are provided, lookup will be done by address
and name
will be ignored.
Options for getting an EVM account.
A promise that resolves to the account.
Gets a CDP EVM account, or creates one if it doesn't exist.
Parameters for getting or creating the account.
Options for getting an EVM account, or creating one if it doesn't exist.
A promise that resolves to the account.
Gets a CDP EVM smart account, or creates one if it doesn't exist. This method first attempts to retrieve an existing smart account with the given parameters. If no account exists, it creates a new one with the specified owner.
Configuration options for getting or creating the smart account.
Options for getting an EVM account, or creating one if it doesn't exist.
The name of the account.
The owner of the account.
A promise that resolves to the retrieved or newly created smart account.
Gets a CDP EVM smart account.
Parameters for getting the smart account.
Either address
or name
must be provided.
If both are provided, lookup will be done by address
and name
will be ignored.
Options for getting an EVM smart account.
Optional
address?: `0x${string}`The address of the account.
Optional
name?: stringThe name of the account.
The owner of the account.
A promise that resolves to the smart account.
Gets the price for a swap between two tokens on an EVM network.
The options for getting a swap price.
A promise that resolves to the swap price result or a response indicating that liquidity is unavailable.
const price = await cdp.evm.getSwapPrice({
network: "ethereum-mainnet",
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
fromAmount: BigInt("1000000000000000000"), // 1 WETH
taker: "0x1234567890123456789012345678901234567890"
});
Gets a user operation for a smart account by user operation hash.
Parameters for getting the user operation.
Options for getting a user operation.
The smart account.
Subject to breaking changes. A function that returns a network-scoped smart account.
The smart account's address.
Funds an EVM account with the specified token amount.
Gets a user operation by its hash.
List the token balances of an account.
Optional
name?: stringThe name of the smart account.
Array of accounts that own and can sign for the smart account (currently only supports one owner but will be extended to support multiple owners in the future).
Gets a quote to fund an EVM account.
Creates a swap quote without executing the transaction. This is useful when you need to get swap details before executing the swap. The taker is automatically set to the smart account's address.
const swapQuote = await smartAccount.quoteSwap({
network: "base",
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
});
if (swapQuote.liquidityAvailable) {
console.log(`Can swap for ${swapQuote.toAmount} USDC`);
}
Requests funds from an EVM faucet.
Sends a user operation.
Executes a token swap on the specified network via a user operation. This method handles all the steps required for a swap, including Permit2 signatures if needed. The taker is automatically set to the smart account's address.
// First create a swap quote
const swapQuote = await cdp.evm.createSwapQuote({
network: "base",
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
taker: smartAccount.address,
signerAddress: smartAccount.owners[0].address
});
// Check if liquidity is available
if (!swapQuote.liquidityAvailable) {
console.error("Insufficient liquidity for swap");
return;
}
// Execute the swap
const { userOpHash } = await smartAccount.swap({
swapQuote: swapQuote
});
console.log(`Swap executed with user op hash: ${userOpHash}`);
// Create and execute swap in one call
const { userOpHash } = await smartAccount.swap({
network: "base",
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
});
console.log(`Swap executed with user op hash: ${userOpHash}`);
Transfer an amount of a token from an account to another account.
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: 10000n, // equivalent to 0.01 USDC
token: "usdc",
network: "base-sepolia",
});
Using parseUnits to specify USDC amount
import { parseUnits } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseUnits("0.01", 6), // USDC uses 6 decimal places
token: "usdc",
network: "base-sepolia",
});
Transfer ETH
import { parseEther } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseEther("0.000001"),
token: "eth",
network: "base-sepolia",
});
Using a contract address
import { parseEther } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseEther("0.000001"),
token: "0x4200000000000000000000000000000000000006", // WETH on Base Sepolia
network: "base-sepolia",
});
Transfer to another account
const sender = await cdp.evm.createAccount({ name: "Sender" });
const receiver = await cdp.evm.createAccount({ name: "Receiver" });
const { userOpHash } = await sender.transfer({
to: receiver,
amount: 10000n, // equivalent to 0.01 USDC
token: "usdc",
network: "base-sepolia",
});
Identifier for the smart account type.
Waits for a user operation to complete or fail.
// Send a user operation and get the user operation hash
const { userOpHash } = await smartAccount.sendUserOperation({
network: "base-sepolia",
calls: [
{
to: "0x0000000000000000000000000000000000000000",
value: parseEther("0.000001"),
data: "0x",
},
],
});
// Wait for the user operation to complete or fail
const result = await smartAccount.waitForUserOperation({
userOpHash: userOp.userOpHash,
});
Waits for a fund operation to complete and returns the transfer receipt.
The options for the wait for fund operation.
The ID of the transfer to wait for.
Optional
waitOptions?: WaitOptionsOptional options for the wait operation.
A promise that resolves to the completed transfer receipt containing details about the funding operation.
The user operation hash.
A promise that resolves to the user operation.
Imports a CDP EVM account from an external source.
Parameters for importing the account.
Options for importing an EVM server account.
A promise that resolves to the imported account.
const account = await cdp.evm.importAccount({
privateKey: "0x123456"
});
const account = await cdp.evm.importAccount({
privateKey: "0x123456",
name: "MyAccount"
});
const idempotencyKey = uuidv4();
// First call
await cdp.evm.importAccount({
privateKey: "0x123456",
idempotencyKey,
});
// Second call with the same idempotency key will return the same account
await cdp.evm.importAccount({
privateKey: "0x123456"
idempotencyKey,
});
Lists CDP EVM accounts.
Optional
options: ListServerAccountsOptions = {}Optional parameters for listing the accounts.
Options for listing EVM accounts.
A promise that resolves to an array of accounts, and a token to paginate through the accounts.
Lists CDP EVM smart accounts.
Parameters for listing the smart accounts.
Options for listing EVM smart accounts.
A promise that resolves to an array of smart accounts, and a token to paginate through the smart accounts.
Lists CDP EVM token balances.
Parameters for listing the token balances.
Options for listing EVM token balances.
The address of the account.
The network.
Optional
pageSize?: numberThe page size to paginate through the token balances.
Optional
pageToken?: stringThe page token to paginate through the token balances.
A promise that resolves to an array of token balances, and a token to paginate through the token balances.
const tokenBalances = await cdp.evm.listTokenBalances({
address: "0x1234567890123456789012345678901234567890",
network: "base-sepolia",
});
With pagination
let page = await cdp.evm.listTokenBalances({
address: "0x1234567890123456789012345678901234567890",
network: "base-sepolia",
});
while (page.nextPageToken) {
page = await cdp.evm.listTokenBalances({
address: "0x1234567890123456789012345678901234567890",
network: "base-sepolia",
pageToken: page.nextPageToken,
});
Prepares a user operation for a smart account.
Parameters for preparing the user operation.
Options for preparing a user operation.
The calls.
The network.
Optional
paymasterUrl?: stringThe paymaster URL.
The smart account.
Subject to breaking changes. A function that returns a network-scoped smart account.
The smart account's address.
Funds an EVM account with the specified token amount.
Gets a user operation by its hash.
List the token balances of an account.
Optional
name?: stringThe name of the smart account.
Array of accounts that own and can sign for the smart account (currently only supports one owner but will be extended to support multiple owners in the future).
Gets a quote to fund an EVM account.
Creates a swap quote without executing the transaction. This is useful when you need to get swap details before executing the swap. The taker is automatically set to the smart account's address.
const swapQuote = await smartAccount.quoteSwap({
network: "base",
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
});
if (swapQuote.liquidityAvailable) {
console.log(`Can swap for ${swapQuote.toAmount} USDC`);
}
Requests funds from an EVM faucet.
Sends a user operation.
Executes a token swap on the specified network via a user operation. This method handles all the steps required for a swap, including Permit2 signatures if needed. The taker is automatically set to the smart account's address.
// First create a swap quote
const swapQuote = await cdp.evm.createSwapQuote({
network: "base",
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
taker: smartAccount.address,
signerAddress: smartAccount.owners[0].address
});
// Check if liquidity is available
if (!swapQuote.liquidityAvailable) {
console.error("Insufficient liquidity for swap");
return;
}
// Execute the swap
const { userOpHash } = await smartAccount.swap({
swapQuote: swapQuote
});
console.log(`Swap executed with user op hash: ${userOpHash}`);
// Create and execute swap in one call
const { userOpHash } = await smartAccount.swap({
network: "base",
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
});
console.log(`Swap executed with user op hash: ${userOpHash}`);
Transfer an amount of a token from an account to another account.
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: 10000n, // equivalent to 0.01 USDC
token: "usdc",
network: "base-sepolia",
});
Using parseUnits to specify USDC amount
import { parseUnits } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseUnits("0.01", 6), // USDC uses 6 decimal places
token: "usdc",
network: "base-sepolia",
});
Transfer ETH
import { parseEther } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseEther("0.000001"),
token: "eth",
network: "base-sepolia",
});
Using a contract address
import { parseEther } from "viem";
const { userOpHash } = await sender.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseEther("0.000001"),
token: "0x4200000000000000000000000000000000000006", // WETH on Base Sepolia
network: "base-sepolia",
});
Transfer to another account
const sender = await cdp.evm.createAccount({ name: "Sender" });
const receiver = await cdp.evm.createAccount({ name: "Receiver" });
const { userOpHash } = await sender.transfer({
to: receiver,
amount: 10000n, // equivalent to 0.01 USDC
token: "usdc",
network: "base-sepolia",
});
Identifier for the smart account type.
Waits for a user operation to complete or fail.
// Send a user operation and get the user operation hash
const { userOpHash } = await smartAccount.sendUserOperation({
network: "base-sepolia",
calls: [
{
to: "0x0000000000000000000000000000000000000000",
value: parseEther("0.000001"),
data: "0x",
},
],
});
// Wait for the user operation to complete or fail
const result = await smartAccount.waitForUserOperation({
userOpHash: userOp.userOpHash,
});
Waits for a fund operation to complete and returns the transfer receipt.
The options for the wait for fund operation.
The ID of the transfer to wait for.
Optional
waitOptions?: WaitOptionsOptional options for the wait operation.
A promise that resolves to the completed transfer receipt containing details about the funding operation.
A promise that resolves to the user operation hash.
Requests funds from an EVM faucet.
Parameters for requesting funds from the EVM faucet.
Options for requesting funds from an EVM faucet.
A promise that resolves to the transaction hash.
Signs an EVM transaction and sends it to the specified network using the Coinbase API. This method handles nonce management and gas estimation automatically.
Configuration options for sending the transaction.
A promise that resolves to the transaction hash.
Sending an RLP-encoded transaction
import { parseEther, serializeTransaction } from "viem";
import { baseSepolia } from "viem/chains";
const { transactionHash } = await cdp.evm.sendTransaction({
address: account.address,
transaction: serializeTransaction({
to: "0x4252e0c9A3da5A2700e7d91cb50aEf522D0C6Fe8",
value: parseEther("0.000001"),
chainId: baseSepolia.id,
// Fields below are optional, CDP API will populate them if omitted.
// nonce
// maxPriorityFeePerGas
// maxFeePerGas
// gas
}),
network: "base-sepolia",
});
Sending an EIP-1559 transaction request object
const { transactionHash } = await cdp.evm.sendTransaction({
address: account.address,
transaction: {
to: "0x4252e0c9A3da5A2700e7d91cb50aEf522D0C6Fe8",
value: parseEther("0.000001"),
// Fields below are optional, CDP API will populate them if omitted.
// nonce
// maxPriorityFeePerGas
// maxFeePerGas
// gas
},
network: "base-sepolia",
});
Sends a user operation.
Parameters for sending the user operation.
Array of contract calls to execute in the user operation. Each call can either be:
to
, value
, and data
.to
, abi
, functionName
, and args
.Optional
idempotencyKey?: stringThe idempotency key.
Chain ID of the network to execute on.
Optional
paymasterUrl?: stringOptional URL of the paymaster service to use for gas sponsorship. Must be ERC-7677 compliant.
The smart account.
A promise that resolves to an object containing the smart account address, the user operation hash, and the status of the user operation.
Signs an EVM hash.
Parameters for signing the hash.
Options for signing an EVM hash.
A promise that resolves to the signature.
Signs an EVM message.
Parameters for signing the message.
Options for signing an EVM message.
A promise that resolves to the signature.
Signs an EVM transaction.
Configuration options for signing the transaction.
A promise that resolves to the signature.
import { parseEther, serializeTransaction } from "viem";
import { baseSepolia } from "viem/chains";
// Create a new EVM server account to sign with
const ethAccount = await cdp.createEvmServerAccount({});
const serializedTx = serializeTransaction(
{
chainId: baseSepolia.id,
data: "0x",
to: "0x4252e0c9A3da5A2700e7d91cb50aEf522D0C6Fe8",
type: "eip1559",
value: parseEther("0.000001"),
},
);
const signature = await cdp.evm.signTransaction({
address: ethAccount.address,
transaction: serializedTx,
});
Signs an EIP-712 message.
Parameters for signing the EIP-712 message.
A promise that resolves to the signature.
const signature = await cdp.evm.signTypedData({
address: account.address,
domain: {
name: "Permit2",
chainId: 1,
verifyingContract: "0x000000000022D473030F116dDEE9F6B43aC78BA3",
},
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
PermitTransferFrom: [
{ name: "permitted", type: "TokenPermissions" },
{ name: "spender", type: "address" },
{ name: "nonce", type: "uint256" },
{ name: "deadline", type: "uint256" },
],
TokenPermissions: [
{ name: "token", type: "address" },
{ name: "amount", type: "uint256" },
],
},
primaryType: "PermitTransferFrom",
message: {
permitted: {
token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
amount: "1000000",
},
spender: "0xFfFfFfFFfFFfFFfFFfFFFFFffFFFffffFfFFFfFf",
nonce: "0",
deadline: "1717123200",
},
});
Updates a CDP EVM account.
Optional
options: UpdateEvmAccountOptionsOptional parameters for creating the account.
Options for creating an EVM server account.
The address of the account.
Optional
idempotencyKey?: stringThe idempotency key.
The updates to apply to the account
A promise that resolves to the updated account.
const account = await cdp.evm.updateAccount({ address: "0x...", update: { name: "New Name" } });
const account = await cdp.evm.updateAccount({ address: "0x...", update: { accountPolicy: "73bcaeeb-d7af-4615-b064-42b5fe83a31e" } });
const idempotencyKey = uuidv4();
// First call
await cdp.evm.updateAccount({
address: "0x...",
update: { accountPolicy: "73bcaeeb-d7af-4615-b064-42b5fe83a31e" },
idempotencyKey,
});
// Second call with the same idempotency key will not update
await cdp.evm.updateAccount({
address: '0x...',
update: { name: "" },
idempotencyKey,
});
Waits for a user operation to complete or fail.
Parameters for waiting for the user operation.
Options for waiting for a user operation.
The smart account address.
The user operation hash.
Optional
waitOptions?: WaitOptionsThe wait options.
A promise that resolves to the transaction receipt.
// Send a user operation and get the user operation hash
const { userOpHash } = await cdp.evm.sendUserOperation({
smartAccount,
network: "base-sepolia",
calls: [
{
to: "0x0000000000000000000000000000000000000000",
value: parseEther("0.000001"),
data: "0x",
},
],
});
// Wait for the user operation to complete or fail
const result = await cdp.evm.waitForUserOperation({
smartAccountAddress: smartAccount.address,
userOpHash: userOp.userOpHash,
});
The namespace containing all EVM methods.