Skip to content

Commit

Permalink
fix: move around tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Aug 9, 2024
1 parent 1214bcc commit 93a819d
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 130 deletions.
2 changes: 2 additions & 0 deletions src/periphery/contracts/static-a-token/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ As long as the vault is paused, minting, burning, transfers and claiming of rewa
While there are already mechanisms to price the `StaticATokenLM` implemented by 3th parties for improved UX/DX the `StaticATokenLM` now exposes `latestAnswer`.
`latestAnswer` returns the asset price priced as `underlying_price * excahngeRate`.
It is important to note that:

- `underlying_price` is fetched from the AaveOracle, which means it is subject to mechanisms implemented by the DAO on top of the Chainlink price feeds.
- the `latestAnswer` is a scaled response returning the price in the same denomination as `underlying_price` which means the sprice can be undervalued by up to 1 wei
- while this should be obvious deviations in the price - even when limited to 1 wei per share - will compound per full share
Expand Down Expand Up @@ -101,5 +102,6 @@ index a7e3105..89e0967 100644

The upgrade can be performed independent(before) from any umbrella changes as it has no dependencies.
The upgrade will need to:

- upgrade the `StaticATokenFactory` with a new version, replacing the `STATIC_A_TOKEN_IMPL`.
- upgrade existing stata tokens via `upgradeToAndCall` to the new implementation. While the tokens are already initialized, due to changing the `Initializable` the corresponding storage is lost.
156 changes: 156 additions & 0 deletions tests/periphery/static-a-token/Rewards.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.10;

import {AToken} from '../../../src/core/contracts/protocol/tokenization/AToken.sol';
import {IERC20} from '../../../src/periphery/contracts/static-a-token/StaticATokenLM.sol';
import {BaseTest} from './TestBase.sol';

contract StataTokenRewardsTest is BaseTest {
function setUp() public override {
super.setUp();

_configureLM();

vm.startPrank(user);
}

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);
}

// 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_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);
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);
}
}
130 changes: 0 additions & 130 deletions tests/periphery/static-a-token/StaticATokenLM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -208,136 +208,6 @@ contract StaticATokenLMTest is BaseTest {
staticATokenLM.mint(amountToDeposit, 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_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);
assertGt(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);
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);
assertGt(AToken(UNDERLYING).balanceOf(user), 5 ether);
}

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

0 comments on commit 93a819d

Please sign in to comment.