Using Compass API with ZeroDev Smart Accounts

This guide demonstrates how to integrate Compass API SDK with ZeroDev to create and use smart accounts for DeFi operations. The combination enables:

  • Account abstraction features
  • Gas fee abstraction (pay in any token)
  • Simplified DeFi operations
  • Enhanced security through smart account features

Prerequisites

1

Install Dependencies

Install the required packages:

npm install @compass-labs/api-sdk @zerodev/sdk @zerodev/ecdsa-validator viem
2

Set Environment Variables

Create a .env file in your project root:

# .env
ZERODEV_RPC="your_zerodev_rpc_url"
PRIVATE_KEY="your_private_key"
COMPASS_API_KEY="your_compass_api_key"

Implementation Guide

1

Import dependencies and initialize clients

First, import the necessary dependencies, load your environment variables, and initialize the public client and entry point.

2

Create signer, validator, kernel account, and paymaster

Set up the signer, ECDSA validator, kernel account, paymaster, and kernel client. This step prepares your smart account for batched DeFi operations.

3

Create and prepare batched operations

Use the Compass API SDK to create batched DeFi operations (e.g., allowance increase and Aave supply). Convert the result to the format expected by the kernel client.

4

Send batched operations and handle result

Send the batched user operation and wait for confirmation. The script will print the transaction hash and confirmation receipt, or an error if the operation fails.

Full Code

Here is the full script from the tutorial. Copy and paste in your code editor and play around!

Common Operation Examples

Here are some examples of different DeFi operations you can perform:

Token Swaps

{
  body: {
    actionType: "UNISWAP_BUY_EXACTLY",
    tokenIn: "WETH",
    tokenOut: "USDC",
    fee: "0.01",
    amountOut: 1.5,
    amountInMaximum: 1.6,
    wrapEth: true,
  }
}

Lending Operations

{
  body: {
    actionType: "AAVE_SUPPLY",
    amount: 1000,
    asset: "USDC",
  }
}

Token Approvals

{
  body: {
    actionType: "ALLOWANCE_INCREASE",
    amount: 1000,
    token: "WETH",
    contract_name: "UniswapV3Router",
  }
}

Resources