Skip to content

allora-network/connector-coding-task

Repository files navigation

DeFi Leverage Strategy — Interview Task

60-minute technical interview for DeFi protocol integration.

Build a 1.5x leveraged LONG position on ETH using Aave V3 and Uniswap V2.


📋 Table of Contents

  1. Setup
  2. Task Overview
  3. Testing Your Solution
  4. Common Pitfalls
  5. Evaluation Criteria

🚀 Setup

Prerequisites

  • Python 3.10+
  • Foundry (for Anvil)

Quick Anvil Installation

macOS/Linux:

curl -L https://foundry.paradigm.xyz | bash
foundryup

Windows:

Option 1: Using WSL2 (Recommended)

# Install WSL2 first (if not already installed)
wsl --install

# Inside WSL2, install Foundry
curl -L https://foundry.paradigm.xyz | bash
foundryup

Option 2: Pre-built Binaries

  1. Download latest release from Foundry Releases
  2. Extract anvil.exe to a folder in your PATH
  3. Open PowerShell and verify: anvil --version

Verify installation:

anvil --version
# Should output: anvil 0.2.0 (or higher)

Troubleshooting: See Foundry Installation Guide for detailed instructions.

Project Installation

# 1. Navigate to project directory
cd connector-coding-task

# 2. Install Python dependencies
pip install -r requirements.txt

# 3. Copy environment template
cp .env.example .env

Start the Test Environment

Terminal 1: Start Anvil Fork

anvil --fork-url https://eth.llamarpc.com \
      --fork-block-number 23726437 \
      --chain-id 1 \
      --auto-impersonate

What this does:

  • Forks Ethereum mainnet at block 23,726,437
  • --auto-impersonate allows sending transactions from any address (unlocks all accounts)
  • Provides deterministic testing environment with WETH-rich test wallet

Keep this terminal running during development.


🎯 Task Overview

The Goal

Build a 1.5x leveraged LONG position on ETH:

  • You have 1 ETH worth $3,500
  • Create exposure to 1.5 ETH (worth $5,250)
  • Use Aave for lending and Uniswap for swapping

The Strategy

  1. Supply 1 ETH to Aave as collateral
  2. Borrow $1,750 USDC from Aave (50% LTV)
  3. Swap $1,750 USDC → 0.5 ETH on Uniswap
  4. Result: 1.5 ETH total exposure with 1 ETH capital

What You'll Implement

File: strategy_skeleton.py

  1. calculate_borrow_amount() (15 min)

    • Calculate how much USDC to borrow for target leverage
  2. build_position() (30 min)

    • Execute the 6-step transaction flow
  3. Discussion (15 min)

    • Risk management, monitoring, unwinding strategy

🧪 Testing Your Solution

Terminal 2: Run Test

python test_solution.py

What the Test Validates

The test checks your actual Aave position (not your return value):

Collateral supplied > 0 ✅ USDC debt > 0 (borrowed successfully) ✅ Leverage within 20% of target (1.5x) ✅ Health factor > 1.0 (safe from liquidation)

Expected Output

================================================================================
                          LEVERAGE STRATEGY TEST
================================================================================

✅ Connected to chain ID 1
📦 Block: 23,726,437
💼 Test wallet: 0x49c23...

Initial State:
  WETH Balance: 1672.2008 WETH
  USDC Balance: 0.00 USDC
  ETH Price: $3,500.00

Expected Outcome:
  Collateral: 1 ETH ($3,500.00)
  Target Leverage: 1.5x
  Expected Borrow: ~$1,750.00 USDC
  Expected Exposure: ~1.50 ETH

================================================================================
Building 1.5x leveraged position...
================================================================================

[Your implementation logs here]

================================================================================
                       POSITION STATUS (from Aave)
================================================================================
  Collateral:           $3,499.71
  Debt:                 $1,714.69
  Health Factor:        1.69
  LTV:                  80.50%
  Liquidation Threshold: 83.00%
  ETH in Aave:          0.9999 ETH
  ETH Purchased:        0.4878 ETH
  Total ETH Exposure:   1.4878 ETH
  Actual Leverage:      1.49x

================================================================================
                          VALIDATION RESULTS
================================================================================

✅ Collateral supplied: $3,499.71
✅ USDC borrowed: $1,714.69
✅ Leverage: 1.49x (target: 1.5x, variance: 0.7%)
✅ Collateral matches expected (variance: 0.0%)

================================================================================
                            TEST SUMMARY
================================================================================
  Position Size:        $3,499.71
  Leverage Achieved:    1.49x / 1.50x target
  Health Factor:        1.69
  Position Safety:      ✅ SAFE
================================================================================

🎉 ALL TESTS PASSED!

🐛 Common Pitfalls

1. Decimal Confusion ❌

# WRONG
usdc_wei = int(1750 * 10**18)  # USDC is 6 decimals, not 18!

# CORRECT
usdc_wei = int(1750 * 10**6)   # USDC has 6 decimals
weth_wei = int(1.0 * 10**18)   # WETH has 18 decimals

2. Missing Approvals ❌

# WRONG - Will revert!
tx = self.aave.supply(WETH, amount, ...)  # No approval first

# CORRECT
tx = self.weth.approve(aave.address, amount, ...)  # Approve first
self.wallet.send_and_wait(tx)
tx = self.aave.supply(WETH, amount, ...)           # Then supply

3. No Slippage Protection ❌

# WRONG - Could get sandwiched!
min_out = 0  # Accepts any amount

# CORRECT - 1% slippage protection
min_out = int(expected_amount * 0.99)

4. Wrong Transaction Order ❌

# WRONG
self.aave.borrow(...)   # Can't borrow without collateral
self.aave.supply(...)   # Supply comes after? Too late!

# CORRECT
self.aave.supply(...)   # Supply collateral first
self.aave.borrow(...)   # Then borrow

5. Forgetting Deadline ❌

# WRONG
deadline = 0  # Transaction can be executed anytime

# CORRECT
deadline = int(time.time()) + 300  # Valid for 5 minutes

📚 Additional Resources


📝 License

This repository is for interview purposes only.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages