DeFi协议开发实战:从AMM到借贷平台的完整指南
引言 DeFi(去中心化金融)是WEB3最重要的应用场景之一。从AMM到借贷协议,DeFi正在重塑传统金融。本文将深入探讨DeFi协议的核心机制和开发实践。 AMM(自动做市商) 恒定乘积AMM(Uniswap V2) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; contract UniswapV2Pair is ReentrancyGuard { string public constant name = "Uniswap V2 Pair"; string public constant symbol = "UNI-V2"; uint256 public constant MINIMUM_LIQUIDITY = 1000; address public token0; address public token1; uint256 public reserve0; uint256 public reserve1; uint256 public totalSupply; mapping(address => uint256) public balanceOf; event Mint(address indexed sender, uint256 amount0, uint256 amount1); event Burn(address indexed sender, uint256 amount0, uint256 amount1); event Swap( address indexed sender, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out, address indexed to ); event Sync(uint256 reserve0, uint256 reserve1); constructor() { factory = msg.sender; } function initialize(address _token0, address _token1) external { require(msg.sender == factory, "Forbidden"); require(_token0 < _token1, "Invalid tokens"); token0 = _token0; token1 = _token1; } // 添加流动性 function mint(address to) external nonReentrant returns (uint256 liquidity) { (uint256 reserve0_, uint256 reserve1_) = getReserves(); uint256 balance0 = IERC20(token0).balanceOf(address(this)); uint256 balance1 = IERC20(token1).balanceOf(address(this)); uint256 amount0 = balance0 - reserve0_; uint256 amount1 = balance1 - reserve1_; uint256 _totalSupply = totalSupply; if (_totalSupply == 0) { // 首次添加流动性 liquidity = Math.sqrt(amount0 * amount1) - MINIMUM_LIQUIDITY; _mint(address(0), MINIMUM_LIQUIDITY); // 永久锁定最小流动性 } else { liquidity = Math.min( (amount0 * _totalSupply) / reserve0_, (amount1 * _totalSupply) / reserve1_ ); } require(liquidity > 0, "Insufficient liquidity minted"); _mint(to, liquidity); _update(balance0, balance1); emit Mint(to, amount0, amount1); } // 移除流动性 function burn(address to) external nonReentrant returns (uint256 amount0, uint256 amount1) { uint256 liquidity = balanceOf[address(this)]; require(liquidity > 0, "No liquidity"); (uint256 reserve0_, uint256 reserve1_) = getReserves(); uint256 _totalSupply = totalSupply; amount0 = (liquidity * reserve0_) / _totalSupply; amount1 = (liquidity * reserve1_) / _totalSupply; _burn(address(this), liquidity); _transfer( token0, address(this), to, amount0 ); _transfer( token1, address(this), to, amount1 ); (uint256 balance0, uint256 balance1) = getBalances(); _update(balance0, balance1); emit Burn(to, amount0, amount1); } // 交换(核心功能) function swap( uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data ) external nonReentrant { require( amount0Out > 0 || amount1Out > 0, "Insufficient output amount" ); (uint256 reserve0_, uint256 reserve1_) = getReserves(); if (amount0Out > 0) { uint256 amount0In = getInputAmount( amount0Out, reserve0_, reserve1_ ); require( amount0In <= reserve0_ - amount0Out, "Insufficient liquidity" ); uint256 balance0Before = IERC20(token0).balanceOf(address(this)); _transfer(token0, msg.sender, address(this), amount0In); uint256 balance0After = IERC20(token0).balanceOf(address(this)); amount0In = balance0After - balance0Before; _transfer(token1, address(this), to, amount0Out); } if (amount1Out > 0) { uint256 amount1In = getInputAmount( amount1Out, reserve1_, reserve0_ ); require( amount1In <= reserve1_ - amount1Out, "Insufficient liquidity" ); uint256 balance1Before = IERC20(token1).balanceOf(address(this)); _transfer(token1, msg.sender, address(this), amount1In); uint256 balance1After = IERC20(token1).balanceOf(address(this)); amount1In = balance1After - balance1Before; _transfer(token0, address(this), to, amount1Out); } (uint256 balance0, uint256 balance1) = getBalances(); _update(balance0, balance1); emit Swap( msg.sender, amount0In, amount1In, amount0Out, amount1Out, to ); } // 计算输入量(恒定乘积公式) function getInputAmount( uint256 outputAmount, uint256 inputReserve, uint256 outputReserve ) public pure returns (uint256 inputAmount) { require(inputReserve > 0 && outputReserve > 0, "Invalid reserves"); require(outputAmount < outputReserve, "Output amount too high"); uint256 numerator = inputReserve * outputAmount * 1000; uint256 denominator = (outputReserve - outputAmount) * 997; return (numerator / denominator) + 1; } // 滑点计算 function getAmountOut( uint256 amountIn, uint256 reserveIn, uint256 reserveOut ) public pure returns (uint256 amountOut) { require(amountIn > 0, "Insufficient input amount"); require(reserveIn > 0 && reserveOut > 0, "Invalid reserves"); uint256 amountInWithFee = amountIn * 997; uint256 numerator = amountInWithFee * reserveOut; uint256 denominator = reserveIn * 1000 + amountInWithFee; return numerator / denominator; } function getReserves() public view returns (uint256, uint256) { return (reserve0, reserve1); } function _update(uint256 balance0, uint256 balance1) private { reserve0 = balance0; reserve1 = balance1; emit Sync(balance0, balance1); } function _mint(address to, uint256 amount) private { totalSupply += amount; balanceOf[to] += amount; } function _burn(address from, uint256 amount) private { require(balanceOf[from] >= amount, "Insufficient balance"); balanceOf[from] -= amount; totalSupply -= amount; } function _transfer( address token, address from, address to, uint256 amount ) private { IERC20(token).transferFrom(from, to, amount); } function getBalances() public view returns (uint256, uint256) { return ( IERC20(token0).balanceOf(address(this)), IERC20(token1).balanceOf(address(this)) ); } } 集中流动性(Uniswap V3) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 // Uniswap V3核心概念 contract UniswapV3Pool { struct Position { uint96 nonce; address operator; address token0; address token1; int24 tickLower; int24 tickUpper; uint128 liquidity; uint256 feeGrowthInside0LastX128; uint256 feeGrowthInside1LastX128; uint128 tokensOwed0; uint128 tokensOwed1; } // Tick(价格)概念 // 价格 = 1.0001^tick // 例如:tick = 1000 => price = 1.0001^1000 ≈ 1.105 int24 internal constant MIN_TICK = -887272; int24 internal constant MAX_TICK = 887272; function getRatioFromTick(int24 tick) public pure returns (uint256) { uint256 ratio = 1.0001e18; int24 absTick = tick < 0 ? -tick : tick; for (int i = 0; i < absTick; i++) { if (tick < 0) { ratio = (ratio * 1e18) / 1000100000000000000; // /1.0001 } else { ratio = (ratio * 1000100000000000000) / 1e18; // *1.0001 } } return ratio; } function getTickFromRatio(uint256 ratio) public pure returns (int24 tick) { // 二分查找 int24 low = MIN_TICK; int24 high = MAX_TICK; while (low < high) { int24 mid = (low + high + 1) / 2; uint256 midRatio = getRatioFromTick(mid); if (ratio < midRatio) { high = mid - 1; } else { low = mid; } } return low; } // 流动性计算 function getLiquidityForAmounts( uint160 sqrtRatioAX96, uint160 sqrtRatioBX96, uint256 amount0, uint256 amount1 ) public pure returns (uint128 liquidity) { if (sqrtRatioAX96 > sqrtRatioBX96) (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96); uint256 intermediate = sqrtRatioAX96 * sqrtRatioBX96 / 96; uint256 amount0Intermediate = (amount0 * intermediate) / sqrtRatioBX96; if (amount0Intermediate <= amount1) { liquidity = uint128(amount0Intermediate); } else { liquidity = uint128((amount1 * sqrtRatioAX96 * sqrtRatioBX96) / 96); } } function getPositionAmounts( uint160 sqrtPriceX96, int24 tickLower, int24 tickUpper, uint128 liquidity ) public pure returns (uint256 amount0, uint256 amount1) { uint160 sqrtRatioAX96 = getSqrtRatioAtTick(tickLower); uint160 sqrtRatioBX96 = getSqrtRatioAtTick(tickUpper); if (sqrtPriceX96 <= sqrtRatioAX96) { amount0 = getAmount0ForLiquidity( sqrtRatioAX96, sqrtRatioBX96, liquidity ); } else if (sqrtPriceX96 < sqrtRatioBX96) { amount0 = getAmount0ForLiquidity( sqrtPriceX96, sqrtRatioBX96, liquidity ); amount1 = getAmount1ForLiquidity( sqrtRatioAX96, sqrtPriceX96, liquidity ); } else { amount1 = getAmount1ForLiquidity( sqrtRatioAX96, sqrtRatioBX96, liquidity ); } } } 借贷协议 Compound风格借贷 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; contract LendingPool is ReentrancyGuard { struct Reserve { uint256 totalSupply; uint256 totalBorrowed; uint256 borrowRate; uint256 supplyRate; uint256 lastUpdate; uint256 index; } struct UserState { uint256 supplied; uint256 borrowed; uint256 borrowIndex; uint256 supplyIndex; uint256 collateralFactor; } mapping(address => Reserve) public reserves; mapping(address => UserState) public users; mapping(address => address[]) public userAssets; address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; uint256 public constant COLLATERAL_FACTOR = 750; // 75% event Supply(address indexed user, address indexed asset, uint256 amount); event Borrow(address indexed user, address indexed asset, uint256 amount); event Repay(address indexed user, address indexed asset, uint256 amount); event Withdraw(address indexed user, address indexed asset, uint256 amount); event Liquidate(address indexed user, address indexed borrower, address indexed asset, uint256 amount); // 供应资产 function supply(address asset, uint256 amount) external nonReentrant { require(amount > 0, "Amount must be greater than 0"); IERC20(asset).transferFrom(msg.sender, address(this), amount); // 更新累计指数 _accrueInterest(asset); // 更新用户状态 UserState storage user = users[msg.sender]; Reserve storage reserve = reserves[asset]; uint256 userSupply = (amount * reserve.index) / 1e18; user.supplied += userSupply; user.supplyIndex = reserve.index; reserve.totalSupply += userSupply; // 记录用户资产 if (!_hasAsset(msg.sender, asset)) { userAssets[msg.sender].push(asset); } emit Supply(msg.sender, asset, amount); } // 借款 function borrow( address asset, uint256 amount ) external nonReentrant { require(amount > 0, "Amount must be greater than 0"); // 更新累计指数 _accrueInterest(asset); UserState storage user = users[msg.sender]; Reserve storage reserve = reserves[asset]; // 检查抵押品 uint256 maxBorrow = _getMaxBorrow(msg.sender); uint256 currentBorrow = _getUserBorrow(msg.sender); require( currentBorrow + amount <= maxBorrow, "Insufficient collateral" ); // 更新借款 uint256 borrowAmount = (amount * 1e18) / reserve.index; user.borrowed += borrowAmount; user.borrowIndex = reserve.index; reserve.totalBorrowed += borrowAmount; // 转出资产 IERC20(asset).transfer(msg.sender, amount); emit Borrow(msg.sender, asset, amount); } // 还款 function repay(address asset, uint256 amount) external nonReentrant { require(amount > 0, "Amount must be greater than 0"); // 更新累计指数 _accrueInterest(asset); UserState storage user = users[msg.sender]; Reserve storage reserve = reserves[asset]; // 计算实际债务 uint256 debt = _getUserBorrow(msg.sender); if (amount >= debt) { amount = debt; // 如果还清,可以提取抵押品 } IERC20(asset).transferFrom(msg.sender, address(this), amount); uint256 repayAmount = (amount * 1e18) / reserve.index; user.borrowed -= repayAmount; reserve.totalBorrowed -= (amount * 1e18) / reserve.index; emit Repay(msg.sender, asset, amount); } // 提取供应的资产 function withdraw(address asset, uint256 amount) external nonReentrant { require(amount > 0, "Amount must be greater than 0"); // 更新累计指数 _accrueInterest(asset); UserState storage user = users[msg.sender]; Reserve storage reserve = reserves[asset]; // 检查是否有未还贷款 uint256 currentBorrow = _getUserBorrow(msg.sender); uint256 maxBorrow = _getMaxBorrow(msg.sender); require( currentBorrow <= maxBorrow, "Cannot withdraw: insufficient collateral" ); // 计算可提取金额 uint256 userSupply = _getUserSupply(msg.sender, asset); require(userSupply >= amount, "Insufficient balance"); uint256 withdrawAmount = (amount * reserve.index) / 1e18; user.supplied -= withdrawAmount; reserve.totalSupply -= withdrawAmount; IERC20(asset).transfer(msg.sender, amount); emit Withdraw(msg.sender, asset, amount); } // 清算 function liquidate( address borrower, address asset, uint256 amount ) external nonReentrant { // 更新累计指数 _accrueInterest(asset); UserState storage user = users[borrower]; // 检查是否需要清算 uint256 currentBorrow = _getUserBorrow(borrower); uint256 maxBorrow = _getMaxBorrow(borrower); require( currentBorrow > maxBorrow, "Not eligible for liquidation" ); // 扣除抵押品 // 这里简化处理,实际需要拍卖机制 emit Liquidate(msg.sender, borrower, asset, amount); } // 计算累计利息 function _accrueInterest(address asset) internal { Reserve storage reserve = reserves[asset]; uint256 timeElapsed = block.timestamp - reserve.lastUpdate; if (timeElapsed == 0) return; // 简化的利息计算 uint256 interest = (reserve.totalBorrowed * reserve.borrowRate * timeElapsed) / (365 days * 1e18); uint256 supplyInterest = (reserve.totalSupply * reserve.supplyRate * timeElapsed) / (365 days * 1e18); reserve.totalBorrowed += interest; reserve.totalSupply += supplyInterest; reserve.lastUpdate = block.timestamp; } function _getUserSupply( address user, address asset ) internal view returns (uint256) { Reserve storage reserve = reserves[asset]; UserState storage userState = users[user]; if (userState.supplyIndex == 0) { return 0; } return (userState.supplied * reserve.index) / userState.supplyIndex; } function _getUserBorrow(address user) internal view returns (uint256) { uint256 totalBorrow = 0; for (uint256 i = 0; i < userAssets[user].length; i++) { address asset = userAssets[user][i]; UserState storage userState = users[user]; Reserve storage reserve = reserves[asset]; if (userState.borrowed > 0) { uint256 borrow = (userState.borrowed * reserve.index) / userState.borrowIndex; totalBorrow += borrow; } } return totalBorrow; } function _getMaxBorrow(address user) internal view returns (uint256) { uint256 totalCollateral = 0; for (uint256 i = 0; i < userAssets[user].length; i++) { address asset = userAssets[user][i]; uint256 supply = _getUserSupply(user, asset); // 假设所有资产都有同样的抵押因子 totalCollateral += (supply * COLLATERAL_FACTOR) / 1000; } return totalCollateral; } function _hasAsset(address user, address asset) internal view returns (bool) { for (uint256 i = 0; i < userAssets[user].length; i++) { if (userAssets[user][i] == asset) { return true; } } return false; } } 收益聚合器(Yearn风格) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract YieldAggregator is Ownable, ReentrancyGuard { struct Strategy { address strategy; uint256 allocation; // 分配比例(基点) uint256 performanceFee; bool active; } mapping(address => Strategy) public strategies; address[] public strategyList; uint256 public constant MAX_ALLOCATION = 10000; // 100% uint256 public constant PERFORMANCE_FEE = 1000; // 10% uint256 public totalShares; mapping(address => uint256) public shares; mapping(address => uint256) public userPrincipal; event Deposit(address indexed user, uint256 amount); event Withdraw(address indexed user, uint256 amount); event Harvest(address indexed strategy, uint256 amount); event Rebalance(address[] strategies, uint256[] allocations); // 存款 function deposit(uint256 amount) external payable nonReentrant { require(amount > 0, "Amount must be greater than 0"); // 转入资产 if (msg.value > 0) { require(amount == msg.value, "ETH amount mismatch"); } else { IERC20(WETH).transferFrom(msg.sender, address(this), amount); IWETH(WETH).deposit{value: amount}(); } // 计算份额 uint256 shares; if (totalShares == 0) { shares = amount; } else { shares = (amount * totalShares) / totalAssets(); } shares[msg.sender] += shares; totalShares += shares; userPrincipal[msg.sender] += amount; // 分配到各个策略 _rebalance(); emit Deposit(msg.sender, amount); } // 提款 function withdraw(uint256 shares) external nonReentrant { require(shares > 0, "Shares must be greater than 0"); require(shares[msg.sender] >= shares, "Insufficient shares"); // 计算可提取金额 uint256 assets = (totalAssets() * shares) / totalShares; // 从策略中提取 _withdrawFromStrategies(assets); // 转出资产 if (address(this).balance >= assets) { payable(msg.sender).transfer(assets); } else { IWETH(WETH).withdraw(assets); payable(msg.sender).transfer(assets); } // 更新份额 shares[msg.sender] -= shares; totalShares -= shares; uint256 principal = (userPrincipal[msg.sender] * shares) / (shares + shares[msg.sender]); userPrincipal[msg.sender] -= principal; emit Withdraw(msg.sender, assets); } // 收获收益 function harvest(address strategy) external onlyOwner { Strategy storage s = strategies[strategy]; require(s.active, "Strategy not active"); // 调用策略的harvest函数 uint256 beforeBalance = address(this).balance; IStrategy(strategy).harvest(); uint256 afterBalance = address(this).balance; uint256 profit = afterBalance - beforeBalance; if (profit > 0) { // 提取性能费 uint256 fee = (profit * PERFORMANCE_FEE) / 10000; uint256 performanceFee = (fee * s.performanceFee) / 10000; payable(owner()).transfer(performanceFee); emit Harvest(strategy, profit); } } // 重新平衡 function _rebalance() internal { uint256 totalAssets = address(this).balance; for (uint256 i = 0; i < strategyList.length; i++) { Strategy storage s = strategies[strategyList[i]]; if (s.active && s.allocation > 0) { uint256 amount = (totalAssets * s.allocation) / MAX_ALLOCATION; IStrategy(s.strategy).invest{value: amount}(); } } } function _withdrawFromStrategies(uint256 amount) internal { uint256 withdrawn; for (uint256 i = 0; i < strategyList.length; i++) { Strategy storage s = strategies[strategyList[i]]; if (s.active && withdrawn < amount) { uint256 toWithdraw = amount - withdrawn; uint256 available = IStrategy(s.strategy).withdraw(toWithdraw); withdrawn += available; if (withdrawn >= amount) { break; } } } } function totalAssets() public view returns (uint256) { uint256 total = address(this).balance; for (uint256 i = 0; i < strategyList.length; i++) { Strategy storage s = strategies[strategyList[i]]; if (s.active) { total += IStrategy(s.strategy).estimatedTotalAssets(); } } return total; } // 添加策略 function addStrategy( address _strategy, uint256 _allocation ) external onlyOwner { require(_strategy != address(0), "Invalid strategy"); strategies[_strategy] = Strategy({ strategy: _strategy, allocation: _allocation, performanceFee: 5000, // 50% active: true }); strategyList.push(_strategy); _validateAllocations(); } // 更新分配比例 function updateAllocation( address _strategy, uint256 _allocation ) external onlyOwner { strategies[_strategy].allocation = _allocation; _validateAllocations(); _rebalance(); } function _validateAllocations() internal view { uint256 total; for (uint256 i = 0; i < strategyList.length; i++) { Strategy storage s = strategies[strategyList[i]]; if (s.active) { total += s.allocation; } } require(total <= MAX_ALLOCATION, "Total allocation exceeds 100%"); } } interface IStrategy { function invest(uint256 amount) external; function withdraw(uint256 amount) external returns (uint256); function harvest() external; function estimatedTotalAssets() external view returns (uint256); } interface IWETH { function deposit() external payable; function withdraw(uint256 wad) external; } 总结 DeFi协议开发需要深入理解: ...