mirror of
https://github.com/Instadapp/Gelato-automations.git
synced 2024-07-29 22:28:07 +00:00
Add Owner check in condition
This commit is contained in:
parent
24604e94fe
commit
9b197be90c
|
@ -9,7 +9,8 @@ import {
|
||||||
_getMakerVaultDebt,
|
_getMakerVaultDebt,
|
||||||
_getMakerVaultCollateralBalance,
|
_getMakerVaultCollateralBalance,
|
||||||
_vaultWillBeSafe,
|
_vaultWillBeSafe,
|
||||||
_newVaultWillBeSafe
|
_newVaultWillBeSafe,
|
||||||
|
_isVaultOwner
|
||||||
} from "../../../functions/dapps/FMaker.sol";
|
} from "../../../functions/dapps/FMaker.sol";
|
||||||
import {DAI} from "../../../constants/CInstaDapp.sol";
|
import {DAI} from "../../../constants/CInstaDapp.sol";
|
||||||
import {
|
import {
|
||||||
|
@ -25,6 +26,7 @@ contract ConditionDestVaultWillBeSafe is GelatoConditionsStandard {
|
||||||
using GelatoBytes for bytes;
|
using GelatoBytes for bytes;
|
||||||
|
|
||||||
function getConditionData(
|
function getConditionData(
|
||||||
|
address _dsa,
|
||||||
uint256 _fromVaultId,
|
uint256 _fromVaultId,
|
||||||
uint256 _destVaultId,
|
uint256 _destVaultId,
|
||||||
string calldata _destColType
|
string calldata _destColType
|
||||||
|
@ -32,6 +34,7 @@ contract ConditionDestVaultWillBeSafe is GelatoConditionsStandard {
|
||||||
return
|
return
|
||||||
abi.encodeWithSelector(
|
abi.encodeWithSelector(
|
||||||
this.destVaultWillBeSafe.selector,
|
this.destVaultWillBeSafe.selector,
|
||||||
|
_dsa,
|
||||||
_fromVaultId,
|
_fromVaultId,
|
||||||
_destVaultId,
|
_destVaultId,
|
||||||
_destColType
|
_destColType
|
||||||
|
@ -44,19 +47,24 @@ contract ConditionDestVaultWillBeSafe is GelatoConditionsStandard {
|
||||||
uint256
|
uint256
|
||||||
) public view virtual override returns (string memory) {
|
) public view virtual override returns (string memory) {
|
||||||
(
|
(
|
||||||
|
address _dsa,
|
||||||
uint256 _fromVaultId,
|
uint256 _fromVaultId,
|
||||||
uint256 _destVaultId,
|
uint256 _destVaultId,
|
||||||
string memory _destColType
|
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(
|
function destVaultWillBeSafe(
|
||||||
|
address _dsa,
|
||||||
uint256 _fromVaultId,
|
uint256 _fromVaultId,
|
||||||
uint256 _destVaultId,
|
uint256 _destVaultId,
|
||||||
string memory _destColType
|
string memory _destColType
|
||||||
) public view returns (string memory) {
|
) public view returns (string memory) {
|
||||||
|
uint256 _destVaultId =
|
||||||
|
_isVaultOwner(_destVaultId, _dsa) ? _destVaultId : 0;
|
||||||
uint256 wDaiToBorrow =
|
uint256 wDaiToBorrow =
|
||||||
_getRealisedDebt(_getMakerVaultDebt(_fromVaultId));
|
_getRealisedDebt(_getMakerVaultDebt(_fromVaultId));
|
||||||
uint256 wColToDeposit =
|
uint256 wColToDeposit =
|
||||||
|
|
|
@ -85,6 +85,7 @@ describe("Gas Measurements: Full Debt Bridge From Maker ETH-A to ETH-B", functio
|
||||||
conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
||||||
inst: contracts.conditionDestVaultWillBeSafe.address,
|
inst: contracts.conditionDestVaultWillBeSafe.address,
|
||||||
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
contracts.dsa.address,
|
||||||
vaultAId,
|
vaultAId,
|
||||||
vaultBId,
|
vaultBId,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
|
|
@ -105,6 +105,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B with vault creat
|
||||||
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
||||||
inst: contracts.conditionDestVaultWillBeSafe.address,
|
inst: contracts.conditionDestVaultWillBeSafe.address,
|
||||||
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
contracts.dsa.address,
|
||||||
vaultAId,
|
vaultAId,
|
||||||
0,
|
0,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
|
|
@ -107,6 +107,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () {
|
||||||
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
||||||
inst: contracts.conditionDestVaultWillBeSafe.address,
|
inst: contracts.conditionDestVaultWillBeSafe.address,
|
||||||
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
contracts.dsa.address,
|
||||||
vaultAId,
|
vaultAId,
|
||||||
vaultBId,
|
vaultBId,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
|
|
@ -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({
|
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
||||||
inst: contracts.conditionDestVaultWillBeSafe.address,
|
inst: contracts.conditionDestVaultWillBeSafe.address,
|
||||||
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
contracts.dsa.address,
|
||||||
vaultAId,
|
vaultAId,
|
||||||
vaultBId,
|
vaultBId,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
|
|
@ -105,6 +105,7 @@ describe("Security: _cast function by example of ETHA-ETHB with disabled Connect
|
||||||
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
const conditionDestVaultWillBeSafe = new GelatoCoreLib.Condition({
|
||||||
inst: contracts.conditionDestVaultWillBeSafe.address,
|
inst: contracts.conditionDestVaultWillBeSafe.address,
|
||||||
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
data: await contracts.conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
contracts.dsa.address,
|
||||||
vaultAId,
|
vaultAId,
|
||||||
vaultBId,
|
vaultBId,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
|
|
@ -182,6 +182,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
//#endregion Borrow
|
//#endregion Borrow
|
||||||
|
|
||||||
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
dsa.address,
|
||||||
cdpAId,
|
cdpAId,
|
||||||
0,
|
0,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
@ -239,6 +240,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
//#endregion Borrow
|
//#endregion Borrow
|
||||||
|
|
||||||
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
dsa.address,
|
||||||
cdpAId,
|
cdpAId,
|
||||||
0,
|
0,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
@ -296,6 +298,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
//#endregion Borrow
|
//#endregion Borrow
|
||||||
|
|
||||||
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
dsa.address,
|
||||||
cdpAId,
|
cdpAId,
|
||||||
cdpBId,
|
cdpBId,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
@ -351,6 +354,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
//#endregion Borrow
|
//#endregion Borrow
|
||||||
|
|
||||||
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
dsa.address,
|
||||||
cdpAId,
|
cdpAId,
|
||||||
cdpBId,
|
cdpBId,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
@ -428,6 +432,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
//#endregion Borrow
|
//#endregion Borrow
|
||||||
|
|
||||||
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
const conditionData = await conditionDestVaultWillBeSafe.getConditionData(
|
||||||
|
dsa.address,
|
||||||
cdpAId,
|
cdpAId,
|
||||||
cdpBId,
|
cdpBId,
|
||||||
"ETH-B"
|
"ETH-B"
|
||||||
|
@ -437,7 +442,161 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
).to.be.equal("OK");
|
).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
|
let amountOfColToDepo = amountToBorrow
|
||||||
.mul(ilkB[1]) // ilk[1] represent accumulated rates
|
.mul(ilkB[1]) // ilk[1] represent accumulated rates
|
||||||
.div(ethers.utils.parseUnits("1", 27));
|
.div(ethers.utils.parseUnits("1", 27));
|
||||||
|
@ -456,7 +615,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
).to.be.false;
|
).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
|
let amountOfColToDepo = amountToBorrow
|
||||||
.mul(ilkB[1]) // ilk[1] represent accumulated rates
|
.mul(ilkB[1]) // ilk[1] represent accumulated rates
|
||||||
.div(ethers.utils.parseUnits("1", 27));
|
.div(ethers.utils.parseUnits("1", 27));
|
||||||
|
@ -475,7 +634,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
).to.be.true;
|
).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", {
|
const openVault = await hre.run("abi-encode-withselector", {
|
||||||
abi: ConnectMaker.abi,
|
abi: ConnectMaker.abi,
|
||||||
functionname: "open",
|
functionname: "open",
|
||||||
|
@ -506,7 +665,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
).to.be.false;
|
).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
|
let amountOfColToDepo = amountToBorrow
|
||||||
.mul(ilkB[1]) // ilk[1] represent accumulated rates
|
.mul(ilkB[1]) // ilk[1] represent accumulated rates
|
||||||
.div(ethers.utils.parseUnits("1", 27));
|
.div(ethers.utils.parseUnits("1", 27));
|
||||||
|
@ -525,7 +684,7 @@ describe("ConditionDestVaultWillBeSafe Unit Test", function () {
|
||||||
).to.be.true;
|
).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
|
let amountOfColToDepo = amountToBorrow
|
||||||
.mul(ilkB[1]) // ilk[1] represent accumulated rates
|
.mul(ilkB[1]) // ilk[1] represent accumulated rates
|
||||||
.div(ethers.utils.parseUnits("1", 27));
|
.div(ethers.utils.parseUnits("1", 27));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user