Skip to content

BuildsWithKing/king-yield-aggregator

Repository files navigation

Coverage Foundry License: MIT


⚠️ Security Notice: This project achieves 100% test coverage and implements common defensive patterns, but has not undergone professional security audit. Do not use in production without independent review.


⭐ If this project inspires you, please give it a star on GitHub — it fuels open-source innovation!


King Yield Aggregator (KYA)

King Yield Aggregator (KYA) is a modular, role-secured yield vault designed to automatically deploy user deposits into external strategies to earn optimized yield.
It follows a clean separation of concerns across vault logic, strategy management, access control, and safety modules.

Table Of Contents

Features

  • Automated Yield Deployment: Deposits can be routed through a Strategy Manager into yield strategies (e.g., Aave, Compound).
  • Share-Based Accounting: Users receive vault shares representing their proportional ownership of total assets (on-vault + deployed).
  • Modular Architecture: Vault, Strategy Manager, Rebalancer and Strategy contracts are separated for extensibility and composability.
  • Secure Access Control: Powered by the custom Kingable access control suite with pausing, roles, and non-zero address checks.
  • Safe Withdrawals: Withdrawals pull liquidity from on-vault balance and fallback to strategy withdrawal when needed.
  • Switchable Strategies: Strategy Manager can switch strategies without affecting user deposits or share balances.

Architecture

┌────────────────────┐
│    VaultCore       │  <-- User-facing, handles deposits/withdrawals
│--------------------│
│ - Share accounting │
│ - Deposit/Withdraw │
│ - Calls            |
|   StrategyManager  │
└─────────┬──────────┘
          │
          ▼
┌────────────────────────┐
│   StrategyManager      │  <-- Handles strategy orchestration
│------------------------│
│ - Active strategy list │
│ - APR tracking         │
│ - Deposit to / Withdraw|
|   from strategy        │
│ - Exposes interface for|
|   Rebalancer           │
└─────────┬──────────────┘
          │
          ▼
 ┌───────────────────┐      ┌─────────────────────┐
 │   Strategy A      │      │   Strategy B        │
 │-------------------│      │---------------------│
 │ - Protocol logic  │      │ - Protocol logic    │
 │ - Yield tracking  │      │ - Yield tracking    │
 └───────────────────┘      └─────────────────────┘

┌────────────────────────┐
│      Rebalancer        │  <-- Independent module
│------------------------│
│ - Reads StrategyManager│
│ - Moves funds A → B    │
│ - Cooldowns, limits    │
└────────────────────────┘

All inter-module interactions occur through interfaces to reduce coupling.

Documentation

Folder Structure

├── src
│   ├── Rebalancer.sol
│   ├── StrategyManager.sol
│   ├── Utils.sol
│   ├── VaultCore.sol
│   ├── interfaces
│   │   ├── IStrategy.sol
│   │   ├── IStrategyManager.sol
│   │   ├── IStrategyManagerRebalancer.sol
│   │   └── IStrategyManagerVault.sol
│   ├── mocks
│   │   ├── AaveStrategyMock.sol
│   │   └── CompoundStrategyMock.sol
│   └── types
│       ├── ManagerTypes.sol
│       ├── StrategyTypes.sol
│       └── VaultTypes.sol
└── test
    ├── BadStrategyMock.t.sol
    ├── BadTokenTest.t.sol
    ├── BaseTest.t.sol
    ├── DummyContract.t.sol
    ├── MockTokenTest.t.sol
    ├── fuzz
    │   └── VaultCoreFuzzTest.t.sol
    └── unit
        ├── AaveStrategyMockTest.t.sol
        ├── CompoundStrategyMockTest.t.sol
        ├── RebalanacerTest.t.sol
        ├── StrategyManagerTest.t.sol
        └── VaultCoreTest.t.sol

Project Summary

King Yield Aggregator (KYA) is a fully-tested modular vault system that aggregates yield from multiple DeFi lending protocols. The architecture separates concerns cleanly through VaultCore, StrategyManager, and Rebalancer contracts, allowing easy addition of new strategies while maintaining security through role-based access control.

This project demonstrates advanced Solidity principles, including:

  • Share-based accounting for fair yield distribution
  • Multi-strategy support with dynamic switching
  • Role-based access control (King/Admin separation)
  • Custom errors and events for gas efficiency
  • Modifiers and helper functions for code reusability
  • Reentrancy guards on critical functions
  • Comprehensive validation (zero checks, balance checks, etc.)
  • Test coverage including fuzz testing
  • Gas optimization through efficient state management
  • KingClaimMistakenETH allows recovery of mistakenly sent ETH.

Usage

This project is designed as a modular reference implementation.

You can interact with it in three ways:

  • Via Foundry tests (recommended for developers)
  • Via Remix using mock strategies
  • Via custom scripts or frontend integration

➡️ See USAGE.md for a step-by-step Remix walkthrough.

Tools Used

  • Language: Solidity ^0.8.30
  • IDE: Remix & VS Code
  • Framework: Foundry
  • Version Control: Git + GitHub (SSH)
  • Libraries: BuildsWithKing Security

Testing

Full unit and fuzz test coverage for all logic paths:

  • VaultCore: Deposit, withdraw, withdrawAll, share calculations, conversions
  • StrategyManager: Add/remove strategies, set active strategy, fund deployment
  • Rebalancer: APR monitoring, rebalance triggers, threshold checks
  • Edge cases: Zero amounts, insufficient shares, bad token transfers, reentrancy
  • Access control: King-only, admin-only, role-based functions
  • Custom error reverts: ZeroAmount, InsufficientShares, TokenTransferFailed, etc.
  • Fuzz testing: Random deposits, withdrawals, and yield scenarios

Note: Static analysis (Aderyn) used. High-severity findings addressed. Remaining low-severity findings were assessed as acceptable design tradeoffs.

Coverage: 100%

Current test coverage: 100% Coverage

Local Development

To run locally:

  • Install Foundry
  • Clone repo
git clone https://github.com/BuildsWithKing/king-yield-aggregator.git
cd king-yield-aggregator
  • Run:
forge install

Compile

forge compile || forge build

Run tests

forge test

Check coverage

forge coverage

Gas snapshot

forge snapshot

Note: Remapping enabled in foundry.toml

remappings = [ "buildswithking-security/=lib/buildswithking-security/contracts/access/" ]

License

MIT License — Feel free to fork, learn, and build with it.

Author

Built by Michealking
Created: Nov 24 - Dec 21, 2025

Acknowledgements

This project was built as part of the Contract.dev Hackathon.


🤝 Contributing: Issues and pull requests welcome! Please ensure all tests pass and maintain 100% coverage for new features.


✅ King Yield Aggregator — Full Coverage Achieved!

Releases

No releases published

Packages

No packages published