diff --git a/.solhintignore b/.solhintignore index 240f776..f55a5b3 100644 --- a/.solhintignore +++ b/.solhintignore @@ -2,4 +2,5 @@ node_modules/ contracts/constants contracts/dependencies contracts/functions +contracts/__mocks__/__dependencies__ contracts/vendor \ No newline at end of file diff --git a/contracts/dependencies/GelatoTestDependencies.sol b/contracts/__mocks__/__dependencies__/DGelatoCore.sol similarity index 84% rename from contracts/dependencies/GelatoTestDependencies.sol rename to contracts/__mocks__/__dependencies__/DGelatoCore.sol index aec4339..fce45e1 100644 --- a/contracts/dependencies/GelatoTestDependencies.sol +++ b/contracts/__mocks__/__dependencies__/DGelatoCore.sol @@ -4,6 +4,6 @@ pragma solidity 0.6.10; import "@gelatonetwork/core/contracts/gelato_core/GelatoCore.sol"; // solhint-disable-next-line no-empty-blocks -contract GelatoTestDependencies { +contract DGelatoCore { } diff --git a/contracts/dependencies/InstaDapp/connectors/compound.sol b/contracts/__mocks__/__dependencies__/InstaDapp/connectors/compound.sol similarity index 98% rename from contracts/dependencies/InstaDapp/connectors/compound.sol rename to contracts/__mocks__/__dependencies__/InstaDapp/connectors/compound.sol index c346e80..0ad0700 100644 --- a/contracts/dependencies/InstaDapp/connectors/compound.sol +++ b/contracts/__mocks__/__dependencies__/InstaDapp/connectors/compound.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.6.0; +pragma solidity >=0.6.0; interface CTokenInterface { function mint(uint256 mintAmount) external returns (uint256); @@ -188,7 +188,7 @@ contract CompoundHelpers is Helpers { } /** - * @dev Return InstaDApp Mapping Addresses + * @dev Return InstaDapp Mapping Addresses */ function getMappingAddr() internal pure returns (address) { return 0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88; // InstaMapping Address @@ -263,7 +263,7 @@ contract BasicResolver is CompoundHelpers { enterMarket(cToken); if (token == getAddressETH()) { _amt = _amt == uint256(-1) ? address(this).balance : _amt; - CETHInterface(cToken).mint.value(_amt)(); + CETHInterface(cToken).mint{value: _amt}(); } else { TokenInterface tokenContract = TokenInterface(token); _amt = _amt == uint256(-1) @@ -398,7 +398,7 @@ contract BasicResolver is CompoundHelpers { if (token == getAddressETH()) { require(address(this).balance >= _amt, "not-enough-eth"); - CETHInterface(cToken).repayBorrow.value(_amt)(); + CETHInterface(cToken).repayBorrow{value: _amt}(); } else { TokenInterface tokenContract = TokenInterface(token); require( @@ -498,7 +498,7 @@ contract ExtraResolver is BasicResolver { if (token == getAddressETH()) { _amt = _amt == uint256(-1) ? address(this).balance : _amt; - CETHInterface(cToken).mint.value(_amt)(); + CETHInterface(cToken).mint{value: _amt}(); } else { TokenInterface tokenContract = TokenInterface(token); _amt = _amt == uint256(-1) @@ -601,7 +601,7 @@ contract ExtraResolver is BasicResolver { : _amt; if (tokenToPay == getAddressETH()) { require(address(this).balance >= _amt, "not-enought-eth"); - CETHInterface(cTokenPay).liquidateBorrow.value(_amt)( + CETHInterface(cTokenPay).liquidateBorrow{value: _amt}( borrower, cTokenColl ); diff --git a/contracts/dependencies/InstaDapp/connectors/makerdao.sol b/contracts/__mocks__/__dependencies__/InstaDapp/connectors/makerdao.sol similarity index 99% rename from contracts/dependencies/InstaDapp/connectors/makerdao.sol rename to contracts/__mocks__/__dependencies__/InstaDapp/connectors/makerdao.sol index e9c5076..373c691 100644 --- a/contracts/dependencies/InstaDapp/connectors/makerdao.sol +++ b/contracts/__mocks__/__dependencies__/InstaDapp/connectors/makerdao.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.6.0; +pragma solidity >=0.6.0; interface TokenInterface { function approve(address, uint256) external; @@ -589,7 +589,7 @@ contract BasicResolver is EventHelper { if (isEth(address(tokenContract))) { _amt = _amt == uint256(-1) ? address(this).balance : _amt; - tokenContract.deposit.value(_amt)(); + tokenContract.deposit{value: _amt}(); } else { _amt = _amt == uint256(-1) ? tokenContract.balanceOf(address(this)) @@ -883,7 +883,7 @@ contract BasicExtraResolver is BasicResolver { _amtDeposit = _amtDeposit == uint256(-1) ? address(this).balance : _amtDeposit; - makerData.tokenContract.deposit.value(_amtDeposit)(); + makerData.tokenContract.deposit{value: _amtDeposit}(); } else { _amtDeposit = _amtDeposit == uint256(-1) ? makerData.tokenContract.balanceOf(address(this)) diff --git a/contracts/dependencies/InstaDapp/dsa-contracts/account.sol b/contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/account.sol similarity index 99% rename from contracts/dependencies/InstaDapp/dsa-contracts/account.sol rename to contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/account.sol index 12d7014..9f0b264 100644 --- a/contracts/dependencies/InstaDapp/dsa-contracts/account.sol +++ b/contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/account.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.6.0; +pragma solidity >=0.6.0; pragma experimental ABIEncoderV2; /** diff --git a/contracts/dependencies/InstaDapp/dsa-contracts/connectors.sol b/contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/connectors.sol similarity index 99% rename from contracts/dependencies/InstaDapp/dsa-contracts/connectors.sol rename to contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/connectors.sol index b5f7ba6..0ccc39f 100644 --- a/contracts/dependencies/InstaDapp/dsa-contracts/connectors.sol +++ b/contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/connectors.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.6.0; +pragma solidity >=0.6.0; /** * @title InstaConnectors diff --git a/contracts/dependencies/InstaDapp/dsa-contracts/index.sol b/contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/index.sol similarity index 98% rename from contracts/dependencies/InstaDapp/dsa-contracts/index.sol rename to contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/index.sol index 24113f3..d57e660 100644 --- a/contracts/dependencies/InstaDapp/dsa-contracts/index.sol +++ b/contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/index.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.6.0; +pragma solidity >=0.6.0; pragma experimental ABIEncoderV2; /** @@ -197,7 +197,7 @@ contract InstaIndex is CloneFactory { ) external payable returns (address _account) { _account = build(_owner, accountVersion, _origin); if (_targets.length > 0) - AccountInterface(_account).cast.value(msg.value)( + AccountInterface(_account).cast{value: msg.value}( _targets, _datas, _origin diff --git a/contracts/dependencies/InstaDapp/dsa-contracts/list.sol b/contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/list.sol similarity index 99% rename from contracts/dependencies/InstaDapp/dsa-contracts/list.sol rename to contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/list.sol index 3109fa9..4101b91 100644 --- a/contracts/dependencies/InstaDapp/dsa-contracts/list.sol +++ b/contracts/__mocks__/__dependencies__/InstaDapp/dsa-contracts/list.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.6.0; +pragma solidity >=0.6.0; /** * @title InstaList diff --git a/contracts/contracts/mocks/MockCDAI.sol b/contracts/__mocks__/dapps/MockCDAI.sol similarity index 100% rename from contracts/contracts/mocks/MockCDAI.sol rename to contracts/__mocks__/dapps/MockCDAI.sol diff --git a/contracts/contracts/mocks/MockDSR.sol b/contracts/__mocks__/dapps/MockDSR.sol similarity index 100% rename from contracts/contracts/mocks/MockDSR.sol rename to contracts/__mocks__/dapps/MockDSR.sol diff --git a/contracts/contracts/mocks/FGelatoDebtBridgeMock.sol b/contracts/__mocks__/functions/FGelatoDebtBridgeMock.sol similarity index 100% rename from contracts/contracts/mocks/FGelatoDebtBridgeMock.sol rename to contracts/__mocks__/functions/FGelatoDebtBridgeMock.sol diff --git a/contracts/contracts/mocks/MockGelatoExecutor.sol b/contracts/__mocks__/gelato/MockGelatoExecutor.sol similarity index 64% rename from contracts/contracts/mocks/MockGelatoExecutor.sol rename to contracts/__mocks__/gelato/MockGelatoExecutor.sol index 6af5efa..4e51bc7 100644 --- a/contracts/contracts/mocks/MockGelatoExecutor.sol +++ b/contracts/__mocks__/gelato/MockGelatoExecutor.sol @@ -2,7 +2,7 @@ pragma solidity 0.7.4; pragma experimental ABIEncoderV2; -// import "hardhat/console.sol"; // Uncomment this line for using gasLeft Method +// import {console} from "hardhat/console.sol"; // Uncomment this line for using gasLeft Method import { TaskReceipt } from "@gelatonetwork/core/contracts/gelato_core/interfaces/IGelatoCore.sol"; @@ -22,12 +22,10 @@ contract MockGelatoExecutor { gelatoCore = _gelatoCore; } - // solhint-disable-next-line - function exec(TaskReceipt memory _TR) external { - // uint256 gasLeft = gasleft(); // Uncomment this line for using gasleft Method - IGelatoCore(gelatoCore).exec(_TR); - // solhint-disable-next-line - // console.log("Gas Cost for Task Execution %s", gasLeft - gasleft());// Uncomment this line for using gasleft Method + function exec(TaskReceipt memory _taskReceipt) external { + // uint256 gasLeft = gasleft(); + IGelatoCore(gelatoCore).exec(_taskReceipt); + // console.log("Gas Cost for Task Execution %s", gasLeft - gasleft()); } function stakeExecutor() external payable { @@ -35,11 +33,15 @@ contract MockGelatoExecutor { } function canExec( - // solhint-disable-next-line - TaskReceipt calldata _TR, + TaskReceipt calldata _taskReceipt, uint256 _gasLimit, uint256 _execTxGasPrice ) external view returns (string memory) { - return IGelatoCore(gelatoCore).canExec(_TR, _gasLimit, _execTxGasPrice); + return + IGelatoCore(gelatoCore).canExec( + _taskReceipt, + _gasLimit, + _execTxGasPrice + ); } } diff --git a/test/5_Full-Debt-Bridge-ETHA-ETHB-Gas-Cost-Mock.test.js b/test/gas/0_Full-Debt-Bridge-ETHA-ETHB-Gas-Cost-Mock.test.js similarity index 97% rename from test/5_Full-Debt-Bridge-ETHA-ETHB-Gas-Cost-Mock.test.js rename to test/gas/0_Full-Debt-Bridge-ETHA-ETHB-Gas-Cost-Mock.test.js index 5a05974..8f80ab3 100644 --- a/test/5_Full-Debt-Bridge-ETHA-ETHB-Gas-Cost-Mock.test.js +++ b/test/gas/0_Full-Debt-Bridge-ETHA-ETHB-Gas-Cost-Mock.test.js @@ -4,8 +4,8 @@ const { deployments, ethers } = hre; const GelatoCoreLib = require("@gelatonetwork/core"); const setupFullRefinanceMakerToMakerWithVaultBCreationMock = require("./helpers/setupFullRefinanceMakerToMakerWithVaultBCreationMock"); -const getRoute = require("./helpers/services/getRoute"); -const getGasCostForFullRefinance = require("./helpers/services/getGasCostForFullRefinance"); +const getInstaPoolV2Route = require("../helpers/services/InstaDapp/getInstaPoolV2Route"); +const getGasCostForFullRefinance = require("../integration/debt_bridge/from_maker/full/helpers/services/getGasCostForFullRefinance"); // This test showcases how to submit a task refinancing a Users debt position from // Maker to Compound using Gelato @@ -201,7 +201,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B For Gas Measurem vaultAId ); - const route = await getRoute( + const route = await getInstaPoolV2Route( contracts.DAI.address, debtOnMakerBefore, contracts.instaPoolResolver diff --git a/test/helpers/services/getMockGelato.js b/test/gas/helpers/services/getMockGelatoExecutor.js similarity index 62% rename from test/helpers/services/getMockGelato.js rename to test/gas/helpers/services/getMockGelatoExecutor.js index 72df7e1..80c6d44 100644 --- a/test/helpers/services/getMockGelato.js +++ b/test/gas/helpers/services/getMockGelatoExecutor.js @@ -1,8 +1,6 @@ const hre = require("hardhat"); const { ethers } = hre; -async function getMockGelato() { +module.exports = async function () { return await ethers.getContract("MockGelatoExecutor"); -} - -module.exports = getMockGelato; +}; diff --git a/test/helpers/services/stakeExecutorMock.js b/test/gas/helpers/services/stakeExecutorMock.js similarity index 92% rename from test/helpers/services/stakeExecutorMock.js rename to test/gas/helpers/services/stakeExecutorMock.js index 8b374c1..a98756f 100644 --- a/test/helpers/services/stakeExecutorMock.js +++ b/test/gas/helpers/services/stakeExecutorMock.js @@ -1,6 +1,6 @@ const { expect } = require("chai"); -async function stakeExecutor( +module.exports = async function ( gelatoExecutorWallet, gelatoExecutorMock, gelatoCore @@ -23,6 +23,4 @@ async function stakeExecutor( .true; //#endregion -} - -module.exports = stakeExecutor; +}; diff --git a/test/helpers/setupFullRefinanceMakerToMakerWithVaultBCreationMock.js b/test/gas/helpers/setupFullRefinanceMakerToMakerWithVaultBCreationMock.js similarity index 59% rename from test/helpers/setupFullRefinanceMakerToMakerWithVaultBCreationMock.js rename to test/gas/helpers/setupFullRefinanceMakerToMakerWithVaultBCreationMock.js index be16028..f71e831 100644 --- a/test/helpers/setupFullRefinanceMakerToMakerWithVaultBCreationMock.js +++ b/test/gas/helpers/setupFullRefinanceMakerToMakerWithVaultBCreationMock.js @@ -1,26 +1,26 @@ -const getWallets = require("./services/getWallets"); -const getContracts = require("./services/getContracts"); -const getConstants = require("./services/getConstants"); -const getMockGelato = require("./services/getMockGelato"); +const getWallets = require("../../helpers/services/getWallets"); +const getContracts = require("../../helpers/services/getContracts"); +const getDebtBridgeFromMakerConstants = require("../../integration/debt_bridge/from_maker/services/getDebtBridgeFromMakerConstants"); +const getMockGelatoExecutor = require("../helpers/services/getMockGelatoExecutor"); const stakeExecutorMock = require("./services/stakeExecutorMock"); -const provideFunds = require("./services/provideFunds"); -const providerAssignsExecutor = require("./services/providerAssignsExecutor"); -const addProviderModuleDSA = require("./services/addProviderModuleDSA"); -const createDSA = require("./services/createDSA"); -const addETHBGemJoinMapping = require("./services/addETHBGemJoinMapping"); -const initializeMakerCdp = require("./services/initializeMakerCdp"); -const createVaultForETHB = require("./services/createVaultForETHB"); -const providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB = require("./services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB"); -const getABI = require("./services/getABI"); +const provideFunds = require("../../helpers/services/gelato/provideFunds"); +const providerAssignsExecutor = require("../../helpers/services/gelato/providerAssignsExecutor"); +const addProviderModuleDSA = require("../../helpers/services/gelato/addProviderModuleDSA"); +const createDSA = require("../../helpers/services/InstaDapp/createDSA"); +const addETHBGemJoinMapping = require("../../helpers/services/maker/addETHBGemJoinMapping"); +const initializeMakerCdp = require("../../helpers/services/maker/initializeMakerCdp"); +const createVaultForETHB = require("../../helpers/services/maker/createVaultForETHB"); +const providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB = require("../../integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB"); +const getABI = require("../../helpers/services/getABI"); async function setupFullRefinanceMakerToMakerWithVaultBCreation() { const wallets = await getWallets(); const contracts = await getContracts(); - const constants = await getConstants(); + const constants = await getDebtBridgeFromMakerConstants(); //#region Mock - contracts.mockGelatoExecutor = await getMockGelato(); + contracts.mockGelatoExecutor = await getMockGelatoExecutor(); //#endregion Mock diff --git a/test/helpers/services/createDSA.js b/test/helpers/services/InstaDapp/createDSA.js similarity index 81% rename from test/helpers/services/createDSA.js rename to test/helpers/services/InstaDapp/createDSA.js index 7f945e2..92dfda1 100644 --- a/test/helpers/services/createDSA.js +++ b/test/helpers/services/InstaDapp/createDSA.js @@ -2,9 +2,9 @@ const { expect } = require("chai"); const hre = require("hardhat"); const { ethers } = hre; -const InstaAccount = require("../../../pre-compiles/InstaAccount.json"); +const InstaAccount = require("../../../../pre-compiles/InstaAccount.json"); -async function createDSA(userAddress, instaIndex, instaList) { +module.exports = async function (userAddress, instaIndex, instaList) { //#region User create a DeFi Smart Account // User create a Instadapp DeFi Smart Account @@ -30,6 +30,4 @@ async function createDSA(userAddress, instaIndex, instaList) { return dsa; //#endregion -} - -module.exports = createDSA; +}; diff --git a/test/helpers/services/getRoute.js b/test/helpers/services/InstaDapp/getInstaPoolV2Route.js similarity index 71% rename from test/helpers/services/getRoute.js rename to test/helpers/services/InstaDapp/getInstaPoolV2Route.js index 69fa728..f0615ce 100644 --- a/test/helpers/services/getRoute.js +++ b/test/helpers/services/InstaDapp/getInstaPoolV2Route.js @@ -1,10 +1,8 @@ -async function getRoute(token, tokenDebtToMove, instaPoolResolver) { +module.exports = async function (token, tokenDebtToMove, instaPoolResolver) { const rData = await instaPoolResolver.getTokenLimit(token); if (rData.dydx > tokenDebtToMove) return 0; if (rData.maker > tokenDebtToMove) return 1; if (rData.compound > tokenDebtToMove) return 2; if (rData.aave > tokenDebtToMove) return 3; -} - -module.exports = getRoute; +}; diff --git a/test/helpers/services/addProviderModuleDSA.js b/test/helpers/services/gelato/addProviderModuleDSA.js similarity index 90% rename from test/helpers/services/addProviderModuleDSA.js rename to test/helpers/services/gelato/addProviderModuleDSA.js index cb6a230..29230b5 100644 --- a/test/helpers/services/addProviderModuleDSA.js +++ b/test/helpers/services/gelato/addProviderModuleDSA.js @@ -1,6 +1,6 @@ const { expect } = require("chai"); -async function addProviderModuleDSA( +module.exports = async function ( gelatoProviderWallet, gelatoCore, dsaProviderModuleAddr @@ -26,6 +26,4 @@ async function addProviderModuleDSA( ).to.be.true; //#endregion -} - -module.exports = addProviderModuleDSA; +}; diff --git a/test/helpers/services/provideFunds.js b/test/helpers/services/gelato/provideFunds.js similarity index 93% rename from test/helpers/services/provideFunds.js rename to test/helpers/services/gelato/provideFunds.js index 6a905a5..31d4406 100644 --- a/test/helpers/services/provideFunds.js +++ b/test/helpers/services/gelato/provideFunds.js @@ -1,6 +1,6 @@ const { expect } = require("chai"); -async function provideFunds( +module.exports = async function ( gelatoProviderWallet, gelatoCore, gasLimit, @@ -33,6 +33,4 @@ async function provideFunds( ); //#endregion -} - -module.exports = provideFunds; +}; diff --git a/test/helpers/services/providerAssignsExecutor.js b/test/helpers/services/gelato/providerAssignsExecutor.js similarity index 88% rename from test/helpers/services/providerAssignsExecutor.js rename to test/helpers/services/gelato/providerAssignsExecutor.js index 5730273..b46c97d 100644 --- a/test/helpers/services/providerAssignsExecutor.js +++ b/test/helpers/services/gelato/providerAssignsExecutor.js @@ -1,6 +1,6 @@ const { expect } = require("chai"); -async function providerAssignsExecutor( +module.exports = async function ( gelatoProviderWallet, gelatoExecutorAddress, gelatoCore @@ -23,6 +23,4 @@ async function providerAssignsExecutor( ).to.be.equal(gelatoExecutorAddress); //#endregion -} - -module.exports = providerAssignsExecutor; +}; diff --git a/test/helpers/services/stakeExecutor.js b/test/helpers/services/gelato/stakeExecutor.js similarity index 87% rename from test/helpers/services/stakeExecutor.js rename to test/helpers/services/gelato/stakeExecutor.js index ad95de5..f5d4eee 100644 --- a/test/helpers/services/stakeExecutor.js +++ b/test/helpers/services/gelato/stakeExecutor.js @@ -1,6 +1,6 @@ const { expect } = require("chai"); -async function stakeExecutor(gelatoExecutorWallet, gelatoCore) { +module.exports = async function (gelatoExecutorWallet, gelatoCore) { //#region Executor Stake on Gelato // For task execution provider will ask a executor to watch the @@ -20,6 +20,4 @@ async function stakeExecutor(gelatoExecutorWallet, gelatoCore) { .true; //#endregion -} - -module.exports = stakeExecutor; +}; diff --git a/test/helpers/services/getABI.js b/test/helpers/services/getABI.js index 47c3f7c..a265e6e 100644 --- a/test/helpers/services/getABI.js +++ b/test/helpers/services/getABI.js @@ -2,11 +2,9 @@ const ConnectGelatoABI = require("../../../pre-compiles/ConnectGelato.json") .abi; const ConnectAuthABI = require("../../../pre-compiles/ConnectAuth.json").abi; -function getABI() { +module.exports = function () { return { ConnectGelatoABI: ConnectGelatoABI, ConnectAuthABI: ConnectAuthABI, }; -} - -module.exports = getABI; +}; diff --git a/test/helpers/services/getContracts.js b/test/helpers/services/getContracts.js index 197f1f4..263e23d 100644 --- a/test/helpers/services/getContracts.js +++ b/test/helpers/services/getContracts.js @@ -19,7 +19,7 @@ const CompoundResolver = require("../../../pre-compiles/InstaCompoundResolver.js const DsaProviderModuleABI = require("../../../pre-compiles/ProviderModuleDsa_ABI.json"); const InstaPoolResolver = require("../../../artifacts/contracts/interfaces/InstaDapp/resolvers/IInstaPoolResolver.sol/IInstaPoolResolver.json"); -async function getContracts() { +module.exports = async function () { const instaMaster = await ethers.provider.getSigner( hre.network.config.InstaMaster ); @@ -134,6 +134,4 @@ async function getContracts() { dsaProviderModule, conditionDebtBridgeIsAffordable, }; -} - -module.exports = getContracts; +}; diff --git a/test/helpers/services/getWallets.js b/test/helpers/services/getWallets.js index 9e0baa4..f6933e3 100644 --- a/test/helpers/services/getWallets.js +++ b/test/helpers/services/getWallets.js @@ -2,7 +2,7 @@ const { expect } = require("chai"); const hre = require("hardhat"); const { ethers } = hre; -async function getWallets() { +module.exports = async function () { let userWallet; let userAddress; let gelatoProviderWallet; @@ -30,6 +30,4 @@ async function getWallets() { gelatoExecutorWallet: gelatoExecutorWallet, gelatoExecutorAddress: gelatoExecutorAddress, }; -} - -module.exports = getWallets; +}; diff --git a/test/helpers/services/addETHBGemJoinMapping.js b/test/helpers/services/maker/addETHBGemJoinMapping.js similarity index 85% rename from test/helpers/services/addETHBGemJoinMapping.js rename to test/helpers/services/maker/addETHBGemJoinMapping.js index 00161fd..ddfc970 100644 --- a/test/helpers/services/addETHBGemJoinMapping.js +++ b/test/helpers/services/maker/addETHBGemJoinMapping.js @@ -2,7 +2,7 @@ const { expect } = require("chai"); const hre = require("hardhat"); const { ethers } = hre; -async function addETHBGemJoinMapping(userWallet, instaMapping, instaMaster) { +module.exports = async function (userWallet, instaMapping, instaMaster) { await userWallet.sendTransaction({ to: hre.network.config.InstaMaster, value: ethers.utils.parseEther("0.1"), @@ -22,6 +22,4 @@ async function addETHBGemJoinMapping(userWallet, instaMapping, instaMaster) { method: "hardhat_stopImpersonatingAccount", params: [await instaMaster.getAddress()], }); -} - -module.exports = addETHBGemJoinMapping; +}; diff --git a/test/helpers/services/createVaultForETHB.js b/test/helpers/services/maker/createVaultForETHB.js similarity index 82% rename from test/helpers/services/createVaultForETHB.js rename to test/helpers/services/maker/createVaultForETHB.js index 4739f55..745f76f 100644 --- a/test/helpers/services/createVaultForETHB.js +++ b/test/helpers/services/maker/createVaultForETHB.js @@ -1,9 +1,9 @@ const { expect } = require("chai"); const hre = require("hardhat"); -const ConnectMaker = require("../../../pre-compiles/ConnectMaker.json"); +const ConnectMaker = require("../../../../pre-compiles/ConnectMaker.json"); -async function createVaultForETHB( +module.exports = async function ( userAddress, DAI, dsa, @@ -30,6 +30,4 @@ async function createVaultForETHB( //#endregion return vaultId; -} - -module.exports = createVaultForETHB; +}; diff --git a/test/helpers/services/initializeMakerCdp.js b/test/helpers/services/maker/initializeMakerCdp.js similarity index 90% rename from test/helpers/services/initializeMakerCdp.js rename to test/helpers/services/maker/initializeMakerCdp.js index 931b390..009daf3 100644 --- a/test/helpers/services/initializeMakerCdp.js +++ b/test/helpers/services/maker/initializeMakerCdp.js @@ -1,9 +1,9 @@ const { expect } = require("chai"); const hre = require("hardhat"); -const ConnectMaker = require("../../../pre-compiles/ConnectMaker.json"); +const ConnectMaker = require("../../../../pre-compiles/ConnectMaker.json"); -async function initializeMakerCdp( +module.exports = async function ( userAddress, DAI, dsa, @@ -61,6 +61,4 @@ async function initializeMakerCdp( //#endregion return vaultId; -} - -module.exports = initializeMakerCdp; +}; diff --git a/test/0_setup-DSA-Gelato.test.js b/test/integration/0_setup-DSA-Gelato.test.js similarity index 93% rename from test/0_setup-DSA-Gelato.test.js rename to test/integration/0_setup-DSA-Gelato.test.js index 05d451e..6bf2172 100644 --- a/test/0_setup-DSA-Gelato.test.js +++ b/test/integration/0_setup-DSA-Gelato.test.js @@ -10,13 +10,13 @@ const GelatoCoreLib = require("@gelatonetwork/core"); const ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // Contracts -const InstaIndex = require("../pre-compiles/InstaIndex.json"); -const InstaList = require("../pre-compiles/InstaList.json"); -const InstaConnectors = require("../pre-compiles/InstaConnectors.json"); -const InstaAccount = require("../pre-compiles/InstaAccount.json"); -const ConnectAuth = require("../pre-compiles/ConnectAuth.json"); -const ConnectBasic = require("../pre-compiles/ConnectBasic.json"); -const ProviderModuleDsa_ABI = require("../pre-compiles/ProviderModuleDsa_ABI.json"); +const InstaIndex = require("../../pre-compiles/InstaIndex.json"); +const InstaList = require("../../pre-compiles/InstaList.json"); +const InstaConnectors = require("../../pre-compiles/InstaConnectors.json"); +const InstaAccount = require("../../pre-compiles/InstaAccount.json"); +const ConnectAuth = require("../../pre-compiles/ConnectAuth.json"); +const ConnectBasic = require("../../pre-compiles/ConnectBasic.json"); +const ProviderModuleDsa_ABI = require("../../pre-compiles/ProviderModuleDsa_ABI.json"); describe("DSA setup with Gelato Tests", function () { this.timeout(50000); diff --git a/test/2_Full-Debt-Bridge-Maker-Compound.test.js b/test/integration/debt_bridge/from_maker/full/0_To-Compound.test.js similarity index 98% rename from test/2_Full-Debt-Bridge-Maker-Compound.test.js rename to test/integration/debt_bridge/from_maker/full/0_To-Compound.test.js index 930aaf2..4d0b2bf 100644 --- a/test/2_Full-Debt-Bridge-Maker-Compound.test.js +++ b/test/integration/debt_bridge/from_maker/full/0_To-Compound.test.js @@ -5,7 +5,7 @@ const { deployments, ethers } = hre; const GelatoCoreLib = require("@gelatonetwork/core"); const setupFullRefinanceMakerToCompound = require("./helpers/setupFullRefinanceMakerToCompound"); -const getRoute = require("./helpers/services/getRoute"); +const getInstaPoolV2Route = require("../../../../helpers/services/InstaDapp/getInstaPoolV2Route"); const getGasCostForFullRefinance = require("./helpers/services/getGasCostForFullRefinance"); // This test showcases how to submit a task refinancing a Users debt position from @@ -199,7 +199,7 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () vaultId ); - const route = await getRoute( + const route = await getInstaPoolV2Route( contracts.DAI.address, debtOnMakerBefore, contracts.instaPoolResolver diff --git a/test/3_Full-Debt-Bridge-ETHA-ETHB-With-Vault-Creation.test.js b/test/integration/debt_bridge/from_maker/full/1_ETHA-ETHB-With-Vault-Creation.test.js similarity index 98% rename from test/3_Full-Debt-Bridge-ETHA-ETHB-With-Vault-Creation.test.js rename to test/integration/debt_bridge/from_maker/full/1_ETHA-ETHB-With-Vault-Creation.test.js index 58ea569..da56378 100644 --- a/test/3_Full-Debt-Bridge-ETHA-ETHB-With-Vault-Creation.test.js +++ b/test/integration/debt_bridge/from_maker/full/1_ETHA-ETHB-With-Vault-Creation.test.js @@ -4,7 +4,7 @@ const { deployments, ethers } = hre; const GelatoCoreLib = require("@gelatonetwork/core"); const setupFullRefinanceMakerToMaker = require("./helpers/setupFullRefinanceMakerToMaker"); -const getRoute = require("./helpers/services/getRoute"); +const getInstaPoolV2Route = require("../../../../helpers/services/InstaDapp/getInstaPoolV2Route"); const getGasCostForFullRefinance = require("./helpers/services/getGasCostForFullRefinance"); // This test showcases how to submit a task refinancing a Users debt position from @@ -201,7 +201,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B with vault creat vaultAId ); - const route = await getRoute( + const route = await getInstaPoolV2Route( contracts.DAI.address, debtOnMakerBefore, contracts.instaPoolResolver diff --git a/test/4_Full-Debt-Bridge-ETHA-ETHB.test.js b/test/integration/debt_bridge/from_maker/full/2_ETHA-ETHB.test.js similarity index 98% rename from test/4_Full-Debt-Bridge-ETHA-ETHB.test.js rename to test/integration/debt_bridge/from_maker/full/2_ETHA-ETHB.test.js index 2355f9a..9159fd4 100644 --- a/test/4_Full-Debt-Bridge-ETHA-ETHB.test.js +++ b/test/integration/debt_bridge/from_maker/full/2_ETHA-ETHB.test.js @@ -4,7 +4,7 @@ const { deployments, ethers } = hre; const GelatoCoreLib = require("@gelatonetwork/core"); const setupFullRefinanceMakerToMakerWithVaultBCreation = require("./helpers/setupFullRefinanceMakerToMakerWithVaultBCreation"); -const getRoute = require("./helpers/services/getRoute"); +const getInstaPoolV2Route = require("../../../../helpers/services/InstaDapp/getInstaPoolV2Route"); const getGasCostForFullRefinance = require("./helpers/services/getGasCostForFullRefinance"); // This test showcases how to submit a task refinancing a Users debt position from @@ -201,7 +201,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { vaultAId ); - const route = await getRoute( + const route = await getInstaPoolV2Route( contracts.DAI.address, debtOnMakerBefore, contracts.instaPoolResolver diff --git a/test/helpers/services/getGasCostForFullRefinance.js b/test/integration/debt_bridge/from_maker/full/helpers/services/getGasCostForFullRefinance.js similarity index 86% rename from test/helpers/services/getGasCostForFullRefinance.js rename to test/integration/debt_bridge/from_maker/full/helpers/services/getGasCostForFullRefinance.js index d2ab981..15a6106 100644 --- a/test/helpers/services/getGasCostForFullRefinance.js +++ b/test/integration/debt_bridge/from_maker/full/helpers/services/getGasCostForFullRefinance.js @@ -1,4 +1,4 @@ -async function getGasCostForFullRefinance(route) { +module.exports = async function (route) { switch (route) { case 0: return 2519000; // 2290000 * 1,1 // gas left method measure : 2290000 - 2106637 = 183363 | gas reporter : 2290000 - 1789126 = 500874 @@ -11,6 +11,4 @@ async function getGasCostForFullRefinance(route) { default: break; } -} - -module.exports = getGasCostForFullRefinance; +}; diff --git a/test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js b/test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js similarity index 92% rename from test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js rename to test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js index 06cac34..fdd46f7 100644 --- a/test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js +++ b/test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js @@ -4,12 +4,7 @@ const { deployments, ethers } = hre; const GelatoCoreLib = require("@gelatonetwork/core"); // Instadapp UI should do the same implementation for submitting debt bridge task -async function providerWhiteListTaskForMakerETHAToMakerETHB( - wallets, - contracts, - constants, - vaultId -) { +module.exports = async function (wallets, contracts, constants, vaultId) { //#region Step 9 Provider should whitelist task // By WhiteList task, the provider can constrain the type @@ -75,6 +70,4 @@ async function providerWhiteListTaskForMakerETHAToMakerETHB( //#endregion return spells; -} - -module.exports = providerWhiteListTaskForMakerETHAToMakerETHB; +}; diff --git a/test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB.js b/test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB.js similarity index 93% rename from test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB.js rename to test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB.js index fea774c..6bb1d07 100644 --- a/test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB.js +++ b/test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB.js @@ -4,7 +4,7 @@ const { deployments, ethers } = hre; const GelatoCoreLib = require("@gelatonetwork/core"); // Instadapp UI should do the same implementation for submitting debt bridge task -async function providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB( +module.exports = async function ( wallets, contracts, constants, @@ -76,6 +76,4 @@ async function providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB( //#endregion return spells; -} - -module.exports = providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB; +}; diff --git a/test/helpers/services/providerWhiteListTaskForMakerToCompound.js b/test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerToCompound.js similarity index 92% rename from test/helpers/services/providerWhiteListTaskForMakerToCompound.js rename to test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerToCompound.js index 053bed4..4e4e9a8 100644 --- a/test/helpers/services/providerWhiteListTaskForMakerToCompound.js +++ b/test/integration/debt_bridge/from_maker/full/helpers/services/providerWhiteListTaskForMakerToCompound.js @@ -4,12 +4,7 @@ const { ethers, deployments } = hre; const GelatoCoreLib = require("@gelatonetwork/core"); // Instadapp UI should do the same implementation for submitting debt bridge task -async function providerWhiteListTaskForMakerToCompound( - wallets, - contracts, - constants, - vaultId -) { +module.exports = async function (wallets, contracts, constants, vaultId) { //#region Step 9 Provider should whitelist task // By WhiteList task, the provider can constrain the type @@ -75,6 +70,4 @@ async function providerWhiteListTaskForMakerToCompound( //#endregion return spells; -} - -module.exports = providerWhiteListTaskForMakerToCompound; +}; diff --git a/test/helpers/setupFullRefinanceMakerToCompound.js b/test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToCompound.js similarity index 57% rename from test/helpers/setupFullRefinanceMakerToCompound.js rename to test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToCompound.js index e6f24d5..44ad0e4 100644 --- a/test/helpers/setupFullRefinanceMakerToCompound.js +++ b/test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToCompound.js @@ -1,19 +1,19 @@ -const getWallets = require("./services/getWallets"); -const getConstants = require("./services/getConstants"); -const getContracts = require("./services/getContracts"); -const stakeExecutor = require("./services/stakeExecutor"); -const provideFunds = require("./services/provideFunds"); -const providerAssignsExecutor = require("./services/providerAssignsExecutor"); -const addProviderModuleDSA = require("./services/addProviderModuleDSA"); -const createDSA = require("./services/createDSA"); -const initializeMakerCdp = require("./services/initializeMakerCdp"); +const getWallets = require("../../../../../helpers/services/getWallets"); +const getDebtBridgeFromMakerConstants = require("../../services/getDebtBridgeFromMakerConstants"); +const getContracts = require("../../../../../helpers/services/getContracts"); +const stakeExecutor = require("../../../../../helpers/services/gelato/stakeExecutor"); +const provideFunds = require("../../../../../helpers/services/gelato/provideFunds"); +const providerAssignsExecutor = require("../../../../../helpers/services/gelato/providerAssignsExecutor"); +const addProviderModuleDSA = require("../../../../../helpers/services/gelato/addProviderModuleDSA"); +const createDSA = require("../../../../../helpers/services/InstaDapp/createDSA"); +const initializeMakerCdp = require("../../../../../helpers/services/maker/initializeMakerCdp"); const providerWhiteListTaskForMakerToCompound = require("./services/providerWhiteListTaskForMakerToCompound"); -const getABI = require("./services/getABI"); +const getABI = require("../../../../../helpers/services/getABI"); -async function setupFullRefinanceMakerToCompound() { +module.exports = async function () { const wallets = await getWallets(); const contracts = await getContracts(); - const constants = await getConstants(); + const constants = await getDebtBridgeFromMakerConstants(); // Gelato Testing environment setup. await stakeExecutor(wallets.gelatoExecutorWallet, contracts.gelatoCore); @@ -65,6 +65,4 @@ async function setupFullRefinanceMakerToCompound() { spells, ABI, }; -} - -module.exports = setupFullRefinanceMakerToCompound; +}; diff --git a/test/helpers/setupFullRefinanceMakerToMaker.js b/test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToMaker.js similarity index 57% rename from test/helpers/setupFullRefinanceMakerToMaker.js rename to test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToMaker.js index a7df2d5..e84e264 100644 --- a/test/helpers/setupFullRefinanceMakerToMaker.js +++ b/test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToMaker.js @@ -1,20 +1,20 @@ -const getWallets = require("./services/getWallets"); -const getContracts = require("./services/getContracts"); -const getConstants = require("./services/getConstants"); -const stakeExecutor = require("./services/stakeExecutor"); -const provideFunds = require("./services/provideFunds"); -const providerAssignsExecutor = require("./services/providerAssignsExecutor"); -const addProviderModuleDSA = require("./services/addProviderModuleDSA"); -const createDSA = require("./services/createDSA"); -const addETHBGemJoinMapping = require("./services/addETHBGemJoinMapping"); -const initializeMakerCdp = require("./services/initializeMakerCdp"); +const getWallets = require("../../../../../helpers/services/getWallets"); +const getContracts = require("../../../../../helpers/services/getContracts"); +const getDebtBridgeFromMakerConstants = require("../../services/getDebtBridgeFromMakerConstants"); +const stakeExecutor = require("../../../../../helpers/services/gelato/stakeExecutor"); +const provideFunds = require("../../../../../helpers/services/gelato/provideFunds"); +const providerAssignsExecutor = require("../../../../../helpers/services/gelato/providerAssignsExecutor"); +const addProviderModuleDSA = require("../../../../../helpers/services/gelato/addProviderModuleDSA"); +const createDSA = require("../../../../../helpers/services/InstaDapp/createDSA"); +const addETHBGemJoinMapping = require("../../../../../helpers/services/maker/addETHBGemJoinMapping"); +const initializeMakerCdp = require("../../../../../helpers/services/maker/initializeMakerCdp"); const providerWhiteListTaskForMakerETHAToMakerETHB = require("./services/providerWhiteListTaskForMakerETHAToMakerETHB"); -const getABI = require("./services/getABI"); +const getABI = require("../../../../../helpers/services/getABI"); -async function setupFullRefinanceMakerToMaker() { +module.exports = async function () { const wallets = await getWallets(); const contracts = await getContracts(); - const constants = await getConstants(); + const constants = await getDebtBridgeFromMakerConstants(); // Gelato Testing environment setup. await stakeExecutor(wallets.gelatoExecutorWallet, contracts.gelatoCore); @@ -71,6 +71,4 @@ async function setupFullRefinanceMakerToMaker() { spells, ABI, }; -} - -module.exports = setupFullRefinanceMakerToMaker; +}; diff --git a/test/helpers/setupFullRefinanceMakerToMakerWithVaultBCreation.js b/test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToMakerWithVaultBCreation.js similarity index 58% rename from test/helpers/setupFullRefinanceMakerToMakerWithVaultBCreation.js rename to test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToMakerWithVaultBCreation.js index 80d5be6..63e2549 100644 --- a/test/helpers/setupFullRefinanceMakerToMakerWithVaultBCreation.js +++ b/test/integration/debt_bridge/from_maker/full/helpers/setupFullRefinanceMakerToMakerWithVaultBCreation.js @@ -1,21 +1,21 @@ -const getWallets = require("./services/getWallets"); -const getContracts = require("./services/getContracts"); -const getConstants = require("./services/getConstants"); -const stakeExecutor = require("./services/stakeExecutor"); -const provideFunds = require("./services/provideFunds"); -const providerAssignsExecutor = require("./services/providerAssignsExecutor"); -const addProviderModuleDSA = require("./services/addProviderModuleDSA"); -const createDSA = require("./services/createDSA"); -const addETHBGemJoinMapping = require("./services/addETHBGemJoinMapping"); -const initializeMakerCdp = require("./services/initializeMakerCdp"); -const createVaultForETHB = require("./services/createVaultForETHB"); +const getWallets = require("../../../../../helpers/services/getWallets"); +const getContracts = require("../../../../../helpers/services/getContracts"); +const getDebtBridgeFromMakerConstants = require("../../services/getDebtBridgeFromMakerConstants"); +const stakeExecutor = require("../../../../../helpers/services/gelato/stakeExecutor"); +const provideFunds = require("../../../../../helpers/services/gelato/provideFunds"); +const providerAssignsExecutor = require("../../../../../helpers/services/gelato/providerAssignsExecutor"); +const addProviderModuleDSA = require("../../../../../helpers/services/gelato/addProviderModuleDSA"); +const createDSA = require("../../../../../helpers/services/InstaDapp/createDSA"); +const addETHBGemJoinMapping = require("../../../../../helpers/services/maker/addETHBGemJoinMapping"); +const initializeMakerCdp = require("../../../../../helpers/services/maker/initializeMakerCdp"); +const createVaultForETHB = require("../../../../../helpers/services/maker/createVaultForETHB"); const providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB = require("./services/providerWhiteListTaskForMakerETHAToMakerETHBWithVaultB"); -const getABI = require("./services/getABI"); +const getABI = require("../../../../../helpers/services/getABI"); -async function setupFullRefinanceMakerToMakerWithVaultBCreation() { +module.exports = async function () { const wallets = await getWallets(); const contracts = await getContracts(); - const constants = await getConstants(); + const constants = await getDebtBridgeFromMakerConstants(); // Gelato Testing environment setup. await stakeExecutor(wallets.gelatoExecutorWallet, contracts.gelatoCore); @@ -80,6 +80,4 @@ async function setupFullRefinanceMakerToMakerWithVaultBCreation() { spells, ABI, }; -} - -module.exports = setupFullRefinanceMakerToMakerWithVaultBCreation; +}; diff --git a/test/integration/debt_bridge/from_maker/partial/3_Partial-Refinance-External-Provider.test.js b/test/integration/debt_bridge/from_maker/partial/3_Partial-Refinance-External-Provider.test.js new file mode 100644 index 0000000..eebb263 --- /dev/null +++ b/test/integration/debt_bridge/from_maker/partial/3_Partial-Refinance-External-Provider.test.js @@ -0,0 +1,935 @@ +// const { expect } = require("chai"); +// const hre = require("hardhat"); +// const { deployments, ethers } = hre; +// const GelatoCoreLib = require("@gelatonetwork/core"); + +// // #region Contracts ABI and Constants + +// const InstaIndex = require("../../../../pre-compiles/InstaIndex.json"); +// const InstaList = require("../../../../pre-compiles/InstaList.json"); +// const InstaAccount = require("../../../../pre-compiles/InstaAccount.json"); +// const ConnectGelato = require("../../../../pre-compiles/ConnectGelato.json"); +// const ConnectMaker = require("../../../../pre-compiles/ConnectMaker.json"); +// const ConnectCompound = require("../../../../pre-compiles/ConnectCompound.json"); +// const ConnectInstaPool = require("../../../../pre-compiles/ConnectInstaPool.json"); +// const ConnectAuth = require("../../../../pre-compiles/ConnectAuth.json"); +// const ConnectGelatoFullDebtBridgeFromMakerABI = require("../artifacts/contracts/contracts/connectors/ConnectGelatoPartialDebtBridgeFromMaker.sol/ConnectGelatoPartialDebtBridgeFromMaker.json") +// .abi; +// const ConnectGelatoProviderPaymentABI = require("../../../../artifacts/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol/ConnectGelatoProviderPayment.json") +// .abi; +// const InstaConnector = require("../../../../pre-compiles/InstaConnectors.json"); +// const DssCdpManager = require("../../../../pre-compiles/DssCdpManager.json"); +// const GetCdps = require("../../../../pre-compiles/GetCdps.json"); +// const IERC20 = require("../../../../pre-compiles/IERC20.json"); +// const CTokenInterface = require("../../../../pre-compiles/CTokenInterface.json"); +// const CompoundResolver = require("../../../../pre-compiles/InstaCompoundResolver.json"); +// const ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; +// const GAS_LIMIT = "4000000"; +// const GAS_PRICE_CEIL = ethers.utils.parseUnits("1000", "gwei"); +// const WAD = ethers.utils.parseUnits("1", 18); + +// // const ORACLE_MAKER_ETH_USD = "ETH/USD-Maker-v1"; +// // const ORACLE_MAKER_ETH_USD_ADDR = "0x729D19f657BD0614b4985Cf1D82531c67569197B"; +// // const PRICE_ORACLE_MAKER_PAYLOAD = "0x57de26a4"; // IMakerOracle.read() + +// const MIN_COL_RATIO_MAKER = ethers.utils.parseUnits("3", 18); +// const MIN_COL_RATIO_B = ethers.utils.parseUnits("19", 17); + +// // TO DO: make dynamic based on real time Collateral Price and Ratios +// const MAKER_INITIAL_ETH = ethers.utils.parseEther("10"); +// const MAKER_INITIAL_DEBT = ethers.utils.parseUnits("1000", 18); + +// // #endregion + +// //#region Mock Math Function + +// function wdiv(x, y) { +// return x.mul(WAD).add(y.div(2)).div(y); +// } + +// function wmul(x, y) { +// return x.mul(y).add(WAD.div(2)).div(WAD); +// } + +// function wCalcCollateralToWithdraw( +// minColRatioOnMaker, +// minColRatioOnPositionB, +// collateralPrice, +// pricedCollateral, +// daiDebtOnMaker +// ) { +// //#region CALCULATION REPLICATION + +// let expectedColToWithdraw = wmul( +// wmul(minColRatioOnMaker, minColRatioOnPositionB), +// daiDebtOnMaker +// ); // doc ref : c_r x comp_r x d_2 +// expectedColToWithdraw = expectedColToWithdraw.sub( +// wmul(minColRatioOnMaker, pricedCollateral) +// ); // doc ref : c_r x comp_r x d_2 - c_r x e_2 +// expectedColToWithdraw = wdiv( +// expectedColToWithdraw, +// minColRatioOnPositionB.sub(minColRatioOnMaker) +// ); // doc ref : (c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r) +// expectedColToWithdraw = pricedCollateral.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 +// return expectedColToWithdraw; +// } + +// function wCalcDebtToRepay( +// minColRatioOnMaker, +// minColRatioOnPositionB, +// pricedCollateral, +// daiDebtOnMaker +// ) { +// //#region CALCULATION REPLICATION + +// let expectedBorToPayBack = wmul( +// wmul(minColRatioOnMaker, minColRatioOnPositionB), +// daiDebtOnMaker +// ); // doc ref : c_r x comp_r x d_2 +// expectedBorToPayBack = expectedBorToPayBack.sub( +// wmul(minColRatioOnMaker, pricedCollateral) +// ); // doc ref : c_r x comp_r x d_2 - c_r x e_2 +// expectedBorToPayBack = wdiv( +// expectedBorToPayBack, +// minColRatioOnPositionB.sub(minColRatioOnMaker) +// ); // 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), +// expectedBorToPayBack +// ); // doc ref : (1/c_r)((c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r)) +// expectedBorToPayBack = daiDebtOnMaker.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 +// return expectedBorToPayBack; +// } + +// //#endregion + +// describe("Debt Bridge with External Provider", function () { +// this.timeout(0); +// if (hre.network.name !== "hardhat") { +// console.error("Test Suite is meant to be run on hardhat only"); +// process.exit(1); +// } + +// // Wallet to use for local testing +// let userWallet; +// let userAddress; +// let gelatoProviderWallet; +// let gelatoProviderAddress; +// let gelatoExecutorWallet; +// let gelatoExecutorAddress; + +// // Deployed instances +// let connectGelato; +// let connectMaker; +// let connectInstaPool; +// let connectCompound; +// let instaIndex; +// let instaList; +// let dssCdpManager; +// let getCdps; +// let DAI; +// let gelatoCore; +// let cDaiToken; +// let cEthToken; +// let instaMaster; +// let instaConnectors; +// let compoundResolver; + +// // Contracts to deploy and use for local testing +// let conditionMakerVaultUnsafe; +// let connectGelatoPartialDebtBridgeFromMaker; +// let connectGelatoProviderPayment; +// let priceOracleResolver; +// let dsaProviderModule; + +// // Creation during test +// let dsa; + +// // Payload Params for ConnectGelatoPartialDebtBridgeFromMaker and ConditionMakerVaultUnsafe +// let vaultId; + +// // For TaskSpec and for Task +// let spells = []; + +// before(async function () { +// // Get Test Wallet for local testnet +// [ +// userWallet, +// gelatoProviderWallet, +// gelatoExecutorWallet, +// ] = await ethers.getSigners(); +// userAddress = await userWallet.getAddress(); +// gelatoProviderAddress = await gelatoProviderWallet.getAddress(); +// gelatoExecutorAddress = await gelatoExecutorWallet.getAddress(); + +// instaMaster = await ethers.provider.getSigner( +// hre.network.config.InstaMaster +// ); + +// // Hardhat default accounts prefilled with 100 ETH +// expect(await userWallet.getBalance()).to.be.gt( +// ethers.utils.parseEther("10") +// ); + +// // ===== Get Deployed Contract Instance ================== +// instaIndex = await ethers.getContractAt( +// InstaIndex.abi, +// hre.network.config.InstaIndex +// ); +// instaList = await ethers.getContractAt( +// InstaList.abi, +// hre.network.config.InstaList +// ); +// connectGelato = await ethers.getContractAt( +// ConnectGelato.abi, +// hre.network.config.ConnectGelato +// ); +// connectMaker = await ethers.getContractAt( +// ConnectMaker.abi, +// hre.network.config.ConnectMaker +// ); +// connectInstaPool = await ethers.getContractAt( +// ConnectInstaPool.abi, +// hre.network.config.ConnectInstaPool +// ); +// connectCompound = await ethers.getContractAt( +// ConnectCompound.abi, +// hre.network.config.ConnectCompound +// ); +// dssCdpManager = await ethers.getContractAt( +// DssCdpManager.abi, +// hre.network.config.DssCdpManager +// ); +// getCdps = await ethers.getContractAt( +// GetCdps.abi, +// hre.network.config.GetCdps +// ); +// DAI = await ethers.getContractAt(IERC20.abi, hre.network.config.DAI); +// gelatoCore = await ethers.getContractAt( +// GelatoCoreLib.GelatoCore.abi, +// hre.network.config.GelatoCore +// ); +// cDaiToken = await ethers.getContractAt( +// CTokenInterface.abi, +// hre.network.config.CDAI +// ); +// cEthToken = await ethers.getContractAt( +// CTokenInterface.abi, +// hre.network.config.CETH +// ); +// instaConnectors = await ethers.getContractAt( +// InstaConnector.abi, +// hre.network.config.InstaConnectors +// ); +// compoundResolver = await ethers.getContractAt( +// CompoundResolver.abi, +// hre.network.config.CompoundResolver +// ); +// // instaEvent = await ethers.getContractAt( +// // InstaEvent.abi, +// // hre.network.config.InstaEvent +// // ) + +// // ===== Deploy Needed Contract ================== + +// const PriceOracleResolver = await ethers.getContractFactory( +// "PriceOracleResolver" +// ); +// priceOracleResolver = await PriceOracleResolver.deploy(); +// await priceOracleResolver.deployed(); + +// const ConditionMakerVaultUnsafe = await ethers.getContractFactory( +// "ConditionMakerVaultUnsafe" +// ); +// conditionMakerVaultUnsafe = await ConditionMakerVaultUnsafe.deploy(); +// await conditionMakerVaultUnsafe.deployed(); + +// const ConnectGelatoPartialDebtBridgeFromMaker = await ethers.getContractFactory( +// "ConnectGelatoPartialDebtBridgeFromMaker" +// ); +// connectGelatoPartialDebtBridgeFromMaker = await ConnectGelatoPartialDebtBridgeFromMaker.deploy( +// (await instaConnectors.connectorLength()).add(1) +// ); +// await connectGelatoPartialDebtBridgeFromMaker.deployed(); + +// const ConnectGelatoProviderPayment = await ethers.getContractFactory( +// "ConnectGelatoProviderPayment" +// ); +// connectGelatoProviderPayment = await ConnectGelatoProviderPayment.deploy( +// (await instaConnectors.connectorLength()).add(2) +// ); +// await connectGelatoProviderPayment.deployed(); + +// const ProviderModuleDsa = await ethers.getContractFactory( +// "ProviderModuleDsa" +// ); +// dsaProviderModule = await ProviderModuleDsa.deploy( +// hre.network.config.GelatoCore, +// connectGelatoProviderPayment.address +// ); +// await dsaProviderModule.deployed(); + +// /////////////////////////////////////////////////////////////////////////////////////////////////// +// /////////////////////////////////////////////////////////////////////////////////////////////////// +// /////////////////////////////// After Contracts Deployement : Setup /////////////////////////// +// /////////////////////////////////////////////////////////////////////////////////////////////////// +// /////////////////////////////////////////////////////////////////////////////////////////////////// + +// // Gelato Testing environment setup. +// // Step 1 : Add EUR/USD Maker Medianizer in the PriceOracleResolver +// // Step 2 : Enable Debt Bridge Connector and Gelato Provider Payment Connector +// // Step 3 : Executor Staking on Gelato +// // Step 4 : Provider put some fund on gelato for paying future tasks executions +// // Step 5 : Provider choose a executor +// // Step 6 : Provider will add a module +// // Step 7 : User create a DeFi Smart Account +// // Step 8 : User open a Vault, put some ether on it and borrow some dai +// // Step 9 : User give authorization to gelato to use his DSA on his behalf. +// // Step 10 : Provider should whitelist task + +// //#region Step 1 Add EUR/USD Maker Medianizer in the PriceOracleResolver + +// // PriceOracleResolver is a price feeder aggregator +// // You will be able to query price from multiple source through this aggregator +// // For the demo we add the ETH/USD Medianizer to the aggregator +// // MakerDAO price oracle are called Medianizer + +// // await priceOracleResolver.addOracle( +// // ORACLE_MAKER_ETH_USD, +// // ORACLE_MAKER_ETH_USD_ADDR, +// // PRICE_ORACLE_MAKER_PAYLOAD +// // ); + +// //#endregion + +// //#region Step 2 Enable Debt Bridge Connector and Gelato Provider Payment Connector + +// // Debt Bridge Connector is used during refinancing of debt +// // This Connect help the user to split a position in one protocol. +// // to 2 protocol in a safe way. Both debt position will be safe. + +// // Gelato Provider Payment Connector is used for paying the provider +// // for task execution. So when futur task will be executed, through a self financing +// // transaction (user will pay during the execution of the task) task will +// // be executed. Improvind user experience. + +// await userWallet.sendTransaction({ +// to: hre.network.config.InstaMaster, +// value: ethers.utils.parseEther("0.1"), +// }); + +// await hre.network.provider.request({ +// method: "hardhat_impersonateAccount", +// params: [await instaMaster.getAddress()], +// }); + +// await instaConnectors +// .connect(instaMaster) +// .enable(connectGelatoPartialDebtBridgeFromMaker.address); + +// await instaConnectors +// .connect(instaMaster) +// .enable(connectGelatoProviderPayment.address); + +// await hre.network.provider.request({ +// method: "hardhat_stopImpersonatingAccount", +// params: [await instaMaster.getAddress()], +// }); + +// expect( +// await instaConnectors.isConnector([ +// connectGelatoPartialDebtBridgeFromMaker.address, +// ]) +// ).to.be.true; +// expect( +// await instaConnectors.isConnector([connectGelatoProviderPayment.address]) +// ).to.be.true; + +// //#endregion + +// //#region Step 3 Executor Staking on Gelato + +// // For task execution provider will ask a executor to watch the +// // blockchain for possible execution autorization given by +// // the condition that user choose when submitting the task. +// // And if all condition are meet executor will execute the task. +// // For safety measure Gelato ask the executor to stake a minimum +// // amount. + +// await gelatoCore.connect(gelatoExecutorWallet).stakeExecutor({ +// value: await gelatoCore.minExecutorStake(), +// }); + +// expect( +// await gelatoCore.isExecutorMinStaked(gelatoExecutorAddress) +// ).to.be.true; + +// //#endregion + +// //#region Step 4 Provider put some fund on gelato for paying future tasks executions + +// // Provider put some funds in gelato system for paying the +// // Executor when this one will execute task on behalf of the +// // Provider. At each provider's task execution, some funds (approximatively +// // the gas cost value) will be transfered to the Executor stake. + +// const TASK_AUTOMATION_FUNDS = await gelatoCore.minExecProviderFunds( +// GAS_LIMIT, +// GAS_PRICE_CEIL +// ); + +// await expect( +// gelatoCore +// .connect(gelatoProviderWallet) +// .provideFunds(gelatoProviderAddress, { +// value: TASK_AUTOMATION_FUNDS, +// }) +// ).to.emit(gelatoCore, "LogFundsProvided"); + +// expect(await gelatoCore.providerFunds(gelatoProviderAddress)).to.be.equal( +// TASK_AUTOMATION_FUNDS +// ); + +// //#endregion + +// //#region Step 5 Provider choose a executor + +// // Provider choose a executor who will execute futur task +// // for the provider, it will be compensated by the provider. + +// await expect( +// gelatoCore +// .connect(gelatoProviderWallet) +// .providerAssignsExecutor(gelatoExecutorAddress) +// ).to.emit(gelatoCore, "LogProviderAssignedExecutor"); + +// expect( +// await gelatoCore.executorByProvider(gelatoProviderAddress) +// ).to.be.equal(gelatoExecutorAddress); + +// //#endregion + +// //#region Step 6 Provider will add a module + +// // By adding a module the provider will format future task's +// // payload by adding some specificity like his address to the +// // Payment connector for receiving payment of User. + +// await expect( +// gelatoCore +// .connect(gelatoProviderWallet) +// .addProviderModules([dsaProviderModule.address]) +// ).to.emit(gelatoCore, "LogProviderModuleAdded"); + +// expect( +// await gelatoCore +// .connect(gelatoProviderWallet) +// .isModuleProvided(gelatoProviderAddress, dsaProviderModule.address) +// ).to.be.true; + +// //#endregion + +// //#region Step 7 User create a DeFi Smart Account + +// // User create a Instadapp DeFi Smart Account +// // who give him the possibility to interact +// // with a large list of DeFi protocol through one +// // Proxy 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) +// ); + +// //#endregion + +// //#region Step 8 User open a Vault, put some ether on it and borrow some dai + +// // User open a maker vault +// // He deposit 10 Eth on it +// // He borrow a 1000 DAI + +// const openVault = await hre.run("abi-encode-withselector", { +// abi: ConnectMaker.abi, +// functionname: "open", +// inputs: ["ETH-A"], +// }); + +// await dsa.cast([hre.network.config.ConnectMaker], [openVault], userAddress); + +// const cdps = await getCdps.getCdpsAsc(dssCdpManager.address, dsa.address); +// vaultId = String(cdps.ids[0]); +// expect(cdps.ids[0].isZero()).to.be.false; + +// await dsa.cast( +// [hre.network.config.ConnectMaker], +// [ +// await hre.run("abi-encode-withselector", { +// abi: ConnectMaker.abi, +// functionname: "deposit", +// inputs: [vaultId, MAKER_INITIAL_ETH, 0, 0], +// }), +// ], +// userAddress, +// { +// value: MAKER_INITIAL_ETH, +// } +// ); + +// await dsa.cast( +// [hre.network.config.ConnectMaker], +// [ +// await hre.run("abi-encode-withselector", { +// abi: ConnectMaker.abi, +// functionname: "borrow", +// inputs: [vaultId, MAKER_INITIAL_DEBT, 0, 0], +// }), +// ], +// userAddress +// ); + +// expect(await DAI.balanceOf(dsa.address)).to.be.equal(MAKER_INITIAL_DEBT); + +// //#endregion + +// //#region Step 9 User give authorization to gelato to use his DSA on his behalf. + +// // Instadapp DSA contract give the possibility to the user to delegate +// // action by giving authorization. +// // In this case user give authorization to gelato to execute +// // task for him if needed. + +// await dsa.cast( +// [hre.network.config.ConnectAuth], +// [ +// await hre.run("abi-encode-withselector", { +// abi: ConnectAuth.abi, +// functionname: "add", +// inputs: [gelatoCore.address], +// }), +// ], +// userAddress +// ); + +// expect(await dsa.isAuth(gelatoCore.address)).to.be.true; + +// //#endregion + +// //#region Step 10 Provider should whitelist task + +// // By WhiteList task, the provider can constrain the type +// // of task the user can submitting. + +// //#region Actions + +// const debtBridgeCalculation = new GelatoCoreLib.Action({ +// addr: connectGelatoPartialDebtBridgeFromMaker.address, +// data: await hre.run("abi-encode-withselector", { +// abi: ConnectGelatoFullDebtBridgeFromMakerABI, +// functionname: "savePartialRefinanceDataToMemory", +// inputs: [ +// vaultId, +// MIN_COL_RATIO_MAKER, +// MIN_COL_RATIO_B, +// priceOracleResolver.address, +// await hre.run("abi-encode-withselector", { +// abi: (await deployments.getArtifcat("PriceOracleResolver")).abi, +// functionname: "getMockPrice", +// inputs: [userAddress], +// }), +// 0, +// 0, +// ], +// }), +// operation: GelatoCoreLib.Operation.Delegatecall, +// }); + +// spells.push(debtBridgeCalculation); + +// const flashBorrow = new GelatoCoreLib.Action({ +// addr: connectInstaPool.address, +// data: await hre.run("abi-encode-withselector", { +// abi: ConnectInstaPool.abi, +// functionname: "flashBorrow", +// inputs: [hre.network.config.DAI, 0, "600", 0], +// }), +// operation: GelatoCoreLib.Operation.Delegatecall, +// }); + +// spells.push(flashBorrow); + +// const paybackMaker = new GelatoCoreLib.Action({ +// addr: connectMaker.address, +// data: await hre.run("abi-encode-withselector", { +// abi: ConnectMaker.abi, +// functionname: "payback", +// inputs: [vaultId, 0, "601", 0], +// }), +// operation: GelatoCoreLib.Operation.Delegatecall, +// }); + +// spells.push(paybackMaker); + +// const withdrawMaker = new GelatoCoreLib.Action({ +// addr: connectMaker.address, +// data: await hre.run("abi-encode-withselector", { +// abi: ConnectMaker.abi, +// functionname: "withdraw", +// inputs: [vaultId, 0, "602", 0], +// }), +// operation: GelatoCoreLib.Operation.Delegatecall, +// }); + +// spells.push(withdrawMaker); + +// const depositCompound = new GelatoCoreLib.Action({ +// addr: connectCompound.address, +// data: await hre.run("abi-encode-withselector", { +// abi: ConnectCompound.abi, +// functionname: "deposit", +// inputs: [ETH, 0, "603", 0], +// }), +// operation: GelatoCoreLib.Operation.Delegatecall, +// }); + +// spells.push(depositCompound); + +// const borrowCompound = new GelatoCoreLib.Action({ +// addr: connectCompound.address, +// data: await hre.run("abi-encode-withselector", { +// abi: ConnectCompound.abi, +// functionname: "borrow", +// inputs: [hre.network.config.DAI, 0, "604", 0], +// }), +// operation: GelatoCoreLib.Operation.Delegatecall, +// }); + +// spells.push(borrowCompound); + +// const flashPayBack = new GelatoCoreLib.Action({ +// addr: connectInstaPool.address, +// data: await hre.run("abi-encode-withselector", { +// abi: ConnectInstaPool.abi, +// functionname: "flashPayback", +// inputs: [hre.network.config.DAI, 0, 0], +// }), +// operation: GelatoCoreLib.Operation.Delegatecall, +// }); + +// spells.push(flashPayBack); + +// const payProvider = new GelatoCoreLib.Action({ +// addr: connectGelatoProviderPayment.address, +// data: await hre.run("abi-encode-withselector", { +// abi: ConnectGelatoProviderPaymentABI, +// functionname: "payProvider", +// inputs: [gelatoProviderAddress, ETH, 0, "605", 0], +// }), +// operation: GelatoCoreLib.Operation.Delegatecall, +// }); + +// spells.push(payProvider); + +// const gasPriceCeil = ethers.constants.MaxUint256; + +// const connectGelatoFullDebtBridgeFromMakerTaskSpec = new GelatoCoreLib.TaskSpec( +// { +// conditions: [conditionMakerVaultUnsafe.address], +// actions: spells, +// gasPriceCeil, +// } +// ); + +// await expect( +// gelatoCore +// .connect(gelatoProviderWallet) +// .provideTaskSpecs([connectGelatoFullDebtBridgeFromMakerTaskSpec]) +// ).to.emit(gelatoCore, "LogTaskSpecProvided"); + +// expect( +// await gelatoCore +// .connect(gelatoProviderWallet) +// .isTaskSpecProvided( +// gelatoProviderAddress, +// connectGelatoFullDebtBridgeFromMakerTaskSpec +// ) +// ).to.be.equal("OK"); + +// expect( +// await gelatoCore +// .connect(gelatoProviderWallet) +// .taskSpecGasPriceCeil( +// gelatoProviderAddress, +// await gelatoCore +// .connect(gelatoProviderWallet) +// .hashTaskSpec(connectGelatoFullDebtBridgeFromMakerTaskSpec) +// ) +// ).to.be.equal(gasPriceCeil); + +// //#endregion + +// //#endregion +// }); + +// it("#1: Use Maker Compound refinancing if the maker vault become unsafe after a market move.", async function () { +// // User Actions +// // Step 1: User submit a Debt Refinancing task if market move against him +// // Step 2: Market Move against the user (Mock) +// // Step 3: Executor execute the user's task + +// //#region Step 1 User submit a Debt Refinancing task if market move against him + +// // User submit the refinancing task if market move against him. +// // So in this case if the maker vault go to the unsafe area +// // the refinancing task will be executed and the position +// // will be split on two position on maker and compound. +// // It will be done through a algorithm that will optimize the +// // total borrow rate. + +// const conditionMakerVaultUnsafeObj = new GelatoCoreLib.Condition({ +// inst: conditionMakerVaultUnsafe.address, +// data: await conditionMakerVaultUnsafe.getConditionData( +// vaultId, +// priceOracleResolver.address, +// await hre.run("abi-encode-withselector", { +// abi: (await deployments.getArtifact("PriceOracleResolver")).abi, +// functionname: "getMockPrice", +// inputs: [userAddress], +// }), +// MIN_COL_RATIO_MAKER +// ), +// }); + +// // ======= GELATO TASK SETUP ====== +// const refinanceIfVaultUnsafe = new GelatoCoreLib.Task({ +// conditions: [conditionMakerVaultUnsafeObj], +// actions: spells, +// }); + +// const gelatoExternalProvider = new GelatoCoreLib.GelatoProvider({ +// addr: gelatoProviderAddress, +// module: dsaProviderModule.address, +// }); + +// const expiryDate = 0; + +// await expect( +// dsa.cast( +// [connectGelato.address], // targets +// [ +// await hre.run("abi-encode-withselector", { +// abi: ConnectGelato.abi, +// functionname: "submitTask", +// inputs: [ +// gelatoExternalProvider, +// refinanceIfVaultUnsafe, +// expiryDate, +// ], +// }), +// ], // datas +// userAddress, // origin +// { +// gasLimit: 5000000, +// } +// ) +// ).to.emit(gelatoCore, "LogTaskSubmitted"); + +// const taskReceipt = new GelatoCoreLib.TaskReceipt({ +// id: await gelatoCore.currentTaskReceiptId(), +// userProxy: dsa.address, +// provider: gelatoExternalProvider, +// tasks: [refinanceIfVaultUnsafe], +// expiryDate, +// }); + +// //#endregion + +// //#region Step 2 Market Move against the user (Mock) + +// // Ether market price went from the current price to 250$ + +// const gelatoGasPrice = await hre.run("fetchGelatoGasPrice"); +// expect(gelatoGasPrice).to.be.lte(GAS_PRICE_CEIL); + +// // TO DO: base mock price off of real price data +// await priceOracleResolver.setMockPrice(ethers.utils.parseUnits("400", 18)); + +// expect( +// await gelatoCore +// .connect(gelatoExecutorWallet) +// .canExec(taskReceipt, GAS_LIMIT, gelatoGasPrice) +// ).to.be.equal("ConditionNotOk:MakerVaultNotUnsafe"); + +// // TO DO: base mock price off of real price data +// await priceOracleResolver.setMockPrice(ethers.utils.parseUnits("250", 18)); + +// expect( +// await gelatoCore +// .connect(gelatoExecutorWallet) +// .canExec(taskReceipt, GAS_LIMIT, gelatoGasPrice) +// ).to.be.equal("OK"); + +// //#endregion + +// //#region Step 3 Executor execute the user's task + +// // The market move make the vault unsafe, so the executor +// // will execute the user's task to make the user position safe +// // by a debt refinancing in compound. + +// //#region EXPECTED OUTCOME + +// const latestPrice = await priceOracleResolver.getMockPrice(userAddress); +// const gasFeesPaidFromCol = ethers.utils +// .parseUnits(String(1933090 + 19331 * 2), 0) +// .mul(gelatoGasPrice); +// const debtOnMakerBefore = await connectGelatoPartialDebtBridgeFromMaker.getMakerVaultDebt( +// vaultId +// ); +// const pricedCollateral = wmul( +// ( +// await connectGelatoPartialDebtBridgeFromMaker.getMakerVaultCollateralBalance( +// vaultId +// ) +// ).sub(gasFeesPaidFromCol), +// latestPrice +// ); + +// const expectedColWithdrawAmount = wCalcCollateralToWithdraw( +// MIN_COL_RATIO_MAKER, +// MIN_COL_RATIO_B, +// latestPrice, +// pricedCollateral, +// debtOnMakerBefore +// ); + +// const expectedBorAmountToPayBack = wCalcDebtToRepay( +// MIN_COL_RATIO_MAKER, +// MIN_COL_RATIO_B, +// pricedCollateral, +// debtOnMakerBefore +// ); + +// //console.log(String(wdiv(pricedCollateral.sub(wmul(expectedColWithdrawAmount, latestPrice).add(gasFeesPaidFromCol)),debt.sub(expectedBorAmountToPayBack)))); + +// //#endregion +// const providerBalanceBeforeExecution = await gelatoProviderWallet.getBalance(); + +// await expect( +// gelatoCore.connect(gelatoExecutorWallet).exec(taskReceipt, { +// gasPrice: gelatoGasPrice, // Exectutor must use gelatoGasPrice (Chainlink fast gwei) +// gasLimit: GAS_LIMIT, +// }) +// ).to.emit(gelatoCore, "LogExecSuccess"); + +// // 🚧 For Debugging: +// // const txResponse2 = await gelatoCore +// // .connect(gelatoProviderWallet) +// // .exec(taskReceipt, { +// // gasPrice: gelatoGasPrice, +// // gasLimit: GAS_LIMIT, +// // }); +// // const {blockHash} = await txResponse2.wait(); +// // const logs = await ethers.provider.getLogs({blockHash}); +// // const iFace = new ethers.utils.Interface(GelatoCoreLib.GelatoCore.abi); +// // for (const log of logs) { +// // console.log(iFace.parseLog(log).args.reason); +// // } +// // await GelatoCoreLib.sleep(10000); + +// expect(await gelatoProviderWallet.getBalance()).to.be.gt( +// providerBalanceBeforeExecution +// ); + +// // compound position of DSA on cDai and cEth +// const compoundPosition = await compoundResolver.getCompoundData( +// dsa.address, +// [cDaiToken.address, cEthToken.address] +// ); + +// // https://compound.finance/docs/ctokens#exchange-rate +// // calculate cEth/ETH rate to convert back cEth to ETH +// // for comparing with the withdrew Ether to the deposited one. +// const exchangeRateCethToEth = (await cEthToken.getCash()) +// .add(await cEthToken.totalBorrows()) +// .sub(await cEthToken.totalReserves()) +// .div(await cEthToken.totalSupply()); + +// // Estimated amount to borrowed token should be equal to the actual one read on compound contracts +// expect(expectedBorAmountToPayBack).to.be.equal( +// compoundPosition[0].borrowBalanceStoredUser +// ); + +// // Estimated amount of pricedCollateral should be equal to the actual one read on compound contracts +// expect( +// expectedColWithdrawAmount.sub( +// compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth) +// ) +// ).to.be.lt(ethers.utils.parseUnits("1", 12)); + +// const debtOnMakerAfter = await connectGelatoPartialDebtBridgeFromMaker.getMakerVaultDebt( +// vaultId +// ); +// const collateralOnMakerAfter = await connectGelatoPartialDebtBridgeFromMaker.getMakerVaultCollateralBalance( +// vaultId +// ); // in Ether. + +// // Total Borrowed Amount on both protocol should equal to the initial borrowed amount on maker vault. +// expect( +// debtOnMakerAfter +// .add(compoundPosition[0].borrowBalanceStoredUser) +// .sub(MAKER_INITIAL_DEBT) +// ).to.be.lte(ethers.utils.parseUnits("1", 0)); +// // Total Ether col on Maker and Compound (+ gasFeesPaidFromCol) should equal to the initial col on maker vault +// expect( +// compoundPosition[1].balanceOfUser +// .mul(exchangeRateCethToEth) +// .add(gasFeesPaidFromCol) +// .add(collateralOnMakerAfter) +// .sub(ethers.utils.parseEther("10")) +// ).to.be.lt(ethers.utils.parseUnits("1", 12)); + +// // Check Collaterization Ratio of Maker and Compound +// expect( +// wdiv( +// wmul( +// compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth), +// latestPrice +// ), +// compoundPosition[0].borrowBalanceStoredUser +// ).sub(MIN_COL_RATIO_MAKER) +// ).to.be.lt(ethers.utils.parseUnits("1", 12)); +// expect( +// wdiv( +// wmul( +// collateralOnMakerAfter, +// await priceOracleResolver.getMockPrice(userAddress) +// ), +// debtOnMakerAfter +// ).sub(MIN_COL_RATIO_MAKER) +// ).to.be.lt(ethers.utils.parseUnits("1", 1)); + +// // DSA contain 1000 DAI +// expect(await DAI.balanceOf(dsa.address)).to.be.equal(MAKER_INITIAL_DEBT); + +// //#endregion +// }); +// }); diff --git a/test/helpers/services/enableGelatoConnectorsForFromMaker.js b/test/integration/debt_bridge/from_maker/services/enableGelatoConnectorsForFromMaker.js similarity index 93% rename from test/helpers/services/enableGelatoConnectorsForFromMaker.js rename to test/integration/debt_bridge/from_maker/services/enableGelatoConnectorsForFromMaker.js index e92f9a4..39fcc76 100644 --- a/test/helpers/services/enableGelatoConnectorsForFromMaker.js +++ b/test/integration/debt_bridge/from_maker/services/enableGelatoConnectorsForFromMaker.js @@ -2,7 +2,7 @@ const { expect } = require("chai"); const hre = require("hardhat"); const { ethers } = hre; -async function enableGelatoConnectorsForFromMaker( +module.exports = async function ( userWallet, connectGelatoProviderPaymentAddr, connectGelatoDataAddr, @@ -46,6 +46,4 @@ async function enableGelatoConnectorsForFromMaker( .to.be.true; //#endregion -} - -module.exports = enableGelatoConnectorsForFromMaker; +}; diff --git a/test/helpers/services/getConstants.js b/test/integration/debt_bridge/from_maker/services/getDebtBridgeFromMakerConstants.js similarity index 92% rename from test/helpers/services/getConstants.js rename to test/integration/debt_bridge/from_maker/services/getDebtBridgeFromMakerConstants.js index 4f7d2af..a1f3c9d 100644 --- a/test/helpers/services/getConstants.js +++ b/test/integration/debt_bridge/from_maker/services/getDebtBridgeFromMakerConstants.js @@ -13,7 +13,7 @@ const MAKER_INITIAL_DEBT = ethers.utils.parseUnits("1000", 18); const MAX_FEES_IN_PERCENT = ethers.utils.parseUnits("1", 17); -async function getConstants() { +module.exports = async function () { return { ETH: ETH, MIN_COL_RATIO_MAKER: MIN_COL_RATIO_MAKER, @@ -23,6 +23,4 @@ async function getConstants() { MAKER_INITIAL_ETH: MAKER_INITIAL_ETH, MAX_FEES_IN_PERCENT: MAX_FEES_IN_PERCENT, }; -} - -module.exports = getConstants; +}; diff --git a/test/1_mv-DAI-DSR-Compound.test.js b/test/integration/lending_bridge/1_mv-DAI-DSR-Compound.test.js similarity index 95% rename from test/1_mv-DAI-DSR-Compound.test.js rename to test/integration/lending_bridge/1_mv-DAI-DSR-Compound.test.js index bd38180..f7d5a71 100644 --- a/test/1_mv-DAI-DSR-Compound.test.js +++ b/test/integration/lending_bridge/1_mv-DAI-DSR-Compound.test.js @@ -11,15 +11,15 @@ const GelatoCoreLib = require("@gelatonetwork/core"); const DAI_100 = ethers.utils.parseUnits("100", 18); // Contracts -const InstaIndex = require("../pre-compiles/InstaIndex.json"); -const InstaList = require("../pre-compiles/InstaList.json"); -const InstaAccount = require("../pre-compiles/InstaAccount.json"); -const ConnectAuth = require("../pre-compiles/ConnectAuth.json"); -const ConnectGelato_ABI = require("../pre-compiles/ConnectGelato_ABI.json"); -const ConnectMaker = require("../pre-compiles/ConnectMaker.json"); -const ConnectCompound = require("../pre-compiles/ConnectCompound.json"); -const IERC20 = require("../pre-compiles/IERC20.json"); -const IUniswapExchange = require("../pre-compiles/IUniswapExchange.json"); +const InstaIndex = require("../../../pre-compiles/InstaIndex.json"); +const InstaList = require("../../../pre-compiles/InstaList.json"); +const InstaAccount = require("../../../pre-compiles/InstaAccount.json"); +const ConnectAuth = require("../../../pre-compiles/ConnectAuth.json"); +const ConnectGelato_ABI = require("../../../pre-compiles/ConnectGelato_ABI.json"); +const ConnectMaker = require("../../../pre-compiles/ConnectMaker.json"); +const ConnectCompound = require("../../../pre-compiles/ConnectCompound.json"); +const IERC20 = require("../../../pre-compiles/IERC20.json"); +const IUniswapExchange = require("../../../pre-compiles/IUniswapExchange.json"); describe("Move DAI lending from DSR to Compound", function () { this.timeout(0); diff --git a/test/unit_tests/0_ConditionMakerVaultUnsafe.test.js b/test/unit/conditions/0_ConditionMakerVaultUnsafe.test.js similarity index 91% rename from test/unit_tests/0_ConditionMakerVaultUnsafe.test.js rename to test/unit/conditions/0_ConditionMakerVaultUnsafe.test.js index 86790ce..cddde78 100644 --- a/test/unit_tests/0_ConditionMakerVaultUnsafe.test.js +++ b/test/unit/conditions/0_ConditionMakerVaultUnsafe.test.js @@ -4,13 +4,13 @@ const { deployments, ethers } = hre; // #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"); +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"); const ORACLE_MAKER_ETH_USD = "ETH/USD-Maker-v1"; const ORACLE_MAKER_ETH_USD_ADDR = "0x729D19f657BD0614b4985Cf1D82531c67569197B"; diff --git a/test/unit_tests/4_ConditionDebtBridgeIsAffordable.test.js b/test/unit/conditions/1_ConditionDebtBridgeIsAffordable.test.js similarity index 89% rename from test/unit_tests/4_ConditionDebtBridgeIsAffordable.test.js rename to test/unit/conditions/1_ConditionDebtBridgeIsAffordable.test.js index df1d3b6..f7ca53f 100644 --- a/test/unit_tests/4_ConditionDebtBridgeIsAffordable.test.js +++ b/test/unit/conditions/1_ConditionDebtBridgeIsAffordable.test.js @@ -4,13 +4,13 @@ const { deployments, ethers } = hre; // #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"); +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"); // #endregion diff --git a/test/unit_tests/1_ConnectGelatoProviderPayment.test.js b/test/unit/connectors/0_ConnectGelatoProviderPayment.test.js similarity index 92% rename from test/unit_tests/1_ConnectGelatoProviderPayment.test.js rename to test/unit/connectors/0_ConnectGelatoProviderPayment.test.js index 4d2a862..e9cf225 100644 --- a/test/unit_tests/1_ConnectGelatoProviderPayment.test.js +++ b/test/unit/connectors/0_ConnectGelatoProviderPayment.test.js @@ -6,14 +6,14 @@ const GelatoCoreLib = require("@gelatonetwork/core"); // #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 ConnectBasic = require("../../pre-compiles/ConnectBasic.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"); +const ConnectMaker = require("../../../pre-compiles/ConnectMaker.json"); +const GetCdps = require("../../../pre-compiles/GetCdps.json"); +const DssCdpManager = require("../../../pre-compiles/DssCdpManager.json"); +const ConnectBasic = require("../../../pre-compiles/ConnectBasic.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"); // #endregion diff --git a/test/unit_tests/5_FGelatoDebtBridge.test.js b/test/unit/functions/0_FGelatoDebtBridge.test.js similarity index 96% rename from test/unit_tests/5_FGelatoDebtBridge.test.js rename to test/unit/functions/0_FGelatoDebtBridge.test.js index c1082c3..52a0d18 100644 --- a/test/unit_tests/5_FGelatoDebtBridge.test.js +++ b/test/unit/functions/0_FGelatoDebtBridge.test.js @@ -2,10 +2,10 @@ const { expect } = require("chai"); const hre = require("hardhat"); const { deployments, ethers } = hre; -const InstaPoolResolver = require("../../artifacts/contracts/interfaces/InstaDapp/resolvers/IInstaPoolResolver.sol/IInstaPoolResolver.json"); +const InstaPoolResolver = require("../../../artifacts/contracts/interfaces/InstaDapp/resolvers/IInstaPoolResolver.sol/IInstaPoolResolver.json"); const DAI = hre.network.config.DAI; -describe("Debt Partial Refinance Math Unit Test", function () { +describe("FGelatoDebtBridge Unit Tests", function () { this.timeout(0); if (hre.network.name !== "hardhat") { console.error("Test Suite is meant to be run on hardhat only"); diff --git a/test/unit_tests/2_DebtRefinanceMath.test.js b/test/unit/functions/1_DebtPartialRefinanceMath.test.js similarity index 100% rename from test/unit_tests/2_DebtRefinanceMath.test.js rename to test/unit/functions/1_DebtPartialRefinanceMath.test.js diff --git a/test/unit_tests/3_PriceOracleResolver.test.js b/test/unit/resolvers/0_PriceOracleResolver.test.js similarity index 100% rename from test/unit_tests/3_PriceOracleResolver.test.js rename to test/unit/resolvers/0_PriceOracleResolver.test.js diff --git a/test_temp/3_Partial-Refinance-External-Provider.test.js b/test_temp/3_Partial-Refinance-External-Provider.test.js deleted file mode 100644 index 0dfeccd..0000000 --- a/test_temp/3_Partial-Refinance-External-Provider.test.js +++ /dev/null @@ -1,935 +0,0 @@ -const { expect } = require("chai"); -const hre = require("hardhat"); -const { deployments, ethers } = hre; -const GelatoCoreLib = require("@gelatonetwork/core"); - -// #region Contracts ABI and Constants - -const InstaIndex = require("../pre-compiles/InstaIndex.json"); -const InstaList = require("../pre-compiles/InstaList.json"); -const InstaAccount = require("../pre-compiles/InstaAccount.json"); -const ConnectGelato = require("../pre-compiles/ConnectGelato.json"); -const ConnectMaker = require("../pre-compiles/ConnectMaker.json"); -const ConnectCompound = require("../pre-compiles/ConnectCompound.json"); -const ConnectInstaPool = require("../pre-compiles/ConnectInstaPool.json"); -const ConnectAuth = require("../pre-compiles/ConnectAuth.json"); -const ConnectGelatoFullDebtBridgeFromMakerABI = require("../artifacts/contracts/contracts/connectors/ConnectGelatoPartialDebtBridgeFromMaker.sol/ConnectGelatoPartialDebtBridgeFromMaker.json") - .abi; -const ConnectGelatoProviderPaymentABI = require("../artifacts/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol/ConnectGelatoProviderPayment.json") - .abi; -const InstaConnector = require("../pre-compiles/InstaConnectors.json"); -const DssCdpManager = require("../pre-compiles/DssCdpManager.json"); -const GetCdps = require("../pre-compiles/GetCdps.json"); -const IERC20 = require("../pre-compiles/IERC20.json"); -const CTokenInterface = require("../pre-compiles/CTokenInterface.json"); -const CompoundResolver = require("../pre-compiles/InstaCompoundResolver.json"); -const ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; -const GAS_LIMIT = "4000000"; -const GAS_PRICE_CEIL = ethers.utils.parseUnits("1000", "gwei"); -const WAD = ethers.utils.parseUnits("1", 18); - -// const ORACLE_MAKER_ETH_USD = "ETH/USD-Maker-v1"; -// const ORACLE_MAKER_ETH_USD_ADDR = "0x729D19f657BD0614b4985Cf1D82531c67569197B"; -// const PRICE_ORACLE_MAKER_PAYLOAD = "0x57de26a4"; // IMakerOracle.read() - -const MIN_COL_RATIO_MAKER = ethers.utils.parseUnits("3", 18); -const MIN_COL_RATIO_B = ethers.utils.parseUnits("19", 17); - -// TO DO: make dynamic based on real time Collateral Price and Ratios -const MAKER_INITIAL_ETH = ethers.utils.parseEther("10"); -const MAKER_INITIAL_DEBT = ethers.utils.parseUnits("1000", 18); - -// #endregion - -//#region Mock Math Function - -function wdiv(x, y) { - return x.mul(WAD).add(y.div(2)).div(y); -} - -function wmul(x, y) { - return x.mul(y).add(WAD.div(2)).div(WAD); -} - -function wCalcCollateralToWithdraw( - minColRatioOnMaker, - minColRatioOnPositionB, - collateralPrice, - pricedCollateral, - daiDebtOnMaker -) { - //#region CALCULATION REPLICATION - - let expectedColToWithdraw = wmul( - wmul(minColRatioOnMaker, minColRatioOnPositionB), - daiDebtOnMaker - ); // doc ref : c_r x comp_r x d_2 - expectedColToWithdraw = expectedColToWithdraw.sub( - wmul(minColRatioOnMaker, pricedCollateral) - ); // doc ref : c_r x comp_r x d_2 - c_r x e_2 - expectedColToWithdraw = wdiv( - expectedColToWithdraw, - minColRatioOnPositionB.sub(minColRatioOnMaker) - ); // doc ref : (c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r) - expectedColToWithdraw = pricedCollateral.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 - return expectedColToWithdraw; -} - -function wCalcDebtToRepay( - minColRatioOnMaker, - minColRatioOnPositionB, - pricedCollateral, - daiDebtOnMaker -) { - //#region CALCULATION REPLICATION - - let expectedBorToPayBack = wmul( - wmul(minColRatioOnMaker, minColRatioOnPositionB), - daiDebtOnMaker - ); // doc ref : c_r x comp_r x d_2 - expectedBorToPayBack = expectedBorToPayBack.sub( - wmul(minColRatioOnMaker, pricedCollateral) - ); // doc ref : c_r x comp_r x d_2 - c_r x e_2 - expectedBorToPayBack = wdiv( - expectedBorToPayBack, - minColRatioOnPositionB.sub(minColRatioOnMaker) - ); // 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), - expectedBorToPayBack - ); // doc ref : (1/c_r)((c_r x comp_r x d_2 - c_r x e_2)/ (comp_r - c_r)) - expectedBorToPayBack = daiDebtOnMaker.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 - return expectedBorToPayBack; -} - -//#endregion - -describe("Debt Bridge with External Provider", function () { - this.timeout(0); - if (hre.network.name !== "hardhat") { - console.error("Test Suite is meant to be run on hardhat only"); - process.exit(1); - } - - // Wallet to use for local testing - let userWallet; - let userAddress; - let gelatoProviderWallet; - let gelatoProviderAddress; - let gelatoExecutorWallet; - let gelatoExecutorAddress; - - // Deployed instances - let connectGelato; - let connectMaker; - let connectInstaPool; - let connectCompound; - let instaIndex; - let instaList; - let dssCdpManager; - let getCdps; - let DAI; - let gelatoCore; - let cDaiToken; - let cEthToken; - let instaMaster; - let instaConnectors; - let compoundResolver; - - // Contracts to deploy and use for local testing - let conditionMakerVaultUnsafe; - let connectGelatoPartialDebtBridgeFromMaker; - let connectGelatoProviderPayment; - let priceOracleResolver; - let dsaProviderModule; - - // Creation during test - let dsa; - - // Payload Params for ConnectGelatoPartialDebtBridgeFromMaker and ConditionMakerVaultUnsafe - let vaultId; - - // For TaskSpec and for Task - let spells = []; - - before(async function () { - // Get Test Wallet for local testnet - [ - userWallet, - gelatoProviderWallet, - gelatoExecutorWallet, - ] = await ethers.getSigners(); - userAddress = await userWallet.getAddress(); - gelatoProviderAddress = await gelatoProviderWallet.getAddress(); - gelatoExecutorAddress = await gelatoExecutorWallet.getAddress(); - - instaMaster = await ethers.provider.getSigner( - hre.network.config.InstaMaster - ); - - // Hardhat default accounts prefilled with 100 ETH - expect(await userWallet.getBalance()).to.be.gt( - ethers.utils.parseEther("10") - ); - - // ===== Get Deployed Contract Instance ================== - instaIndex = await ethers.getContractAt( - InstaIndex.abi, - hre.network.config.InstaIndex - ); - instaList = await ethers.getContractAt( - InstaList.abi, - hre.network.config.InstaList - ); - connectGelato = await ethers.getContractAt( - ConnectGelato.abi, - hre.network.config.ConnectGelato - ); - connectMaker = await ethers.getContractAt( - ConnectMaker.abi, - hre.network.config.ConnectMaker - ); - connectInstaPool = await ethers.getContractAt( - ConnectInstaPool.abi, - hre.network.config.ConnectInstaPool - ); - connectCompound = await ethers.getContractAt( - ConnectCompound.abi, - hre.network.config.ConnectCompound - ); - dssCdpManager = await ethers.getContractAt( - DssCdpManager.abi, - hre.network.config.DssCdpManager - ); - getCdps = await ethers.getContractAt( - GetCdps.abi, - hre.network.config.GetCdps - ); - DAI = await ethers.getContractAt(IERC20.abi, hre.network.config.DAI); - gelatoCore = await ethers.getContractAt( - GelatoCoreLib.GelatoCore.abi, - hre.network.config.GelatoCore - ); - cDaiToken = await ethers.getContractAt( - CTokenInterface.abi, - hre.network.config.CDAI - ); - cEthToken = await ethers.getContractAt( - CTokenInterface.abi, - hre.network.config.CETH - ); - instaConnectors = await ethers.getContractAt( - InstaConnector.abi, - hre.network.config.InstaConnectors - ); - compoundResolver = await ethers.getContractAt( - CompoundResolver.abi, - hre.network.config.CompoundResolver - ); - // instaEvent = await ethers.getContractAt( - // InstaEvent.abi, - // hre.network.config.InstaEvent - // ) - - // ===== Deploy Needed Contract ================== - - const PriceOracleResolver = await ethers.getContractFactory( - "PriceOracleResolver" - ); - priceOracleResolver = await PriceOracleResolver.deploy(); - await priceOracleResolver.deployed(); - - const ConditionMakerVaultUnsafe = await ethers.getContractFactory( - "ConditionMakerVaultUnsafe" - ); - conditionMakerVaultUnsafe = await ConditionMakerVaultUnsafe.deploy(); - await conditionMakerVaultUnsafe.deployed(); - - const ConnectGelatoPartialDebtBridgeFromMaker = await ethers.getContractFactory( - "ConnectGelatoPartialDebtBridgeFromMaker" - ); - connectGelatoPartialDebtBridgeFromMaker = await ConnectGelatoPartialDebtBridgeFromMaker.deploy( - (await instaConnectors.connectorLength()).add(1) - ); - await connectGelatoPartialDebtBridgeFromMaker.deployed(); - - const ConnectGelatoProviderPayment = await ethers.getContractFactory( - "ConnectGelatoProviderPayment" - ); - connectGelatoProviderPayment = await ConnectGelatoProviderPayment.deploy( - (await instaConnectors.connectorLength()).add(2) - ); - await connectGelatoProviderPayment.deployed(); - - const ProviderModuleDsa = await ethers.getContractFactory( - "ProviderModuleDsa" - ); - dsaProviderModule = await ProviderModuleDsa.deploy( - hre.network.config.GelatoCore, - connectGelatoProviderPayment.address - ); - await dsaProviderModule.deployed(); - - /////////////////////////////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////////////////////////////// - /////////////////////////////// After Contracts Deployement : Setup /////////////////////////// - /////////////////////////////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////////////////////////////// - - // Gelato Testing environment setup. - // Step 1 : Add EUR/USD Maker Medianizer in the PriceOracleResolver - // Step 2 : Enable Debt Bridge Connector and Gelato Provider Payment Connector - // Step 3 : Executor Staking on Gelato - // Step 4 : Provider put some fund on gelato for paying future tasks executions - // Step 5 : Provider choose a executor - // Step 6 : Provider will add a module - // Step 7 : User create a DeFi Smart Account - // Step 8 : User open a Vault, put some ether on it and borrow some dai - // Step 9 : User give authorization to gelato to use his DSA on his behalf. - // Step 10 : Provider should whitelist task - - //#region Step 1 Add EUR/USD Maker Medianizer in the PriceOracleResolver - - // PriceOracleResolver is a price feeder aggregator - // You will be able to query price from multiple source through this aggregator - // For the demo we add the ETH/USD Medianizer to the aggregator - // MakerDAO price oracle are called Medianizer - - // await priceOracleResolver.addOracle( - // ORACLE_MAKER_ETH_USD, - // ORACLE_MAKER_ETH_USD_ADDR, - // PRICE_ORACLE_MAKER_PAYLOAD - // ); - - //#endregion - - //#region Step 2 Enable Debt Bridge Connector and Gelato Provider Payment Connector - - // Debt Bridge Connector is used during refinancing of debt - // This Connect help the user to split a position in one protocol. - // to 2 protocol in a safe way. Both debt position will be safe. - - // Gelato Provider Payment Connector is used for paying the provider - // for task execution. So when futur task will be executed, through a self financing - // transaction (user will pay during the execution of the task) task will - // be executed. Improvind user experience. - - await userWallet.sendTransaction({ - to: hre.network.config.InstaMaster, - value: ethers.utils.parseEther("0.1"), - }); - - await hre.network.provider.request({ - method: "hardhat_impersonateAccount", - params: [await instaMaster.getAddress()], - }); - - await instaConnectors - .connect(instaMaster) - .enable(connectGelatoPartialDebtBridgeFromMaker.address); - - await instaConnectors - .connect(instaMaster) - .enable(connectGelatoProviderPayment.address); - - await hre.network.provider.request({ - method: "hardhat_stopImpersonatingAccount", - params: [await instaMaster.getAddress()], - }); - - expect( - await instaConnectors.isConnector([ - connectGelatoPartialDebtBridgeFromMaker.address, - ]) - ).to.be.true; - expect( - await instaConnectors.isConnector([connectGelatoProviderPayment.address]) - ).to.be.true; - - //#endregion - - //#region Step 3 Executor Staking on Gelato - - // For task execution provider will ask a executor to watch the - // blockchain for possible execution autorization given by - // the condition that user choose when submitting the task. - // And if all condition are meet executor will execute the task. - // For safety measure Gelato ask the executor to stake a minimum - // amount. - - await gelatoCore.connect(gelatoExecutorWallet).stakeExecutor({ - value: await gelatoCore.minExecutorStake(), - }); - - expect( - await gelatoCore.isExecutorMinStaked(gelatoExecutorAddress) - ).to.be.true; - - //#endregion - - //#region Step 4 Provider put some fund on gelato for paying future tasks executions - - // Provider put some funds in gelato system for paying the - // Executor when this one will execute task on behalf of the - // Provider. At each provider's task execution, some funds (approximatively - // the gas cost value) will be transfered to the Executor stake. - - const TASK_AUTOMATION_FUNDS = await gelatoCore.minExecProviderFunds( - GAS_LIMIT, - GAS_PRICE_CEIL - ); - - await expect( - gelatoCore - .connect(gelatoProviderWallet) - .provideFunds(gelatoProviderAddress, { - value: TASK_AUTOMATION_FUNDS, - }) - ).to.emit(gelatoCore, "LogFundsProvided"); - - expect(await gelatoCore.providerFunds(gelatoProviderAddress)).to.be.equal( - TASK_AUTOMATION_FUNDS - ); - - //#endregion - - //#region Step 5 Provider choose a executor - - // Provider choose a executor who will execute futur task - // for the provider, it will be compensated by the provider. - - await expect( - gelatoCore - .connect(gelatoProviderWallet) - .providerAssignsExecutor(gelatoExecutorAddress) - ).to.emit(gelatoCore, "LogProviderAssignedExecutor"); - - expect( - await gelatoCore.executorByProvider(gelatoProviderAddress) - ).to.be.equal(gelatoExecutorAddress); - - //#endregion - - //#region Step 6 Provider will add a module - - // By adding a module the provider will format future task's - // payload by adding some specificity like his address to the - // Payment connector for receiving payment of User. - - await expect( - gelatoCore - .connect(gelatoProviderWallet) - .addProviderModules([dsaProviderModule.address]) - ).to.emit(gelatoCore, "LogProviderModuleAdded"); - - expect( - await gelatoCore - .connect(gelatoProviderWallet) - .isModuleProvided(gelatoProviderAddress, dsaProviderModule.address) - ).to.be.true; - - //#endregion - - //#region Step 7 User create a DeFi Smart Account - - // User create a Instadapp DeFi Smart Account - // who give him the possibility to interact - // with a large list of DeFi protocol through one - // Proxy 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) - ); - - //#endregion - - //#region Step 8 User open a Vault, put some ether on it and borrow some dai - - // User open a maker vault - // He deposit 10 Eth on it - // He borrow a 1000 DAI - - const openVault = await hre.run("abi-encode-withselector", { - abi: ConnectMaker.abi, - functionname: "open", - inputs: ["ETH-A"], - }); - - await dsa.cast([hre.network.config.ConnectMaker], [openVault], userAddress); - - const cdps = await getCdps.getCdpsAsc(dssCdpManager.address, dsa.address); - vaultId = String(cdps.ids[0]); - expect(cdps.ids[0].isZero()).to.be.false; - - await dsa.cast( - [hre.network.config.ConnectMaker], - [ - await hre.run("abi-encode-withselector", { - abi: ConnectMaker.abi, - functionname: "deposit", - inputs: [vaultId, MAKER_INITIAL_ETH, 0, 0], - }), - ], - userAddress, - { - value: MAKER_INITIAL_ETH, - } - ); - - await dsa.cast( - [hre.network.config.ConnectMaker], - [ - await hre.run("abi-encode-withselector", { - abi: ConnectMaker.abi, - functionname: "borrow", - inputs: [vaultId, MAKER_INITIAL_DEBT, 0, 0], - }), - ], - userAddress - ); - - expect(await DAI.balanceOf(dsa.address)).to.be.equal(MAKER_INITIAL_DEBT); - - //#endregion - - //#region Step 9 User give authorization to gelato to use his DSA on his behalf. - - // Instadapp DSA contract give the possibility to the user to delegate - // action by giving authorization. - // In this case user give authorization to gelato to execute - // task for him if needed. - - await dsa.cast( - [hre.network.config.ConnectAuth], - [ - await hre.run("abi-encode-withselector", { - abi: ConnectAuth.abi, - functionname: "add", - inputs: [gelatoCore.address], - }), - ], - userAddress - ); - - expect(await dsa.isAuth(gelatoCore.address)).to.be.true; - - //#endregion - - //#region Step 10 Provider should whitelist task - - // By WhiteList task, the provider can constrain the type - // of task the user can submitting. - - //#region Actions - - const debtBridgeCalculation = new GelatoCoreLib.Action({ - addr: connectGelatoPartialDebtBridgeFromMaker.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectGelatoFullDebtBridgeFromMakerABI, - functionname: "savePartialRefinanceDataToMemory", - inputs: [ - vaultId, - MIN_COL_RATIO_MAKER, - MIN_COL_RATIO_B, - priceOracleResolver.address, - await hre.run("abi-encode-withselector", { - abi: (await deployments.getArtifcat("PriceOracleResolver")).abi, - functionname: "getMockPrice", - inputs: [userAddress], - }), - 0, - 0, - ], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(debtBridgeCalculation); - - const flashBorrow = new GelatoCoreLib.Action({ - addr: connectInstaPool.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectInstaPool.abi, - functionname: "flashBorrow", - inputs: [hre.network.config.DAI, 0, "600", 0], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(flashBorrow); - - const paybackMaker = new GelatoCoreLib.Action({ - addr: connectMaker.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectMaker.abi, - functionname: "payback", - inputs: [vaultId, 0, "601", 0], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(paybackMaker); - - const withdrawMaker = new GelatoCoreLib.Action({ - addr: connectMaker.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectMaker.abi, - functionname: "withdraw", - inputs: [vaultId, 0, "602", 0], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(withdrawMaker); - - const depositCompound = new GelatoCoreLib.Action({ - addr: connectCompound.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectCompound.abi, - functionname: "deposit", - inputs: [ETH, 0, "603", 0], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(depositCompound); - - const borrowCompound = new GelatoCoreLib.Action({ - addr: connectCompound.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectCompound.abi, - functionname: "borrow", - inputs: [hre.network.config.DAI, 0, "604", 0], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(borrowCompound); - - const flashPayBack = new GelatoCoreLib.Action({ - addr: connectInstaPool.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectInstaPool.abi, - functionname: "flashPayback", - inputs: [hre.network.config.DAI, 0, 0], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(flashPayBack); - - const payProvider = new GelatoCoreLib.Action({ - addr: connectGelatoProviderPayment.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectGelatoProviderPaymentABI, - functionname: "payProvider", - inputs: [gelatoProviderAddress, ETH, 0, "605", 0], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(payProvider); - - const gasPriceCeil = ethers.constants.MaxUint256; - - const connectGelatoFullDebtBridgeFromMakerTaskSpec = new GelatoCoreLib.TaskSpec( - { - conditions: [conditionMakerVaultUnsafe.address], - actions: spells, - gasPriceCeil, - } - ); - - await expect( - gelatoCore - .connect(gelatoProviderWallet) - .provideTaskSpecs([connectGelatoFullDebtBridgeFromMakerTaskSpec]) - ).to.emit(gelatoCore, "LogTaskSpecProvided"); - - expect( - await gelatoCore - .connect(gelatoProviderWallet) - .isTaskSpecProvided( - gelatoProviderAddress, - connectGelatoFullDebtBridgeFromMakerTaskSpec - ) - ).to.be.equal("OK"); - - expect( - await gelatoCore - .connect(gelatoProviderWallet) - .taskSpecGasPriceCeil( - gelatoProviderAddress, - await gelatoCore - .connect(gelatoProviderWallet) - .hashTaskSpec(connectGelatoFullDebtBridgeFromMakerTaskSpec) - ) - ).to.be.equal(gasPriceCeil); - - //#endregion - - //#endregion - }); - - it("#1: Use Maker Compound refinancing if the maker vault become unsafe after a market move.", async function () { - // User Actions - // Step 1: User submit a Debt Refinancing task if market move against him - // Step 2: Market Move against the user (Mock) - // Step 3: Executor execute the user's task - - //#region Step 1 User submit a Debt Refinancing task if market move against him - - // User submit the refinancing task if market move against him. - // So in this case if the maker vault go to the unsafe area - // the refinancing task will be executed and the position - // will be split on two position on maker and compound. - // It will be done through a algorithm that will optimize the - // total borrow rate. - - const conditionMakerVaultUnsafeObj = new GelatoCoreLib.Condition({ - inst: conditionMakerVaultUnsafe.address, - data: await conditionMakerVaultUnsafe.getConditionData( - vaultId, - priceOracleResolver.address, - await hre.run("abi-encode-withselector", { - abi: (await deployments.getArtifact("PriceOracleResolver")).abi, - functionname: "getMockPrice", - inputs: [userAddress], - }), - MIN_COL_RATIO_MAKER - ), - }); - - // ======= GELATO TASK SETUP ====== - const refinanceIfVaultUnsafe = new GelatoCoreLib.Task({ - conditions: [conditionMakerVaultUnsafeObj], - actions: spells, - }); - - const gelatoExternalProvider = new GelatoCoreLib.GelatoProvider({ - addr: gelatoProviderAddress, - module: dsaProviderModule.address, - }); - - const expiryDate = 0; - - await expect( - dsa.cast( - [connectGelato.address], // targets - [ - await hre.run("abi-encode-withselector", { - abi: ConnectGelato.abi, - functionname: "submitTask", - inputs: [ - gelatoExternalProvider, - refinanceIfVaultUnsafe, - expiryDate, - ], - }), - ], // datas - userAddress, // origin - { - gasLimit: 5000000, - } - ) - ).to.emit(gelatoCore, "LogTaskSubmitted"); - - const taskReceipt = new GelatoCoreLib.TaskReceipt({ - id: await gelatoCore.currentTaskReceiptId(), - userProxy: dsa.address, - provider: gelatoExternalProvider, - tasks: [refinanceIfVaultUnsafe], - expiryDate, - }); - - //#endregion - - //#region Step 2 Market Move against the user (Mock) - - // Ether market price went from the current price to 250$ - - const gelatoGasPrice = await hre.run("fetchGelatoGasPrice"); - expect(gelatoGasPrice).to.be.lte(GAS_PRICE_CEIL); - - // TO DO: base mock price off of real price data - await priceOracleResolver.setMockPrice(ethers.utils.parseUnits("400", 18)); - - expect( - await gelatoCore - .connect(gelatoExecutorWallet) - .canExec(taskReceipt, GAS_LIMIT, gelatoGasPrice) - ).to.be.equal("ConditionNotOk:MakerVaultNotUnsafe"); - - // TO DO: base mock price off of real price data - await priceOracleResolver.setMockPrice(ethers.utils.parseUnits("250", 18)); - - expect( - await gelatoCore - .connect(gelatoExecutorWallet) - .canExec(taskReceipt, GAS_LIMIT, gelatoGasPrice) - ).to.be.equal("OK"); - - //#endregion - - //#region Step 3 Executor execute the user's task - - // The market move make the vault unsafe, so the executor - // will execute the user's task to make the user position safe - // by a debt refinancing in compound. - - //#region EXPECTED OUTCOME - - const latestPrice = await priceOracleResolver.getMockPrice(userAddress); - const gasFeesPaidFromCol = ethers.utils - .parseUnits(String(1933090 + 19331 * 2), 0) - .mul(gelatoGasPrice); - const debtOnMakerBefore = await connectGelatoPartialDebtBridgeFromMaker.getMakerVaultDebt( - vaultId - ); - const pricedCollateral = wmul( - ( - await connectGelatoPartialDebtBridgeFromMaker.getMakerVaultCollateralBalance( - vaultId - ) - ).sub(gasFeesPaidFromCol), - latestPrice - ); - - const expectedColWithdrawAmount = wCalcCollateralToWithdraw( - MIN_COL_RATIO_MAKER, - MIN_COL_RATIO_B, - latestPrice, - pricedCollateral, - debtOnMakerBefore - ); - - const expectedBorAmountToPayBack = wCalcDebtToRepay( - MIN_COL_RATIO_MAKER, - MIN_COL_RATIO_B, - pricedCollateral, - debtOnMakerBefore - ); - - //console.log(String(wdiv(pricedCollateral.sub(wmul(expectedColWithdrawAmount, latestPrice).add(gasFeesPaidFromCol)),debt.sub(expectedBorAmountToPayBack)))); - - //#endregion - const providerBalanceBeforeExecution = await gelatoProviderWallet.getBalance(); - - await expect( - gelatoCore.connect(gelatoExecutorWallet).exec(taskReceipt, { - gasPrice: gelatoGasPrice, // Exectutor must use gelatoGasPrice (Chainlink fast gwei) - gasLimit: GAS_LIMIT, - }) - ).to.emit(gelatoCore, "LogExecSuccess"); - - // 🚧 For Debugging: - // const txResponse2 = await gelatoCore - // .connect(gelatoProviderWallet) - // .exec(taskReceipt, { - // gasPrice: gelatoGasPrice, - // gasLimit: GAS_LIMIT, - // }); - // const {blockHash} = await txResponse2.wait(); - // const logs = await ethers.provider.getLogs({blockHash}); - // const iFace = new ethers.utils.Interface(GelatoCoreLib.GelatoCore.abi); - // for (const log of logs) { - // console.log(iFace.parseLog(log).args.reason); - // } - // await GelatoCoreLib.sleep(10000); - - expect(await gelatoProviderWallet.getBalance()).to.be.gt( - providerBalanceBeforeExecution - ); - - // compound position of DSA on cDai and cEth - const compoundPosition = await compoundResolver.getCompoundData( - dsa.address, - [cDaiToken.address, cEthToken.address] - ); - - // https://compound.finance/docs/ctokens#exchange-rate - // calculate cEth/ETH rate to convert back cEth to ETH - // for comparing with the withdrew Ether to the deposited one. - const exchangeRateCethToEth = (await cEthToken.getCash()) - .add(await cEthToken.totalBorrows()) - .sub(await cEthToken.totalReserves()) - .div(await cEthToken.totalSupply()); - - // Estimated amount to borrowed token should be equal to the actual one read on compound contracts - expect(expectedBorAmountToPayBack).to.be.equal( - compoundPosition[0].borrowBalanceStoredUser - ); - - // Estimated amount of pricedCollateral should be equal to the actual one read on compound contracts - expect( - expectedColWithdrawAmount.sub( - compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth) - ) - ).to.be.lt(ethers.utils.parseUnits("1", 12)); - - const debtOnMakerAfter = await connectGelatoPartialDebtBridgeFromMaker.getMakerVaultDebt( - vaultId - ); - const collateralOnMakerAfter = await connectGelatoPartialDebtBridgeFromMaker.getMakerVaultCollateralBalance( - vaultId - ); // in Ether. - - // Total Borrowed Amount on both protocol should equal to the initial borrowed amount on maker vault. - expect( - debtOnMakerAfter - .add(compoundPosition[0].borrowBalanceStoredUser) - .sub(MAKER_INITIAL_DEBT) - ).to.be.lte(ethers.utils.parseUnits("1", 0)); - // Total Ether col on Maker and Compound (+ gasFeesPaidFromCol) should equal to the initial col on maker vault - expect( - compoundPosition[1].balanceOfUser - .mul(exchangeRateCethToEth) - .add(gasFeesPaidFromCol) - .add(collateralOnMakerAfter) - .sub(ethers.utils.parseEther("10")) - ).to.be.lt(ethers.utils.parseUnits("1", 12)); - - // Check Collaterization Ratio of Maker and Compound - expect( - wdiv( - wmul( - compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth), - latestPrice - ), - compoundPosition[0].borrowBalanceStoredUser - ).sub(MIN_COL_RATIO_MAKER) - ).to.be.lt(ethers.utils.parseUnits("1", 12)); - expect( - wdiv( - wmul( - collateralOnMakerAfter, - await priceOracleResolver.getMockPrice(userAddress) - ), - debtOnMakerAfter - ).sub(MIN_COL_RATIO_MAKER) - ).to.be.lt(ethers.utils.parseUnits("1", 1)); - - // DSA contain 1000 DAI - expect(await DAI.balanceOf(dsa.address)).to.be.equal(MAKER_INITIAL_DEBT); - - //#endregion - }); -});