Skip to content

Commit

Permalink
feat: improve v3 tests (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra authored Jun 6, 2023
1 parent ccadc3c commit 7aebb79
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
49 changes: 49 additions & 0 deletions src/ProtocolV3TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct ReserveConfig {
bool borrowingEnabled;
address interestRateStrategy;
bool stableBorrowRateEnabled;
bool isPaused;
bool isActive;
bool isFrozen;
bool isSiloed;
Expand Down Expand Up @@ -108,6 +109,46 @@ contract ProtocolV3TestBase is CommonTestBase {
vm.revertTo(snapshot);
}

function e2eTestAsset(
IPool pool,
ReserveConfig memory collateralConfig,
ReserveConfig memory testAssetConfig
) public {
address collateralSupplier = vm.addr(3);
address testAssetSupplier = vm.addr(4);
require(collateralConfig.usageAsCollateralEnabled, 'COLLATERAL_CONFIG_MUST_BE_COLLATERAL');
uint256 testAssetAmount = 10 * 10 ** testAssetConfig.decimals;
_deposit(collateralConfig, pool, collateralSupplier, 100_000 * 10 ** collateralConfig.decimals);
_deposit(testAssetConfig, pool, testAssetSupplier, testAssetAmount);
uint256 snapshot = vm.snapshot();
// test withdrawal
assertGe(
_withdraw(testAssetConfig, pool, testAssetSupplier, type(uint256).max),
testAssetAmount
);
assertEq(IERC20(testAssetConfig.aToken).balanceOf(testAssetSupplier), 0);
vm.revertTo(snapshot);
if (testAssetConfig.borrowingEnabled) {
_e2eTestBorrowRepay(pool, collateralSupplier, testAssetConfig, testAssetAmount, false);
vm.revertTo(snapshot);
if (testAssetConfig.stableBorrowRateEnabled) {
_e2eTestBorrowRepay(pool, collateralSupplier, testAssetConfig, testAssetAmount, true);
vm.revertTo(snapshot);
}
}
}

function _e2eTestBorrowRepay(
IPool pool,
address borrower,
ReserveConfig memory testAssetConfig,
uint256 amount,
bool stable
) internal {
this._borrow(testAssetConfig, pool, borrower, amount, stable);
_repay(testAssetConfig, pool, borrower, amount, stable);
}

/**
* @dev returns the first collateral in the list that cannot be borrowed in stable mode
*/
Expand Down Expand Up @@ -183,6 +224,10 @@ contract ProtocolV3TestBase is CommonTestBase {
address user,
uint256 amount
) internal {
require(!config.isFrozen, 'DEPOSIT(): FROZEN_RESERVE');
require(config.isActive, 'DEPOSIT(): INACTIVE_RESERVE');
require(!config.isPaused, 'DEPOSIT(): PAUSED_RESERVE');
console.log(config.isPaused);
vm.startPrank(user);
uint256 aTokenBefore = IERC20(config.aToken).balanceOf(user);
deal(config.underlying, user, amount);
Expand Down Expand Up @@ -370,6 +415,7 @@ contract ProtocolV3TestBase is CommonTestBase {
vm.serializeBool(key, 'usageAsCollateralEnabled', config.usageAsCollateralEnabled);
vm.serializeBool(key, 'borrowingEnabled', config.borrowingEnabled);
vm.serializeBool(key, 'stableBorrowRateEnabled', config.stableBorrowRateEnabled);
vm.serializeBool(key, 'isPaused', config.isPaused);
vm.serializeBool(key, 'isActive', config.isActive);
vm.serializeBool(key, 'isFrozen', config.isFrozen);
vm.serializeBool(key, 'isSiloed', config.isSiloed);
Expand Down Expand Up @@ -535,6 +581,7 @@ contract ProtocolV3TestBase is CommonTestBase {
localConfig.interestRateStrategy = pool
.getReserveData(reserve.tokenAddress)
.interestRateStrategyAddress;
localConfig.isPaused = pdp.getPaused(reserve.tokenAddress);
localConfig.isActive = isActive;
localConfig.isFrozen = isFrozen;
localConfig.isSiloed = pdp.getSiloedBorrowing(reserve.tokenAddress);
Expand Down Expand Up @@ -574,6 +621,7 @@ contract ProtocolV3TestBase is CommonTestBase {
borrowingEnabled: config.borrowingEnabled,
interestRateStrategy: config.interestRateStrategy,
stableBorrowRateEnabled: config.stableBorrowRateEnabled,
isPaused: config.isPaused,
isActive: config.isActive,
isFrozen: config.isFrozen,
isSiloed: config.isSiloed,
Expand Down Expand Up @@ -1038,6 +1086,7 @@ contract ProtocolV3_0_1TestBase is ProtocolV3TestBase {
localConfig.interestRateStrategy = pool
.getReserveData(reserve.tokenAddress)
.interestRateStrategyAddress;
localConfig.isPaused = pdp.getPaused(reserve.tokenAddress);
localConfig.isActive = isActive;
localConfig.isFrozen = isFrozen;
localConfig.isSiloed = pdp.getSiloedBorrowing(reserve.tokenAddress);
Expand Down
9 changes: 8 additions & 1 deletion src/test/AaveV3ConfigEngineTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ contract AaveV3ConfigEngineTest is ProtocolV3TestBase {
interestRateStrategy: _findReserveConfigBySymbol(allConfigsAfter, 'AAVE')
.interestRateStrategy,
stableBorrowRateEnabled: false,
isPaused: false,
isActive: true,
isFrozen: false,
isSiloed: false,
Expand Down Expand Up @@ -154,6 +155,7 @@ contract AaveV3ConfigEngineTest is ProtocolV3TestBase {
interestRateStrategy: _findReserveConfigBySymbol(allConfigsAfter, 'AAVE')
.interestRateStrategy,
stableBorrowRateEnabled: true,
isPaused: false,
isActive: true,
isFrozen: false,
isSiloed: false,
Expand Down Expand Up @@ -235,6 +237,7 @@ contract AaveV3ConfigEngineTest is ProtocolV3TestBase {
borrowingEnabled: allConfigsBefore[6].borrowingEnabled,
interestRateStrategy: allConfigsBefore[6].interestRateStrategy,
stableBorrowRateEnabled: allConfigsBefore[6].stableBorrowRateEnabled,
isPaused: allConfigsBefore[6].isPaused,
isActive: allConfigsBefore[6].isActive,
isFrozen: allConfigsBefore[6].isFrozen,
isSiloed: allConfigsBefore[6].isSiloed,
Expand Down Expand Up @@ -290,6 +293,7 @@ contract AaveV3ConfigEngineTest is ProtocolV3TestBase {
borrowingEnabled: allConfigsBefore[6].borrowingEnabled,
interestRateStrategy: allConfigsBefore[6].interestRateStrategy,
stableBorrowRateEnabled: allConfigsBefore[6].stableBorrowRateEnabled,
isPaused: allConfigsBefore[6].isPaused,
isActive: allConfigsBefore[6].isActive,
isFrozen: allConfigsBefore[6].isFrozen,
isSiloed: allConfigsBefore[6].isSiloed,
Expand Down Expand Up @@ -376,6 +380,7 @@ contract AaveV3ConfigEngineTest is ProtocolV3TestBase {
borrowingEnabled: allConfigsBefore[6].borrowingEnabled,
interestRateStrategy: allConfigsBefore[6].interestRateStrategy,
stableBorrowRateEnabled: allConfigsBefore[6].stableBorrowRateEnabled,
isPaused: allConfigsBefore[6].isPaused,
isActive: allConfigsBefore[6].isActive,
isFrozen: allConfigsBefore[6].isFrozen,
isSiloed: allConfigsBefore[6].isSiloed,
Expand Down Expand Up @@ -406,7 +411,7 @@ contract AaveV3ConfigEngineTest is ProtocolV3TestBase {
payload.execute();
}

function testCollateralUpdateCorrectBonus() public {
function testCollateralUpdateCorrectBonus() public {
vm.createSelectFork(vm.rpcUrl('avalanche'), 30344870);

IAaveV3ConfigEngine engine = IAaveV3ConfigEngine(DeployEngineAvaLib.deploy());
Expand Down Expand Up @@ -446,6 +451,7 @@ function testCollateralUpdateCorrectBonus() public {
borrowingEnabled: allConfigsBefore[6].borrowingEnabled,
interestRateStrategy: allConfigsBefore[6].interestRateStrategy,
stableBorrowRateEnabled: allConfigsBefore[6].stableBorrowRateEnabled,
isPaused: allConfigsBefore[6].isPaused,
isActive: allConfigsBefore[6].isActive,
isFrozen: allConfigsBefore[6].isFrozen,
isSiloed: allConfigsBefore[6].isSiloed,
Expand Down Expand Up @@ -498,6 +504,7 @@ function testCollateralUpdateCorrectBonus() public {
borrowingEnabled: true,
interestRateStrategy: allConfigsBefore[6].interestRateStrategy,
stableBorrowRateEnabled: allConfigsBefore[6].stableBorrowRateEnabled,
isPaused: allConfigsBefore[6].isPaused,
isActive: allConfigsBefore[6].isActive,
isFrozen: allConfigsBefore[6].isFrozen,
isSiloed: allConfigsBefore[6].isSiloed,
Expand Down
38 changes: 25 additions & 13 deletions src/test/ProtocolV3TestBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,38 @@
pragma solidity ^0.8.0;

import 'forge-std/Test.sol';
import {ProtocolV3TestBase} from '../ProtocolV3TestBase.sol';
import {ProtocolV3_0_1TestBase, ReserveConfig} from '../ProtocolV3TestBase.sol';
import {AaveV3Polygon} from 'aave-address-book/AaveV3Polygon.sol';
import {AaveV3Optimism, AaveV3OptimismAssets} from 'aave-address-book/AaveV3Optimism.sol';

contract ProtocolV3TestBaseTest is ProtocolV3TestBase {
contract ProtocolV3TestBaseTest is ProtocolV3_0_1TestBase {
function setUp() public {
vm.createSelectFork('polygon', 36329200);
}

function testSnpashot() public {
this.createConfigurationSnapshot('pre-x', AaveV3Polygon.POOL);
// do sth
// this.createConfigurationSnapshot('post-x', AaveV3Polygon.POOL);
// function testSnpashot() public {
// this.createConfigurationSnapshot('pre-x', AaveV3Polygon.POOL);
// // do sth
// // this.createConfigurationSnapshot('post-x', AaveV3Polygon.POOL);

// requires --ffi
// diffReports('pre-x', 'post-x');
}
// // requires --ffi
// // diffReports('pre-x', 'post-x');
// }

// commented out as it is insanely slow with public rpcs
// function testE2E() public {
// address user = address(3);
// this.e2eTest(AaveV3Polygon.POOL, user);
// }
}

contract ProtocolV3TestE2ETestAsset is ProtocolV3_0_1TestBase {
function setUp() public {
vm.createSelectFork('optimism', 105016991);
}

function test_e2eTestAsset() public {
ReserveConfig[] memory configs = _getReservesConfigs(AaveV3Optimism.POOL);
e2eTestAsset(
AaveV3Optimism.POOL,
_findReserveConfig(configs, AaveV3OptimismAssets.DAI_UNDERLYING), // DAI
_findReserveConfig(configs, AaveV3OptimismAssets.MAI_UNDERLYING) // MAI
);
}
}

0 comments on commit 7aebb79

Please sign in to comment.