Aave Looping with Compass SDK

This guide demonstrates how to implement Aave looping strategies using the Compass API SDK. Aave looping involves supplying collateral, borrowing assets, and reinvesting them to maximize yield.

Prerequisites

1

Install Dependencies

Install the required packages:

pip install compass-api-sdk python-dotenv web3
2

Set Environment Variables

Create a .env file in your project root:

# .env
PRIVATE_KEY="your_wallet_private_key"
RPC_URL="your_ethereum_rpc_url"
COMPASS_API_KEY="your_compass_api_key"

Implementation Guide

1. Initial Setup

First, import the necessary dependencies and set up environment variables:

from compass_api_sdk import CompassApiSDK
import os
import dotenv
from web3 import Web3
from eth_account import Account

dotenv.load_dotenv()

PRIVATE_KEY = os.getenv("PRIVATE_KEY")
RPC_URL = os.getenv("RPC_URL")
COMPASS_API_KEY = os.getenv("COMPASS_API_KEY")

2. Initialize SDK and Web3

Set up the Compass API SDK and Web3 connection:

def main():
    # Initialize Compass SDK
    sdk = CompassApiSDK(api_key=COMPASS_API_KEY)
    
    # Ensure private key has 0x prefix
    private_key = PRIVATE_KEY
    if not private_key.startswith("0x"):
        private_key = f"0x{private_key}"
    
    # Initialize Web3 and account
    w3 = Web3(Web3.HTTPProvider(RPC_URL))
    account = Account.from_key(private_key)

3. Get Authorization

Before executing Aave looping transactions, you need to get and sign an authorization:

# Get authorization for transaction batching
    auth = sdk.transaction_batching.authorization(
        chain="ethereum:mainnet",
        sender=account.address
    )
    
    # Sign the authorization
    signed_auth = Account.sign_authorization(auth, private_key)

4. Execute Aave Looping

Configure and execute the Aave looping strategy:

# Create Aave looping transaction
    looping_tx = sdk.transaction_batching.aave_loop(
        chain="ethereum:mainnet",
        sender=account.address,
        signed_authorization=signed_auth.model_dump(),
        collateral_token="USDC",      # Token to supply as collateral
        borrow_token="WETH",          # Token to borrow
        initial_collateral_amount=10,  # Amount of collateral to supply
        multiplier=2.0,               # Leverage multiplier
        max_slippage_percent=1,       # Maximum allowed slippage
        loan_to_value=80              # Loan-to-value ratio in percentage
    )
    
    # Sign and send the transaction
    signed_tx = w3.eth.account.sign_transaction(looping_tx, private_key)
    tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
    
    print(f"Transaction hash: {tx_hash.hex()}")

if __name__ == "__main__":
    main()

Understanding the Parameters

Let’s break down the key parameters for the Aave looping strategy:

Aave Loop Parameters

  • collateral_token: The token you want to supply as collateral (e.g., “USDC”, “WETH”, “WBTC”)
  • borrow_token: The token you want to borrow (e.g., “WETH”, “USDC”)
  • initial_collateral_amount: The amount of collateral token to supply initially
  • multiplier: The leverage multiplier (e.g., 2.0 means double exposure)
  • max_slippage_percent: Maximum allowed slippage for token swaps (1-100)
  • loan_to_value: The loan-to-value ratio in percentage (0-100)

The loan-to-value (LTV) ratio determines how much you can borrow against your collateral. For example, if LTV is 80%, you can borrow up to 80% of your collateral’s value. Be cautious with high LTV ratios as they increase liquidation risk.

The maximum possible multiplier is determined by the formula: 1 / (1 - loan_to_value/100). For example, with an LTV of 80%, the maximum multiplier would be 1 / (1 - 80/100) = 1 / 0.2 = 5. This represents the theoretical maximum leverage possible at that LTV ratio.

Example Strategies

Here are some example configurations for different risk appetites:

Conservative Strategy

looping_tx = sdk.transaction_batching.aave_loop(
    # ... other parameters ...
    collateral_token="USDC",
    borrow_token="WETH",
    initial_collateral_amount=1000,
    multiplier=1.5,
    max_slippage_percent=0.5,
    loan_to_value=65
)

Moderate Strategy

looping_tx = sdk.transaction_batching.aave_loop(
    # ... other parameters ...
    collateral_token="WETH",
    borrow_token="USDC",
    initial_collateral_amount=1,
    multiplier=2.0,
    max_slippage_percent=1,
    loan_to_value=75
)

Aggressive Strategy

looping_tx = sdk.transaction_batching.aave_loop(
    # ... other parameters ...
    collateral_token="WBTC",
    borrow_token="USDC",
    initial_collateral_amount=0.1,
    multiplier=2.5,
    max_slippage_percent=1,
    loan_to_value=80
)

Risk Considerations

When implementing Aave looping strategies, consider the following risks:

  1. Liquidation Risk: Higher LTV ratios increase the risk of liquidation if the collateral value drops
  2. Interest Rate Risk: Borrowing rates may increase, affecting the profitability of your position
  3. Slippage Risk: Large trades may experience significant slippage, especially in volatile markets
  4. Smart Contract Risk: Always verify contract addresses and permissions

Resources