-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(refundRatio): add test and helpers
- Loading branch information
1 parent
d258698
commit f8a5744
Showing
4 changed files
with
374 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.