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.
DeFi's superpower is composability: one protocol's output is another's input. A user deposits ETH into Lido, gets stETH, deposits stETH into Aave as collateral, borrows USDC, deposits USDC into Yearn — earning yield at every layer. Knowing how to compose protocols safely is the difference between elegant DeFi infrastructure and a stack of cards.
Build a composed position in code.
Use these three in order. Each builds on the one before.
In one paragraph, explain DeFi composability and money legos.
Walk me through the Composer contract — each step and what it produces.
Given a 4-deep composition (Lido → Aave → Yearn → Curve LP), enumerate each smart-contract risk and design the audit checklist that covers all layers.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface ILido {
function submit(address _referral) external payable returns (uint256);
}
interface IAavePool {
function supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode) external;
function borrow(address asset, uint256 amount, uint256 interestRateMode, uint16 referralCode, address onBehalfOf) external;
}
contract Composer {
ILido constant LIDO = ILido(0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84);
IAavePool constant AAVE = IAavePool(0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2);
address constant STETH = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
function loop(uint256 borrowAmount) external payable {
// 1. ETH → stETH (Lido)
uint256 stETHReceived = LIDO.submit{value: msg.value}(address(0));
// 2. Supply stETH to Aave as collateral
IERC20(STETH).approve(address(AAVE), stETHReceived);
AAVE.supply(STETH, stETHReceived, msg.sender, 0);
// 3. Borrow USDC against stETH collateral
AAVE.borrow(USDC, borrowAmount, 2 /* variable rate */, 0, msg.sender);
// 4. Transfer borrowed USDC to user
IERC20(USDC).transfer(msg.sender, borrowAmount);
}
}
// User ends up with:
// - stETH supplied to Aave (earning ~3% staking yield + Aave deposit APY)
// - USDC in their wallet (to spend or deposit elsewhere)
// - Aave debt position (paying ~5% borrow APY)
//
// Net position: leveraged stETH exposure + USDC liquidity
// Risk: if stETH/ETH depegs OR liquidations trigger, user loses principal
//
// This is what "composability" looks like at the contract level:
// one transaction, four protocols, atomic.