Skip to content

Commit

Permalink
Add event tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EridianAlpha committed Jun 20, 2024
1 parent 85bb54c commit 73a14a0
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 8 deletions.
2 changes: 2 additions & 0 deletions test/unit/AavePM/AaveBorrowAndWithdrawUSDCTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ contract AavePMBorrowAndWithdrawUSDCTests is AavePMTestSetup {
aavePM.aaveSupplyFromContractBalance();

// Borrow USDC immediately, without reinvesting
vm.expectEmit();
emit IAavePM.AaveBorrowedAndWithdrawnUSDC(owner1, USDC_BORROW_AMOUNT);
aavePM.aaveBorrowAndWithdrawUSDC(USDC_BORROW_AMOUNT, owner1);

// Check the USDC balance of owner1
Expand Down
5 changes: 5 additions & 0 deletions test/unit/AavePM/AavePMUpdateTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ contract AavePMUpdateTests is AavePMTestSetup {
uint16 newManagerDailyInvocationLimit =
previousManagerDailyInvocationLimit + MANAGER_DAILY_INVOCATION_LIMIT_CHANGE;

vm.expectEmit();
emit IAavePM.ManagerDailyInvocationLimitUpdated(
previousManagerDailyInvocationLimit, newManagerDailyInvocationLimit
);

vm.startPrank(owner1);
aavePM.updateManagerDailyInvocationLimit(newManagerDailyInvocationLimit);
assertEq(aavePM.getManagerDailyInvocationLimit(), newManagerDailyInvocationLimit);
Expand Down
4 changes: 4 additions & 0 deletions test/unit/AavePM/AaveRepayUSDCFromContractBalanceTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ contract AaveRepayUSDCFromContractBalanceTests is AavePMTestSetup {
assertEq(aavePM.getWithdrawnUSDCTotal(), USDC_BORROW_AMOUNT);

// Repay the USDC debt from the contract balance
vm.expectEmit(true, true, true, false);
emit IAavePM.AaveRepayedUSDCFromContractBalance(0); // The data is a placeholder and not checked
aavePM.aaveRepayUSDCFromContractBalance();

// Check that the debt has been repaid
Expand Down Expand Up @@ -139,6 +141,8 @@ contract AaveRepayUSDCFromContractBalanceTests is AavePMTestSetup {

// Close the position
vm.startPrank(manager1);
vm.expectEmit();
emit IAavePM.AaveClosedPosition(owner1);
aavePM.aaveClosePosition(owner1);
vm.stopPrank();

Expand Down
3 changes: 3 additions & 0 deletions test/unit/AavePM/AaveWithdrawWstETHTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ contract AaveWithdrawWstETHTests is AavePMTestSetup {
IPool(aavePM.getContractAddress("aavePool")).getUserAccountData(address(aavePM));

// Withdraw immediately

vm.expectEmit();
emit IAavePM.AaveWithdrawnWstETH(owner1, aavePM.getContractBalance("awstETH"));
uint256 collateralDeltaBase = aavePM.aaveWithdrawWstETH(aavePM.getContractBalance("awstETH"), owner1);

assertEq(collateralDeltaBase, totalCollateralBaseBefore);
Expand Down
7 changes: 7 additions & 0 deletions test/unit/AavePM/ContractUpgradeTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.24;

import {AavePMTestSetup} from "test/unit/AavePM/TestSetupTest.t.sol";

import {IAavePM} from "src/interfaces/IAavePM.sol";
import {AavePM} from "src/AavePM.sol";
import {ERC1967Utils} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Utils.sol";
import {InvalidUpgrade} from "test/testHelperContracts/InvalidUpgrade.sol";
Expand All @@ -19,8 +20,14 @@ contract AavePMContractUpgradeTests is AavePMTestSetup {
// Check version before upgrade
assertEq(keccak256(abi.encodePacked(aavePM.getVersion())), keccak256(abi.encodePacked(VERSION)));

// This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1
bytes32 slot = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
address previousImplementation = address(uint160(uint256(vm.load(address(aavePM), slot))));

// Upgrade
vm.prank(owner1);
vm.expectEmit();
emit IAavePM.AavePMUpgraded(previousImplementation, address(aavePMUpgradeExample));
aavePM.upgradeToAndCall(address(aavePMUpgradeExample), "");
assertEq(keccak256(abi.encodePacked(aavePM.getVersion())), keccak256(abi.encodePacked(UPGRADE_EXAMPLE_VERSION)));
}
Expand Down
6 changes: 6 additions & 0 deletions test/unit/AavePM/DeleverageTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ contract DeleverageTests is AavePMTestSetup {
// Get healthFactorTarget before deleverage.
uint16 healthFactorTargetBefore = aavePM.getHealthFactorTarget();

// The first parameter: Whether to check the event signature.
// The second parameter: Whether to check the indexed parameters (topics) of the event.
// The third parameter: Whether to check the unindexed parameters (data) of the event.
// The fourth parameter: Whether to check the event data's values.
vm.expectEmit(true, true, true, false);
emit IAavePM.Deleveraged(0); // The data is a placeholder and not checked
aavePM.deleverage();
// Check position debt is zero
(, uint256 totalDebtBaseAfter,,,,) =
Expand Down
15 changes: 7 additions & 8 deletions test/unit/AavePM/InitializeTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
pragma solidity 0.8.24;

import {AavePMTestSetup} from "test/unit/AavePM/TestSetupTest.t.sol";
import {Test, console} from "forge-std/Test.sol";

import {console} from "forge-std/Test.sol";
import {IAavePM} from "src/interfaces/IAavePM.sol";
import {AavePM} from "src/AavePM.sol";

import {HelperConfig} from "script/HelperConfig.s.sol";
import {DeployAavePM} from "script/DeployAavePM.s.sol";
import {HelperFunctions} from "script/HelperFunctions.s.sol";

// ================================================================
// │ INITIALIZE TESTS │
Expand All @@ -17,12 +23,5 @@ contract AavePMInitializeTests is AavePMTestSetup {

assert(aavePM.hasRole(keccak256("MANAGER_ROLE"), owner1));
assert(aavePM.getRoleAdmin(keccak256("MANAGER_ROLE")) == keccak256("OWNER_ROLE"));

// TODO: Update these tests to use eventBlockNumbers
// string memory currentVersion = aavePM.getVersion();
// string memory upgradeHistoryVersion = aavePM.getUpgradeHistory()[0].version;
// assert(keccak256(abi.encodePacked(currentVersion)) == keccak256(abi.encodePacked(upgradeHistoryVersion)));
// assert(aavePM.getUpgradeHistory()[0].upgradeTime == block.timestamp);
// assert(aavePM.getUpgradeHistory()[0].upgradeInitiator == defaultFoundryCaller);
}
}
7 changes: 7 additions & 0 deletions test/unit/AavePM/RebalanceTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ contract RebalanceTests is AavePMTestSetup {
// Setup contract using the standard rebalance test
test_RebalanceSetup();
vm.startPrank(manager1);

// The first parameter: Whether to check the event signature.
// The second parameter: Whether to check the indexed parameters (topics) of the event.
// The third parameter: Whether to check the unindexed parameters (data) of the event.
// The fourth parameter: Whether to check the event data's values.
vm.expectEmit(true, true, true, false);
emit IAavePM.Rebalanced(0); // The data is a placeholder and not checked
aavePM.rebalance();
checkEndHealthFactor(address(aavePM));
vm.stopPrank();
Expand Down
7 changes: 7 additions & 0 deletions test/unit/AavePM/ReinvestTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ contract ReinvestTests is AavePMTestSetup {
// Used as the setup for other reinvest tests.
vm.startPrank(manager1);
sendEth(address(aavePM), SEND_VALUE);

// The first parameter: Whether to check the event signature.
// The second parameter: Whether to check the indexed parameters (topics) of the event.
// The third parameter: Whether to check the unindexed parameters (data) of the event.
// The fourth parameter: Whether to check the event data's values.
vm.expectEmit(true, true, true, false);
emit IAavePM.Reinvested(0); // The data is a placeholder and not checked
aavePM.reinvest();
checkEndHealthFactor(address(aavePM));
vm.stopPrank();
Expand Down
2 changes: 2 additions & 0 deletions test/unit/AavePM/TestSetupTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ contract AavePMTestSetup is Test, HelperFunctions, AavePM {

// Call the _initialize function to set up this test contract,
// initialized with the same config as the AavePM contract
vm.expectEmit();
emit IAavePM.AavePMInitialized(msg.sender);
_initializeState(
defaultFoundryCaller,
config.contractAddresses,
Expand Down
9 changes: 9 additions & 0 deletions test/unit/AavePM/WithdrawTokensFromContractBalanceTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ contract WithdrawTokensFromContractBalanceTest is AavePMTestSetup {
function withdrawToken_SetUp() public {
vm.startPrank(manager1);
sendEth(address(aavePM), SEND_VALUE);

// The first parameter: Whether to check the event signature.
// The second parameter: Whether to check the indexed parameters (topics) of the event.
// The third parameter: Whether to check the unindexed parameters (data) of the event.
// The fourth parameter: Whether to check the event data's values.
vm.expectEmit(true, true, true, false);
emit IAavePM.AaveSuppliedFromContractBalance(0); // The data is a placeholder and not checked
aavePM.aaveSupplyFromContractBalance();
aavePM.aaveBorrowAndWithdrawUSDC(USDC_BORROW_AMOUNT, owner1);
aavePM.reinvest();
Expand Down Expand Up @@ -58,6 +65,8 @@ contract WithdrawTokensFromContractBalanceTest is AavePMTestSetup {
uint256 contractBalanceBefore = IERC20(USDC).balanceOf(address(aavePM));
uint256 ownerBalanceBefore = IERC20(USDC).balanceOf(owner1);

vm.expectEmit();
emit IAavePM.TokensWithdrawnFromContractBalance("USDC", contractBalanceBefore);
aavePM.withdrawTokensFromContractBalance("USDC", owner1);

uint256 contractBalanceAfter = IERC20(USDC).balanceOf(address(aavePM));
Expand Down

0 comments on commit 73a14a0

Please sign in to comment.