Gelato-automations/test/unit_tests/6_DebtRefinanceMath.test.js

136 lines
5.0 KiB
JavaScript
Raw Normal View History

2020-10-19 23:16:23 +00:00
const {expect} = require("chai");
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);
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);
}
let fGelatoDebtBridgeMock;
2020-10-19 23:16:23 +00:00
before(async function () {
const FGelatoDebtBridgeMock = await ethers.getContractFactory(
"FGelatoDebtBridgeMock"
2020-10-19 23:16:23 +00:00
);
fGelatoDebtBridgeMock = await FGelatoDebtBridgeMock
.deploy
//ethers.constants.AddressZero
();
fGelatoDebtBridgeMock.deployed();
2020-10-19 23:16:23 +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
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
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(
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(
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,
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(
await fGelatoDebtBridgeMock.wCalcCollateralToWithdraw(
minColRatioOnMaker,
minColRatioOnPositionB,
collateralPrice,
2020-10-19 23:16:23 +00:00
collateral,
borrowedToken
2020-10-19 23:16:23 +00:00
)
).to.be.equal(expectedColToWithdraw);
});
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
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
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(
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(
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,
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(
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(
await fGelatoDebtBridgeMock.wCalcDebtToRepay(
minColRatioOnMaker,
minColRatioOnPositionB,
2020-10-19 23:16:23 +00:00
collateral,
borrowedToken
)
).to.be.equal(expectedBorToPayBack);
});
});