🕐 Time to complete: ±10 minutes 🧰 What you need:

Introduction

Leverage lending (“looping”) lets you boost yield by borrowing against your deposit, swapping the loan, and re-depositing — all to multiply exposure. With Compass SDK, you can loop in one atomic call, no Solidity, no manual steps, minimal gas. Normally this would require complex smart contract logic and multiple transaction steps. With Compass SDK, you abstract all of that away. This tutorial walks you through implementing AAVE looping strategies to achieve your target leverage.

Example

Aave Looping Diagram In this example, you’re depositing USDC, borrowing ETH, swapping it back into USDC, and re-supplying — on repeat — to achieve a target leverage (e.g. 2.19x). Compass bundles all of this into one atomic transaction. This illustration demonstrates AAVE looping strategy with the following parameters, assuming that the price of ETH is 2333 USDC:
  • Collateral Asset: USDC
  • Loan Asset: ETH
  • Initial Collateral Amount: 1000 USDC
  • Loan to Value Ratio: 70%
  • Leverage: 2.19x
The API will figure out the optimal values for each loop and automate all actions.

Setup

1

Install Dependencies

Install the required packages.
npm install @compass-labs/api-sdk viem dotenv
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"
3

Import Libraries & Environment Variables

4

Initialize SDK and Account

The full, uninterrupted code is available at the end of the tutorial.

Get Authorization

Before you can execute AAVE looping, you need to get an authorization from the Compass API and sign it with your private key. This ensures only you can execute the batch.
1

Get and Sign Authorization

Request authorization from the Compass API and sign it with your wallet to authenticate the transaction batching.

Configure AAVE Looping Strategy

Now that we have authorization, let’s configure the AAVE looping strategy. This involves setting up the parameters for your leverage strategy including collateral token, borrow token, initial amount, and target multiplier.
1

Configure Looping Parameters

Set up the AAVE looping strategy with your desired parameters. This includes specifying the collateral and borrow tokens, initial amount, leverage multiplier, and risk parameters.

Execute the Transaction

The final step is to sign and broadcast the transaction to the network. This will execute your AAVE looping strategy in a single atomic transaction.
1

Sign and Broadcast Transaction

Sign the returned transaction with your private key and broadcast it to the network. This is the final step to actually send your AAVE looping transaction to Ethereum.

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

// TypeScript
const loopingTx = await sdk.transactionBatching.aaveLoop({
  // ... other parameters ...
  collateral_token: "USDC",
  borrow_token: "WETH",
  initial_collateral_amount: 1000,
  multiplier: 1.5,
  max_slippage_percent: 0.5,
  loan_to_value: 65
});
# Python
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

// TypeScript
const loopingTx = await sdk.transactionBatching.aaveLoop({
  // ... other parameters ...
  collateral_token: "WETH",
  borrow_token: "USDC",
  initial_collateral_amount: 1,
  multiplier: 2.0,
  max_slippage_percent: 1,
  loan_to_value: 75
});
# Python
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

// TypeScript
const loopingTx = await sdk.transactionBatching.aaveLoop({
  // ... other parameters ...
  collateral_token: "WBTC",
  borrow_token: "USDC",
  initial_collateral_amount: 0.1,
  multiplier: 2.5,
  max_slippage_percent: 1,
  loan_to_value: 80
});
# Python
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
For further practice, try different token pairs, test with various leverage multipliers, or explore other Compass Bundler features to bundle additional transactions.

Full Code

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

Resources