Skip to content

Commit

Permalink
setup moch testing explorer for constant & stable, added tests to con…
Browse files Browse the repository at this point in the history
…stant product
  • Loading branch information
jiro-ono committed Nov 24, 2022
1 parent c11e58c commit b045e09
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"mochaExplorer.files": ["test/stable-pool/*.test.ts"],
"mochaExplorer.files": ["test/constant-product/*.test.ts", "test/stable-pool/*test.ts"],
"mochaExplorer.fullTrace": true,
"mochaExplorer.exit": true,
"solidity.compileUsingRemoteVersion": "v0.8.7+commit.e28d00a7",
Expand Down
115 changes: 111 additions & 4 deletions test/constant-product/ConstantProductPool.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { BigNumber } from "ethers";
import { BigNumber, utils } from "ethers";
import { deployments, ethers, getNamedAccounts } from "hardhat";

import {
Expand Down Expand Up @@ -111,11 +111,65 @@ describe("Constant Product Pool", () => {
});

describe("#burn", function () {
//
it("burns all liquidity to token0 and token1 balances", async () => {
const deployer = await ethers.getNamedSigner("deployer");
const bob = await ethers.getNamedSigner("bob");
const pool = await initializedConstantProductPool();
const token0 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token0());
const token1 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token1());

await pool.transfer(pool.address, await pool.balanceOf(deployer.address));
const burnData = ethers.utils.defaultAbiCoder.encode(["address", "bool"], [bob.address, true]);
await pool.burn(burnData);

expect(await token0.balanceOf(bob.address)).to.be.above(0);

expect(await token1.balanceOf(bob.address)).to.be.above(0);
});
});

describe("#burnSingle", function () {
//
it("removes liquidity all in token0", async () => {
const bob = await ethers.getNamedSigner("bob");
const pool = await initializedConstantProductPool();
const token0 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token0());

await pool.transfer(pool.address, await "1000");
const burnData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "bool"],
[token0.address, bob.address, true]
);
await pool.burnSingle(burnData);

expect(await token0.balanceOf(bob.address)).to.be.above(0);
});

it("removes liquidity all in token1", async () => {
const bob = await ethers.getNamedSigner("bob");
const pool = await initializedConstantProductPool();
const token1 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token1());

await pool.transfer(pool.address, await "1000");
const burnData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "bool"],
[token1.address, bob.address, true]
);
await pool.burnSingle(burnData);

expect(await token1.balanceOf(bob.address)).to.be.above(0);
});

it("reverts if tokenOut is not equal to token0 or token1", async () => {
const deployer = await ethers.getNamedSigner("deployer");
const pool = await initializedConstantProductPool();
await pool.transfer(pool.address, await pool.balanceOf(deployer.address));
const burnData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "bool"],
["0x0000000000000000000000000000000000000003", deployer.address, true]
);

expect(pool.burnSingle(burnData)).to.be.revertedWith("InvalidOutputToken()");
});
});

describe("#swap", function () {
Expand All @@ -127,6 +181,54 @@ describe("Constant Product Pool", () => {
);
await expect(pool.swap(data)).to.be.revertedWith("PoolUninitialized()");
});

it("swaps token0 to token1", async () => {
const bob = await ethers.getNamedSigner("bob");
const bento = await ethers.getContract<BentoBoxV1>("BentoBoxV1");
const pool = await initializedConstantProductPool();
const token0 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token0());
const token1 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token1());

await token0.transfer(bento.address, "10000000");
await bento.deposit(token0.address, bento.address, pool.address, "10000000", 0);
const swapData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "bool"],
[token0.address, bob.address, true]
);
await pool.swap(swapData);

expect(await token1.balanceOf(bob.address)).to.be.above(0);
});

it("simple swap token1 to token0", async () => {
const bob = await ethers.getNamedSigner("bob");
const bento = await ethers.getContract<BentoBoxV1>("BentoBoxV1");
const pool = await initializedConstantProductPool();
const token0 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token0());
const token1 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token1());

await token1.transfer(bento.address, "10000000");
await bento.deposit(token1.address, bento.address, pool.address, "10000000", 0);
const swapData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "bool"],
[token1.address, bob.address, true]
);
await pool.swap(swapData);

expect(await token0.balanceOf(bob.address)).to.be.above(0);
});

it("reverts if tokenOut is not equal to token0 or token1", async () => {
const deployer = await ethers.getNamedSigner("deployer");
const pool = await initializedConstantProductPool();

const swapData = ethers.utils.defaultAbiCoder.encode(
["address", "address", "bool"],
["0x0000000000000000000000000000000000000003", deployer.address, true]
);

expect(pool.swap(swapData)).to.be.revertedWith("InvalidInputToken()");
});
});

describe("#flashSwap", function () {
Expand Down Expand Up @@ -290,7 +392,12 @@ describe("Constant Product Pool", () => {
});

describe("#poolIdentifier", function () {
//
it("returns correct identifier for Constant Product Pools", async () => {
const pool = await initializedConstantProductPool();
expect(await (await pool.poolIdentifier()).toString()).to.equal(
utils.formatBytes32String("Trident:ConstantProduct")
);
});
});

describe("#getAssets", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/stable-pool/StablePool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe("Stable Pool", () => {
// console.log(ethers.utils.formatUnits(await pool.getAmountOut(getAmountOutData), '18'));
});

it.skip("simple adds small quantity of liqudity", async () => {
it.skip("simple adds small quantity of liquidity", async () => {
const deployer = await ethers.getNamedSigner("deployer");
const pool = await initializedStablePool();
const token0 = await ethers.getContractAt<ERC20Mock>("ERC20Mock", await pool.token0());
Expand Down

0 comments on commit b045e09

Please sign in to comment.