test: Replace gt, gte, lt, lte with eq when possible

This commit is contained in:
Lasse Herskind 2021-06-02 10:20:22 +02:00
parent 20635c1933
commit 6c34a062af
3 changed files with 76 additions and 99 deletions

View File

@ -67,7 +67,7 @@ const getCommonNetworkConfig = (networkName: eNetwork, networkId: number) => ({
const mainnetFork = MAINNET_FORK
? {
blockNumber: 12541468,
blockNumber: 12521999,
url: ALCHEMY_KEY
? `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_KEY}`
: `https://mainnet.infura.io/v3/${INFURA_KEY}`,

View File

@ -158,16 +158,19 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
await waitForTx(await staticAToken.claimRewards(userSigner._address));
const pendingRewards5 = await staticAToken.getClaimableRewards(userSigner._address);
const claimedRewards5 = await stkAave.balanceOf(userSigner._address);
expect(pendingRewards2).to.be.gt(pendingRewards1);
expect(pendingRewards3).to.be.gt(pendingRewards2);
expect(pendingRewards4).to.be.gt(pendingRewards3);
expect(pendingRewards5).to.be.eq(0);
expect(claimedRewards4).to.be.eq(0);
expect(claimedRewards5).to.be.eq(pendingRewards4);
});
it('Check getters', async () => {
const amountToDeposit = utils.parseEther('5');
const amountToWithdraw = MAX_UINT_AMOUNT;
// Just preparation
await waitForTx(await weth.deposit({ value: amountToDeposit.mul(2) }));
@ -190,7 +193,7 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(dynamicBalance).to.be.eq(dynamicBalanceFromStatic);
});
it('Multiple updates in one block', async () => {
it.skip('Multiple updates in one block (Breaks if GasReport enabled)', async () => {
const amountToDeposit = utils.parseEther('5');
// Just preparation
@ -298,16 +301,10 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(recipientPendingRewards1).to.be.eq(0);
expect(recipientPendingRewards2).to.be.eq(0);
});
// Those that checks that subs could not be wrong or something other
});
it('Multiple users deposit WETH on stataWETH, wait 1 hour, update rewards, one user transfer, then claim and update rewards.', async () => {
// In this case, the recipient should have approx twice the rewards.
// Note that he has not held the 2x balance for this entire time, but only for one block.
// He have gotten this extra reward from the sender, because there was not a update prior.
// Only diff here is if we wait, transfer, wait
// In this case, the recipient should have approx 1.5 the rewards of the others.
// 1. Deposit
// 2. Wait 3600 seconds
@ -317,9 +314,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
// 5. Claim rewards
// 6. Update rewards
// When doing so, it should be clear that the recipient also gets the 'uncollected' rewards to the protocol that the value has accrued since last update.
// The thought is that since it is expensive to retrieve these rewards, a small holder may rather want to give away the extra rewards (if rewards < gas).
const amountToDeposit = utils.parseEther('5');
const allusers = await DRE.ethers.getSigners();
const users = [allusers[0], allusers[1], allusers[2], allusers[3], allusers[4]];
@ -383,8 +377,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
let pendingRewardsSumFinal = BigNumber.from(0);
for (let i = 0; i < 5; i++) {
expect(usersDataInitial[i].stkAaveBalance).to.be.eq(0);
// Everyone else than i == 1, should have no change in pending rewards.
// i == 1, will get additional rewards that have accrue
expect(usersDataAfterTransferAndClaim[i].stkAaveBalance).to.be.eq(
usersDataInitial[i].pendingRewards
);
@ -404,9 +396,10 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
}
// Expect user 0 to accrue zero fees after the transfer
expect(usersDataFinal[0].pendingRewards).to.be.eq(0);
expect(usersDataAfterTransferAndClaim[0].staticBalance).to.be.eq(0);
expect(usersDataAfterTransferAndClaim[0].pendingRewards).to.be.eq(0);
expect(usersDataFinal[0].staticBalance).to.be.eq(0);
expect(usersDataFinal[0].pendingRewards).to.be.eq(0);
// Expect user 1 to have received funds
expect(usersDataAfterTransferAndClaim[1].staticBalance).to.be.eq(
@ -416,7 +409,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
* Expect user 1 to have accrued more than twice in pending rewards.
* note that we get very little rewards in the transfer, because of the fresh update.
*/
// Expect the pending of user to be a lot
expect(usersDataFinal[1].pendingRewards).to.be.gt(usersDataFinal[2].pendingRewards.mul(2));
// Expect his total fees to be almost 1.5 as large. Because of the small initial diff
expect(usersDataFinal[1].pendingRewards.add(usersDataFinal[1].stkAaveBalance)).to.be.gt(
@ -435,14 +427,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(pendingRewardsSumFinal).to.be.lte(staticATokenStkAaveBalFinal);
expect(staticATokenStkAaveBalFinal.sub(pendingRewardsSumFinal)).to.be.lte(DUST);
expect(usersDataInitial[0].pendingRewards).to.be.eq(
usersDataAfterTransferAndClaim[0].stkAaveBalance
);
expect(usersDataAfterTransferAndClaim[0].pendingRewards).to.be.eq(0);
expect(usersDataAfterTransferAndClaim[1].staticBalance).to.be.eq(
usersDataInitial[1].staticBalance.add(usersDataInitial[0].staticBalance)
);
});
it('Multiple users deposit WETH on stataWETH, wait 1 hour, one user transfer, then claim and update rewards.', async () => {
@ -450,8 +434,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
// Note that he has not held the 2x balance for this entire time, but only for one block.
// He have gotten this extra reward from the sender, because there was not a update prior.
// Only diff here is if we wait, transfer, wait
// 1. Deposit
// 2. Wait 3600 seconds
// 3. Transfer
@ -459,10 +441,7 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
// 5. Claim rewards
// 6. Update rewards
// When doing so, it should be clear that the recipient also gets the 'uncollected' rewards to the protocol that the value has accrued since last update.
// The thought is that since it is expensive to retrieve these rewards, a small holder may rather want to give away the extra rewards (if rewards < gas).
const amountToDeposit = utils.parseEther('5'); //'5');
const amountToDeposit = utils.parseEther('5');
const allusers = await DRE.ethers.getSigners();
const users = [allusers[0], allusers[1], allusers[2], allusers[3], allusers[4]];
@ -540,8 +519,9 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
}
// Expect user 0 to accrue zero fees after the transfer
expect(usersDataFinal[0].pendingRewards).to.be.eq(0);
expect(usersDataAfterTransfer[0].pendingRewards).to.be.eq(0);
expect(usersDataAfterTransfer[0].staticBalance).to.be.eq(0);
expect(usersDataFinal[0].pendingRewards).to.be.eq(0);
expect(usersDataFinal[0].staticBalance).to.be.eq(0);
// Expect user 1 to have received funds
@ -570,16 +550,8 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(pendingRewardsSumAfter).to.be.lte(staticATokenStkAaveBalAfterTransfer);
expect(staticATokenStkAaveBalAfterTransfer.sub(pendingRewardsSumAfter)).to.be.lte(DUST);
// We got an error here, pendingRewardsSumFinal = actual + 1
expect(pendingRewardsSumFinal).to.be.lte(staticATokenStkAaveBalFinal);
expect(staticATokenStkAaveBalFinal.sub(pendingRewardsSumFinal)).to.be.lte(DUST); // How small should we say dust is?
// Expect zero rewards after all is claimed. But there is some dust left.
expect(usersDataInitial[0].pendingRewards).to.be.eq(usersDataAfterTransfer[0].stkAaveBalance);
expect(usersDataAfterTransfer[0].pendingRewards).to.be.eq(0);
expect(usersDataAfterTransfer[1].staticBalance).to.be.eq(
usersDataInitial[1].staticBalance.add(usersDataInitial[0].staticBalance)
);
});
it('Mass deposit, then mass claim', async () => {

View File

@ -25,7 +25,7 @@ import {
advanceTimeAndBlock,
} from '../../../../helpers/misc-utils';
import { BigNumber, providers, Signer, utils } from 'ethers';
import { rayMul } from '../../../../helpers/ray-math';
import { rayDiv, rayMul } from '../../../../helpers/ray-math';
import { MAX_UINT_AMOUNT, ZERO_ADDRESS } from '../../../../helpers/constants';
import { tEthereumAddress } from '../../../../helpers/types';
import { AbiCoder, formatEther, verifyTypedData } from 'ethers/lib/utils';
@ -66,15 +66,17 @@ type tBalancesInvolved = {
staticATokenATokenBalance: BigNumber;
staticATokenStkAaveBalance: BigNumber;
staticATokenUnderlyingBalance: BigNumber;
staticATokenExpectedSupply: BigNumber;
staticATokenScaledBalanceAToken: BigNumber;
userStkAaveBalance: BigNumber;
userATokenBalance: BigNumber;
userScaledBalanceAToken: BigNumber;
userUnderlyingBalance: BigNumber;
userStaticATokenBalance: BigNumber;
userDynamicStaticATokenBalance: BigNumber;
userPendingRewards: BigNumber;
user2StkAaveBalance: BigNumber;
user2ATokenBalance: BigNumber;
user2ScaledBalanceAToken: BigNumber;
user2UnderlyingBalance: BigNumber;
user2StaticATokenBalance: BigNumber;
user2DynamicStaticATokenBalance: BigNumber;
@ -105,15 +107,17 @@ const getContext = async ({
staticATokenATokenBalance: await aToken.balanceOf(staticAToken.address),
staticATokenStkAaveBalance: await stkAave.balanceOf(staticAToken.address),
staticATokenUnderlyingBalance: await underlying.balanceOf(staticAToken.address),
staticATokenExpectedSupply: await aToken.scaledBalanceOf(staticAToken.address),
staticATokenScaledBalanceAToken: await aToken.scaledBalanceOf(staticAToken.address),
userStaticATokenBalance: await staticAToken.balanceOf(user),
userStkAaveBalance: await stkAave.balanceOf(user),
userATokenBalance: await aToken.balanceOf(user),
userScaledBalanceAToken: await aToken.scaledBalanceOf(user),
userUnderlyingBalance: await underlying.balanceOf(user),
userDynamicStaticATokenBalance: await staticAToken.dynamicBalanceOf(user),
userPendingRewards: await staticAToken.getClaimableRewards(user),
user2StkAaveBalance: await stkAave.balanceOf(user2),
user2ATokenBalance: await aToken.balanceOf(user2),
user2ScaledBalanceAToken: await aToken.scaledBalanceOf(user2),
user2UnderlyingBalance: await underlying.balanceOf(user2),
user2StaticATokenBalance: await staticAToken.balanceOf(user2),
user2DynamicStaticATokenBalance: await staticAToken.dynamicBalanceOf(user2),
@ -214,22 +218,21 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
const ctxtAfterClaim = await getContext(ctxtParams);
// Check values throughout
expect(ctxtInitial.staticATokenExpectedSupply).to.be.eq(ctxtInitial.staticATokenSupply);
expect(ctxtAfterDeposit.staticATokenExpectedSupply).to.be.eq(
// Check that scaledAToken balance is equal to the static aToken supply at every stage.
expect(ctxtInitial.staticATokenScaledBalanceAToken).to.be.eq(ctxtInitial.staticATokenSupply);
expect(ctxtAfterDeposit.staticATokenScaledBalanceAToken).to.be.eq(
ctxtAfterDeposit.staticATokenSupply
);
expect(ctxtAfterWithdrawal.staticATokenExpectedSupply).to.be.eq(
expect(ctxtAfterWithdrawal.staticATokenScaledBalanceAToken).to.be.eq(
ctxtAfterWithdrawal.staticATokenSupply
);
expect(ctxtAfterClaim.staticATokenExpectedSupply).to.be.eq(ctxtAfterClaim.staticATokenSupply);
expect(ctxtAfterClaim.staticATokenScaledBalanceAToken).to.be.eq(
ctxtAfterClaim.staticATokenSupply
);
// Check that aWETH balance of staticAToken contract is increased as expected
expect(ctxtAfterDeposit.staticATokenATokenBalance).to.be.eq(
ctxtInitial.staticATokenATokenBalance.add(amountToDeposit)
);
// Check user WETH balance of user is decreased as expected
expect(ctxtAfterDeposit.userUnderlyingBalance).to.be.eq(
ctxtInitial.userUnderlyingBalance.sub(amountToDeposit)
);
@ -246,34 +249,12 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(ctxtAfterDeposit.userStkAaveBalance).to.be.eq(0);
expect(ctxtAfterDeposit.staticATokenStkAaveBalance).to.be.eq(0);
expect(
ctxtAfterWithdrawal.staticATokenATokenBalance,
'INVALID_ATOKEN_BALANCE_ON_STATICATOKEN_AFTER_WITHDRAW'
).to.be.eq(
BigNumber.from(
rayMul(
new bnjs(
ctxtAfterWithdrawal.staticATokenSupply
.add(ctxtAfterDeposit.userStaticATokenBalance)
.toString()
),
new bnjs(ctxtAfterWithdrawal.currentRate.toString())
)
.minus(
rayMul(
new bnjs(ctxtAfterDeposit.userStaticATokenBalance.toString()),
new bnjs(ctxtAfterWithdrawal.currentRate.toString())
)
)
.toString()
)
);
expect(ctxtAfterWithdrawal.userStaticATokenBalance).to.be.eq(0);
expect(ctxtAfterWithdrawal.staticATokenATokenBalance).to.be.eq(0);
expect(ctxtAfterWithdrawal.staticATokenSupply).to.be.eq(0);
expect(ctxtAfterWithdrawal.staticATokenUnderlyingBalance).to.be.eq(0);
// Check with possible rounding error. Sometimes we have an issue with it being 0 lower as well.
// Check with possible rounding error.
expect(ctxtAfterWithdrawal.staticATokenStkAaveBalance).to.be.gte(
ctxtAfterWithdrawal.userPendingRewards
);
@ -324,15 +305,18 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(ctxtAfterDeposit.userStaticATokenBalance).to.be.eq(ctxtAfterDeposit.staticATokenSupply);
expect(ctxtAfterDeposit.staticATokenATokenBalance).to.be.eq(amountToDeposit);
expect(ctxtAfterWithdrawal.userDynamicStaticATokenBalance).to.be.lt(
ctxtAfterDeposit.userDynamicStaticATokenBalance
);
expect(ctxtAfterWithdrawal.userDynamicStaticATokenBalance).to.be.gt(
ctxtAfterDeposit.userDynamicStaticATokenBalance.sub(expectedATokenWithdraw)
expect(ctxtAfterWithdrawal.userDynamicStaticATokenBalance).to.be.eq(
BigNumber.from(
rayMul(
new bnjs(ctxtAfterDeposit.userStaticATokenBalance.sub(amountToWithdraw).toString()),
new bnjs(ctxtAfterWithdrawal.currentRate.toString())
).toString()
)
);
expect(ctxtAfterWithdrawal.userStaticATokenBalance).to.be.eq(
ctxtAfterDeposit.userStaticATokenBalance.sub(amountToWithdraw)
);
expect(ctxtAfterClaim.userStkAaveBalance).to.be.eq(ctxtAfterWithdrawal.userPendingRewards);
});
@ -363,7 +347,12 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(ctxtInitial.staticATokenSupply).to.be.eq(0);
expect(ctxtInitial.userATokenBalance).to.be.eq(0);
expect(ctxtAfterDeposit.userDynamicStaticATokenBalance).to.be.eq(amountToDeposit);
expect(ctxtAfterWithdrawal.userATokenBalance).to.be.gt(amountToDeposit);
expect(ctxtAfterWithdrawal.userATokenBalance).to.be.eq(
rayMul(
ctxtAfterDeposit.userStaticATokenBalance.toString(),
ctxtAfterWithdrawal.currentRate.toString()
).toString()
);
expect(ctxtAfterWithdrawal.userStaticATokenBalance).to.be.eq(0);
});
@ -383,9 +372,8 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
defaultTxParams
)
);
await waitForTx(await aweth.approve(staticAToken.address, amountToDeposit, defaultTxParams));
const ctxtInitial = await getContext(ctxtParams);
await waitForTx(await aweth.approve(staticAToken.address, amountToDeposit, defaultTxParams));
// Deposit
await waitForTx(
@ -393,7 +381,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
);
const ctxtAfterDeposit = await getContext(ctxtParams);
const expectedATokenWithdraw = await staticAToken.staticToDynamicAmount(amountToWithdraw);
// Withdraw
await waitForTx(
@ -403,7 +390,7 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
const ctxtAfterWithdrawal = await getContext(ctxtParams);
expect(ctxtInitial.userStaticATokenBalance).to.be.eq(0);
expect(ctxtInitial.userATokenBalance).to.gt(amountToDeposit);
expect(ctxtInitial.userATokenBalance).to.eq(amountToDeposit);
expect(ctxtInitial.staticATokenSupply).to.be.eq(0);
expect(ctxtInitial.staticATokenUnderlyingBalance).to.be.eq(0);
@ -416,13 +403,24 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
expect(ctxtAfterWithdrawal.userStaticATokenBalance).to.be.eq(
ctxtAfterDeposit.userStaticATokenBalance.sub(amountToWithdraw)
);
expect(ctxtAfterWithdrawal.userDynamicStaticATokenBalance).to.be.lt(
ctxtAfterDeposit.userDynamicStaticATokenBalance
expect(ctxtAfterWithdrawal.userDynamicStaticATokenBalance).to.be.eq(
BigNumber.from(
rayMul(
new bnjs(ctxtAfterDeposit.userStaticATokenBalance.sub(amountToWithdraw).toString()),
new bnjs(ctxtAfterWithdrawal.currentRate.toString())
).toString()
)
);
expect(ctxtAfterWithdrawal.userDynamicStaticATokenBalance).to.be.gt(
ctxtAfterDeposit.userDynamicStaticATokenBalance.sub(expectedATokenWithdraw)
expect(ctxtAfterWithdrawal.userATokenBalance).to.be.eq(
BigNumber.from(
rayMul(
new bnjs(ctxtAfterDeposit.userScaledBalanceAToken.add(amountToWithdraw).toString()),
new bnjs(ctxtAfterWithdrawal.currentRate.toString())
).toString()
)
);
expect(ctxtAfterWithdrawal.userATokenBalance).to.gt(ctxtAfterDeposit.userATokenBalance);
});
it('Transfer with permit()', async () => {
@ -569,7 +567,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
});
it('Deposit using metaDeposit()', async () => {
// What is a metadeposit
const amountToDeposit = utils.parseEther('5');
const chainId = DRE.network.config.chainId ? DRE.network.config.chainId : 1;
@ -592,7 +589,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
await waitForTx(await weth.deposit({ value: amountToDeposit }));
await waitForTx(await weth.approve(staticAToken.address, amountToDeposit, defaultTxParams));
// Here it begins
const tokenName = await staticAToken.name();
const nonce = (await staticAToken._nonces(userSigner._address)).toNumber();
const value = amountToDeposit.toString();
@ -600,7 +596,7 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
const depositor = userSigner._address;
const recipient = userSigner._address;
const fromUnderlying = true;
const deadline = MAX_UINT_AMOUNT; // (await timeLatest()).plus(60 * 60).toFixed();
const deadline = MAX_UINT_AMOUNT;
const msgParams = buildMetaDepositParams(
chainId,
@ -690,7 +686,9 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
const ctxtAfterDeposit = await getContext(ctxtParams);
expect(ctxtInitial.userStaticATokenBalance).to.be.eq(0);
expect(ctxtAfterDeposit.userStaticATokenBalance).to.be.gt(0);
expect(ctxtAfterDeposit.userStaticATokenBalance).to.be.eq(
BigNumber.from(rayDiv(value.toString(), ctxtAfterDeposit.currentRate.toString()).toString())
);
expect(ctxtAfterDeposit.userDynamicStaticATokenBalance).to.be.eq(value);
});
@ -725,15 +723,23 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
await waitForTx(await staticAToken.claimRewards(userSigner._address));
const ctxtAfterClaim = await getContext(ctxtParams);
expect(ctxtAfterWithdrawal.userDynamicStaticATokenBalance).to.be.gt(
ctxtBeforeWithdrawal.userDynamicStaticATokenBalance.sub(amountToWithdraw)
expect(ctxtBeforeWithdrawal.userATokenBalance).to.be.eq(0);
expect(ctxtBeforeWithdrawal.staticATokenATokenBalance).to.be.eq(amountToDeposit);
expect(ctxtAfterWithdrawal.userATokenBalance).to.be.eq(amountToWithdraw);
expect(ctxtAfterWithdrawal.userDynamicStaticATokenBalance).to.be.eq(
BigNumber.from(
rayMul(
new bnjs(ctxtBeforeWithdrawal.userStaticATokenBalance.toString()),
new bnjs(ctxtAfterWithdrawal.currentRate.toString())
).toString()
).sub(amountToWithdraw)
);
expect(ctxtAfterWithdrawal.userStkAaveBalance).to.be.eq(0);
expect(ctxtAfterClaim.userStkAaveBalance).to.be.eq(ctxtAfterWithdrawal.userPendingRewards);
});
it('Withdraw using metaWithdraw()', async () => {
// What is a metadeposit
const amountToDeposit = utils.parseEther('5');
const chainId = DRE.network.config.chainId ? DRE.network.config.chainId : 1;
@ -864,7 +870,6 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
});
it('Withdraw using metaWithdraw() (expect to fail)', async () => {
// What is a metadeposit
const amountToDeposit = utils.parseEther('5');
const chainId = DRE.network.config.chainId ? DRE.network.config.chainId : 1;
@ -902,7 +907,7 @@ describe('StaticATokenLM: aToken wrapper with static balances and liquidity mini
await await staticAToken.dynamicBalanceOf(userSigner._address)
).toString();
const toUnderlying = true;
const deadline = MAX_UINT_AMOUNT; // (await timeLatest()).plus(60 * 60).toFixed();
const deadline = MAX_UINT_AMOUNT;
const msgParams = buildMetaWithdrawParams(
chainId,