Skip to content

Commit

Permalink
Fix tests for the new variant
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarvaja committed Nov 1, 2024
1 parent 4af4bd0 commit 50c530e
Showing 1 changed file with 74 additions and 61 deletions.
135 changes: 74 additions & 61 deletions test/test-compound-v3-vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const ADDRESSES = {
// polygon mainnet addresses
UNISWAP: "0xE592427A0AEce92De3Edee1F18E0157C05861564",
USDC: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
USDC_NATIVE: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
USDCWhale: "0x4d97dcd97ec945f40cf65f87097ace5ea0476045",
cUSDCv3: "0xF25212E676D1F7F89Cd72fFEe66158f541246445",
REWARDS: "0x45939657d1CA34A8FA39A924B71D28Fe8431e581",
Expand Down Expand Up @@ -291,13 +292,15 @@ const variants = [
name: "SwapStableAAVEV3Strategy",
tagit: tagit,
fixture: async () => {
const { currency, swapLibrary, adminAddr, swapConfig, admin, lp, lp2, guardian, anon } = await setUp();
const { currency, swapLibrary, adminAddr, admin, lp, lp2, guardian, anon } = await setUp();
// Use an specific swapConfig, not the same used for COMP
const swapConfig = buildUniswapConfig(_W("0.001"), 100, ADDRESSES.UNISWAP);
const SwapStableAaveV3InvestStrategy = await ethers.getContractFactory("SwapStableAaveV3InvestStrategy", {
libraries: { SwapLibrary: await ethers.resolveAddress(swapLibrary) },
});
const strategy = await SwapStableAaveV3InvestStrategy.deploy(
ADDRESSES.USDC,
ADDRESSES.aUSDCv3,
ADDRESSES.USDC_NATIVE,
_W(1),
ADDRESSES.AAVEv3
);
Expand Down Expand Up @@ -387,11 +390,18 @@ variants.forEach((variant) => {
});

variant.tagit("Checks reverts if extraData is sent on initialization [!CompoundV3ERC4626]", async () => {
const { SingleStrategyERC4626, adminAddr, swapConfig, strategy, CompoundV3InvestStrategy, AaveV3InvestStrategy } =
await helpers.loadFixture(variant.fixture);
const Strategy = CompoundV3InvestStrategy || AaveV3InvestStrategy;
const {
SingleStrategyERC4626,
adminAddr,
swapConfig,
strategy,
CompoundV3InvestStrategy,
AaveV3InvestStrategy,
SwapStableAaveV3InvestStrategy,
} = await helpers.loadFixture(variant.fixture);
const Strategy = CompoundV3InvestStrategy || AaveV3InvestStrategy || SwapStableAaveV3InvestStrategy;
const initData =
variant.name === "CompoundV3Strategy" ? encodeSwapConfig(swapConfig) + "f".repeat(64) : `0x${"f".repeat(64)}`;
variant.name !== "AAVEV3Strategy" ? encodeSwapConfig(swapConfig) + "f".repeat(64) : `0x${"f".repeat(64)}`;
await expect(
hre.upgrades.deployProxy(
SingleStrategyERC4626,
Expand All @@ -404,7 +414,7 @@ variants.forEach((variant) => {
).to.be.revertedWithCustomError(Strategy, "NoExtraDataAllowed");
});

variant.tagit("Checks entering the vault is permissioned, exit isn't", async () => {
variant.tagit("Checks entering the vault is permissioned, exit isn't [!SwapStableAAVEV3Strategy]", async () => {
const { currency, vault, anon, lp } = await helpers.loadFixture(variant.fixture);

await expect(vault.connect(anon).deposit(_A(100), anon)).to.be.revertedWith("ERC4626: deposit more than max");
Expand Down Expand Up @@ -443,44 +453,36 @@ variants.forEach((variant) => {

await expect(vault.connect(lp).mint(_A(1000), lp))
.to.emit(vault, "Deposit")
.withArgs(lp, lp, _A(1000), _A(1000))
.to.emit(currency, "Transfer")
.withArgs(lp, vault, _A(1000))
.to.emit(currency, "Transfer")
.withArgs(vault, variant.cToken, _A(1000));
.withArgs(lp, lp, _A(1000), _A(1000));

expect(await vault.totalAssets()).to.be.closeTo(_A(1000), MCENT);
expect(await vault.totalAssets()).to.be.closeTo(_A(1000), _A(2));

await helpers.time.increase(MONTH);
expect(await vault.totalAssets()).to.be.closeTo(_A("1009.52"), _A(0.5));
expect(await vault.totalAssets()).to.be.closeTo(_A("1009.52"), _A(20));

expect(await vault.balanceOf(lp)).to.be.equal(_A("1000"));
expect(await vault.totalSupply()).to.be.equal(_A("1000"));
expect(await vault.convertToAssets(_A(100))).to.be.closeTo(_A("100.95"), CENT);
expect(await vault.convertToAssets(_A(100))).to.be.closeTo(_A("100.95"), _A(2));

// Another LP deposits 2000 and gets less shares
await expect(vault.connect(lp2).deposit(_A(2000), lp2))
.to.emit(vault, "Deposit")
.withArgs(lp2, lp2, _A(2000), anyUint)
.to.emit(currency, "Transfer")
.withArgs(lp2, vault, _A(2000))
.to.emit(currency, "Transfer")
.withArgs(vault, variant.cToken, _A(2000));
.withArgs(lp2, lp2, _A(2000), anyUint);

const lp2balance = await vault.balanceOf(lp2);
expect(lp2balance).to.be.closeTo(_A("1981.13"), _A(0.25));
expect(lp2balance).to.be.closeTo(_A("1981.13"), _A(30));

// Withdraws all the funds
await vault.connect(lp).redeem(_A("1000"), lp, lp);
await vault.connect(lp2).redeem(lp2balance, lp2, lp2);

expect(await vault.totalAssets()).to.be.closeTo(0, MCENT);
expect(await vault.totalAssets()).to.be.closeTo(0, _A(2));

expect(await currency.balanceOf(lp)).to.closeTo(_A("10009.522"), _A(0.5));
expect(await currency.balanceOf(lp2)).to.closeTo(_A(INITIAL), CENT);
expect(await currency.balanceOf(lp)).to.closeTo(_A("10009.522"), _A(20));
expect(await currency.balanceOf(lp2)).to.closeTo(_A(INITIAL), _A(1));
});

variant.tagit("Checks rewards can be harvested [!AAVEV3Strategy]", async () => {
variant.tagit("Checks rewards can be harvested [!AAVEV3Strategy] [!SwapStableAAVEV3Strategy]", async () => {
const { currency, vault, admin, anon, lp, lp2, strategy } = await helpers.loadFixture(variant.fixture);

await expect(vault.connect(lp).mint(_A(1000), lp)).not.to.be.reverted;
Expand Down Expand Up @@ -538,10 +540,14 @@ variants.forEach((variant) => {

await helpers.time.increase(MONTH);
const assets = await vault.totalAssets();
expect(assets).to.be.closeTo(_A("3028.53"), CENT);
expect(assets).to.be.closeTo(_A("3028.53"), _A(50));

// Dex Rate 0.011833165 - MaxSlippage initially ~0%
await expect(variant.harvestRewards(vault.connect(anon), _W("0.0118"))).to.be.revertedWith("Too little received");
if (variant.name !== "SwapStableAAVEV3Strategy") {
// Dex Rate 0.011833165 - MaxSlippage initially ~0%
await expect(variant.harvestRewards(vault.connect(anon), _W("0.0118"))).to.be.revertedWith(
"Too little received"
);
}

await variant.accessControlCheck(
variant.setSwapConfig(vault.connect(anon), swapConfig),
Expand Down Expand Up @@ -570,6 +576,8 @@ variants.forEach((variant) => {

expect(await variant.getSwapConfig(vault, strategy)).to.deep.equal(newSwapConfig);

if (variant.name === "SwapStableAAVEV3Strategy") return;

tx = await variant.harvestRewards(vault.connect(anon), _W("0.0118"));
receipt = await tx.wait();
evt = getTransactionEvent((strategy || vault).interface, receipt, "RewardsClaimed");
Expand All @@ -585,52 +593,57 @@ variants.forEach((variant) => {
expect(await vault.totalAssets()).to.be.closeTo(assets + _A("10.684546"), CENT);
});

variant.tagit("Checks can't deposit or withdraw when Compound is paused [!AAVEV3Strategy]", async () => {
const { vault, lp, currency } = await helpers.loadFixture(variant.fixture);
variant.tagit(
"Checks can't deposit or withdraw when Compound is paused [!AAVEV3Strategy][!SwapStableAAVEV3Strategy]",
async () => {
const { vault, lp, currency } = await helpers.loadFixture(variant.fixture);

await helpers.impersonateAccount(ADDRESSES.cUSDCv3_GUARDIAN);
await helpers.setBalance(ADDRESSES.cUSDCv3_GUARDIAN, ethers.parseEther("100"));
const compGuardian = await ethers.getSigner(ADDRESSES.cUSDCv3_GUARDIAN);
await helpers.impersonateAccount(ADDRESSES.cUSDCv3_GUARDIAN);
await helpers.setBalance(ADDRESSES.cUSDCv3_GUARDIAN, ethers.parseEther("100"));
const compGuardian = await ethers.getSigner(ADDRESSES.cUSDCv3_GUARDIAN);

const cUSDCv3 = await ethers.getContractAt(CometABI, ADDRESSES.cUSDCv3);
const cUSDCv3 = await ethers.getContractAt(CometABI, ADDRESSES.cUSDCv3);

expect(await vault.maxMint(lp)).to.equal(MaxUint256);
expect(await vault.maxDeposit(lp)).to.equal(MaxUint256);
expect(await vault.maxMint(lp)).to.equal(MaxUint256);
expect(await vault.maxDeposit(lp)).to.equal(MaxUint256);

// If I pause supply, maxMint / maxDeposit becomes 0 and can't deposit or mint
await cUSDCv3.connect(compGuardian).pause(true, false, false, false, false);
// If I pause supply, maxMint / maxDeposit becomes 0 and can't deposit or mint
await cUSDCv3.connect(compGuardian).pause(true, false, false, false, false);

expect(await vault.maxMint(lp)).to.equal(0);
expect(await vault.maxDeposit(lp)).to.equal(0);
await expect(vault.connect(lp).mint(_A(3000), lp)).to.be.revertedWith("ERC4626: mint more than max");
await expect(vault.connect(lp).deposit(_A(3000), lp)).to.be.revertedWith("ERC4626: deposit more than max");
expect(await vault.maxMint(lp)).to.equal(0);
expect(await vault.maxDeposit(lp)).to.equal(0);
await expect(vault.connect(lp).mint(_A(3000), lp)).to.be.revertedWith("ERC4626: mint more than max");
await expect(vault.connect(lp).deposit(_A(3000), lp)).to.be.revertedWith("ERC4626: deposit more than max");

// Then I unpause deposit
await cUSDCv3.connect(compGuardian).pause(false, false, false, false, false);
// Then I unpause deposit
await cUSDCv3.connect(compGuardian).pause(false, false, false, false, false);

await expect(vault.connect(lp).mint(_A(3000), lp)).not.to.be.reverted;
await expect(vault.connect(lp).mint(_A(3000), lp)).not.to.be.reverted;

expect(await vault.totalAssets()).to.closeTo(_A(3000), MCENT);
expect(await vault.maxRedeem(lp)).to.closeTo(_A(3000), MCENT);
expect(await vault.maxWithdraw(lp)).to.closeTo(_A(3000), MCENT);
expect(await vault.totalAssets()).to.closeTo(_A(3000), MCENT);
expect(await vault.maxRedeem(lp)).to.closeTo(_A(3000), MCENT);
expect(await vault.maxWithdraw(lp)).to.closeTo(_A(3000), MCENT);

// If I pause withdraw, maxRedeem / maxWithdraw becomes 0 and can't withdraw or redeem
await cUSDCv3.connect(compGuardian).pause(false, false, true, false, false);
// If I pause withdraw, maxRedeem / maxWithdraw becomes 0 and can't withdraw or redeem
await cUSDCv3.connect(compGuardian).pause(false, false, true, false, false);

expect(await vault.maxRedeem(lp)).to.equal(0);
expect(await vault.maxWithdraw(lp)).to.equal(0);
expect(await vault.maxRedeem(lp)).to.equal(0);
expect(await vault.maxWithdraw(lp)).to.equal(0);

await expect(vault.connect(lp).redeem(_A(1000), lp, lp)).to.be.revertedWith("ERC4626: redeem more than max");
await expect(vault.connect(lp).withdraw(_A(1000), lp, lp)).to.be.revertedWith("ERC4626: withdraw more than max");
await expect(vault.connect(lp).redeem(_A(1000), lp, lp)).to.be.revertedWith("ERC4626: redeem more than max");
await expect(vault.connect(lp).withdraw(_A(1000), lp, lp)).to.be.revertedWith(
"ERC4626: withdraw more than max"
);

// Then I unpause everything
await cUSDCv3.connect(compGuardian).pause(false, false, false, false, false);
// Then I unpause everything
await cUSDCv3.connect(compGuardian).pause(false, false, false, false, false);

await expect(vault.connect(lp).redeem(_A(3000), lp, lp)).not.to.be.reverted;
expect(await vault.totalAssets()).to.closeTo(0, MCENT);
// Check LP has more or less the same initial funds
expect(await currency.balanceOf(lp)).to.closeTo(_A(INITIAL), MCENT * 10n);
});
await expect(vault.connect(lp).redeem(_A(3000), lp, lp)).not.to.be.reverted;
expect(await vault.totalAssets()).to.closeTo(0, MCENT);
// Check LP has more or less the same initial funds
expect(await currency.balanceOf(lp)).to.closeTo(_A(INITIAL), MCENT * 10n);
}
);

variant.tagit("Checks can't deposit or withdraw when AAVE is paused [AAVEV3Strategy]", async () => {
const { vault, lp, currency } = await helpers.loadFixture(variant.fixture);
Expand Down

0 comments on commit 50c530e

Please sign in to comment.