Skip to content

Moses-main/aave-autopilot

Repository files navigation

πŸš€ Aave Autopilot Vault

License: MIT Built with Foundry

Current Status: In Development

  • Network: Polygon Amoy Testnet (Chain ID: 80002)
  • Status: Core Implementation Complete
  • Last Updated: 2025-11-09

An ERC-4626 vault that automates Aave v3 position management with Chainlink Automation to prevent liquidations.

πŸ“– Documentation

✨ Features

  • Automated Health Factor Management: Monitors and maintains healthy positions on Aave v3
  • Chainlink Automation: Integrated with Chainlink Keepers for automated rebalancing
  • Gas Efficient: Optimized for minimal gas usage
  • Secure: Implements OpenZeppelin's security patterns
  • Test Coverage: Comprehensive test suite with 85%+ coverage
  • Decentralized: Non-custodial and permissionless

πŸš€ Quick Start

Prerequisites

Installation

# Clone the repository
git clone https://github.com/Moses-main/aave-autopilot.git
cd aave-autopilot

# Install dependencies
foundryup
forge install

# Set up environment
cp .env.example .env
# Edit .env with your configuration

Configuration

Edit the .env file with your configuration:

# Sepolia RPC URL
RPC_URL=https://eth-sepolia.g.alchemy.com/v2/YOUR_API_KEY

# Contract Addresses (Sepolia)
USDC=0x94a9D9AC8a22534E3FaCa9F4e7F2E2cf85d5E4C8
A_USDC=0x16dA4541aD1807f4443d92D26044C1147406EB10
AAVE_POOL=0x6Ae43d3271fF6888e7Fc43Fd7321a503fF738951
AAVE_DATA_PROVIDER=0x9B2F5546AaE6fC2eE3BEaD55c59eB7eD8648aFe1
ETH_USD_PRICE_FEED=0x694AA1769357215DE4FAC081bf1f309aDC325306
LINK_TOKEN=0x779877A7B0D9E8603169DdbD7836e478b4624789
KEEPER_REGISTRY=0xE16Df59B887e3Caa439E0b29B42bA2e7976FD8b2

# Deployment
PRIVATE_KEY=your_private_key_here
ETHERSCAN_API_KEY=your_etherscan_api_key_here

Testing

# Run unit tests
forge test

# Run tests with gas report
forge test --gas-report

πŸš€ Deployment

For detailed deployment instructions, see the Deployment Guide.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgements

  • Aave for the lending protocol
  • Chainlink for price feeds and keepers
  • OpenZeppelin for secure smart contract libraries
  • Foundry for the development framework 0x
    5000000000000000000
    $YOUR_ADDRESS
    
    
  1. Current Deployment:

Chainlink Automation Setup

Prerequisites

  • The contract must be deployed and funded with LINK tokens
  • You'll need the contract address and owner private key

1. Fund the Contract with LINK

# Load environment variables
source .env

# Fund the contract with 10 LINK (18 decimals)
cast send $LINK_TOKEN \
  --rpc-url $RPC_URL \
  --private-key $PRIVATE_KEY \
  "transfer(address,uint256)" \
  0xaFf8c2337df3A7ce17525E6aa1BABCbb926F1421 \
  10000000000000000000  # 10 LINK

# Verify LINK balance
cast call $LINK_TOKEN \
  --rpc-url $RPC_URL \
  "balanceOf(address)" \
  0xcDe14d966e546D70F9B0b646c203cFC1BdC2a961

2. Register with Chainlink Keepers

# Load environment variables
source .env

# Approve Keeper Registry to spend LINK
export KEEPER_REGISTRY=0xE16Df59B403e9B01F5f28a3b09a4e71c9F3509dF

cast send $LINK_TOKEN \
  --rpc-url $RPC_URL \
  --private-key $PRIVATE_KEY \
  "approve(address,uint256)" \
  $KEEPER_REGISTRY \
  1000000000000000000  # 1 LINK

# Register with Chainlink Keepers
forge script script/RegisterWithKeepers.s.sol \
  --rpc-url $RPC_URL \
  --broadcast \
  -vvvv

3. Verify Keeper Registration

# Check if the contract is registered
cast call $KEEPER_REGISTRY \
  --rpc-url $RPC_URL \
  "getUpkeepCount()"

# Check the registered upkeep (replace 0 with your upkeep ID if known)
cast call $KEEPER_REGISTRY \
  --rpc-url $RPC_URL \
  "getUpkeep(uint256)" \
  0

4. Test Keeper Automation

  1. Deposit funds to trigger position monitoring
  2. Simulate price movement to test rebalancing
  3. Verify Keeper execution in Tenderly dashboard

Example registration parameters:

  • Name: AaveAutopilot Keeper
  • Gas Limit: 500,000
  • Trigger Type: Custom Logic
  • Check Data: Encoded user address (or empty for all users)
  • Gas Lane: 500 gwei
  • Amount: 5 LINK

Security

Audits

This code has not been audited. Use at your own risk.

Security Features

  • Reentrancy protection with OpenZeppelin's ReentrancyGuard
  • Role-based access control
  • Pausable functionality for emergency stops
  • Input validation
  • Secure token handling with OpenZeppelin's SafeERC20

Known Limitations

  • The keeper may not be able to rebalance if gas prices are extremely high
  • The contract doesn't handle all edge cases of Aave v3's complex liquidation mechanics
  • The keeper may need to be topped up with ETH for gas costs

License

MIT

πŸ“– Overview

The AAVE Autopilot Vault provides a simple interface for users to deposit USDC, which is then automatically supplied to AAVE v3 to earn yield. The vault actively manages the health factor of the position and automatically rebalances when necessary to prevent liquidations, providing a hands-off DeFi experience.

✨ Key Features

  • Automated Health Factor Management: Continuously monitors and maintains optimal health factor (target: 1.5x, minimum: 1.05x)
  • Chainlink Price Feeds: Uses real-time ETH/USD price data for accurate collateral valuation
  • Chainlink Keepers Integration: Automated rebalancing when health factor falls below threshold (1.1x)
  • Gas Optimization: Optimized for network's gas efficiency with minimal external calls
  • Test Coverage: 85%+
  • Audit Status: Pending
  • Mainnet Readiness: Under Review

Previous Deployments

Key Milestones

  • Core smart contract development
  • Comprehensive test suite
  • Ethereum Sepolia deployment
  • Security audit
  • Mainnet deployment
  • Frontend integration
  • DAO governance

πŸš€ Quick Start

Prerequisites

  • Foundry (latest version recommended)
  • Node.js v16+ (for deployment scripts)
  • Git
  • Slither (for static analysis, optional)

Installation

# Clone the repository
git clone https://github.com/Moses-main/aave-autopilot.git
cd aave-autopilot

# Install Foundry (if not already installed)
curl -L https://foundry.paradigm.xyz | bash
foundryup

# Install dependencies
forge install

# Build the project
forge build

Running Tests

# Run all tests
forge test -vvv

# Run tests with gas reporting
forge test --gas-report

# Run specific test
forge test --match-test testDeposit -vvv

πŸ—οΈ Deployment

Environment Setup

  1. Create a .env file in the project root:
# Required
PRIVATE_KEY=your_private_key_here
RPC_URL=https://virtual.mainnet.eu.rpc.tenderly.co/your-api-key

# Optional (for verification)
ETHERSCAN_API_KEY=your_etherscan_api_key

# Testnet Configuration ( Sepolia ETH)
USDC=0x036CbD53842c5426634e7929541eC2318f3dCF7e
AAVE_POOL=0x6dcb6D1E0D487EDAE6B45D1d1B86e1A4AD8d4a2C
AAVE_DATA_PROVIDER=0x2d8A3C5677189723C4cB8873CfC9C8976FDF38Ac
A_USDC=0x4C5aE35b3f16fAcaA5a41f4Ba145D9aD887e8a5a
ETH_USD_PRICE_FEED=0x71041DDDAd094AE566B4d4cd0FA6C97e45B01E60

Deployed Contract Details

Sepolia ETH

Contract Address: 0x4e9A9676b3E24E406a42710A06120561D5A9A045
Constructor Parameters:
  - USDC: 0x94a9D9AC8a22534E3FaCa9F4e7F2E2cf85d5E4C8
  - AAVE_POOL: 0x6Ae43d3271fF6888e7Fc43Fd7321a503fF738951
  - AAVE_DATA_PROVIDER: 0x9B2F5546AaE6fC2eE3BEaD55c59eB7eD8648aFe1
  - A_USDC: 0x16dA4541aD1807f4443d92D26044C1147406EB10
  - ETH_USD_PRICE_FEED: 0x694AA1769357215DE4FAC081bf1f309aDC325306
  - Keeper Registry: 0xE16Df59B887e3Caa439E0b29B42bA2e7976FD8b2
  - LINK Token: 0x779877A7B0D9E8603169DdbD7836e478b4624789
  - Keeper Update Interval: 1 hour
  - Health Factor Target: 1.5x
  - Health Factor Threshold: 1.1x
  - Max Slippage: 1%

Deploy to Sepolia

  1. Deploy the contract:
# Load environment variables
source .env

# Deploy using Forge Script
forge script script/Deploy.s.sol:DeployScript \
  --rpc-url $RPC_URL \
  --private-key $PRIVATE_KEY \
  --broadcast \
  --verify \
  --etherscan-api-key $ETHERSCAN_API_KEY \
  -vvvv

Mainnet Deployment

For mainnet deployment, update the contract addresses in the deployment script to the mainnet addresses and run:

forge script script/Deploy.s.sol:DeployScript \
  --rpc-url $MAINNET_RPC_URL \
  --private-key $PRIVATE_KEY \
  --broadcast \
  --verify \
  --etherscan-api-key $ETHERSCAN_API_KEY \
  --slow

πŸ” Verification

Deployment

Deploy to Mainnet

  1. Ensure your .env file is properly configured with Mainnet settings
  2. Run the deployment script:
    forge script script/Deploy.s.sol --rpc-url $RPC_URL --broadcast --verify -vvvv

Verify on Etherscan

After deployment, verify your contract on Etherscan using the following command:

forge verify-contract \
  --chain-id 1 \
  --num-of-optimizations 200 \
  --watch \
  --constructor-args $(cast abi-encode "constructor(address,string,string,address,address,address,address,address)" \
    0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 \
    "Aave Autopilot Vault" \
    "aAuto-USDC" \
    0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2 \
    0x7B4EB56E7CD4b454BA8fF71E4518426369a138a3 \
    0x98C23E9d8f34FEFb1B7BD6a91B7BB122F4e16F5c \
    0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419 \
    0xE16Df59B403e9B01F5f28a3b09a4e71c9F3509dF) \
  --compiler-version v0.8.20+commit.a1b79de6 \
  --etherscan-api-key $ETHERSCAN_API_KEY \
  <YOUR_DEPLOYED_CONTRACT_ADDRESS> \
  src/AaveAutopilot.sol:AaveAutopilot
forge verify-contract \
  --chain-id 11155111 \
  --compiler-version v0.8.20+commit.a1b79de6 \
  --constructor-args $(cast abi-encode "constructor(address,string,string,address,address,address,address)" \
    0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238 \
    "Aave Autopilot USDC" \
    "apUSDC" \
    0x6Ae43d3271ff6888e7Fc43Fd7321a503ff738951 \
    0x3e9708D80F7b3e431180130bF478987472f950aF \
    0x16dA4541aD1807f4443d92D26044C1147406EB80 \
    0x694AA1769357215DE4FAC081bf1f309aDC325306 \
    0x03a33E8A69f1A5b61178f70BC5c8E674aB571334) \
  --num-of-optimizations 200 \
  AaveAutopilot \
  0xaE2202566bE5325e2A5746b66E13F0D6f7E248b6 \
  --verifier etherscan \
  --etherscan-api-key $ETHERSCAN_API_KEY

2. Deploy the contract:

```bash
forge script script/Deploy.s.sol:DeployScript --rpc-url $RPC_URL --broadcast --verify -vvvv

Deployed Addresses ( Sepolia ETH)

  • AaveAutopilot: 0xaE2202566bE5325e2A5746b66E13F0D6f7E248b6
  • USDC: 0x036CbD53842c5426634e7929541eC2318f3dCF7e
  • Aave Pool: 0x6dcb6d1e0d487edae6b45d1d1b86e1a4ad8d4a2c
  • ETH/USD Price Feed: 0x71041dddad094ae566b4d4cd0fa6c97e45b01e60

πŸ”„ Workflow

Deposit Flow

  1. User approves USDC spending by the vault
  2. User deposits USDC into the vault
  3. Vault supplies USDC to AAVE v3
  4. Vault receives aUSDC tokens in return
  5. User receives vault shares representing their deposit

Withdrawal Flow

  1. User requests withdrawal of USDC
  2. Vault calculates the share of aUSDC to redeem
  3. Vault withdraws USDC from AAVE v3
  4. USDC is transferred to the user's wallet
  5. User's vault shares are burned

Rebalancing Flow (Automated)

  1. Chainlink Keeper calls checkUpkeep() at regular intervals
  2. If health factor < threshold, performUpkeep() is triggered
  3. Vault automatically repays debt or adjusts collateral to maintain target health factor
  4. Transaction is submitted by the Keeper network

πŸ“Š Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 β”‚     β”‚             β”‚     β”‚               β”‚
β”‚  AAVE Autopilot │◄───►│  AAVE v3    │◄───►│  Chainlink    β”‚
β”‚  Vault (ERC4626)β”‚     β”‚  Protocol   β”‚     β”‚  Price Feeds  β”‚
β”‚                 β”‚     β”‚             β”‚     β”‚               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  β”‚                               β”‚
         └─►│  Chainlink Keepers Network    β”‚
            β”‚  (Automated Rebalancing)      β”‚
            β”‚                               β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ” Core Functions

Deposit & Withdraw

  • deposit(uint256 assets, address receiver) - Deposit USDC and receive vault shares
  • mint(uint256 shares, address receiver) - Mint vault shares by depositing USDC
  • withdraw(uint256 assets, address receiver, address owner) - Withdraw USDC by burning vault shares
  • redeem(uint256 shares, address receiver, address owner) - Redeem vault shares for USDC

Keeper Functions

  • checkUpkeep(bytes calldata) - Checks if the vault needs rebalancing
  • performUpkeep(bytes calldata) - Executes the rebalancing logic
  • getCurrentHealthFactor() - Returns the current health factor
  • getCurrentPosition() - Returns the current position details

Admin Functions

  • pause() - Pause all deposits and withdrawals (emergency only)
  • unpause() - Unpause the contract
  • setKeeper(address) - Update the keeper address
  • setHealthFactorThresholds(uint256, uint256) - Update health factor thresholds

πŸ”’ Security Considerations

Audits & Testing

  • Comprehensive test coverage (>85%)
  • Automated security analysis with Slither and MythX
  • Formal verification of critical functions

Risk Factors

  • Smart contract risks
  • Oracle risks (price feed manipulation)
  • AAVE protocol risks
  • Keeper network reliability

Emergency Procedures

  • Pause functionality for critical issues
  • Timelock for parameter updates
  • Multi-sig admin controls

πŸ“ˆ Performance Metrics

Gas Optimization

  • Average deposit: ~150k gas
  • Average withdrawal: ~180k gas
  • Rebalancing: ~250k-500k gas (varies with position size)

APY Simulation

Based on current AAVE v3 rates and historical performance:

  • Estimated ETH APY: 2-4% (USDC supply)
  • Additional yield from leverage: 3-6%
  • Net APY after fees: 4-8%

πŸ“… Development Roadmap

Short-term (Q4 2025)

  • Complete security audit
  • Deploy to ETH MAINNET
  • Implement frontend dashboard
  • Add multi-collateral support

Medium-term (Q1 2026)

  • Cross-chain deployment (Optimism, Arbitrum)
  • Advanced strategies (LP positions, yield optimization)
  • DAO governance

Long-term (2026+)

  • Permissionless strategy marketplace
  • Advanced risk management
  • Institutional features

πŸš€ Quick Start

Interact with Deployed Contract

  1. Approve USDC for Deposit (if using Sepolia USDC):

    cast send 0x94a9D9AC8a22534E3FaCa9F4e7F2E2cf85d5E4C8 \
      "approve(address,uint256)" \
      0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      1000000 \
      --private-key $PRIVATE_KEY \
      --rpc-url $RPC_URL
  2. Deposit USDC:

    cast send 0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      "deposit(uint256,address)" \
      1000000 \
      0xYourWalletAddress \
      --private-key $PRIVATE_KEY \
      --rpc-url $RPC_URL
  3. Check Your Balance:

    cast call 0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      "balanceOf(address)(uint256)" \
      0xYourWalletAddress \
      --rpc-url $RPC_URL
  4. Withdraw USDC:

    cast send 0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      "withdraw(uint256,address,address)" \
      500000 \
      0xYourWalletAddress \
      0xYourWalletAddress \
      --private-key $PRIVATE_KEY \
      --rpc-url $RPC_URL

Monitoring

Check Contract State

  1. Check Total Assets:

    cast call 0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      "totalAssets()(uint256)" \
      --rpc-url $RPC_URL
  2. Check Health Factor:

    cast call 0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      "getHealthFactor()(uint256)" \
      --rpc-url $RPC_URL
  3. Check Keeper Status:

    # Check if checkUpkeep returns true
    cast call 0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      "checkUpkeep(bytes)(bool,bytes)" \
      0x \
      --rpc-url $RPC_URL

View Events

  1. Deposit Events:

    cast logs --from-block 0 --to latest \
      --address 0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      --topic 0xdcbc1c05240f31ff3ad067ef1ee35ce4997762752e3a095284754544f4c709d7 \
      --rpc-url $RPC_URL
  2. Withdraw Events:

    cast logs --from-block 0 --to latest \
      --address 0xA076ecA49434a4475a9FF716c2E9f20ccc453c20 \
      --topic 0x5b8f46461c1dd69fb968f1a003acee221ea3e19540e350233b612ddb43433b55 \
      --rpc-url $RPC_URL

Troubleshooting

Common Issues

  1. Insufficient Gas:

    • Ensure you have enough Sepolia ETH for gas fees
    • Get test ETH from a Sepolia faucet
  2. Transaction Reverted:

    • Check the error message in the transaction receipt
    • Verify token approvals and balances
    • Ensure you're using the correct contract addresses
  3. Keeper Not Triggering:

    • Check if the contract has enough LINK
    • Verify the upkeep is registered on Chainlink Keepers App
    • Ensure checkUpkeep returns true

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For support or questions, please open an issue on GitHub.

πŸ™ Acknowledgements


### Test

```shell
$ forge test

Format

$ forge fmt

Gas Snapshots

$ forge snapshot

Anvil

$ anvil

Deploy

$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>

Cast

$ cast <subcommand>

Help

$ forge --help
$ anvil --help
$ cast --help

About

An intelligent ERC-4626 vault that manages Aave v3 positions automatically, monitors health factors, and prevents liquidations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors