Skip to content

Commit 5bf9ed4

Browse files
craterface77Haypierre
authored andcommitted
Tests: Added testcases for AaveVault.sol + mock contracts
1 parent d882c4e commit 5bf9ed4

File tree

8 files changed

+806
-16
lines changed

8 files changed

+806
-16
lines changed

src/mocks/MockAavePool.sol

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

src/mocks/MockERC20.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ contract MockERC20 is ERC20 {
99
function mint(address sender, uint256 amount) external {
1010
_mint(sender, amount);
1111
}
12+
13+
function burn(address sender, uint256 amount) external {
14+
_burn(sender, amount);
15+
}
1216
}

src/mocks/MockL2AavePool.sol

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
import {CalldataLogic} from "aave-v3-origin/core/contracts/protocol/libraries/logic/CalldataLogic.sol";
8+
9+
/**
10+
* @title MockL2AavePool
11+
* @notice A simplified mock for Aave L2 Pool to test interactions with AaveVault on L2.
12+
*/
13+
contract MockL2AavePool {
14+
using SafeERC20 for IERC20;
15+
16+
struct Reserve {
17+
uint256 totalLiquidity;
18+
address aTokenAddress;
19+
address asset;
20+
}
21+
22+
Reserve public reserve;
23+
24+
function addReserve(address underlyingToken, address aToken) external {
25+
reserve = Reserve({totalLiquidity: 0, aTokenAddress: aToken, asset: underlyingToken});
26+
}
27+
28+
function supply(bytes32 args) external {
29+
// Simulate supply logic. Simulate amount just for testing purposes
30+
IERC20(reserve.asset).safeTransferFrom(msg.sender, address(this), 100 ether); // Transfer underlying to pool
31+
IERC20(reserve.aTokenAddress).safeTransfer(msg.sender, 100 ether); // Mint aTokens to user
32+
33+
reserve.totalLiquidity += 100 ether;
34+
}
35+
36+
function withdraw(bytes32 args) external {
37+
// Simulate withdraw logic. Simulate amount just for testing purposes
38+
IERC20(reserve.asset).safeTransfer(msg.sender, 100 ether); // Transfer underlying to user
39+
40+
reserve.totalLiquidity -= 100 ether;
41+
}
42+
43+
function getTotalLiquidity() external view returns (uint256) {
44+
return reserve.totalLiquidity;
45+
}
46+
}

src/mocks/MockL2Encoder.sol

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import {IPool} from "aave-v3-origin/core/contracts/interfaces/IPool.sol";
5+
6+
/**
7+
* @title MockL2Encoder
8+
* @dev A mock implementation of the L2Encoder for testing purposes.
9+
*/
10+
contract MockL2Encoder {
11+
IPool public pool;
12+
13+
/**
14+
* @notice Constructor to initialize the mock with a reference to the Aave Pool.
15+
* @param _pool Address of the Aave Pool.
16+
*/
17+
constructor(address _pool) {
18+
pool = IPool(_pool);
19+
}
20+
21+
/**
22+
* @notice Encodes the parameters for a supply operation.
23+
* @param asset The address of the asset being supplied.
24+
* @param amount The amount of the asset being supplied.
25+
* @param referralCode The referral code for the supply operation.
26+
* @return Encoded parameters as bytes32.
27+
*/
28+
function encodeSupplyParams(address asset, uint256 amount, uint16 referralCode) external pure returns (bytes32) {
29+
return keccak256(abi.encodePacked(asset, amount, referralCode));
30+
}
31+
32+
/**
33+
* @notice Encodes the parameters for a withdrawal operation.
34+
* @param asset The address of the asset being withdrawn.
35+
* @param amount The amount of the asset being withdrawn.
36+
* @return Encoded parameters as bytes32.
37+
*/
38+
function encodeWithdrawParams(address asset, uint256 amount) external pure returns (bytes32) {
39+
return keccak256(abi.encodePacked(asset, amount));
40+
}
41+
}

0 commit comments

Comments
 (0)