Skip to content

Commit

Permalink
fix: cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Aug 9, 2024
1 parent 5d88280 commit 1f9d17c
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 568 deletions.
2 changes: 1 addition & 1 deletion src/periphery/contracts/static-a-token/StataOracle.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;

import {IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import {IERC4626} from '@openzeppelin/contracts/interfaces/IERC4626.sol';
import {IPool} from '../../../core/contracts/interfaces/IPool.sol';
import {IPoolAddressesProvider} from '../../../core/contracts/interfaces/IPoolAddressesProvider.sol';
import {IAaveOracle} from '../../../core/contracts/interfaces/IAaveOracle.sol';
Expand Down
111 changes: 1 addition & 110 deletions src/periphery/contracts/static-a-token/StaticATokenLM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ERC20Upgradeable} from 'openzeppelin-contracts-upgradeable/contracts/tok
import {ERC20PermitUpgradeable} from 'openzeppelin-contracts-upgradeable/contracts/token/ERC20/extensions/ERC20PermitUpgradeable.sol';
import {ERC20PausableUpgradeable} from 'openzeppelin-contracts-upgradeable/contracts/token/ERC20/extensions/ERC20PausableUpgradeable.sol';
import {ERC4626Upgradeable} from 'openzeppelin-contracts-upgradeable/contracts/token/ERC20/extensions/ERC4626Upgradeable.sol';
import {IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import {IERC4626} from '@openzeppelin/contracts/interfaces/IERC4626.sol';

import {IPool} from '../../../core/contracts/interfaces/IPool.sol';
import {IPoolAddressesProvider} from '../../../core/contracts/interfaces/IPoolAddressesProvider.sol';
Expand Down Expand Up @@ -49,15 +49,6 @@ contract StaticATokenLM is
using WadRayMath for uint256;
using RayMathExplicitRounding for uint256;

bytes32 public constant METADEPOSIT_TYPEHASH =
keccak256(
'Deposit(address depositor,address receiver,uint256 assets,uint16 referralCode,bool depositToAave,uint256 nonce,uint256 deadline)'
);
bytes32 public constant METAWITHDRAWAL_TYPEHASH =
keccak256(
'Withdraw(address owner,address receiver,uint256 shares,uint256 assets,bool withdrawFromAave,uint256 nonce,uint256 deadline)'
);

uint256 public constant STATIC__ATOKEN_LM_REVISION = 3;

IPool public immutable POOL;
Expand Down Expand Up @@ -148,106 +139,6 @@ contract StaticATokenLM is
return shares;
}

// ///@inheritdoc IStaticATokenLM
// function metaDeposit(
// address depositor,
// address receiver,
// uint256 assets,
// uint16 referralCode,
// bool depositToAave,
// uint256 deadline,
// PermitParams calldata permit,
// SignatureParams calldata sigParams
// ) external returns (uint256) {
// require(depositor != address(0), StaticATokenErrors.INVALID_DEPOSITOR);
// //solium-disable-next-line
// require(deadline >= block.timestamp, StaticATokenErrors.INVALID_EXPIRATION);
// // Unchecked because the only math done is incrementing
// // the owner's nonce which cannot realistically overflow.
// unchecked {
// bytes32 digest = keccak256(
// abi.encodePacked(
// '\x19\x01',
// _domainSeparatorV4(),
// keccak256(
// abi.encode(
// METADEPOSIT_TYPEHASH,
// depositor,
// receiver,
// assets,
// referralCode,
// depositToAave,
// _useNonce(depositor),
// deadline
// )
// )
// )
// );
// require(
// depositor == ecrecover(digest, sigParams.v, sigParams.r, sigParams.s),
// StaticATokenErrors.INVALID_SIGNATURE
// );
// }
// // assume if deadline 0 no permit was supplied
// if (permit.deadline != 0) {
// try
// IERC20WithPermit(depositToAave ? address(_aTokenUnderlying) : address(_aToken)).permit(
// depositor,
// address(this),
// permit.value,
// permit.deadline,
// permit.v,
// permit.r,
// permit.s
// )
// {} catch {}
// }
// (uint256 shares, ) = _deposit(depositor, receiver, 0, assets, referralCode, depositToAave);
// return shares;
// }

// ///@inheritdoc IStaticATokenLM
// function metaWithdraw(
// address owner,
// address receiver,
// uint256 shares,
// uint256 assets,
// bool withdrawFromAave,
// uint256 deadline,
// SignatureParams calldata sigParams
// ) external returns (uint256, uint256) {
// require(owner != address(0), StaticATokenErrors.INVALID_OWNER);
// //solium-disable-next-line
// require(deadline >= block.timestamp, StaticATokenErrors.INVALID_EXPIRATION);
// // Unchecked because the only math done is incrementing
// // the owner's nonce which cannot realistically overflow.
// unchecked {
// bytes32 digest = keccak256(
// abi.encodePacked(
// '\x19\x01',
// _domainSeparatorV4(),
// keccak256(
// abi.encode(
// METAWITHDRAWAL_TYPEHASH,
// owner,
// receiver,
// shares,
// assets,
// withdrawFromAave,
// _useNonce(owner),
// deadline
// )
// )
// )
// );
// require(
// owner == ecrecover(digest, sigParams.v, sigParams.r, sigParams.s),
// StaticATokenErrors.INVALID_SIGNATURE
// );
// }
// return _withdraw(owner, receiver, shares, assets, withdrawFromAave);
// }

///@inheritdoc IStaticATokenLM
function rate() public view returns (uint256) {
return POOL.getReserveNormalizedIncome(_aTokenUnderlying);
Expand Down
2 changes: 1 addition & 1 deletion tests/periphery/static-a-token/Pausable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {IStaticATokenLM} from '../../../src/periphery/contracts/static-a-token/i
import {SigUtils} from '../../utils/SigUtils.sol';
import {BaseTest, TestnetERC20} from './TestBase.sol';

contract Pausable is BaseTest {
contract StataPausableTest is BaseTest {
using RayMathExplicitRounding for uint256;

function test_setPaused_shouldRevertForInvalidCaller(address actor) external {
Expand Down
209 changes: 209 additions & 0 deletions tests/periphery/static-a-token/Rewards.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;

import {IRescuable} from 'solidity-utils/contracts/utils/Rescuable.sol';
import {ERC20PermitUpgradeable} from 'openzeppelin-contracts-upgradeable/contracts/token/ERC20/extensions/ERC20PermitUpgradeable.sol';
import {Initializable} from 'openzeppelin-contracts-upgradeable/contracts/proxy/utils/Initializable.sol';
import {AToken} from '../../../src/core/contracts/protocol/tokenization/AToken.sol';
import {DataTypes} from '../../../src/core/contracts/protocol/libraries/configuration/ReserveConfiguration.sol';
import {IERC20, IERC20Metadata} from '../../../src/periphery/contracts/static-a-token/StaticATokenLM.sol';
import {RayMathExplicitRounding} from '../../../src/periphery/contracts/libraries/RayMathExplicitRounding.sol';
import {IStaticATokenLM} from '../../../src/periphery/contracts/static-a-token/interfaces/IStaticATokenLM.sol';
import {SigUtils} from '../../utils/SigUtils.sol';
import {BaseTest, TestnetERC20} from './TestBase.sol';
import {IPool} from '../../../src/core/contracts/interfaces/IPool.sol';

contract StataRewardsTest is BaseTest {
using RayMathExplicitRounding for uint256;

function setUp() public override {
super.setUp();

_configureLM();

vm.startPrank(user);
}

// test rewards
function test_collectAndUpdateRewards() public {
uint128 amountToDeposit = 5 ether;
_fundUser(amountToDeposit, user);

_depositAToken(amountToDeposit, user);

_skipBlocks(60);
assertEq(IERC20(REWARD_TOKEN).balanceOf(address(staticATokenLM)), 0);
uint256 claimable = staticATokenLM.getTotalClaimableRewards(REWARD_TOKEN);
staticATokenLM.collectAndUpdateRewards(REWARD_TOKEN);
assertEq(IERC20(REWARD_TOKEN).balanceOf(address(staticATokenLM)), claimable);
}

function test_claimableRewards() external {
uint128 amountToDeposit = 5 ether;
_fundUser(amountToDeposit, user);
_depositAToken(amountToDeposit, user);

vm.warp(block.timestamp + 200);
uint256 claimable = staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
assertEq(claimable, 200 * 0.00385 ether);
}

function test_claimRewardsToSelf() public {
uint128 amountToDeposit = 5 ether;
_fundUser(amountToDeposit, user);

_depositAToken(amountToDeposit, user);

_skipBlocks(60);

uint256 claimable = staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
staticATokenLM.claimRewardsToSelf(rewardTokens);
assertEq(IERC20(REWARD_TOKEN).balanceOf(user), claimable);
assertEq(staticATokenLM.getClaimableRewards(user, REWARD_TOKEN), 0);
}

function test_claimRewards() public {
uint128 amountToDeposit = 5 ether;
_fundUser(amountToDeposit, user);

_depositAToken(amountToDeposit, user);

_skipBlocks(60);

uint256 claimable = staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
staticATokenLM.claimRewards(user, rewardTokens);
assertEq(claimable, IERC20(REWARD_TOKEN).balanceOf(user));
assertEq(IERC20(REWARD_TOKEN).balanceOf(address(staticATokenLM)), 0);
assertEq(staticATokenLM.getClaimableRewards(user, REWARD_TOKEN), 0);
}

// should fail as user1 is not a valid claimer
function testFail_claimRewardsOnBehalfOf() public {
uint128 amountToDeposit = 5 ether;
_fundUser(amountToDeposit, user);

_depositAToken(amountToDeposit, user);

_skipBlocks(60);

vm.stopPrank();
vm.startPrank(user1);

staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
staticATokenLM.claimRewardsOnBehalf(user, user1, rewardTokens);
}

function test_depositATokenClaimWithdrawClaim() public {
uint128 amountToDeposit = 5 ether;
_fundUser(amountToDeposit, user);

// deposit aweth
_depositAToken(amountToDeposit, user);

// forward time
_skipBlocks(60);

// claim
assertEq(IERC20(REWARD_TOKEN).balanceOf(user), 0);
uint256 claimable0 = staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
assertEq(staticATokenLM.getTotalClaimableRewards(REWARD_TOKEN), claimable0);
assertGt(claimable0, 0);
staticATokenLM.claimRewardsToSelf(rewardTokens);
assertEq(IERC20(REWARD_TOKEN).balanceOf(user), claimable0);

// forward time
_skipBlocks(60);

// redeem
staticATokenLM.redeem(staticATokenLM.maxRedeem(user), user, user);
uint256 claimable1 = staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
assertEq(staticATokenLM.getTotalClaimableRewards(REWARD_TOKEN), claimable1);
assertGt(claimable1, 0);

// claim on behalf of other user
staticATokenLM.claimRewardsToSelf(rewardTokens);
assertEq(IERC20(REWARD_TOKEN).balanceOf(user), claimable1 + claimable0);
assertEq(staticATokenLM.balanceOf(user), 0);
assertEq(staticATokenLM.getClaimableRewards(user, REWARD_TOKEN), 0);
assertEq(staticATokenLM.getTotalClaimableRewards(REWARD_TOKEN), 0);
assertGe(AToken(UNDERLYING).balanceOf(user), 5 ether);
}

function test_depositWETHClaimWithdrawClaim() public {
uint128 amountToDeposit = 5 ether;
_fundUser(amountToDeposit, user);

_depositAToken(amountToDeposit, user);

// forward time
_skipBlocks(60);

// claim
assertEq(IERC20(REWARD_TOKEN).balanceOf(user), 0);
uint256 claimable0 = staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
assertEq(staticATokenLM.getTotalClaimableRewards(REWARD_TOKEN), claimable0);
assertGt(claimable0, 0);
staticATokenLM.claimRewardsToSelf(rewardTokens);
assertEq(IERC20(REWARD_TOKEN).balanceOf(user), claimable0);

// forward time
_skipBlocks(60);

// redeem
staticATokenLM.redeem(staticATokenLM.maxRedeem(user), user, user);
uint256 claimable1 = staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
assertEq(staticATokenLM.getTotalClaimableRewards(REWARD_TOKEN), claimable1);
assertGe(claimable1, 0);

// claim on behalf of other user
staticATokenLM.claimRewardsToSelf(rewardTokens);
assertEq(IERC20(REWARD_TOKEN).balanceOf(user), claimable1 + claimable0);
assertEq(staticATokenLM.balanceOf(user), 0);
assertEq(staticATokenLM.getClaimableRewards(user, REWARD_TOKEN), 0);
assertEq(staticATokenLM.getTotalClaimableRewards(REWARD_TOKEN), 0);
assertGe(AToken(UNDERLYING).balanceOf(user), 5 ether);
}

function test_transfer() public {
uint128 amountToDeposit = 10 ether;
_fundUser(amountToDeposit, user);

_depositAToken(amountToDeposit, user);

// transfer to 2nd user
staticATokenLM.transfer(user1, amountToDeposit / 2);
assertEq(staticATokenLM.getClaimableRewards(user1, REWARD_TOKEN), 0);

// forward time
_skipBlocks(60);

// redeem for both
uint256 claimableUser = staticATokenLM.getClaimableRewards(user, REWARD_TOKEN);
staticATokenLM.redeem(staticATokenLM.maxRedeem(user), user, user);
staticATokenLM.claimRewardsToSelf(rewardTokens);
assertEq(IERC20(REWARD_TOKEN).balanceOf(user), claimableUser);
vm.stopPrank();
vm.startPrank(user1);
uint256 claimableUser1 = staticATokenLM.getClaimableRewards(user1, REWARD_TOKEN);
staticATokenLM.redeem(staticATokenLM.maxRedeem(user1), user1, user1);
staticATokenLM.claimRewardsToSelf(rewardTokens);
assertEq(IERC20(REWARD_TOKEN).balanceOf(user1), claimableUser1);
assertGt(claimableUser1, 0);

assertEq(staticATokenLM.getTotalClaimableRewards(REWARD_TOKEN), 0);
assertEq(staticATokenLM.getClaimableRewards(user, REWARD_TOKEN), 0);
assertEq(staticATokenLM.getClaimableRewards(user1, REWARD_TOKEN), 0);
}

// getUnclaimedRewards
function test_getUnclaimedRewards() public {
uint128 amountToDeposit = 5 ether;
_fundUser(amountToDeposit, user);

uint256 shares = _depositAToken(amountToDeposit, user);
assertEq(staticATokenLM.getUnclaimedRewards(user, REWARD_TOKEN), 0);
_skipBlocks(1000);
staticATokenLM.redeem(shares, user, user);
assertGt(staticATokenLM.getUnclaimedRewards(user, REWARD_TOKEN), 0);
}
}
Loading

0 comments on commit 1f9d17c

Please sign in to comment.