Skip to content

Commit

Permalink
reworked the structure of fixtures and add vanillaInitializedstablePool
Browse files Browse the repository at this point in the history
  • Loading branch information
jiro-ono committed Nov 22, 2022
1 parent 15b38c5 commit 218756e
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 24 deletions.
2 changes: 2 additions & 0 deletions test/fixtures/constant-product/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { initializedConstantProductPool } from "./initializedConstantProductPool";
export { uninitializedConstantProductPool } from "./uninitializedConstantProductPool";
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ConstantProductPool__factory,
ERC20Mock__factory,
MasterDeployer,
} from "../../types";
} from "../../../types";

export const initializedConstantProductPool = deployments.createFixture(
async (
Expand Down Expand Up @@ -76,7 +76,6 @@ export const initializedConstantProductPool = deployments.createFixture(
.then((tx) => tx.wait());

await bento

.transfer(token1.address, deployer.address, contractReceipt.events?.[0].args?.pool, "1000000000000000000")
.then((tx) => tx.wait());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ConstantProductPool__factory,
ERC20Mock__factory,
MasterDeployer,
} from "../../types";
} from "../../../types";

export const uninitializedConstantProductPool = deployments.createFixture(async ({ deployments, ethers }, options) => {
await deployments.fixture(["ConstantProductPoolFactory"]); // ensure you start from a fresh deployments
Expand Down
5 changes: 2 additions & 3 deletions test/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { initializedConstantProductPool } from "./initializedConstantProductPool";
export { uninitializedConstantProductPool } from "./uninitializedConstantProductPool";
export { initializedStablePool, uninitializedStablePool } from "./stable-pool";
export * from "./constant-product";
export * from "./stable-pool";
1 change: 1 addition & 0 deletions test/fixtures/stable-pool/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { initializedStablePool } from "./initializedStablePool";
export { uninitializedStablePool } from "./uninitializedStablePool";
export { vanillaInitializedStablePool } from "./vanillaInitializedStablePool";
83 changes: 83 additions & 0 deletions test/fixtures/stable-pool/vanillaInitializedStablePool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { ethers, deployments } from "hardhat";
import { BentoBoxV1, StablePoolFactory, StablePool__factory, ERC20Mock__factory, MasterDeployer } from "../../../types";

export const vanillaInitializedStablePool = deployments.createFixture(
async (
{
deployments,
ethers: {
getNamedSigners,
constants: { MaxUint256 },
},
},
options
) => {
await deployments.fixture(["StablePoolFactory"], { keepExistingDeployments: true }); // ensure you start from a fresh deployments
const { deployer } = await getNamedSigners();

const ERC20 = await ethers.getContractFactory<ERC20Mock__factory>("ERC20Mock");

const token0 = await ERC20.deploy("Token 0", "TOKEN0", ethers.constants.MaxUint256);
await token0.deployed();

const token1 = await ERC20.deploy("Token 1", "TOKEN1", ethers.constants.MaxUint256);
await token1.deployed();

const masterDeployer = await ethers.getContract<MasterDeployer>("MasterDeployer");

const stablePoolFactory = await ethers.getContract<StablePoolFactory>("StablePoolFactory");

const deployData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "uint256"],
[token0.address, token1.address, 0]
);

const contractReceipt = await masterDeployer
.deployPool(stablePoolFactory.address, deployData)
.then((tx) => tx.wait());

const bento = await ethers.getContract<BentoBoxV1>("BentoBoxV1");

await bento.whitelistMasterContract("0x0000000000000000000000000000000000000001", true);

await token0.approve(bento.address, MaxUint256).then((tx) => tx.wait());

await token1.approve(bento.address, MaxUint256).then((tx) => tx.wait());

await bento
.setMasterContractApproval(
deployer.address,
"0x0000000000000000000000000000000000000001",
true,
"0",
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000000000"
)
.then((tx) => tx.wait());

await bento
.deposit(token0.address, deployer.address, deployer.address, "1000000000000000000", 0)
.then((tx) => tx.wait());

await bento
.deposit(token1.address, deployer.address, deployer.address, "1000000000000000000", 0)
.then((tx) => tx.wait());

await bento
.transfer(token0.address, deployer.address, contractReceipt.events?.[0].args?.pool, "1000000000000000000")
.then((tx) => tx.wait());

await bento
.transfer(token1.address, deployer.address, contractReceipt.events?.[0].args?.pool, "1000000000000000000")
.then((tx) => tx.wait());

const Pool = await ethers.getContractFactory<StablePool__factory>("StablePool");

const pool = Pool.attach(contractReceipt.events?.[0].args?.pool);

await pool.mint(ethers.utils.defaultAbiCoder.encode(["address"], [deployer.address])).then((tx) => tx.wait());

return pool;
},
"vanillaInitializedStablePool"
);
31 changes: 13 additions & 18 deletions test/stable-pool/StablePool.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, util } from "chai";
import { expect } from "chai";
import { BigNumber } from "ethers";
import { deployments, ethers, getNamedAccounts } from "hardhat";

Expand All @@ -12,11 +12,8 @@ import {
FlashSwapMock,
FlashSwapMock__factory,
MasterDeployer,
StablePool,
} from "../../types";
import { initializedStablePool, uninitializedStablePool } from "../fixtures";
import { initialize } from "../harness/ConstantProduct";
import { ADDRESS_ZERO } from "../utilities";
import { initializedStablePool, uninitializedStablePool, vanillaInitializedStablePool } from "../fixtures";

describe("Stable Pool", () => {
before(async () => {
Expand Down Expand Up @@ -104,7 +101,7 @@ describe("Stable Pool", () => {

it("reverts if insufficient liquidity minted", async () => {
const deployer = await ethers.getNamedSigner("deployer");
const pool = await initializedStablePool();
const pool = await vanillaInitializedStablePool();
const token0 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token0());
const token1 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token1());
await token0.transfer(pool.address, 1);
Expand Down Expand Up @@ -266,7 +263,7 @@ describe("Stable Pool", () => {
describe("#flashSwap", function () {
it("reverts on call", async () => {
// flashSwap not supported on StablePool
const pool = await initializedStablePool();
const pool = await vanillaInitializedStablePool();
const token0 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token0());
const token1 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token1());
const bento = await ethers.getContract<BentoBoxV1>("BentoBoxV1");
Expand Down Expand Up @@ -327,7 +324,7 @@ describe("Stable Pool", () => {

describe("#updateBarParameters", function () {
it("mutates bar fee if changed on master deployer", async () => {
const pool = await initializedStablePool();
const pool = await vanillaInitializedStablePool();

const masterDeployer = await ethers.getContract<MasterDeployer>("MasterDeployer");

Expand Down Expand Up @@ -368,24 +365,23 @@ describe("Stable Pool", () => {
describe("#getAmountOut", function () {
it("returns 1000000000 given input of token0 in 1e18:1e18 pool, with bar fee 0 & swap fee 0", async () => {
const bentoBox = await ethers.getContract<BentoBoxV1>("BentoBoxV1");
const pool = await initializedStablePool({ fee: 0 });
const pool = await vanillaInitializedStablePool();
const shareIn = await bentoBox.toShare(await pool.token0(), 1000000000, false);
const shareOut = await pool.getAmountOut(
ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [await pool.token0(), shareIn])
);
expect(await bentoBox.toAmount(await pool.token1(), shareOut, false)).to.equal("1000000000");
expect(await bentoBox.toAmount(await pool.token1(), shareOut, false)).to.equal("999000000");
});

it("returns 1000000000 given input of token1 in 1e18:1e18 pool, with bar fee 0 & swap fee 0", async () => {
//todo: need to rework the fixture for these
const bentoBox = await ethers.getContract<BentoBoxV1>("BentoBoxV1");
const pool = await initializedStablePool({ fee: 0 });
console.log(await pool.getReserves());
const pool = await vanillaInitializedStablePool();
const shareIn = await bentoBox.toShare(await pool.token1(), 1000000000, false);
const shareOut = await pool.getAmountOut(
ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [await pool.token1(), shareIn])
);
expect(await bentoBox.toAmount(await pool.token0(), shareOut, false)).to.equal("1000000000");
expect(await bentoBox.toAmount(await pool.token0(), shareOut, false)).to.equal("999000000");
});

it("reverts if tokenIn is not equal to token0 and token1", async () => {
Expand All @@ -399,7 +395,6 @@ describe("Stable Pool", () => {
["address", "address", "uint256"],
[token0.address, token1.address, 30]
);
await masterDeployer.deployPool(stableFactory.address, deployData);

if (token0.address > token1.address) {
const saveToken = token0;
Expand All @@ -414,14 +409,14 @@ describe("Stable Pool", () => {
["address", "uint256"],
["0x0000000000000000000000000000000000000003", 0]
);
await expect(stablePool.getAmountIn(data)).to.be.revertedWith("InvalidInputToken()");
expect(stablePool.getAmountIn(data)).to.be.revertedWith("InvalidInputToken()");
});
});

describe("#getAmountIn", function () {
it("reverts on call", async () => {
// getAmountIn not supported on StablePool
const pool = await initializedStablePool();
const pool = await vanillaInitializedStablePool();

const data = ethers.utils.defaultAbiCoder.encode(["address", "uint256"], [await pool.token0(), "1000000000"]);
await expect(pool.getAmountIn(data)).to.be.reverted;
Expand All @@ -431,7 +426,7 @@ describe("Stable Pool", () => {
describe("#getReserves", function () {
it("returns expected values for initiliazedStablePool", async () => {
// same imp as getReserves()
const pool = await initializedStablePool();
const pool = await vanillaInitializedStablePool();
const [reserve0, reserve1] = await pool.getNativeReserves();
//todo: currently reserve0 is set to 909090909090909090, maybe fix this?
expect(reserve0).equal("1000000000000000000");
Expand All @@ -442,7 +437,7 @@ describe("Stable Pool", () => {
describe("#getNativeReserves", function () {
it("returns expected values for initiliazedStablePool", async () => {
// same imp as getReserves()
const pool = await initializedStablePool();
const pool = await vanillaInitializedStablePool();
const [reserve0, reserve1] = await pool.getNativeReserves();
//todo: currently reserve0 is set to 909090909090909090, maybe fix this?
expect(reserve0).equal("1000000000000000000");
Expand Down

0 comments on commit 218756e

Please sign in to comment.