fix: Remove block number constraint from collectAndUpdateRewards() + add test

This commit is contained in:
Lasse Herskind 2021-06-21 16:07:44 +02:00
parent 80854db63b
commit 2644aaca7f
2 changed files with 89 additions and 20 deletions

View File

@ -464,7 +464,6 @@ contract StaticATokenLM is ERC20 {
* @dev Claims rewards from `INCENTIVES_CONTROLLER` and updates internal accounting of rewards. * @dev Claims rewards from `INCENTIVES_CONTROLLER` and updates internal accounting of rewards.
*/ */
function collectAndUpdateRewards() public { function collectAndUpdateRewards() public {
if (block.number > _lastRewardBlock) {
_lastRewardBlock = block.number; _lastRewardBlock = block.number;
uint256 supply = totalSupply(); uint256 supply = totalSupply();
@ -485,10 +484,9 @@ contract StaticATokenLM is ERC20 {
if (rewardsAccrued > 0) { if (rewardsAccrued > 0) {
_lifetimeRewards = lifetimeRewards; _lifetimeRewards = lifetimeRewards;
} }
// Unsure if we can also move this in
_lifetimeRewardsClaimed = lifetimeRewards; _lifetimeRewardsClaimed = lifetimeRewards;
} }
}
/** /**
* @dev Claim rewards for a user. * @dev Claim rewards for a user.

View File

@ -839,4 +839,75 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(await stkAave.balanceOf(await users[i].getAddress())).to.be.eq(pendingReward); expect(await stkAave.balanceOf(await users[i].getAddress())).to.be.eq(pendingReward);
} }
}); });
it('Checks that withdraw and collect in different blocks updates _lifetimeRewardsClaimed as expected', async () => {
const users = await DRE.ethers.getSigners();
const user = users[0];
const depositAmount = utils.parseEther('1');
// Preparation
await waitForTx(await weth.connect(user).deposit({ value: depositAmount }));
await waitForTx(
await weth.connect(user).approve(staticAToken.address, depositAmount, defaultTxParams)
);
// Deposit
await waitForTx(
await staticAToken
.connect(user)
.deposit(await user.getAddress(), depositAmount, 0, true, defaultTxParams)
);
await advanceTimeAndBlock(60);
expect(await staticAToken.getLifetimeRewardsClaimed()).to.be.eq(0);
expect(await staticAToken.getClaimableRewards(user.address)).to.be.gt(0);
expect(await stkAave.balanceOf(user.address)).to.be.eq(0);
await waitForTx(await staticAToken.connect(user).withdraw(user.address, MAX_UINT_AMOUNT, true));
await staticAToken.collectAndUpdateRewards();
await staticAToken.connect(user).claimRewards(user.address, false);
expect(await staticAToken.getLifetimeRewardsClaimed()).to.be.gt(0);
expect(await staticAToken.getClaimableRewards(user.address)).to.be.eq(0);
expect(await stkAave.balanceOf(user.address)).to.be.gt(0);
});
it('Checks that withdraw and collect in the same block updates _lifetimeRewardsClaimed as expected', async () => {
const users = await DRE.ethers.getSigners();
const user = users[0];
const depositAmount = utils.parseEther('1');
// Preparation
await waitForTx(await weth.connect(user).deposit({ value: depositAmount }));
await waitForTx(
await weth.connect(user).approve(staticAToken.address, depositAmount, defaultTxParams)
);
// Deposit
await waitForTx(
await staticAToken
.connect(user)
.deposit(await user.getAddress(), depositAmount, 0, true, defaultTxParams)
);
await advanceTimeAndBlock(60);
expect(await staticAToken.getLifetimeRewardsClaimed()).to.be.eq(0);
expect(await staticAToken.getClaimableRewards(user.address)).to.be.gt(0);
expect(await stkAave.balanceOf(user.address)).to.be.eq(0);
await DRE.network.provider.send('evm_setAutomine', [false]);
await staticAToken.connect(user).withdraw(user.address, MAX_UINT_AMOUNT, true);
await staticAToken.collectAndUpdateRewards();
await staticAToken.connect(user).claimRewards(user.address, false);
await DRE.network.provider.send('evm_mine', []);
await DRE.network.provider.send('evm_setAutomine', [true]);
expect(await staticAToken.getLifetimeRewardsClaimed()).to.be.gt(0);
expect(await staticAToken.getClaimableRewards(user.address)).to.be.eq(0);
expect(await stkAave.balanceOf(user.address)).to.be.gt(0);
});
}); });