|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.10; |
| 3 | + |
| 4 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 5 | +import {MockERC20} from "./MockERC20.sol"; |
| 6 | +import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @title MockAavePool |
| 10 | + * @notice A simplified mock for Aave Pool to test interactions with AaveVault. |
| 11 | + */ |
| 12 | +contract MockAavePool { |
| 13 | + using SafeERC20 for IERC20; |
| 14 | + |
| 15 | + struct Reserve { |
| 16 | + uint256 totalLiquidity; // Total liquidity in the pool |
| 17 | + address aTokenAddress; // Address of the associated aToken |
| 18 | + } |
| 19 | + |
| 20 | + mapping(address => Reserve) public reserves; |
| 21 | + |
| 22 | + event Supply(address indexed user, address indexed asset, uint256 amount); |
| 23 | + event Withdraw(address indexed user, address indexed asset, uint256 amount); |
| 24 | + event YieldGenerated(address indexed user, address indexed asset, uint256 amount); |
| 25 | + |
| 26 | + /** |
| 27 | + * @notice Adds support for a new token and links it with an aToken. |
| 28 | + * @param underlyingToken Address of the underlying token. |
| 29 | + * @param aToken Address of the associated aToken. |
| 30 | + */ |
| 31 | + function addReserve(address underlyingToken, address aToken) external { |
| 32 | + require(reserves[underlyingToken].aTokenAddress == address(0), "MockAavePool: Reserve already exists"); |
| 33 | + reserves[underlyingToken] = Reserve({totalLiquidity: 0, aTokenAddress: aToken}); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @notice Supplies liquidity to the pool and mints aTokens. |
| 38 | + * @param asset Address of the token to deposit. |
| 39 | + * @param amount Amount of tokens to deposit. |
| 40 | + * @param onBehalfOf Address to mint aTokens for. |
| 41 | + * @dev referralCode Referral code (ignored in mock implementation). |
| 42 | + */ |
| 43 | + function supply(address asset, uint256 amount, address onBehalfOf, uint16) external { |
| 44 | + Reserve storage reserve = reserves[asset]; |
| 45 | + require(reserve.aTokenAddress != address(0), "MockAavePool: Unsupported asset"); |
| 46 | + |
| 47 | + IERC20(asset).safeTransferFrom(msg.sender, address(this), amount); |
| 48 | + IERC20(reserve.aTokenAddress).safeTransfer(onBehalfOf, amount); // Mint aTokens 1:1 |
| 49 | + |
| 50 | + reserve.totalLiquidity += amount; |
| 51 | + |
| 52 | + emit Supply(onBehalfOf, asset, amount); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @notice Withdraws liquidity from the pool and burns aTokens. |
| 57 | + * @param asset Address of the token to withdraw. |
| 58 | + * @param amount Amount of tokens to withdraw. |
| 59 | + * @param to Address to send the withdrawn tokens to. |
| 60 | + */ |
| 61 | + function withdraw(address asset, uint256 amount, address to) external { |
| 62 | + Reserve storage reserve = reserves[asset]; |
| 63 | + require(reserve.aTokenAddress != address(0), "MockAavePool: Unsupported asset"); |
| 64 | + |
| 65 | + IERC20(asset).safeTransfer(to, amount); |
| 66 | + |
| 67 | + reserve.totalLiquidity -= amount; |
| 68 | + |
| 69 | + emit Withdraw(msg.sender, asset, amount); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @notice Generates yield by minting additional aTokens for a user. |
| 74 | + * @param asset Address of the underlying token. |
| 75 | + * @param amount Amount of yield to generate. |
| 76 | + * @param user Address of the user to mint aTokens for. |
| 77 | + */ |
| 78 | + function generateYield(address asset, uint256 amount, address user) external { |
| 79 | + Reserve storage reserve = reserves[asset]; |
| 80 | + require(reserve.aTokenAddress != address(0), "MockAavePool: Unsupported asset"); |
| 81 | + |
| 82 | + IERC20(reserve.aTokenAddress).safeTransfer(user, amount); // Mint yield as aTokens |
| 83 | + |
| 84 | + emit YieldGenerated(user, asset, amount); |
| 85 | + } |
| 86 | +} |
0 commit comments