Skip to content

Commit

Permalink
refactor: fork tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PraneshASP committed May 22, 2024
1 parent 3fabd3f commit 74a97ad
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 77 deletions.
178 changes: 103 additions & 75 deletions contracts/test/strategies/aero-oeth-amo.base.fork-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ const { run } = require("hardhat");
const addresses = require("../../utils/addresses");
const { units, oethUnits, isCI } = require("../helpers");
const { aeroOETHAMOFixture } = require("../_fixture");
const { createFixtureLoader } = require("../_fixture");
const { defaultBaseFixture } = require("../_fixture-base");
const { impersonateAndFund } = require("../../utils/signers");
const { BigNumber, ethers } = require("ethers");

const log = require("../../utils/logger")("test:fork:aero-oeth:metapool");

const baseFixture = createFixtureLoader(defaultBaseFixture);

describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
this.timeout(0);
// Retry up to 3 times on CI
Expand All @@ -19,7 +23,7 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {

describe("", () => {
beforeEach(async () => {
fixture = await aeroOETHAMOFixture();
fixture = await baseFixture();
});
it("Should have constants and immutables set", async () => {
const { aerodromeEthStrategy, oethReserveIndex, wethReserveIndex } =
Expand Down Expand Up @@ -57,7 +61,7 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {

const balance = await aerodromeEthStrategy.checkBalance(weth.address);
log(`check balance ${balance}`);
expect(balance).gt(0);
expect(balance).to.eq(0); // will be 0 initially as there are no deposits

// This uses a transaction to call a view function so the gas usage can be reported.
const tx = await aerodromeEthStrategy
Expand All @@ -68,21 +72,12 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
});
describe("with some WETH in the vault", () => {
beforeEach(async () => {
fixture = await aeroOETHAMOFixture();
fixture = await baseFixture();
});
it("Vault should deposit some WETH to AMO strategy", async function () {
const {
aerodromeEthStrategy,
oeth,
pool,
weth,
josh,
oethVault,
oethReserveIndex,
wethReserveIndex,
} = fixture;
const { aerodromeEthStrategy, oeth, weth, josh, oethVault } = fixture;

const wethDepositAmount = await units("1000", weth);
const wethDepositAmount = await units("100", weth);

// Vault transfers WETH to strategy
await weth
Expand Down Expand Up @@ -141,8 +136,7 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
);
});
it("Only vault can deposit some WETH to AMO strategy", async function () {
const { aerodromeEthStrategy, josh, weth, strategist, timelock } =
fixture;
const { aerodromeEthStrategy, josh, weth } = fixture;

const depositAmount = await units("50", weth);

Expand All @@ -164,15 +158,8 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
await expect(tx).to.revertedWith("Caller is not the Vault");
});
it("Only vault can deposit all WETH to AMO strategy", async function () {
const {
aerodromeEthStrategy,
pool,
josh,
weth,
strategist,
timelock,
oethVaultSigner,
} = fixture;
const { aerodromeEthStrategy, pool, josh, weth, oethVaultSigner } =
fixture;

const depositAmount = parseUnits("50");
await weth
Expand All @@ -184,7 +171,6 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
.deposit(weth.address, depositAmount);
await expect(tx).to.revertedWith("Caller is not the Vault");

// for (const signer of [strategist, timelock, josh]) { -> Check why this fails
for (const signer of [josh]) {
const tx = aerodromeEthStrategy.connect(signer).depositAll();
await expect(tx).to.revertedWith("Caller is not the Vault");
Expand All @@ -198,7 +184,7 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
describe("with the strategy having some OETH and WETH in the Pool", () => {
beforeEach(async () => {
fixture = await aeroOETHAMOFixture({
wethMintAmount: 5000,
wethMintAmount: 50,
depositToStrategy: true,
balancePool: true,
});
Expand Down Expand Up @@ -288,7 +274,7 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
wethReserveIndex,
} = fixture;

const withdrawAmount = oethUnits("1000");
const withdrawAmount = oethUnits("30");

const { oethBurnAmount, aeroBalances: aeroBalancesBefore } =
await calcOethWithdrawAmount(fixture, withdrawAmount);
Expand Down Expand Up @@ -381,16 +367,23 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
});
});
describe("with pool rebalance", () => {
const oethToSwap = parseUnits("100");
beforeEach(async () => {
fixture = await aeroOETHAMOFixture();
});
it("Should be able to mint OETH and send received weth to recipient", async function () {
const { oeth, weth, josh, aeroRouter, aerodromeEthStrategy, oethVault } =
fixture;
const {
oeth,
weth,
josh,
aeroRouter,
aerodromeEthStrategy,
oethVault,
oethVaultSigner,
} = fixture;

let joshWethBalanceBefore = await weth.balanceOf(josh.address);

const oethToSwap = parseUnits("1000");
await oeth.connect(oethVaultSigner).mint(josh.address, oethUnits("100"));
await oeth.connect(josh).approve(aeroRouter.address, oethToSwap);

// Perform swap to imbalance the pool
Expand Down Expand Up @@ -425,9 +418,11 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
fixture: JSON.stringify(fixture),
output: false,
});
const strategistAddress = await oethVault.strategistAddr();
const sStrategist = await impersonateAndFund(strategistAddress);
// Rebalance pool
tx = await aerodromeEthStrategy
.connect(josh)
.connect(sStrategist)
.swapAndRebalancePool(amountIn, 0, tokenIn, josh.address);

const receipt = await tx.wait();
Expand Down Expand Up @@ -457,49 +452,47 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
});

it("Should not rebalance pool by making the WETH reserve > OETH reserve", async function () {
const { oeth, weth, josh, aeroRouter, aerodromeEthStrategy } = fixture;
const oethToSwap = parseUnits("1000");
await oeth.connect(josh).approve(aeroRouter.address, oethToSwap);

// Perform swap to imbalance the pool
await aeroRouter
.connect(josh)
.swapExactTokensForTokens(
oethToSwap,
0,
[
[
oeth.address,
weth.address,
true,
addresses.base.aeroFactoryAddress,
],
],
josh.address,
parseInt(Date.now() / 1000) + 5 * 360
);
const {
oeth,
weth,
josh,
aeroRouter,
aerodromeEthStrategy,
oethVaultSigner,
oethVault,
} = fixture;

// Try to make the pool balance even worse (should revert)
const { tokenIn, amountIn } = await getParamsForPoolRebalance(
fixture,
49,
51
48,
52
);
if (tokenIn == weth.address) {
await weth
.connect(josh)
.transfer(aerodromeEthStrategy.address, amountIn);
}

const strategistAddress = await oethVault.strategistAddr();
const sStrategist = await impersonateAndFund(strategistAddress);
// Rebalance pool should revert with error message
tx = aerodromeEthStrategy
.connect(josh)
.connect(sStrategist)
.swapAndRebalancePool(amountIn, 0, tokenIn, josh.address);

await expect(tx).to.revertedWith("WETH reserves exceeds OETH");
});
it("Should not rebalance pool more than allowed threshold", async function () {
const { oeth, weth, josh, aeroRouter, aerodromeEthStrategy } = fixture;
const oethToSwap = parseUnits("1000");
const {
oeth,
weth,
josh,
aeroRouter,
aerodromeEthStrategy,
oethVault,
oethVaultSigner,
} = fixture;
await oeth.connect(oethVaultSigner).mint(josh.address, oethToSwap);
await oeth.connect(josh).approve(aeroRouter.address, oethToSwap);

// Perform swap to imbalance the pool
Expand Down Expand Up @@ -533,12 +526,15 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
.transfer(aerodromeEthStrategy.address, amountIn);
}

const strategistAddress = await oethVault.strategistAddr();
const sStrategist = await impersonateAndFund(strategistAddress);
// Rebalance pool
await aerodromeEthStrategy
.connect(josh)
.connect(sStrategist)
.swapAndRebalancePool(amountIn, 0, tokenIn, josh.address);
// Try to make the pool balance even worse (should revert)
const { tokenIn: tokenIn2, amountIn: amountIn2 } =
await getParamsForPoolRebalance(fixture, 57, 43);
await getParamsForPoolRebalance(fixture, 60, 40);
if (tokenIn2 == weth.address) {
await weth
.connect(josh)
Expand All @@ -547,7 +543,7 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {

// Rebalance pool should revert with error message
tx = aerodromeEthStrategy
.connect(josh)
.connect(sStrategist)
.swapAndRebalancePool(amountIn2, 0, tokenIn2, josh.address);
await expect(tx).to.revertedWith("Pool imbalance worsened");
});
Expand All @@ -559,6 +555,7 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
const wethDepositAmount = await units("100", weth);

// Vault transfers WETH to strategy
await weth.connect(josh).deposit({ value: wethDepositAmount });
await weth
.connect(josh)
.transfer(aerodromeEthStrategy.address, wethDepositAmount);
Expand Down Expand Up @@ -617,6 +614,7 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
await rebalancePool(fixture);

const depositAmount = parseUnits("50");
await weth.connect(josh).deposit({ value: depositAmount });
await weth
.connect(josh)
.transfer(aerodromeEthStrategy.address, depositAmount);
Expand Down Expand Up @@ -649,16 +647,30 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
aerodromeEthStrategy,
pool,
oeth,
josh,
oethVaultSigner,
weth,
oethVault,
oethReserveIndex,
wethReserveIndex,
} = fixture;

// deposit first
const wethDepositAmount = await units("100", weth);

// Vault transfers WETH to strategy
await weth.connect(josh).deposit({ value: wethDepositAmount });
await weth
.connect(josh)
.transfer(aerodromeEthStrategy.address, wethDepositAmount);

await aerodromeEthStrategy
.connect(oethVaultSigner)
.deposit(weth.address, wethDepositAmount);

await rebalancePool(fixture);

const withdrawAmount = oethUnits("1000");
const withdrawAmount = oethUnits("80");

const { oethBurnAmount, aeroBalances: aeroBalancesBefore } =
await calcOethWithdrawAmount(fixture, withdrawAmount);
Expand Down Expand Up @@ -726,7 +738,20 @@ describe("ForkTest: OETH AMO Aerodrome Strategy", function () {
weth,
oethReserveIndex,
wethReserveIndex,
josh,
} = fixture;
// deposit first
const wethDepositAmount = await units("100", weth);

// Vault transfers WETH to strategy
await weth.connect(josh).deposit({ value: wethDepositAmount });
await weth
.connect(josh)
.transfer(aerodromeEthStrategy.address, wethDepositAmount);

await aerodromeEthStrategy
.connect(oethVaultSigner)
.deposit(weth.address, wethDepositAmount);

await rebalancePool(fixture);

Expand Down Expand Up @@ -959,13 +984,6 @@ async function getParamsForPoolRebalance(
oethReserveIndex,
wethReserveIndex,
} = fixture;
await oeth
.connect(josh)
.approve(aeroRouter.address, ethers.utils.parseUnits("1000"));

await weth
.connect(josh)
.approve(aeroRouter.address, ethers.utils.parseUnits("1000"));

log(
"Stable liq ratio before",
Expand Down Expand Up @@ -1061,10 +1079,18 @@ async function getParamsForPoolRebalance(
}

async function rebalancePool(fixture) {
const { oeth, weth, josh, aeroRouter, aerodromeEthStrategy } = fixture;
const oethToSwap = parseUnits("1000");
const {
oeth,
weth,
josh,
aeroRouter,
aerodromeEthStrategy,
oethVaultSigner,
oethVault,
} = fixture;
const oethToSwap = parseUnits("100");
await oeth.connect(oethVaultSigner).mint(josh.address, oethUnits("100"));
await oeth.connect(josh).approve(aeroRouter.address, oethToSwap);

// Perform swap to imbalance the pool
await aeroRouter
.connect(josh)
Expand All @@ -1087,9 +1113,11 @@ async function rebalancePool(fixture) {
fixture: JSON.stringify(fixture),
output: false,
});
const strategistAddress = await oethVault.strategistAddr();
const sStrategist = await impersonateAndFund(strategistAddress);
// Rebalance pool
tx = await aerodromeEthStrategy
.connect(josh)
.connect(sStrategist)
.swapAndRebalancePool(amountIn, 0, tokenIn, josh.address);

const receipt = await tx.wait();
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/vault/oeth-vault.base.fork-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ describe("ForkTest: OETH Vault Base", function () {

describe("post deployment", () => {
it("Should have the correct governor address set", async () => {
const { oethVault, oethDripper, oeth, woeth, oethHarvester } = fixture;
const { oethVault, oethDripper, oeth, woeth, aeroHarvester } = fixture;

const oethContracts = [
oethVault,
oethDripper,
oeth,
woeth,
oethHarvester,
aeroHarvester,
];

for (let i = 0; i < oethContracts.length; i++) {
Expand Down

0 comments on commit 74a97ad

Please sign in to comment.