Add Owner check in condition

This commit is contained in:
Shivva 2020-11-27 07:01:53 +01:00 committed by Luis Schliesske
parent 24604e94fe
commit 9b197be90c
7 changed files with 180 additions and 8 deletions

View File

@ -9,7 +9,8 @@ import {
_getMakerVaultDebt,
_getMakerVaultCollateralBalance,
_vaultWillBeSafe,
_newVaultWillBeSafe
_newVaultWillBeSafe,
_isVaultOwner
} from "../../../functions/dapps/FMaker.sol";
import {DAI} from "../../../constants/CInstaDapp.sol";
import {
@ -25,6 +26,7 @@ contract ConditionDestVaultWillBeSafe is GelatoConditionsStandard {
using GelatoBytes for bytes;
function getConditionData(
address _dsa,
uint256 _fromVaultId,
uint256 _destVaultId,
string calldata _destColType
@ -32,6 +34,7 @@ contract ConditionDestVaultWillBeSafe is GelatoConditionsStandard {
return
abi.encodeWithSelector(
this.destVaultWillBeSafe.selector,
_dsa,
_fromVaultId,
_destVaultId,
_destColType
@ -44,19 +47,24 @@ contract ConditionDestVaultWillBeSafe is GelatoConditionsStandard {
uint256
) public view virtual override returns (string memory) {
(
address _dsa,
uint256 _fromVaultId,
uint256 _destVaultId,
string memory _destColType
) = abi.decode(_conditionData[4:], (uint256, uint256, string));
) = abi.decode(_conditionData[4:], (address, uint256, uint256, string));
return destVaultWillBeSafe(_fromVaultId, _destVaultId, _destColType);
return
destVaultWillBeSafe(_dsa, _fromVaultId, _destVaultId, _destColType);
}
function destVaultWillBeSafe(
address _dsa,
uint256 _fromVaultId,
uint256 _destVaultId,
string memory _destColType
) public view returns (string memory) {
uint256 _destVaultId =
_isVaultOwner(_destVaultId, _dsa) ? _destVaultId : 0;
uint256 wDaiToBorrow =
_getRealisedDebt(_getMakerVaultDebt(_fromVaultId));
uint256 wColToDeposit =

View File

@ -85,6 +85,7 @@ describe("Gas Measurements: Full Debt Bridge From Maker ETH-A to ETH-B", functio
conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
inst: contracts.conditionDestVaultWillBeSafe.address,
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
contracts.dsa.address,
vaultAId,
vaultBId,
"ETH-B"

View File

@ -105,6 +105,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B with vault creat
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
inst: contracts.conditionDestVaultWillBeSafe.address,
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
contracts.dsa.address,
vaultAId,
0,
"ETH-B"

View File

@ -107,6 +107,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () {
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
inst: contracts.conditionDestVaultWillBeSafe.address,
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
contracts.dsa.address,
vaultAId,
vaultBId,
"ETH-B"

View File

@ -107,6 +107,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B with Vault B cre
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
inst: contracts.conditionDestVaultWillBeSafe.address,
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
contracts.dsa.address,
vaultAId,
vaultBId,
"ETH-B"

View File

@ -105,6 +105,7 @@ describe("Security: _cast function by example of ETHA-ETHB with disabled Connect
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
inst: contracts.conditionDestVaultWillBeSafe.address,
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
contracts.dsa.address,
vaultAId,
vaultBId,
"ETH-B"

View File

@ -182,6 +182,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
//#endregion Borrow
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
dsa.address,
cdpAId,
0,
"ETH-B"
@ -239,6 +240,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
//#endregion Borrow
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
dsa.address,
cdpAId,
0,
"ETH-B"
@ -296,6 +298,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
//#endregion Borrow
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
dsa.address,
cdpAId,
cdpBId,
"ETH-B"
@ -351,6 +354,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
//#endregion Borrow
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
dsa.address,
cdpAId,
cdpBId,
"ETH-B"
@ -428,6 +432,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
//#endregion Borrow
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
dsa.address,
cdpAId,
cdpBId,
"ETH-B"
@ -437,7 +442,161 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
).to.be.equal("OK");
});
it("#6: New Vault Case : destVaultWillBeSafeExplicit should return false when col is lower than borrow amount / spot", async function () {
it("#6: ok should return DestVaultWillNotBeSafe when the gas fees exceed a user define amount with closing the vaultB", async function () {
// Steps :
// 1 - Deposit in Vault ETH-A
// 4 - Close Vault ETH-B
// 5 - Test if vault ETH-B will be safe after debt bridge action
const amountToDeposit = amountToBorrow
.mul(ethers.utils.parseUnits("1", 27))
.div(ilkA[2]) // ilk[2] represent the liquidation ratio of ilk
.add(ethers.utils.parseUnits("1", 17)); // to be just above the liquidation ratio.
//#region Deposit on Vault ETH-B
await dsa.cast(
[hre.network.config.ConnectMaker],
[
await hre.run("abi-encode-withselector", {
abi: ConnectMaker.abi,
functionname: "close",
inputs: [cdpBId],
}),
],
userAddress,
{
value: amountToDeposit,
}
);
//#endregion Deposit on Vault ETH-B
//#region Deposit on Vault ETH-A
await dsa.cast(
[hre.network.config.ConnectMaker],
[
await hre.run("abi-encode-withselector", {
abi: ConnectMaker.abi,
functionname: "deposit",
inputs: [cdpAId, amountToDeposit, 0, 0],
}),
],
userAddress,
{
value: amountToDeposit,
}
);
//#endregion Deposit
//#region Borrow
await dsa.cast(
[hre.network.config.ConnectMaker],
[
await hre.run("abi-encode-withselector", {
abi: ConnectMaker.abi,
functionname: "borrow",
inputs: [cdpAId, amountToBorrow, 0, 0],
}),
],
userAddress
);
expect(await DAI.balanceOf(dsa.address)).to.be.equal(amountToBorrow);
//#endregion Borrow
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
dsa.address,
cdpAId,
cdpBId,
"ETH-B"
);
expect(
await conditionDestVaultWillBeSafe.ok(0, conditionData, 0)
).to.be.equal("DestVaultWillNotBeSafe");
});
it("#7: ok should return Ok when the gas fees didn't exceed a user define amount with closing the vaultB", async function () {
// Steps :
// 1 - Deposit in Vault ETH-A
// 4 - Close Vault ETH-B
// 5 - Test if vault ETH-B will be safe after debt bridge action
const amountToDeposit = amountToBorrow
.mul(ethers.utils.parseUnits("1", 27))
.div(ilkA[2]) // ilk[2] represent the liquidation ratio of ilk
.add(ethers.utils.parseUnits("4", 17)); // to be just above the liquidation ratio.
//#region Deposit on Vault ETH-B
await dsa.cast(
[hre.network.config.ConnectMaker],
[
await hre.run("abi-encode-withselector", {
abi: ConnectMaker.abi,
functionname: "close",
inputs: [cdpBId],
}),
],
userAddress,
{
value: amountToDeposit,
}
);
//#endregion Deposit on Vault ETH-B
//#region Deposit on Vault ETH-A
await dsa.cast(
[hre.network.config.ConnectMaker],
[
await hre.run("abi-encode-withselector", {
abi: ConnectMaker.abi,
functionname: "deposit",
inputs: [cdpAId, amountToDeposit, 0, 0],
}),
],
userAddress,
{
value: amountToDeposit,
}
);
//#endregion Deposit
//#region Borrow
await dsa.cast(
[hre.network.config.ConnectMaker],
[
await hre.run("abi-encode-withselector", {
abi: ConnectMaker.abi,
functionname: "borrow",
inputs: [cdpAId, amountToBorrow, 0, 0],
}),
],
userAddress
);
expect(await DAI.balanceOf(dsa.address)).to.be.equal(amountToBorrow);
//#endregion Borrow
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
dsa.address,
cdpAId,
cdpBId,
"ETH-B"
);
expect(
await conditionDestVaultWillBeSafe.ok(0, conditionData, 0)
).to.be.equal("OK");
});
it("#8: New Vault Case : destVaultWillBeSafeExplicit should return false when col is lower than borrow amount / spot", async function () {
let amountOfColToDepo = amountToBorrow
.mul(ilkB[1]) // ilk[1] represent accumulated rates
.div(ethers.utils.parseUnits("1", 27));
@ -456,7 +615,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
).to.be.false;
});
it("#7: New Vault Case : destVaultWillBeSafeExplicit should return true when col is greater than borrow amount / spot", async function () {
it("#9: New Vault Case : destVaultWillBeSafeExplicit should return true when col is greater than borrow amount / spot", async function () {
let amountOfColToDepo = amountToBorrow
.mul(ilkB[1]) // ilk[1] represent accumulated rates
.div(ethers.utils.parseUnits("1", 27));
@ -475,7 +634,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
).to.be.true;
});
it("#8: Old Vault Case : destVaultWillBeSafeExplicit should return false when col is lower than borrow amount / spot", async function () {
it("#10: Old Vault Case : destVaultWillBeSafeExplicit should return false when col is lower than borrow amount / spot", async function () {
const openVault = await hre.run("abi-encode-withselector", {
abi: ConnectMaker.abi,
functionname: "open",
@ -506,7 +665,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
).to.be.false;
});
it("#9: Old Vault Case : destVaultWillBeSafeExplicit should return true when col is lower than borrow amount / spot", async function () {
it("#11: Old Vault Case : destVaultWillBeSafeExplicit should return true when col is lower than borrow amount / spot", async function () {
let amountOfColToDepo = amountToBorrow
.mul(ilkB[1]) // ilk[1] represent accumulated rates
.div(ethers.utils.parseUnits("1", 27));
@ -525,7 +684,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
).to.be.true;
});
it("#10: Old Vault Case with existing deposit : destVaultWillBeSafeExplicit should return true when col is lower than borrow amount / spot due to initial deposit on Vault B", async function () {
it("#12: Old Vault Case with existing deposit : destVaultWillBeSafeExplicit should return true when col is lower than borrow amount / spot due to initial deposit on Vault B", async function () {
let amountOfColToDepo = amountToBorrow
.mul(ilkB[1]) // ilk[1] represent accumulated rates
.div(ethers.utils.parseUnits("1", 27));