Skip to content

Commit

Permalink
test(refundRatio): add test and helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
zgorizzo69 committed Nov 18, 2021
1 parent d258698 commit f8a5744
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 13 deletions.
8 changes: 7 additions & 1 deletion contracts/Foundry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import "./interfaces/IHub.sol";
import "./interfaces/IFoundry.sol";
import "./libs/WeightedAverage.sol";
import "./libs/Details.sol";
import "hardhat/console.sol";

contract Foundry is IFoundry, Ownable, Initializable {
using SafeERC20 for IERC20;
Expand Down Expand Up @@ -124,7 +125,7 @@ contract Foundry is IFoundry, Ownable, Initializable {
) {
meToken_ = meTokenRegistry.finishResubscribe(_meToken);
}

console.log("## ## ## burn meToken_.hubId:%s", meToken_.hubId);
// Calculate how many tokens tokens are returned
uint256 tokensReturned = calculateBurnReturn(_meToken, _meTokensBurned);

Expand All @@ -150,6 +151,11 @@ contract Foundry is IFoundry, Ownable, Initializable {
} else {
if (hub_.targetRefundRatio > 0) {
// Hub is updating
console.log(
"## ## ## burn weigthed refundRatio:%s targetRefundRatio:%s",
hub_.refundRatio,
hub_.targetRefundRatio
);
actualTokensReturned =
(tokensReturned *
WeightedAverage.calculate(
Expand Down
125 changes: 125 additions & 0 deletions test/contracts/libs/WeightedAverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { BigNumber } from "@ethersproject/bignumber";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { ethers } from "hardhat";
import { WeightedAverage } from "../../../artifacts/types/WeightedAverage";
import { deploy, weightedAverageSimulation } from "../../utils/helpers";

describe("WeightedAverage.sol", () => {
let wa: WeightedAverage;
let account0: SignerWithAddress;
let account1: SignerWithAddress;
before(async () => {
[account0, account1] = await ethers.getSigners();
wa = await deploy<WeightedAverage>("WeightedAverage");

await wa.deployed();
});

describe("calculate()", () => {
it("Returns amount if block.timestamp < startTime", async () => {
const block = await ethers.provider.getBlock("latest");

const cur = await wa.calculate(42, 245646, block.timestamp + 1, 1);
expect(cur).to.equal(42);
});
it("Returns targetAmount if block.timestamp > endTime", async () => {
const block = await ethers.provider.getBlock("latest");
const cur = await wa.calculate(
42,
245646,
block.timestamp - 2,
block.timestamp - 1
);
expect(cur).to.equal(245646);
});
it("works with different amount and target amount", async () => {
const block = await ethers.provider.getBlock("latest");
const amount = "100";
const targetAmount = "1000";
const startTime = block.timestamp - 50;
const endTime = block.timestamp + 50;
const cur = await wa.calculate(
ethers.utils.parseEther(amount),
ethers.utils.parseEther(targetAmount),
startTime,
endTime
);
const calcRes = weightedAverageSimulation(
Number(amount),
Number(targetAmount),
startTime,
endTime,
block.timestamp
);
expect(calcRes).to.equal(550);
expect(Number(ethers.utils.formatEther(cur))).to.equal(calcRes);
});
it("works at the begining of migration", async () => {
const block = await ethers.provider.getBlock("latest");
const amount = "0";
const targetAmount = "10";
const startTime = block.timestamp - 1;
const endTime = block.timestamp + 9;
const cur = await wa.calculate(
ethers.utils.parseEther(amount),
ethers.utils.parseEther(targetAmount),
startTime,
endTime
);
const calcRes = weightedAverageSimulation(
Number(amount),
Number(targetAmount),
startTime,
endTime,
block.timestamp
);
expect(calcRes).to.equal(1);
expect(Number(ethers.utils.formatEther(cur))).to.equal(calcRes);
});
it("works in the middle of migration", async () => {
const block = await ethers.provider.getBlock("latest");
const amount = "0";
const targetAmount = "10";
const startTime = block.timestamp - 5;
const endTime = block.timestamp + 5;
const cur = await wa.calculate(
ethers.utils.parseEther(amount),
ethers.utils.parseEther(targetAmount),
startTime,
endTime
);
const calcRes = weightedAverageSimulation(
Number(amount),
Number(targetAmount),
startTime,
endTime,
block.timestamp
);
expect(calcRes).to.equal(5);
expect(Number(ethers.utils.formatEther(cur))).to.equal(calcRes);
});
it("works at the end of migration", async () => {
const block = await ethers.provider.getBlock("latest");
const amount = "0";
const targetAmount = "10";
const startTime = block.timestamp - 9;
const endTime = block.timestamp + 1;
const cur = await wa.calculate(
ethers.utils.parseEther(amount),
ethers.utils.parseEther(targetAmount),
startTime,
endTime
);
const calcRes = weightedAverageSimulation(
Number(amount),
Number(targetAmount),
startTime,
endTime,
block.timestamp
);
expect(calcRes).to.equal(9);
expect(Number(ethers.utils.formatEther(cur))).to.equal(calcRes);
});
});
});
Loading

0 comments on commit f8a5744

Please sign in to comment.