Gelato-automations/test/unit_tests/4_ConditionMakerVaultUnsafe.test.js

192 lines
5.4 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
// #region Contracts ABI
const ConnectMaker = require("../../pre-compiles/ConnectMaker.json");
const GetCdps = require("../../pre-compiles/GetCdps.json");
const DssCdpManager = require("../../pre-compiles/DssCdpManager.json");
const InstaList = require("../../pre-compiles/InstaList.json");
const InstaAccount = require("../../pre-compiles/InstaAccount.json");
const InstaIndex = require("../../pre-compiles/InstaIndex.json");
const IERC20 = require("../../pre-compiles/IERC20.json");
2020-10-19 23:16:23 +00:00
const ORACLE_MAKER_ETH_USD = "ETH/USD-Maker-v1";
const ORACLE_MAKER_ETH_USD_ADDR = "0x729D19f657BD0614b4985Cf1D82531c67569197B";
const PRICE_ORACLE_MAKER_PAYLOAD = "0x57de26a4"; // IMakerOracle.read()
2020-10-19 23:16:23 +00:00
// #endregion
describe("ConditionMakerVaultUnsafe 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 userWallet;
let userAddress;
let getCdps;
let dssCdpManager;
let instaList;
let instaIndex;
let daiToken;
let conditionMakerVaultUnsafe;
let priceOracleResolver;
2020-10-19 23:16:23 +00:00
let cdpId;
let dsa;
before(async function () {
// Get Test Wallet for local testnet
[userWallet] = await ethers.getSigners();
userAddress = await userWallet.getAddress();
// Hardhat default accounts prefilled with 100 ETH
2020-10-19 23:16:23 +00:00
expect(await userWallet.getBalance()).to.be.gt(
ethers.utils.parseEther("10")
);
instaIndex = await ethers.getContractAt(
InstaIndex.abi,
hre.network.config.InstaIndex
2020-10-19 23:16:23 +00:00
);
instaList = await ethers.getContractAt(
InstaList.abi,
hre.network.config.InstaList
2020-10-19 23:16:23 +00:00
);
getCdps = await ethers.getContractAt(
GetCdps.abi,
hre.network.config.GetCdps
2020-10-19 23:16:23 +00:00
);
dssCdpManager = await ethers.getContractAt(
DssCdpManager.abi,
hre.network.config.DssCdpManager
2020-10-19 23:16:23 +00:00
);
daiToken = await ethers.getContractAt(IERC20.abi, hre.network.config.DAI);
2020-10-19 23:16:23 +00:00
// ========== Test Setup ============
const PriceOracleResolver = await ethers.getContractFactory(
"PriceOracleResolver"
2020-10-19 23:16:23 +00:00
);
priceOracleResolver = await PriceOracleResolver.deploy();
await priceOracleResolver.deployed();
2020-10-19 23:16:23 +00:00
const ConditionMakerVaultUnsafe = await ethers.getContractFactory(
"ConditionMakerVaultUnsafe"
2020-10-19 23:16:23 +00:00
);
conditionMakerVaultUnsafe = await ConditionMakerVaultUnsafe.deploy();
await conditionMakerVaultUnsafe.deployed();
2020-10-19 23:16:23 +00:00
// Create DeFi Smart Account
const dsaAccountCount = await instaList.accounts();
await expect(instaIndex.build(userAddress, 1, userAddress)).to.emit(
instaIndex,
"LogAccountCreated"
);
const dsaID = dsaAccountCount.add(1);
await expect(await instaList.accounts()).to.be.equal(dsaID);
// Instantiate the DSA
dsa = await ethers.getContractAt(
InstaAccount.abi,
await instaList.accountAddr(dsaID)
);
// Create/Deposit/Borrow a Vault
const openVault = await hre.run("abi-encode-withselector", {
2020-10-19 23:16:23 +00:00
abi: ConnectMaker.abi,
functionname: "open",
inputs: ["ETH-A"],
});
await dsa.cast([hre.network.config.ConnectMaker], [openVault], userAddress);
2020-10-19 23:16:23 +00:00
let cdps = await getCdps.getCdpsAsc(dssCdpManager.address, dsa.address);
cdpId = String(cdps.ids[0]);
expect(cdps.ids[0].isZero()).to.be.false;
await dsa.cast(
[hre.network.config.ConnectMaker],
2020-10-19 23:16:23 +00:00
[
await hre.run("abi-encode-withselector", {
2020-10-19 23:16:23 +00:00
abi: ConnectMaker.abi,
functionname: "deposit",
inputs: [cdpId, ethers.utils.parseEther("10"), 0, 0],
}),
],
userAddress,
{
value: ethers.utils.parseEther("10"),
}
);
await dsa.cast(
[hre.network.config.ConnectMaker],
2020-10-19 23:16:23 +00:00
[
await hre.run("abi-encode-withselector", {
2020-10-19 23:16:23 +00:00
abi: ConnectMaker.abi,
functionname: "borrow",
inputs: [cdpId, ethers.utils.parseUnits("1000", 18), 0, 0],
}),
],
userAddress
);
expect(await daiToken.balanceOf(dsa.address)).to.be.equal(
ethers.utils.parseEther("1000")
);
// Add ETH/USD Maker Medianizer in the PriceOracleResolver
2020-10-19 23:16:23 +00:00
await priceOracleResolver.addOracle(
ORACLE_MAKER_ETH_USD,
ORACLE_MAKER_ETH_USD_ADDR,
PRICE_ORACLE_MAKER_PAYLOAD
2020-10-19 23:16:23 +00:00
);
});
it("#1: ok should return MakerVaultNotUnsafe when the ETH/USD price is above the defined limit", async function () {
const conditionData = await conditionMakerVaultUnsafe.getConditionData(
2020-10-19 23:16:23 +00:00
cdpId,
ORACLE_MAKER_ETH_USD_ADDR,
PRICE_ORACLE_MAKER_PAYLOAD,
2020-10-19 23:16:23 +00:00
ethers.utils.parseUnits("30", 17)
);
expect(await conditionMakerVaultUnsafe.ok(0, conditionData, 0)).to.be.equal(
"MakerVaultNotUnsafe"
2020-10-19 23:16:23 +00:00
);
});
it("#2: ok should return OK when the ETH/USD price is lower than the defined limit", async function () {
const conditionData = await conditionMakerVaultUnsafe.getConditionData(
2020-10-19 23:16:23 +00:00
cdpId,
priceOracleResolver.address,
await hre.run("abi-encode-withselector", {
abi: (await hre.artifacts.readArtifact("PriceOracleResolver")).abi,
functionname: "getMockPrice",
inputs: [userAddress],
}),
2020-10-19 23:16:23 +00:00
ethers.utils.parseUnits("30", 17)
);
//#region Mock Part
priceOracleResolver.setMockPrice(ethers.utils.parseUnits("299", 18));
2020-10-19 23:16:23 +00:00
//#endregion
expect(await conditionMakerVaultUnsafe.ok(0, conditionData, 0)).to.be.equal(
"OK"
2020-10-19 23:16:23 +00:00
);
});
});