2020-10-19 23:16:23 +00:00
|
|
|
const {expect} = require("chai");
|
2020-10-19 16:17:27 +00:00
|
|
|
const hre = require("hardhat");
|
|
|
|
const {ethers} = hre;
|
2020-10-19 23:16:23 +00:00
|
|
|
|
|
|
|
const WAD = ethers.utils.parseUnits("1", 18);
|
|
|
|
|
|
|
|
//#region DSMath function
|
|
|
|
|
|
|
|
let wdiv = (x, y) => {
|
|
|
|
return x.mul(WAD).add(y.div(2)).div(y);
|
|
|
|
};
|
|
|
|
|
|
|
|
let wmul = (x, y) => {
|
|
|
|
return x.mul(y).add(WAD.div(2)).div(WAD);
|
|
|
|
};
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
2020-10-30 17:35:11 +00:00
|
|
|
describe("Debt Partial Refinance Math Unit Test", function () {
|
2020-10-19 23:16:23 +00:00
|
|
|
this.timeout(0);
|
2020-10-19 16:17:27 +00:00
|
|
|
if (hre.network.name !== "hardhat") {
|
|
|
|
console.error("Test Suite is meant to be run on hardhat only");
|
2020-10-19 23:16:23 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2020-11-02 17:50:01 +00:00
|
|
|
let fGelatoDebtBridgeMock;
|
2020-10-19 23:16:23 +00:00
|
|
|
before(async function () {
|
2020-11-02 17:50:01 +00:00
|
|
|
const FGelatoDebtBridgeMock = await ethers.getContractFactory(
|
|
|
|
"FGelatoDebtBridgeMock"
|
2020-10-19 23:16:23 +00:00
|
|
|
);
|
2020-11-02 17:50:01 +00:00
|
|
|
fGelatoDebtBridgeMock = await FGelatoDebtBridgeMock
|
|
|
|
.deploy
|
|
|
|
//ethers.constants.AddressZero
|
|
|
|
();
|
|
|
|
fGelatoDebtBridgeMock.deployed();
|
2020-10-19 23:16:23 +00:00
|
|
|
});
|
|
|
|
|
2020-10-19 16:17:27 +00:00
|
|
|
it("#1: wCalcCollateralToWithdraw should return the amount of collateral to withdraw on protocol 1 and to put on protocol 2", async function () {
|
2020-10-19 23:16:23 +00:00
|
|
|
// 3 times more collateral than borrowed amount in protocol 1
|
2020-10-19 16:17:27 +00:00
|
|
|
let minColRatioOnMaker = ethers.utils.parseUnits("3", 18);
|
2020-10-19 23:16:23 +00:00
|
|
|
// 1.5 times more collateral than borrowed amount in protocol 2
|
2020-10-19 16:17:27 +00:00
|
|
|
let minColRatioOnPositionB = ethers.utils.parseUnits("15", 17);
|
2020-10-19 23:16:23 +00:00
|
|
|
// The amount of collateral locked
|
|
|
|
let col = ethers.utils.parseUnits("1", 18);
|
|
|
|
// The amount of borrowed token
|
|
|
|
let borrowedToken = ethers.utils.parseUnits("100", 18);
|
|
|
|
// how much one collateral is worth on borrowed token
|
|
|
|
let collateralPrice = ethers.utils.parseUnits("250", 18);
|
|
|
|
// the amount of collateral in borrowed token
|
|
|
|
let collateral = col
|
|
|
|
.mul(collateralPrice)
|
|
|
|
.div(ethers.utils.parseUnits("1", 18)); // div to have everything in wad standard
|
|
|
|
|
|
|
|
// Check this document https://drive.google.com/file/d/1OV3ZbJPd2Yr-3l0rst6tK3ycfhhg6Nfh/view?usp=sharing for more details one the used formula
|
|
|
|
|
|
|
|
//#region CALCULATION REPLICATION
|
|
|
|
|
|
|
|
let expectedColToWithdraw = wmul(
|
2020-10-19 16:17:27 +00:00
|
|
|
wmul(minColRatioOnMaker, minColRatioOnPositionB),
|
2020-10-19 23:16:23 +00:00
|
|
|
borrowedToken
|
|
|
|
); // doc ref : c_r x comp_r x d_2
|
|
|
|
expectedColToWithdraw = expectedColToWithdraw.sub(
|
2020-10-19 16:17:27 +00:00
|
|
|
wmul(minColRatioOnMaker, collateral)
|
2020-10-19 23:16:23 +00:00
|
|
|
); // doc ref : c_r x comp_r x d_2 - c_r x e_2
|
|
|
|
expectedColToWithdraw = wdiv(
|
|
|
|
expectedColToWithdraw,
|
2020-10-19 16:17:27 +00:00
|
|
|
minColRatioOnPositionB.sub(minColRatioOnMaker)
|
2020-10-19 23:16:23 +00:00
|
|
|
); // doc ref : (c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r)
|
|
|
|
expectedColToWithdraw = collateral.sub(expectedColToWithdraw); // doc ref : e_2 - ((c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r))
|
|
|
|
|
|
|
|
// Extra step to convert back to col type
|
|
|
|
expectedColToWithdraw = wdiv(expectedColToWithdraw, collateralPrice);
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
expect(
|
2020-11-02 17:50:01 +00:00
|
|
|
await fGelatoDebtBridgeMock.wCalcCollateralToWithdraw(
|
2020-10-19 16:17:27 +00:00
|
|
|
minColRatioOnMaker,
|
|
|
|
minColRatioOnPositionB,
|
|
|
|
collateralPrice,
|
2020-10-19 23:16:23 +00:00
|
|
|
collateral,
|
2020-10-19 16:17:27 +00:00
|
|
|
borrowedToken
|
2020-10-19 23:16:23 +00:00
|
|
|
)
|
|
|
|
).to.be.equal(expectedColToWithdraw);
|
|
|
|
});
|
|
|
|
|
2020-10-19 16:17:27 +00:00
|
|
|
it("#2: _wCalcDebtToRepay should return the amount of borrowed token to pay back on protocol 1", async function () {
|
2020-10-19 23:16:23 +00:00
|
|
|
// 3 times more collateral than borrowed amount in protocol 1
|
2020-10-19 16:17:27 +00:00
|
|
|
let minColRatioOnMaker = ethers.utils.parseUnits("3", 18);
|
2020-10-19 23:16:23 +00:00
|
|
|
// 1.5 times more collateral than borrowed amount in protocol 2
|
2020-10-19 16:17:27 +00:00
|
|
|
let minColRatioOnPositionB = ethers.utils.parseUnits("15", 17);
|
2020-10-19 23:16:23 +00:00
|
|
|
// The amount of collateral locked
|
|
|
|
let col = ethers.utils.parseUnits("1", 18);
|
|
|
|
// The amount of borrowed token
|
|
|
|
let borrowedToken = ethers.utils.parseUnits("100", 18);
|
|
|
|
// how much one collateral is worth on borrowed token
|
|
|
|
let collateralPrice = ethers.utils.parseUnits("250", 18);
|
|
|
|
// the amount of collateral in borrowed token
|
|
|
|
let collateral = col
|
|
|
|
.mul(collateralPrice)
|
|
|
|
.div(ethers.utils.parseUnits("1", 18)); // div to have everything in wad standard
|
|
|
|
|
|
|
|
// Check this document https://drive.google.com/file/d/1OV3ZbJPd2Yr-3l0rst6tK3ycfhhg6Nfh/view?usp=sharing for more details one the used formula
|
|
|
|
|
|
|
|
//#region CALCULATION REPLICATION
|
|
|
|
|
|
|
|
let expectedBorToPayBack = wmul(
|
2020-10-19 16:17:27 +00:00
|
|
|
wmul(minColRatioOnMaker, minColRatioOnPositionB),
|
2020-10-19 23:16:23 +00:00
|
|
|
borrowedToken
|
|
|
|
); // doc ref : c_r x comp_r x d_2
|
|
|
|
expectedBorToPayBack = expectedBorToPayBack.sub(
|
2020-10-19 16:17:27 +00:00
|
|
|
wmul(minColRatioOnMaker, collateral)
|
2020-10-19 23:16:23 +00:00
|
|
|
); // doc ref : c_r x comp_r x d_2 - c_r x e_2
|
|
|
|
expectedBorToPayBack = wdiv(
|
|
|
|
expectedBorToPayBack,
|
2020-10-19 16:17:27 +00:00
|
|
|
minColRatioOnPositionB.sub(minColRatioOnMaker)
|
2020-10-19 23:16:23 +00:00
|
|
|
); // doc ref : (c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r)
|
|
|
|
expectedBorToPayBack = wmul(
|
2020-10-19 16:17:27 +00:00
|
|
|
wdiv(ethers.utils.parseUnits("1", 18), minColRatioOnMaker),
|
2020-10-19 23:16:23 +00:00
|
|
|
expectedBorToPayBack
|
|
|
|
); // doc ref : (1/c_r)((c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r))
|
|
|
|
expectedBorToPayBack = borrowedToken.sub(expectedBorToPayBack); // doc ref : d_2 - (1/c_r)((c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r))
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
expect(
|
2020-11-02 17:50:01 +00:00
|
|
|
await fGelatoDebtBridgeMock.wCalcDebtToRepay(
|
2020-10-19 16:17:27 +00:00
|
|
|
minColRatioOnMaker,
|
|
|
|
minColRatioOnPositionB,
|
2020-10-19 23:16:23 +00:00
|
|
|
collateral,
|
|
|
|
borrowedToken
|
|
|
|
)
|
|
|
|
).to.be.equal(expectedBorToPayBack);
|
|
|
|
});
|
|
|
|
});
|