Skip to content

Commit

Permalink
feat: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Aug 15, 2024
1 parent 588e1fc commit bd5b9a9
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ contract StaticATokenFactory is Initializable, IStaticATokenFactory {
revert NotListedUnderlying(reserveData.aTokenAddress);
bytes memory symbol = abi.encodePacked(
'stat',
IERC20Metadata(reserveData.aTokenAddress).symbol()
IERC20Metadata(reserveData.aTokenAddress).symbol(),
'v2'
);
address staticAToken = TRANSPARENT_PROXY_FACTORY.createDeterministic(
STATIC_A_TOKEN_IMPL,
PROXY_ADMIN,
abi.encodeWithSelector(
StataTokenV2.initialize.selector,
reserveData.aTokenAddress,
string(abi.encodePacked('Static ', IERC20Metadata(reserveData.aTokenAddress).name())),
string(
abi.encodePacked('Static ', IERC20Metadata(reserveData.aTokenAddress).name(), ' v2')
),
string(symbol)
),
bytes32(uint256(uint160(underlyings[i])))
Expand Down
22 changes: 22 additions & 0 deletions tests/periphery/static-a-token/ERC20AaveLMUpgradable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ contract ERC20AaveLMUpgradableTest is TestnetProcedures {
contracts.emissionManager.setEmissionAdmin(rewardToken, emissionAdmin);
}

function test_2701() external view {
assertEq(
keccak256(abi.encode(uint256(keccak256('aave-dao.storage.ERC20AaveLM')) - 1)) &
~bytes32(uint256(0xff)),
0x4fad66563f105be0bff96185c9058c4934b504d3ba15ca31e86294f0b01fd200
);
}

function test_noRewardsInitialized() external {
vm.expectRevert(
abi.encodeWithSelector(IERC20AaveLM.RewardNotInitialized.selector, rewardToken)
);
lmUpgradeable.getClaimableRewards(user, rewardToken);
}

function test_noopWhenNotInitialized() external {
assertEq(IERC20(rewardToken).balanceOf(address(lmUpgradeable)), 0);
assertEq(lmUpgradeable.getTotalClaimableRewards(rewardToken), 0);
assertEq(lmUpgradeable.collectAndUpdateRewards(rewardToken), 0);
assertEq(IERC20(rewardToken).balanceOf(address(lmUpgradeable)), 0);
}

function test_claimableRewards(
uint256 depositAmount,
uint32 emissionEnd,
Expand Down
106 changes: 98 additions & 8 deletions tests/periphery/static-a-token/ERC4626StataTokenUpgradeable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {IERC20Errors} from 'openzeppelin-contracts/contracts/interfaces/draft-IE
import {IERC20} from 'openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol';
import {IPool} from '../../../src/core/contracts/interfaces/IPool.sol';
import {TestnetProcedures, TestnetERC20} from '../../utils/TestnetProcedures.sol';
import {ERC4626StataTokenUpgradeable, IERC4626StataToken} from '../../../src/periphery/contracts/static-a-token/ERC4626StataTokenUpgradeable.sol';
import {ERC4626Upgradeable, ERC4626StataTokenUpgradeable, IERC4626StataToken} from '../../../src/periphery/contracts/static-a-token/ERC4626StataTokenUpgradeable.sol';
import {IRewardsController} from '../../../src/periphery/contracts/rewards/interfaces/IRewardsController.sol';
import {PullRewardsTransferStrategy, ITransferStrategyBase} from '../../../src/periphery/contracts/rewards/transfer-strategies/PullRewardsTransferStrategy.sol';
import {RewardsDataTypes} from '../../../src/periphery/contracts/rewards/libraries/RewardsDataTypes.sol';
Expand Down Expand Up @@ -44,6 +44,24 @@ contract ERC4626StataTokenUpgradeableTest is TestnetProcedures {
erc4626Upgradeable.mockInit(address(reserveDataWETH.aTokenAddress));
}

function test_2701() external view {
assertEq(
keccak256(abi.encode(uint256(keccak256('aave-dao.storage.ERC4626StataToken')) - 1)) &
~bytes32(uint256(0xff)),
0x55029d3f54709e547ed74b2fc842d93107ab1490ab7555dd9dd0bf6451101900
);
}

// ### GETTERS TESTS ###
function test_convertersAndPreviews(uint128 assets) public view {
uint256 shares = erc4626Upgradeable.convertToShares(assets);
assertEq(shares, erc4626Upgradeable.previewDeposit(assets));
assertEq(shares, erc4626Upgradeable.previewWithdraw(assets));
assertEq(erc4626Upgradeable.convertToAssets(shares), assets);
assertEq(erc4626Upgradeable.previewMint(shares), assets);
assertEq(erc4626Upgradeable.previewRedeem(shares), assets);
}

// ### DEPOSIT TESTS ###
function test_depositATokens(uint128 assets, address receiver) public {
vm.assume(receiver != address(0));
Expand All @@ -70,7 +88,7 @@ contract ERC4626StataTokenUpgradeableTest is TestnetProcedures {

vm.expectRevert(); // underflows
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositATokens(env.amount, user);
erc4626Upgradeable.depositATokens(env.amount, user);
}

// ### REDEEM TESTS ###
Expand All @@ -80,10 +98,10 @@ contract ERC4626StataTokenUpgradeableTest is TestnetProcedures {
uint256 shares = _fund4626(env.amount, user);

vm.prank(user);
erc4626Upgradeable.redeemATokens(shares, receiver, user);
uint256 redeemedAssets = erc4626Upgradeable.redeemATokens(shares, receiver, user);

assertEq(erc4626Upgradeable.balanceOf(user), 0);
assertEq(IERC20(aToken).balanceOf(receiver), env.amount);
assertEq(IERC20(aToken).balanceOf(receiver), redeemedAssets);
}

function test_redeemATokens_onBehalf_shouldRevert_insufficientAllowance(
Expand Down Expand Up @@ -113,8 +131,80 @@ contract ERC4626StataTokenUpgradeableTest is TestnetProcedures {
uint256 shares = _fund4626(env.amount, user);

vm.prank(user);
erc4626Upgradeable.approve(address(this), env.amount);
erc4626Upgradeable.redeemATokens(env.amount, address(this), user);
erc4626Upgradeable.approve(address(this), shares);
uint256 redeemedAssets = erc4626Upgradeable.redeemATokens(shares, address(this), user);

assertEq(erc4626Upgradeable.balanceOf(user), 0);
assertEq(IERC20(aToken).balanceOf(address(this)), redeemedAssets);
}

function test_redeem(uint256 assets, address receiver) external {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.amount, user);

vm.prank(user);
uint256 redeemedAssets = erc4626Upgradeable.redeem(shares, receiver, user);
assertEq(erc4626Upgradeable.balanceOf(user), 0);
assertLe(IERC20(underlying).balanceOf(receiver), redeemedAssets);
}

// ### withdraw TESTS ###
function test_withdraw(uint256 assets, address receiver) public {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.amount, user);

vm.prank(user);
uint256 withdrawnShares = erc4626Upgradeable.withdraw(env.amount, receiver, user);
assertEq(withdrawnShares, shares);
assertEq(erc4626Upgradeable.balanceOf(user), 0);
assertLe(IERC20(underlying).balanceOf(receiver), env.amount);
assertApproxEqAbs(IERC20(underlying).balanceOf(receiver), env.amount, 1);
}

function test_withdraw_shouldRevert_moreThenAvailable(uint256 assets, address receiver) public {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fund4626(env.amount, user);

vm.prank(user);
vm.expectRevert(
abi.encodeWithSelector(
ERC4626Upgradeable.ERC4626ExceededMaxWithdraw.selector,
address(user),
env.amount + 1,
env.amount
)
);
erc4626Upgradeable.withdraw(env.amount + 1, receiver, user);
}

// ### mint TESTS ###
function test_mint(uint256 assets, address receiver) public {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundUnderlying(env.amount, user);

vm.startPrank(user);
IERC20(underlying).approve(address(erc4626Upgradeable), env.amount);
uint256 shares = erc4626Upgradeable.previewDeposit(env.amount);
uint256 assetsUsedForMinting = erc4626Upgradeable.mint(shares, receiver);
assertEq(assetsUsedForMinting, env.amount);
assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
}

function test_mint_shouldRevert_mintMoreThenBalance(uint256 assets, address receiver) public {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundUnderlying(env.amount, user);

vm.startPrank(user);
IERC20(underlying).approve(address(erc4626Upgradeable), type(uint256).max);
uint256 shares = erc4626Upgradeable.previewDeposit(env.amount);

vm.expectRevert();
uint256 assetsUsedForMinting = erc4626Upgradeable.mint(shares + 1, receiver);
}

// ### maxDeposit TESTS ###
Expand Down Expand Up @@ -161,7 +251,7 @@ contract ERC4626StataTokenUpgradeableTest is TestnetProcedures {
// ### maxRedeem TESTS ###
function test_maxRedeem_paused(uint128 assets) public {
TestEnv memory env = _setupTestEnv(assets);
uint256 shares = _fund4626(env.amount, user);
_fund4626(env.amount, user);

vm.prank(address(roleList.marketOwner));
contracts.poolConfiguratorProxy.setReservePause(underlying, true);
Expand Down Expand Up @@ -233,7 +323,7 @@ contract ERC4626StataTokenUpgradeableTest is TestnetProcedures {

function _setupTestEnv(uint256 amount) internal returns (TestEnv memory) {
TestEnv memory env;
env.amount = bound(amount, 1, type(uint128).max);
env.amount = bound(amount, 1, type(uint96).max);
return env;
}

Expand Down
128 changes: 0 additions & 128 deletions tests/periphery/static-a-token/Stata4626LM.t.sol

This file was deleted.

35 changes: 35 additions & 0 deletions tests/periphery/static-a-token/StataTokenV2Getters.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;

import {Initializable} from 'openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol';
import {IERC20Metadata, IERC20} from 'openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol';
import {AToken} from '../../../src/core/contracts/protocol/tokenization/AToken.sol';
import {StataTokenV2} from '../../../src/periphery/contracts/static-a-token/StataTokenV2.sol'; // TODO: change import to isolate to 4626
import {BaseTest} from './TestBase.sol';

contract StataTokenV2GettersTest is BaseTest {
function test_initializeShouldRevert() public {
address impl = factory.STATIC_A_TOKEN_IMPL();
vm.expectRevert(Initializable.InvalidInitialization.selector);
StataTokenV2(impl).initialize(aToken, 'hey', 'ho');
}

function test_getters() public view {
assertEq(stataTokenV2.name(), 'Static Aave Local WETH v2');
assertEq(stataTokenV2.symbol(), 'stataLocWETHv2');

address referenceAsset = stataTokenV2.getReferenceAsset();
assertEq(referenceAsset, aToken);

address underlyingAddress = address(stataTokenV2.asset());
assertEq(underlyingAddress, underlying);

IERC20Metadata underlying = IERC20Metadata(underlyingAddress);
assertEq(stataTokenV2.decimals(), underlying.decimals());

assertEq(
address(stataTokenV2.INCENTIVES_CONTROLLER()),
address(AToken(aToken).getIncentivesController())
);
}
}
9 changes: 9 additions & 0 deletions tests/periphery/static-a-token/StataTokenV2Pausable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import {IERC4626StataToken} from '../../../src/periphery/contracts/static-a-toke
import {BaseTest} from './TestBase.sol';

contract StataTokenV2PausableTest is BaseTest {
function test_canPause() external {
assertEq(stataTokenV2.canPause(poolAdmin), true);
}

function test_canPause_shouldReturnFalse(address actor) external {
vm.assume(actor != poolAdmin);
assertEq(stataTokenV2.canPause(actor), false);
}

function test_setPaused_shouldRevertForInvalidCaller(address actor) external {
vm.assume(actor != poolAdmin && actor != proxyAdmin);
vm.expectRevert(abi.encodeWithSelector(IERC4626StataToken.OnlyPauseGuardian.selector, actor));
Expand Down
Loading

0 comments on commit bd5b9a9

Please sign in to comment.