Using Compass API with Safe Smart Accounts

This guide demonstrates how to integrate Compass API SDK with Safe Starter Kit SDK to create and use smart accounts for DeFi operations.

Prerequisites

1

Install Dependencies

Install the required packages:

npm install @safe-global/sdk-starter-kit @compass-labs/api-sdk dotenv
2

Set Environment Variables

Create a .env file in your project root:

# .env
ARBITRUM_RPC_URL="your_arbitrum_rpc_url"
SIGNER_PRIVATE_KEY="your_wallet_private_key"
COMPASS_API_KEY="your_compass_api_key"
# If using existing safe:
SAFE_ADDRESS="your_safe_address"
# If creating new safe:
# SIGNER_ADDRESS="your_signer_address"

Implementation Guide

1

Import dependencies and load environment

First, import the necessary dependencies and load your environment variables.

2

Create Safe client and initialize Compass API SDK

You have two options for setting up your Safe client:

  • Option A: Use an existing Safe by providing its address.
  • Option B: Deploy a new Safe by providing the signer’s address (uncomment the relevant code in the script).

This step also initializes the Compass API SDK for DeFi operations.

3

Create and prepare batched operations

Here’s an example of increasing allowance and supplying tokens to Aave. If you are deploying a new Safe, only include the ALLOWANCE_INCREASE operation in the first transaction. The Safe will not have any tokens initially, so the AAVE_SUPPLY operation should be executed in a subsequent transaction after funding the Safe with USDC.

4

Send batched operations and handle result

Convert the operations to the format expected by Safe and execute them. The script will print the transaction result or an error if the wallet is not funded.

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_EXACT_IN",
    tokenIn: "USDC",
    tokenOut: "WETH",
    amountIn: "100",
    amountOutMinimum: "0.05"
  }
}

Lending Operations

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

Token Approvals

{
  body: {
    actionType: "ALLOWANCE_INCREASE",
    token: "WETH",
    contractName: "UniswapV3Router",
    amount: "1"
  }
}

Resources