Skip to content

Commit

Permalink
test(Foundry): add burn fee tests
Browse files Browse the repository at this point in the history
- failing tests
  • Loading branch information
parv3213 committed Dec 30, 2021
1 parent 49c8c11 commit a66f08a
Showing 1 changed file with 94 additions and 19 deletions.
113 changes: 94 additions & 19 deletions test/integration/MeTokenRegistry/ResubscribeCurveDetails.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO add tests of burn fees
import { ethers, getNamedAccounts } from "hardhat";
import { hubSetup } from "../../utils/hubSetup";
import {
Expand All @@ -8,6 +9,7 @@ import {
toETHNumber,
weightedAverageSimulation,
calculateTokenReturnedFromZero,
fromETHNumber,
} from "../../utils/helpers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { BigNumber, ContractTransaction, Signer } from "ethers";
Expand All @@ -23,6 +25,8 @@ import { MeToken } from "../../../artifacts/types/MeToken";
import { UniswapSingleTransferMigration } from "../../../artifacts/types/UniswapSingleTransferMigration";
import { SingleAssetVault } from "../../../artifacts/types/SingleAssetVault";
import { mineBlock, setAutomine } from "../../utils/hardhatNode";
import { Fees } from "../../../artifacts/types/Fees";
import Decimal from "decimal.js";

const setup = async () => {
describe("MeToken Resubscribe - Same curve, new Curve Details", () => {
Expand All @@ -43,6 +47,7 @@ const setup = async () => {
let account1: SignerWithAddress;
let encodedCurveDetails1: string;
let encodedCurveDetails2: string;
let fees: Fees;

const hubId1 = 1;
const hubId2 = 2;
Expand All @@ -57,11 +62,13 @@ const setup = async () => {
const reserveWeight1 = MAX_WEIGHT / 10;
const reserveWeight2 = MAX_WEIGHT / 2;
const refundRatio = 5000;
const fees = 3000;
const fee = 3000;
const tokenDepositedInETH = 100;
const tokenDeposited = ethers.utils.parseEther(
tokenDepositedInETH.toString()
);
const burnOwnerFee = 1e8;
const burnBuyerFee = 1e9;

before(async () => {
let token: ERC20;
Expand All @@ -84,7 +91,7 @@ const setup = async () => {
const earliestSwapTime = block.timestamp + 600 * 60; // 10h in future
const encodedMigrationArgs = ethers.utils.defaultAbiCoder.encode(
["uint256", "uint24"],
[earliestSwapTime, fees]
[earliestSwapTime, fee]
);

// Register first and second hub
Expand All @@ -99,6 +106,7 @@ const setup = async () => {
account0,
account1,
meTokenRegistry,
fee: fees,
} = await hubSetup(
encodedCurveDetails1,
encodedVaultArgs,
Expand All @@ -124,6 +132,8 @@ const setup = async () => {
await meTokenRegistry.setWarmup(warmup);
await meTokenRegistry.setDuration(duration);
await meTokenRegistry.setCooldown(coolDown);
await fees.setBurnOwnerFee(burnOwnerFee);
await fees.setBurnBuyerFee(burnBuyerFee);

// Deploy uniswap migration and approve it to the registry
migration = await deploy<UniswapSingleTransferMigration>(
Expand Down Expand Up @@ -211,7 +221,7 @@ const setup = async () => {

expect(toETHNumber(ownerMeTokenAfter)).to.be.approximately(
calculatedReturn,
0.000000000000001
1e-15
);
expect(meTokenTotalSupplyAfter).to.be.equal(ownerMeTokenAfter);
expect(vaultDAIAfter.sub(vaultDAIBefore)).to.equal(tokenDeposited);
Expand Down Expand Up @@ -246,18 +256,29 @@ const setup = async () => {
const vaultDAIAfter = await dai.balanceOf(singleAssetVault.address);
const meTokenTotalSupplyAfter = await meToken.totalSupply();

const burnFee = toETHNumber(
(await fees.burnBuyerFee())
.mul(fromETHNumber(assetsReturned))
.div(PRECISION)
);

expect(
toETHNumber(buyerDAIAfter.sub(buyerDAIBefore))
).to.be.approximately(assetsReturned, 0.000000000000001);
).to.be.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
expect(
toETHNumber(vaultDAIBefore.sub(vaultDAIAfter))
).to.be.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
expect(buyerMeTokenAfter).to.equal(0);
expect(toETHNumber(meTokenTotalSupplyAfter)).to.be.approximately(
toETHNumber(meTokenTotalSupply.div(2)),
1e-18
);
expect(toETHNumber(vaultDAIBefore.sub(vaultDAIAfter))).to.approximately(
assetsReturned,
0.000000000000001
);
});
it("burn() [owner]: assets received based on initial Curve details", async () => {
const ownerMeToken = await meToken.balanceOf(account0.address);
Expand Down Expand Up @@ -288,12 +309,21 @@ const setup = async () => {
const vaultDAIAfter = await dai.balanceOf(singleAssetVault.address);
const meTokenTotalSupplyAfter = await meToken.totalSupply();

const burnFee = toETHNumber(
(await fees.burnOwnerFee())
.mul(fromETHNumber(assetsReturned))
.div(PRECISION)
);

expect(vaultDAIBefore.sub(vaultDAIAfter)).to.equal(
ownerDAIAfter.sub(ownerDAIBefore)
);
expect(
toETHNumber(ownerDAIAfter.sub(ownerDAIBefore))
).to.be.approximately(assetsReturned, 0.000000000000001);
).to.be.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
expect(ownerMeTokenAfter).to.equal(0);
expect(toETHNumber(meTokenTotalSupplyAfter)).to.equal(0);
});
Expand Down Expand Up @@ -354,7 +384,7 @@ const setup = async () => {

expect(toETHNumber(ownerMeTokenAfter)).to.be.approximately(
calcWAvgRe,
0.000000000000001
1e-15
);
expect(meTokenTotalSupplyAfter).to.be.equal(ownerMeTokenAfter);
expect(vaultDAIAfter.sub(vaultDAIBefore)).to.equal(0); // new asset goes to migration
Expand Down Expand Up @@ -412,17 +442,29 @@ const setup = async () => {
const migrationWETHAfter = await weth.balanceOf(migration.address);
const meTokenTotalSupplyAfter = await meToken.totalSupply();

const burnFee = toETHNumber(
(await fees.burnBuyerFee())
.mul(fromETHNumber(assetsReturned))
.div(PRECISION)
);

expect(
toETHNumber(buyerWETHAfter.sub(buyerWETHBefore))
).to.be.approximately(assetsReturned, 1e-15);
).to.be.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
expect(buyerMeTokenAfter).to.equal(0);
expect(toETHNumber(meTokenTotalSupplyAfter)).to.be.approximately(
toETHNumber(meTokenTotalSupply.div(2)),
1e-18
);
expect(
toETHNumber(migrationWETHBefore.sub(migrationWETHAfter))
).to.approximately(assetsReturned, 1e-15);
).to.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
});
it("burn() [owner]: assets received based on weighted average Curve details", async () => {
const ownerMeToken = await meToken.balanceOf(account0.address);
Expand Down Expand Up @@ -461,23 +503,32 @@ const setup = async () => {
(toETHNumber(ownerMeToken) / toETHNumber(meTokenTotalSupply)) *
toETHNumber(meTokenDetails.balanceLocked);

await mineBlock(block.timestamp + 1);
await setAutomine(true);
await foundry
.connect(account0)
.burn(meToken.address, ownerMeToken, account0.address);
await mineBlock(block.timestamp + 1);
await setAutomine(true);

const ownerMeTokenAfter = await meToken.balanceOf(account0.address);
const ownerWETHAfter = await weth.balanceOf(account0.address);
const migrationWETHAfter = await weth.balanceOf(migration.address);
const meTokenTotalSupplyAfter = await meToken.totalSupply();

const burnFee = toETHNumber(
(await fees.burnOwnerFee())
.mul(fromETHNumber(assetsReturned))
.div(PRECISION)
);

expect(migrationWETHBefore.sub(migrationWETHAfter)).to.equal(
ownerWETHAfter.sub(ownerWETHBefore)
);
expect(
toETHNumber(ownerWETHAfter.sub(ownerWETHBefore))
).to.be.approximately(assetsReturned, 0.000000000000001);
).to.be.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
expect(ownerMeTokenAfter).to.equal(0);
expect(toETHNumber(meTokenTotalSupplyAfter)).to.equal(0);
});
Expand Down Expand Up @@ -516,7 +567,7 @@ const setup = async () => {

expect(toETHNumber(ownerMeTokenAfter)).to.be.approximately(
calculatedTargetReturn,
0.000000000000001
1e-15
);
expect(meTokenTotalSupplyAfter).to.be.equal(ownerMeTokenAfter);
expect(vaultWETHAfter.sub(vaultWETHBefore)).to.equal(tokenDeposited);
Expand Down Expand Up @@ -554,17 +605,29 @@ const setup = async () => {
const vaultWETHAfter = await weth.balanceOf(singleAssetVault.address);
const meTokenTotalSupplyAfter = await meToken.totalSupply();

const burnFee = toETHNumber(
(await fees.burnBuyerFee())
.mul(fromETHNumber(assetsReturned))
.div(PRECISION)
);

expect(
toETHNumber(buyerWETHAfter.sub(buyerWETHBefore))
).to.be.approximately(assetsReturned, 1e-15);
).to.be.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
expect(buyerMeTokenAfter).to.equal(0);
expect(toETHNumber(meTokenTotalSupplyAfter)).to.be.approximately(
toETHNumber(meTokenTotalSupply.div(2)),
1e-18
);
expect(
toETHNumber(vaultWETHBefore.sub(vaultWETHAfter))
).to.approximately(assetsReturned, 1e-15);
).to.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
});
it("burn() [owner]: assets received based on target Curve details", async () => {
const ownerMeToken = await meToken.balanceOf(account0.address);
Expand Down Expand Up @@ -596,12 +659,24 @@ const setup = async () => {
const vaultWETHAfter = await dai.balanceOf(singleAssetVault.address);
const meTokenTotalSupplyAfter = await meToken.totalSupply();

const burnFee = toETHNumber(
(await fees.burnOwnerFee())
.mul(fromETHNumber(assetsReturned))
.div(PRECISION)
);
console.log(toETHNumber(vaultWETHBefore.sub(vaultWETHAfter)));
console.log(toETHNumber(ownerWETHAfter.sub(ownerWETHBefore)));
console.log(burnFee);

expect(vaultWETHBefore.sub(vaultWETHAfter)).to.equal(
ownerWETHAfter.sub(ownerWETHBefore)
);
expect(
toETHNumber(ownerWETHAfter.sub(ownerWETHBefore))
).to.be.approximately(assetsReturned, 0.000000000000001);
).to.be.approximately(
new Decimal(assetsReturned).sub(new Decimal(burnFee)).toNumber(),
1e-15
);
expect(ownerMeTokenAfter).to.equal(0);
expect(toETHNumber(meTokenTotalSupplyAfter)).to.equal(0);
});
Expand Down

0 comments on commit a66f08a

Please sign in to comment.