Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
The deepest DeFi value comes from stacking protocols. But each composition adds risk. Learning to compose deliberately — knowing what each layer adds and what each layer costs — turns DeFi from gambling into engineering. This is the design pattern every yield protocol uses.
Build a 3-layer position.
Use these three in order. Each builds on the one before.
In one paragraph, explain a 3-layer composed DeFi position.
Walk me through the LayeredPosition contract and what each layer adds.
Given a yield aggregator strategy combining Aave + Curve + Yearn + Convex, design the audit + monitoring strategy.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
interface IVault {
function deposit(uint256 amount, address receiver) external returns (uint256);
function withdraw(uint256 shares, address receiver) external returns (uint256);
}
interface IAavePool {
function supply(address asset, uint256 amount, address onBehalfOf, uint16) external;
function withdraw(address asset, uint256 amount, address to) external returns (uint256);
}
interface ICurve {
function exchange(int128 i, int128 j, uint256 dx, uint256 minDy) external returns (uint256);
}
contract LayeredPosition {
using SafeERC20 for IERC20;
IAavePool constant AAVE = IAavePool(0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2);
ICurve constant CURVE_3POOL = ICurve(0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7);
IVault constant YEARN_USDC = IVault(0xa354F35829Ae975e850e23e9615b11Da1B3dC4DE);
address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
// Strategy: deposit USDC, supply to Aave, harvest yield, swap to DAI,
// deposit to Yearn USDC vault (which auto-compounds)
function enter(uint256 usdcAmount) external {
IERC20(USDC).safeTransferFrom(msg.sender, address(this), usdcAmount);
// Layer 1: supply USDC to Aave (earns ~3% APY)
IERC20(USDC).safeApprove(address(AAVE), usdcAmount);
AAVE.supply(USDC, usdcAmount, address(this), 0);
// Layer 2: swap aUSDC yield to DAI via Curve (when accrued)
// (in a real strategy this would happen on harvest; simplified here)
// Layer 3: deposit a portion to Yearn USDC vault (auto-compounding)
uint256 yearnAmount = usdcAmount / 5; // 20% to Yearn
IERC20(USDC).safeApprove(address(YEARN_USDC), yearnAmount);
YEARN_USDC.deposit(yearnAmount, address(this));
// User now has exposure to:
// Aave deposit yield (~3% APY)
// Curve 3pool swap fees on harvested rewards
// Yearn vault auto-compounding strategies (~5-15% APY)
// ...minus compounded risk at each layer.
}
}
// Risks to flag:
// 1. Aave smart contract risk (large but historically clean)
// 2. Curve 3pool smart contract + economic risk (USDC/DAI depegs)
// 3. Yearn strategy contract risk (multiple sub-strategies)
// 4. Composition risk: any layer pauses = stuck position
//
// Mitigations:
// Limit exposure per layer
// Set time locks for emergency withdrawal
// Monitor each protocol's risk parameters
// Don't compose 5+ deep without dramatic incentive