Development of DeFi Yield Aggregator
Yearn Finance in 2020 formulated idea remaining relevant: user shouldn't manually monitor dozens protocols hunting best APY. Vault accepts deposit, strategy automatically places funds in best protocol, reinvests yield, switches on condition change. User gets yvToken and never thinks about it again.
Building aggregator — solving several unrelated engineering tasks simultaneously: on-chain vault with multiple strategies, off-chain keeper for automation, APY comparison system with normalization, and gas optimization not eating all yield.
Architecture: from simple vault to multi-strategy
Basic ERC-4626 vault
ERC-4626 — standard for tokenized vault (adopted 2022, Ethereum mainnet). Defines single interface: deposit(), withdraw(), convertToShares(), convertToAssets(). Vault issues shares (ERC-20 tokens), price per share grows as yield accumulates.
Key advantage: compatibility. Any protocol supporting ERC-4626 can use your vault as building block. Yearn V3 reworked everything for this standard. Building vault today — means implementing ERC-4626.
// Minimal interface
interface IERC4626 {
function deposit(uint256 assets, address receiver) external returns (uint256 shares);
function withdraw(uint256 assets, address receiver, address owner) external returns (uint256 shares);
function totalAssets() external view returns (uint256);
function convertToShares(uint256 assets) external view returns (uint256);
function convertToAssets(uint256 shares) external view returns (uint256);
}
Multi-strategy allocation
One vault — multiple strategies. Vault distributes funds between strategies by weights. Yearn calls this "allocator". Each strategy — separate contract implementing IStrategy interface with harvest(), report(), withdraw() methods.
Typical strategies for USDC vault:
- Aave V3 USDC lending (base APY ~5%)
- Compound V3 USDC lending (base APY ~4-6%)
- Curve 3pool (base APY ~2-4% + CRV rewards)
- Morpho (optimized Aave/Compound, APY ~6-8%)
Allocator decides how much funds to hold in each strategy. Simplest approach: all in max APY one. More mature: diversification via max_allocation_per_strategy (e.g., max 50% in one protocol) plus liquidity accounting (strategy must withdraw funds reasonably quickly).
Gas problem for small positions
Harvest operation (claim rewards + swap to base asset + reinvest) costs $5-20 on Ethereum mainnet. For vault with $10K TVL daily harvest eats 1-2% annually — can completely destroy APY.
Solutions:
Batch harvesting: harvest not on schedule, but when accumulated unrealized profit exceeds gas_cost * multiplier threshold. If expected profit $50, harvest costs $15 — execute. If profit $5 — wait.
L2-first strategy: on Arbitrum or Base gas for harvest 100-500x cheaper. $50K TVL vault on Arbitrum can harvest hourly without significant loss.
Gas optimization in contracts: batching via Multicall3, using ERC-2612 (permit) instead of separate approve, packed storage.
APY comparison: why you can't just take UI number
APY displayed on Aave, Compound, Curve — not one same metric. Can't compare directly.
Aave: supplyAPY = (1 + supplyRatePerSecond)^(seconds_per_year) - 1. Includes base rate, doesn't include AAVE emissions separately.
Compound V3: supplyRate in wei per second. Plus COMP reward token, must claim separately.
Curve + Convex: base APY from trading fees + CRV emissions (depends on gauge weight voting) + CVX rewards + any extra rewards. Total 4-5 separate streams.
Morpho: optimizes Aave/Compound rates via peer-to-peer matching. APY higher than base, but depends on peer-to-peer utilization.
Normalization: convert all rewards to USD by current price (Chainlink oracle), calculate total_yield_per_day / tvl * 365. This is comparable APY. For forward-looking: use 7-day moving average not spot rate.
Risks and protections
Strategy loss: strategy can lose funds (Aave, Compound, or other exploit). Vault must have loss reporting via report() without panic. OpenZeppelin Vault guard: if strategy reports loss >X% — emergency exit, funds withdrawn from strategy.
Withdrawal queue: user wants to withdraw $1M, but only $200K free in vault (rest in strategies). Need queue: first free balance, then gradual strategy withdraw in priority order (least risky first).
Rugpull via governance: vault owner shouldn't instantly transfer funds anywhere. All strategy changes — via 48-hour timelock. Emergency function (withdraw all) — only to pre-approved address (multisig).
Tools and stack
Foundry for contracts and tests. OpenZeppelin for basic blocks (ERC-4626 base implementation, AccessControl, ReentrancyGuard). The Graph for indexing vault events (deposits, withdrawals, harvests) — needed for analytics dashboard.
Off-chain keeper: TypeScript + viem, runs on Railway or own VPS. Cron-like scheduler checking harvest conditions every 15 minutes. Tenderly for simulation before sending real transactions.
Development process
Architecture and strategies (1 week). Choose target protocols, define allocation logic, gas threshold modeling.
Core vault + basic strategy (2 weeks). ERC-4626 vault, one strategy (Aave or Compound), keeper for harvest.
Additional strategies (1 week per strategy). Integrate with Curve, Morpho, Convex as needed.
APY engine + frontend data (1 week). Off-chain service normalizing APY, API for frontend.
Audit. Recommended for any public vault with others' funds.
Timeline: simple single-strategy vault on one network — 2-3 weeks. Multi-strategy with keeper, APY engine and analytics — 2-3 months. Cost calculated after clarifying target protocols.







