diff --git a/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol b/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol index dc54e66..7592df1 100644 --- a/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol +++ b/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol @@ -38,12 +38,22 @@ contract ConnectGelatoProviderPayment is uint256 internal immutable _id; address internal immutable _this; - constructor(uint256 id, address _gelatoProvider) { + constructor(uint256 id, address _gelatoProvider) + noAddressZeroProvider(_gelatoProvider) + { _id = id; _this = address(this); gelatoProvider = _gelatoProvider; } + modifier noAddressZeroProvider(address _gelatoProvider) { + require( + _gelatoProvider != address(0x0), + "ConnectGelatoProviderPayment.noAddressZeroProvider" + ); + _; + } + /// @dev Connector Details function connectorID() external @@ -55,7 +65,12 @@ contract ConnectGelatoProviderPayment is } /// @notice Set the gelatoProvider address that will be paid for executing a task - function setProvider(address _gelatoProvider) external override onlyOwner { + function setProvider(address _gelatoProvider) + external + override + onlyOwner + noAddressZeroProvider(_gelatoProvider) + { gelatoProvider = _gelatoProvider; } @@ -77,10 +92,6 @@ contract ConnectGelatoProviderPayment is ) external payable override { address provider = IConnectGelatoProviderPayment(_this) .gelatoProvider(); - require( - provider != address(0x0), - "ConnectGelatoProviderPayment.payProvider:!provider" - ); uint256 amt = _getUint(_getId, _amt); _setUint(_setId, amt); diff --git a/deploy/connectors/ConnectGelatoDataForFullRefinance.deploy.js b/deploy/connectors/ConnectGelatoDataForFullRefinance.deploy.js index bdfdd3b..b7f5743 100644 --- a/deploy/connectors/ConnectGelatoDataForFullRefinance.deploy.js +++ b/deploy/connectors/ConnectGelatoDataForFullRefinance.deploy.js @@ -1,3 +1,6 @@ +const hre = require("hardhat"); +const {ethers} = hre; + const {sleep} = require("@gelatonetwork/core"); const InstaConnector = require("../../pre-compiles/InstaConnectors.json"); @@ -32,6 +35,34 @@ module.exports = async (hre) => { gasPrice: hre.network.config.gasPrice, log: hre.network.name === "mainnet" ? true : false, }); + + if (hre.network.name === "hardhat") { + const deployerWallet = await ethers.provider.getSigner(deployer); + const instaMaster = await ethers.provider.getSigner( + hre.network.config.InstaMaster + ); + + await deployerWallet.sendTransaction({ + to: await instaMaster.getAddress(), + value: ethers.utils.parseEther("0.1"), + }); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [await instaMaster.getAddress()], + }); + + await instaConnectors + .connect(instaMaster) + .enable( + (await ethers.getContract("ConnectGelatoDataForFullRefinance")).address + ); + + await hre.network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [await instaMaster.getAddress()], + }); + } }; module.exports.dependencies = ["ConnectGelatoProviderPayment"]; diff --git a/deploy/connectors/ConnectGelatoDataForPartialRefinance.deploy.js b/deploy/connectors/ConnectGelatoDataForPartialRefinance.deploy.js index 93c036e..4b56a66 100644 --- a/deploy/connectors/ConnectGelatoDataForPartialRefinance.deploy.js +++ b/deploy/connectors/ConnectGelatoDataForPartialRefinance.deploy.js @@ -1,3 +1,6 @@ +const hre = require("hardhat"); +const {ethers} = hre; + const {sleep} = require("@gelatonetwork/core"); const InstaConnector = require("../../pre-compiles/InstaConnectors.json"); @@ -32,6 +35,35 @@ module.exports = async (hre) => { gasPrice: hre.network.config.gasPrice, log: hre.network.name === "mainnet" ? true : false, }); + + if (hre.network.name === "hardhat") { + const deployerWallet = await ethers.provider.getSigner(deployer); + const instaMaster = await ethers.provider.getSigner( + hre.network.config.InstaMaster + ); + + await deployerWallet.sendTransaction({ + to: await instaMaster.getAddress(), + value: ethers.utils.parseEther("0.1"), + }); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [await instaMaster.getAddress()], + }); + + await instaConnectors + .connect(instaMaster) + .enable( + (await ethers.getContract("ConnectGelatoDataForPartialRefinance")) + .address + ); + + await hre.network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [await instaMaster.getAddress()], + }); + } }; module.exports.skip = async (hre) => { diff --git a/deploy/connectors/ConnectGelatoProviderPayment.deploy.js b/deploy/connectors/ConnectGelatoProviderPayment.deploy.js index 468ee00..bd9f3b7 100644 --- a/deploy/connectors/ConnectGelatoProviderPayment.deploy.js +++ b/deploy/connectors/ConnectGelatoProviderPayment.deploy.js @@ -1,3 +1,6 @@ +const hre = require("hardhat"); +const {ethers} = hre; + const {sleep} = require("@gelatonetwork/core"); const InstaConnector = require("../../pre-compiles/InstaConnectors.json"); @@ -28,6 +31,34 @@ module.exports = async (hre) => { gasPrice: hre.network.config.gasPrice, log: hre.network.name === "mainnet" ? true : false, }); + + if (hre.network.name === "hardhat") { + const deployerWallet = await ethers.provider.getSigner(deployer); + const instaMaster = await ethers.provider.getSigner( + hre.network.config.InstaMaster + ); + + await deployerWallet.sendTransaction({ + to: await instaMaster.getAddress(), + value: ethers.utils.parseEther("0.1"), + }); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [await instaMaster.getAddress()], + }); + + await instaConnectors + .connect(instaMaster) + .enable( + (await ethers.getContract("ConnectGelatoProviderPayment")).address + ); + + await hre.network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [await instaMaster.getAddress()], + }); + } }; module.exports.tags = ["ConnectGelatoProviderPayment"]; diff --git a/hardhat.config.js b/hardhat.config.js index 87647cd..6cdcfd2 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -3,6 +3,7 @@ const {task, types} = require("hardhat/config"); require("@nomiclabs/hardhat-ethers"); require("@nomiclabs/hardhat-waffle"); require("hardhat-deploy"); +require("hardhat-deploy-ethers"); // Libraries const assert = require("assert"); @@ -31,6 +32,9 @@ module.exports = { default: 0, mainnet: DEPLOYER, }, + user: { + default: 0, + }, gelatoProvider: { default: 1, mainnet: DEPLOYER, diff --git a/package.json b/package.json index 4250775..9edda41 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "lint": "eslint --cache . && yarn lint:sol", "lint:sol": "solhint 'contracts/**/*.sol'", "lint:fix": "eslint --cache --fix . && solhint --fix contracts/**/*.sol", - "test": "yarn compile && npx hardhat test", + "test": "npx hardhat test", "debug": "DEBUG=true yarn compile && npx hardhat test" }, "devDependencies": { @@ -27,6 +27,7 @@ "ethers": "5.0.19", "hardhat": "2.0.2", "hardhat-deploy": "0.7.0-beta.28", + "hardhat-deploy-ethers": "^0.3.0-beta.5", "husky": ">=4", "lint-staged": "10.5.1", "prettier": "2.1.2", diff --git a/test/1_mv-DAI-DSR-Compound.test.js b/test/1_mv-DAI-DSR-Compound.test.js index cb9dfda..fa33398 100644 --- a/test/1_mv-DAI-DSR-Compound.test.js +++ b/test/1_mv-DAI-DSR-Compound.test.js @@ -2,15 +2,13 @@ // => only dependency we need is "chai" const {expect} = require("chai"); const hre = require("hardhat"); -const {ethers} = hre; +const {deployments, ethers} = hre; + const GelatoCoreLib = require("@gelatonetwork/core"); //const { sleep } = GelatoCoreLib; // Constants const DAI_100 = ethers.utils.parseUnits("100", 18); -const APY_2_PERCENT_IN_SECONDS = ethers.BigNumber.from( - "1000000000627937192491029810" -); // Contracts const InstaIndex = require("../pre-compiles/InstaIndex.json"); @@ -48,6 +46,9 @@ describe("Move DAI lending from DSR to Compound", function () { let conditionCompareUints; before(async function () { + // Reset back to a fresh forked state during runtime + await deployments.fixture(); + // Get Test Wallet for local testnet [userWallet] = await ethers.getSigners(); userAddress = await userWallet.getAddress(); @@ -107,21 +108,14 @@ describe("Move DAI lending from DSR to Compound", function () { ); expect(await dsa.isAuth(gelatoCore.address)).to.be.true; - // Deploy Mocks for Testing - const MockCDAI = await ethers.getContractFactory("MockCDAI"); - mockCDAI = await MockCDAI.deploy(APY_2_PERCENT_IN_SECONDS); - await mockCDAI.deployed(); + // Deployed Mocks for Testing + mockCDAI = await ethers.getContract("MockCDAI"); + mockDSR = await ethers.getContract("MockDSR"); - const MockDSR = await ethers.getContractFactory("MockDSR"); - mockDSR = await MockDSR.deploy(APY_2_PERCENT_IN_SECONDS); - await mockDSR.deployed(); - - // Deploy Gelato Conditions for Testing - const ConditionCompareUintsFromTwoSources = await ethers.getContractFactory( + // Deployed Gelato Conditions for Testing + conditionCompareUints = await ethers.getContract( "ConditionCompareUintsFromTwoSources" ); - conditionCompareUints = await ConditionCompareUintsFromTwoSources.deploy(); - await conditionCompareUints.deployed(); // ===== Dapp Dependencies SETUP ================== // This test assumes our user has 100 DAI deposited in Maker DSR @@ -351,7 +345,7 @@ describe("Move DAI lending from DSR to Compound", function () { ); // Let's first check if our Task is executable. Since both MockDSR and MockCDAI - // start with a normalized per second rate of APY_2_PERCENT_IN_SECONDS + // are deployed with a normalized per second rate of APY_2_PERCENT_IN_SECONDS // (1000000000627937192491029810 in 10**27 precision) in both of them, we // expect ConditionNotOk because ANotGreaterOrEqualToBbyMinspread. // Check out contracts/ConditionCompareUintsFromTwoSources.sol to see how diff --git a/test/2_Full-Debt-Bridge-Maker-Compound.test.js b/test/2_Full-Debt-Bridge-Maker-Compound.test.js index 13851bd..d50651e 100644 --- a/test/2_Full-Debt-Bridge-Maker-Compound.test.js +++ b/test/2_Full-Debt-Bridge-Maker-Compound.test.js @@ -1,9 +1,10 @@ const {expect} = require("chai"); const hre = require("hardhat"); -const {ethers} = hre; +const {deployments, ethers} = hre; + const GelatoCoreLib = require("@gelatonetwork/core"); -const makerToCompoundSetup = require("./helpers/Full-Refinance-Maker-To-Compound.helper"); +const setupFullRefinanceMakerToCompound = require("./helpers/setupFullRefinanceMakerToCompound"); // This test showcases how to submit a task refinancing a Users debt position from // Maker to Compound using Gelato @@ -29,7 +30,9 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () let taskReceipt; before(async function () { - const result = await makerToCompoundSetup(); + await deployments.fixture(); + + const result = await setupFullRefinanceMakerToCompound(); wallets = result.wallets; contracts = result.contracts; vaultId = result.vaultId; @@ -79,7 +82,7 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () vaultId, contracts.priceOracleResolver.address, await hre.run("abi-encode-withselector", { - abi: ABI.PriceOracleResolverABI, + abi: (await deployments.getArtifact("PriceOracleResolver")).abi, functionname: "getMockPrice", inputs: [wallets.userAddress], }), @@ -94,7 +97,7 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () }); const gelatoExternalProvider = new GelatoCoreLib.GelatoProvider({ - addr: wallets.providerAddress, // Gelato Provider Address + addr: wallets.gelatoProviderAddress, // Gelato Provider Address module: contracts.dsaProviderModule.address, // Gelato DSA module }); @@ -154,7 +157,7 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () expect( await contracts.gelatoCore - .connect(wallets.executorWallet) + .connect(wallets.gelatoExecutorWallet) .canExec(taskReceipt, constants.GAS_LIMIT, gelatoGasPrice) ).to.be.equal("ConditionNotOk:MakerVaultNotUnsafe"); @@ -165,7 +168,7 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () expect( await contracts.gelatoCore - .connect(wallets.executorWallet) + .connect(wallets.gelatoExecutorWallet) .canExec(taskReceipt, constants.GAS_LIMIT, gelatoGasPrice) ).to.be.equal("OK"); @@ -193,19 +196,21 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () //#endregion const providerBalanceBeforeExecution = await contracts.gelatoCore.providerFunds( - wallets.providerAddress + wallets.gelatoProviderAddress ); await expect( - contracts.gelatoCore.connect(wallets.executorWallet).exec(taskReceipt, { - gasPrice: gelatoGasPrice, // Exectutor must use gelatoGasPrice (Chainlink fast gwei) - gasLimit: constants.GAS_LIMIT, - }) + contracts.gelatoCore + .connect(wallets.gelatoExecutorWallet) + .exec(taskReceipt, { + gasPrice: gelatoGasPrice, // Exectutor must use gelatoGasPrice (Chainlink fast gwei) + gasLimit: constants.GAS_LIMIT, + }) ).to.emit(contracts.gelatoCore, "LogExecSuccess"); // 🚧 For Debugging: // const txResponse2 = await contracts.gelatoCore - // .connect(wallets.executorWallet) + // .connect(wallets.gelatoExecutorWallet) // .exec(taskReceipt, { // gasPrice: gelatoGasPrice, // gasLimit: constants.GAS_LIMIT, @@ -219,7 +224,7 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () // await GelatoCoreLib.sleep(10000); expect( - await contracts.gelatoCore.providerFunds(wallets.providerAddress) + await contracts.gelatoCore.providerFunds(wallets.gelatoProviderAddress) ).to.be.gt( providerBalanceBeforeExecution.sub( gasFeesPaidFromCol diff --git a/test/3_Full-Debt-Bridge-ETHA-ETHB.test.js b/test/3_Full-Debt-Bridge-ETHA-ETHB.test.js index aff0bc9..4204190 100644 --- a/test/3_Full-Debt-Bridge-ETHA-ETHB.test.js +++ b/test/3_Full-Debt-Bridge-ETHA-ETHB.test.js @@ -1,9 +1,9 @@ const {expect} = require("chai"); const hre = require("hardhat"); -const {ethers} = hre; +const {deployments, ethers} = hre; const GelatoCoreLib = require("@gelatonetwork/core"); -const makerETHAToMakerETHBSetup = require("./helpers/Full-Refinance-Maker-To-Maker.helper"); +const setupFullRefinanceMakerToMaker = require("./helpers/setupFullRefinanceMakerToMaker"); // This test showcases how to submit a task refinancing a Users debt position from // Maker to Compound using Gelato @@ -30,8 +30,9 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { before(async function () { // Reset back to a fresh forked state during runtime - await hre.run("hardhatReset"); - const result = await makerETHAToMakerETHBSetup(); + await deployments.fixture(); + + const result = await setupFullRefinanceMakerToMaker(); wallets = result.wallets; contracts = result.contracts; @@ -83,7 +84,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { vaultAId, contracts.priceOracleResolver.address, await hre.run("abi-encode-withselector", { - abi: ABI.PriceOracleResolverABI, + abi: (await deployments.getArtifact("PriceOracleResolver")).abi, functionname: "getMockPrice", inputs: [wallets.userAddress], }), @@ -98,7 +99,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { }); const gelatoExternalProvider = new GelatoCoreLib.GelatoProvider({ - addr: wallets.providerAddress, // Gelato Provider Address + addr: wallets.gelatoProviderAddress, // Gelato Provider Address module: contracts.dsaProviderModule.address, // Gelato DSA module }); @@ -159,7 +160,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { expect( await contracts.gelatoCore - .connect(wallets.executorWallet) + .connect(wallets.gelatoExecutorWallet) .canExec(taskReceipt, constants.GAS_LIMIT, gelatoGasPrice) ).to.be.equal("ConditionNotOk:MakerVaultNotUnsafe"); @@ -170,7 +171,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { expect( await contracts.gelatoCore - .connect(wallets.executorWallet) + .connect(wallets.gelatoExecutorWallet) .canExec(taskReceipt, constants.GAS_LIMIT, gelatoGasPrice) ).to.be.equal("OK"); @@ -197,19 +198,21 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { //#endregion const providerBalanceBeforeExecution = await contracts.gelatoCore.providerFunds( - wallets.providerAddress + wallets.gelatoProviderAddress ); await expect( - contracts.gelatoCore.connect(wallets.executorWallet).exec(taskReceipt, { - gasPrice: gelatoGasPrice, // Exectutor must use gelatoGasPrice (Chainlink fast gwei) - gasLimit: constants.GAS_LIMIT, - }) + contracts.gelatoCore + .connect(wallets.gelatoExecutorWallet) + .exec(taskReceipt, { + gasPrice: gelatoGasPrice, // Exectutor must use gelatoGasPrice (Chainlink fast gwei) + gasLimit: constants.GAS_LIMIT, + }) ).to.emit(contracts.gelatoCore, "LogExecSuccess"); // 🚧 For Debugging: // const txResponse2 = await contracts.gelatoCore - // .connect(wallets.executorWallet) + // .connect(wallets.gelatoExecutorWallet) // .exec(taskReceipt, { // gasPrice: gelatoGasPrice, // gasLimit: constants.GAS_LIMIT, @@ -237,7 +240,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { ); expect( - await contracts.gelatoCore.providerFunds(wallets.providerAddress) + await contracts.gelatoCore.providerFunds(wallets.gelatoProviderAddress) ).to.be.gt( providerBalanceBeforeExecution.sub( gasFeesPaidFromCol diff --git a/test/helpers/Full-Refinance-Maker-To-Compound.helper.js b/test/helpers/Full-Refinance-Maker-To-Compound.helper.js deleted file mode 100644 index ffaa5b1..0000000 --- a/test/helpers/Full-Refinance-Maker-To-Compound.helper.js +++ /dev/null @@ -1,154 +0,0 @@ -const {expect} = require("chai"); -const hre = require("hardhat"); -const {ethers} = hre; -const GelatoCoreLib = require("@gelatonetwork/core"); - -const executorDoStaking = require("./setups/Doing-Staking-As-Executor.helper"); -const providerDoFunding = require("./setups/Doing-Funding-As-Provider.helper"); -const providerAddCustomModuleForPayment = require("./setups/Adding-Custom-Module-As-Provider.helper"); -const providerChooseExecutor = require("./setups/Choosing-Executor-As-Provider.helper"); -const userCreateADSA = require("./setups/Creating-DSA-As-User.helper"); -const userOpenDepositBorrowOnMakerVault = require("./setups/Open-Deposit-Borrow-On-Maker-As-User.helper"); -const getWallets = require("./setups/Wallets.helper"); -const getConstants = require("./setups/Constants.helper"); -const getABI = require("./setups/ABI.helper"); -const getAllContracts = require("./setups/Contracts-For-Full-Refinancing.helper"); -const enableGelatoConnectorsForFromMaker = require("./setups/Enabling-New-Connectors-For-Full-Refinance.helper"); - -const ConnectGelatoDataForFullRefinanceABI = require("../../artifacts/contracts/contracts/connectors/ConnectGelatoDataForFullRefinance.sol/ConnectGelatoDataForFullRefinance.json") - .abi; - -async function makerToCompoundSetup() { - const wallets = await getWallets(); - const contracts = await getAllContracts(wallets.providerAddress); - const constants = await getConstants(); - let vaultId; - // Gelato Testing environment setup. - await enableGelatoConnectorsForFromMaker( - wallets.userWallet, - contracts.connectGelatoProviderPayment.address, - contracts.connectGelatoData.address, - contracts.instaMaster, - contracts.instaConnectors - ); - await executorDoStaking(wallets.executorWallet, contracts.gelatoCore); - await providerDoFunding( - wallets.providerWallet, - contracts.gelatoCore, - constants.GAS_LIMIT, - constants.GAS_PRICE_CEIL - ); - await providerChooseExecutor( - wallets.providerWallet, - wallets.executorAddress, - contracts.gelatoCore - ); - await providerAddCustomModuleForPayment( - wallets.providerWallet, - contracts.gelatoCore, - contracts.dsaProviderModule.address - ); - contracts.dsa = await userCreateADSA( - wallets.userAddress, - contracts.instaIndex, - contracts.instaList - ); - vaultId = await userOpenDepositBorrowOnMakerVault( - wallets.userAddress, - contracts.DAI, - contracts.dsa, - contracts.getCdps, - contracts.dssCdpManager, - constants.MAKER_INITIAL_ETH, - constants.MAKER_INITIAL_DEBT - ); - - let ABI = await getABI(); - - const spells = await providerWhiteListTaskForMakerToCompound( - wallets, - contracts, - constants, - vaultId - ); - - return { - wallets, - contracts, - vaultId, - spells, - constants, - ABI, - }; -} - -// Instadapp UI should do the same implementation for submitting debt bridge task -async function providerWhiteListTaskForMakerToCompound( - wallets, - contracts, - constants, - vaultId -) { - //#region Step 9 Provider should whitelist task - - // By WhiteList task, the provider can constrain the type - // of task the user can submitting. - - //#region Actions - - const spells = []; - - const debtBridgeCalculationForFullRefinance = new GelatoCoreLib.Action({ - addr: contracts.connectGelatoData.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectGelatoDataForFullRefinanceABI, - functionname: "getDataAndCastForFromMakerToCompound", - inputs: [vaultId, constants.ETH], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(debtBridgeCalculationForFullRefinance); - - const gasPriceCeil = ethers.constants.MaxUint256; - - const connectGelatoFullDebtBridgeFromMakerTaskSpec = new GelatoCoreLib.TaskSpec( - { - conditions: [contracts.conditionMakerVaultUnsafe.address], - actions: spells, - gasPriceCeil, - } - ); - - await expect( - contracts.gelatoCore - .connect(wallets.providerWallet) - .provideTaskSpecs([connectGelatoFullDebtBridgeFromMakerTaskSpec]) - ).to.emit(contracts.gelatoCore, "LogTaskSpecProvided"); - - expect( - await contracts.gelatoCore - .connect(wallets.providerWallet) - .isTaskSpecProvided( - wallets.providerAddress, - connectGelatoFullDebtBridgeFromMakerTaskSpec - ) - ).to.be.equal("OK"); - - expect( - await contracts.gelatoCore - .connect(wallets.providerWallet) - .taskSpecGasPriceCeil( - wallets.providerAddress, - await contracts.gelatoCore - .connect(wallets.providerWallet) - .hashTaskSpec(connectGelatoFullDebtBridgeFromMakerTaskSpec) - ) - ).to.be.equal(gasPriceCeil); - - //#endregion - - return spells; -} - -module.exports = makerToCompoundSetup; diff --git a/test/helpers/Full-Refinance-Maker-To-Maker.helper.js b/test/helpers/Full-Refinance-Maker-To-Maker.helper.js deleted file mode 100644 index 3d5c5a5..0000000 --- a/test/helpers/Full-Refinance-Maker-To-Maker.helper.js +++ /dev/null @@ -1,160 +0,0 @@ -const {expect} = require("chai"); -const hre = require("hardhat"); -const {ethers} = hre; -const GelatoCoreLib = require("@gelatonetwork/core"); - -const executorDoStaking = require("./setups/Doing-Staking-As-Executor.helper"); -const providerDoFunding = require("./setups/Doing-Funding-As-Provider.helper"); -const providerAddCustomModuleForPayment = require("./setups/Adding-Custom-Module-As-Provider.helper"); -const providerChooseExecutor = require("./setups/Choosing-Executor-As-Provider.helper"); -const userCreateADSA = require("./setups/Creating-DSA-As-User.helper"); -const masterAddETHBOnGemJoinMapping = require("./setups/Adding-ETHB-On-InstaMapping-As-InstaMaster.helper"); -const userOpenDepositBorrowOnMakerVault = require("./setups/Open-Deposit-Borrow-On-Maker-As-User.helper"); -const getWallets = require("./setups/Wallets.helper"); -const getConstants = require("./setups/Constants.helper"); -const getABI = require("./setups/ABI.helper"); -const getAllContracts = require("./setups/Contracts-For-Full-Refinancing.helper"); -const enableGelatoConnectorsForFromMaker = require("./setups/Enabling-New-Connectors-For-Full-Refinance.helper"); - -const ConnectGelatoDataForFullRefinanceABI = require("../../artifacts/contracts/contracts/connectors/ConnectGelatoDataForFullRefinance.sol/ConnectGelatoDataForFullRefinance.json") - .abi; - -async function makerETHAToMakerETHBSetup() { - const wallets = await getWallets(); - const contracts = await getAllContracts(wallets.providerAddress); - const constants = await getConstants(); - let vaultAId; - // Gelato Testing environment setup. - await enableGelatoConnectorsForFromMaker( - wallets.userWallet, - contracts.connectGelatoProviderPayment.address, - contracts.connectGelatoData.address, - contracts.instaMaster, - contracts.instaConnectors - ); - await executorDoStaking(wallets.executorWallet, contracts.gelatoCore); - await providerDoFunding( - wallets.providerWallet, - contracts.gelatoCore, - constants.GAS_LIMIT, - constants.GAS_PRICE_CEIL - ); - await providerChooseExecutor( - wallets.providerWallet, - wallets.executorAddress, - contracts.gelatoCore - ); - await providerAddCustomModuleForPayment( - wallets.providerWallet, - contracts.gelatoCore, - contracts.dsaProviderModule.address - ); - contracts.dsa = await userCreateADSA( - wallets.userAddress, - contracts.instaIndex, - contracts.instaList - ); - await masterAddETHBOnGemJoinMapping( - wallets.userWallet, - contracts.instaMapping, - contracts.instaMaster - ); - vaultAId = await userOpenDepositBorrowOnMakerVault( - wallets.userAddress, - contracts.DAI, - contracts.dsa, - contracts.getCdps, - contracts.dssCdpManager, - constants.MAKER_INITIAL_ETH, - constants.MAKER_INITIAL_DEBT - ); - - let ABI = await getABI(); - - const spells = await providerWhiteListTaskForMakerETHAToMakerETHB( - wallets, - contracts, - constants, - vaultAId - ); - - return { - wallets, - contracts, - vaultAId, - spells, - constants, - ABI, - }; -} - -// Instadapp UI should do the same implementation for submitting debt bridge task -async function providerWhiteListTaskForMakerETHAToMakerETHB( - wallets, - contracts, - constants, - vaultId -) { - //#region Step 9 Provider should whitelist task - - // By WhiteList task, the provider can constrain the type - // of task the user can submitting. - - //#region Actions - - const spells = []; - - const debtBridgeCalculationForFullRefinance = new GelatoCoreLib.Action({ - addr: contracts.connectGelatoData.address, - data: await hre.run("abi-encode-withselector", { - abi: ConnectGelatoDataForFullRefinanceABI, - functionname: "getDataAndCastForFromMakerToMaker", - inputs: [vaultId, constants.ETH, "ETH-B"], - }), - operation: GelatoCoreLib.Operation.Delegatecall, - }); - - spells.push(debtBridgeCalculationForFullRefinance); - - const gasPriceCeil = ethers.constants.MaxUint256; - - const connectGelatoFullDebtBridgeFromMakerTaskSpec = new GelatoCoreLib.TaskSpec( - { - conditions: [contracts.conditionMakerVaultUnsafe.address], - actions: spells, - gasPriceCeil, - } - ); - - await expect( - contracts.gelatoCore - .connect(wallets.providerWallet) - .provideTaskSpecs([connectGelatoFullDebtBridgeFromMakerTaskSpec]) - ).to.emit(contracts.gelatoCore, "LogTaskSpecProvided"); - - expect( - await contracts.gelatoCore - .connect(wallets.providerWallet) - .isTaskSpecProvided( - wallets.providerAddress, - connectGelatoFullDebtBridgeFromMakerTaskSpec - ) - ).to.be.equal("OK"); - - expect( - await contracts.gelatoCore - .connect(wallets.providerWallet) - .taskSpecGasPriceCeil( - wallets.providerAddress, - await contracts.gelatoCore - .connect(wallets.providerWallet) - .hashTaskSpec(connectGelatoFullDebtBridgeFromMakerTaskSpec) - ) - ).to.be.equal(gasPriceCeil); - - //#endregion - - return spells; -} - -module.exports = makerETHAToMakerETHBSetup; diff --git a/test/helpers/setups/Adding-ETHB-On-InstaMapping-As-InstaMaster.helper.js b/test/helpers/services/addETHBGemJoinMapping.js similarity index 68% rename from test/helpers/setups/Adding-ETHB-On-InstaMapping-As-InstaMaster.helper.js rename to test/helpers/services/addETHBGemJoinMapping.js index 653cb89..a71d5b1 100644 --- a/test/helpers/setups/Adding-ETHB-On-InstaMapping-As-InstaMaster.helper.js +++ b/test/helpers/services/addETHBGemJoinMapping.js @@ -2,11 +2,7 @@ const {expect} = require("chai"); const hre = require("hardhat"); const {ethers} = hre; -async function masterAddETHBOnGemJoinMapping( - userWallet, - instaMapping, - instaMaster -) { +async function addETHBGemJoinMapping(userWallet, instaMapping, instaMaster) { await userWallet.sendTransaction({ to: hre.network.config.InstaMaster, value: ethers.utils.parseEther("0.1"), @@ -21,6 +17,11 @@ async function masterAddETHBOnGemJoinMapping( await expect( instaMapping.connect(instaMaster).addGemJoinMapping([ethBGemJoin]) ).to.emit(instaMapping, "LogAddGemJoinMapping"); + + await hre.network.provider.request({ + method: "hardhat_stopImpersonatingAccount", + params: [await instaMaster.getAddress()], + }); } -module.exports = masterAddETHBOnGemJoinMapping; +module.exports = addETHBGemJoinMapping; diff --git a/test/helpers/setups/Adding-Custom-Module-As-Provider.helper.js b/test/helpers/services/addProviderModuleDSA.js similarity index 61% rename from test/helpers/setups/Adding-Custom-Module-As-Provider.helper.js rename to test/helpers/services/addProviderModuleDSA.js index 77da5ff..d8d560f 100644 --- a/test/helpers/setups/Adding-Custom-Module-As-Provider.helper.js +++ b/test/helpers/services/addProviderModuleDSA.js @@ -1,7 +1,7 @@ const {expect} = require("chai"); -async function providerAddCustomModuleForPayment( - providerWallet, +async function addProviderModuleDSA( + gelatoProviderWallet, gelatoCore, dsaProviderModuleAddr ) { @@ -11,21 +11,21 @@ async function providerAddCustomModuleForPayment( // payload by adding some specificity like his address to the // Payment connector for receiving payment of User. - let providerAddress = await providerWallet.getAddress(); + const gelatoProviderAddress = await gelatoProviderWallet.getAddress(); await expect( gelatoCore - .connect(providerWallet) + .connect(gelatoProviderWallet) .addProviderModules([dsaProviderModuleAddr]) ).to.emit(gelatoCore, "LogProviderModuleAdded"); expect( await gelatoCore - .connect(providerWallet) - .isModuleProvided(providerAddress, dsaProviderModuleAddr) + .connect(gelatoProviderWallet) + .isModuleProvided(gelatoProviderAddress, dsaProviderModuleAddr) ).to.be.true; //#endregion } -module.exports = providerAddCustomModuleForPayment; +module.exports = addProviderModuleDSA; diff --git a/test/helpers/setups/Creating-DSA-As-User.helper.js b/test/helpers/services/createDSA.js similarity index 82% rename from test/helpers/setups/Creating-DSA-As-User.helper.js rename to test/helpers/services/createDSA.js index 14f7cdb..0e3b095 100644 --- a/test/helpers/setups/Creating-DSA-As-User.helper.js +++ b/test/helpers/services/createDSA.js @@ -4,7 +4,7 @@ const {ethers} = hre; const InstaAccount = require("../../../pre-compiles/InstaAccount.json"); -async function userCreateADSA(userAddress, instaIndex, instaList) { +async function createDSA(userAddress, instaIndex, instaList) { //#region User create a DeFi Smart Account // User create a Instadapp DeFi Smart Account @@ -19,7 +19,7 @@ async function userCreateADSA(userAddress, instaIndex, instaList) { "LogAccountCreated" ); const dsaID = dsaAccountCount.add(1); - await expect(await instaList.accounts()).to.be.equal(dsaID); + expect(await instaList.accounts()).to.be.equal(dsaID); // Instantiate the DSA const dsa = await ethers.getContractAt( @@ -32,4 +32,4 @@ async function userCreateADSA(userAddress, instaIndex, instaList) { //#endregion } -module.exports = userCreateADSA; +module.exports = createDSA; diff --git a/test/helpers/setups/Enabling-New-Connectors-For-Full-Refinance.helper.js b/test/helpers/services/enableGelatoConnectorsForFromMaker.js similarity index 97% rename from test/helpers/setups/Enabling-New-Connectors-For-Full-Refinance.helper.js rename to test/helpers/services/enableGelatoConnectorsForFromMaker.js index b7a9c80..a01a6da 100644 --- a/test/helpers/setups/Enabling-New-Connectors-For-Full-Refinance.helper.js +++ b/test/helpers/services/enableGelatoConnectorsForFromMaker.js @@ -22,7 +22,7 @@ async function enableGelatoConnectorsForFromMaker( await userWallet.sendTransaction({ to: hre.network.config.InstaMaster, - value: ethers.utils.parseEther("0.1"), + value: ethers.utils.parseEther("0.01"), }); await hre.network.provider.request({ diff --git a/test/helpers/services/getABI.js b/test/helpers/services/getABI.js new file mode 100644 index 0000000..47c3f7c --- /dev/null +++ b/test/helpers/services/getABI.js @@ -0,0 +1,12 @@ +const ConnectGelatoABI = require("../../../pre-compiles/ConnectGelato.json") + .abi; +const ConnectAuthABI = require("../../../pre-compiles/ConnectAuth.json").abi; + +function getABI() { + return { + ConnectGelatoABI: ConnectGelatoABI, + ConnectAuthABI: ConnectAuthABI, + }; +} + +module.exports = getABI; diff --git a/test/helpers/setups/Constants.helper.js b/test/helpers/services/getConstants.js similarity index 100% rename from test/helpers/setups/Constants.helper.js rename to test/helpers/services/getConstants.js diff --git a/test/helpers/setups/Common-Contracts.helper.js b/test/helpers/services/getContracts.js similarity index 53% rename from test/helpers/setups/Common-Contracts.helper.js rename to test/helpers/services/getContracts.js index 1cd6ff6..3ff71d9 100644 --- a/test/helpers/setups/Common-Contracts.helper.js +++ b/test/helpers/services/getContracts.js @@ -18,126 +18,93 @@ const CTokenInterface = require("../../../pre-compiles/CTokenInterface.json"); const CompoundResolver = require("../../../pre-compiles/InstaCompoundResolver.json"); const DsaProviderModuleABI = require("../../../pre-compiles/ProviderModuleDsa_ABI.json"); -async function getContracts(providerAddress) { - // 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 instaMapping; - let instaConnectors; - let compoundResolver; - let dsaProviderModule; - // Contracts to deploy and use for local testing - let conditionMakerVaultUnsafe; - let connectGelatoProviderPayment; - let priceOracleResolver; - let connectGelatoData; - let debtBridgeFromMakerForFullRefinance; - - instaMaster = await ethers.provider.getSigner(hre.network.config.InstaMaster); +async function getContracts() { + const instaMaster = await ethers.provider.getSigner( + hre.network.config.InstaMaster + ); // ===== Get Deployed Contract Instance ================== - instaIndex = await ethers.getContractAt( + const instaIndex = await ethers.getContractAt( InstaIndex.abi, hre.network.config.InstaIndex ); - instaMapping = await ethers.getContractAt( + const instaMapping = await ethers.getContractAt( InstaMapping.abi, hre.network.config.InstaMapping ); - instaList = await ethers.getContractAt( + const instaList = await ethers.getContractAt( InstaList.abi, hre.network.config.InstaList ); - connectGelato = await ethers.getContractAt( + const connectGelato = await ethers.getContractAt( ConnectGelato.abi, hre.network.config.ConnectGelato ); - connectMaker = await ethers.getContractAt( + const connectMaker = await ethers.getContractAt( ConnectMaker.abi, hre.network.config.ConnectMaker ); - connectInstaPool = await ethers.getContractAt( + const connectInstaPool = await ethers.getContractAt( ConnectInstaPool.abi, hre.network.config.ConnectInstaPool ); - connectCompound = await ethers.getContractAt( + const connectCompound = await ethers.getContractAt( ConnectCompound.abi, hre.network.config.ConnectCompound ); - dssCdpManager = await ethers.getContractAt( + const 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( + const getCdps = await ethers.getContractAt( + GetCdps.abi, + hre.network.config.GetCdps + ); + const DAI = await ethers.getContractAt(IERC20.abi, hre.network.config.DAI); + const gelatoCore = await ethers.getContractAt( GelatoCoreLib.GelatoCore.abi, hre.network.config.GelatoCore ); - cDaiToken = await ethers.getContractAt( + const cDaiToken = await ethers.getContractAt( CTokenInterface.abi, hre.network.config.CDAI ); - cEthToken = await ethers.getContractAt( + const cEthToken = await ethers.getContractAt( CTokenInterface.abi, hre.network.config.CETH ); - instaConnectors = await ethers.getContractAt( + const instaConnectors = await ethers.getContractAt( InstaConnector.abi, hre.network.config.InstaConnectors ); - compoundResolver = await ethers.getContractAt( + const compoundResolver = await ethers.getContractAt( CompoundResolver.abi, hre.network.config.CompoundResolver ); - dsaProviderModule = await ethers.getContractAt( + const dsaProviderModule = await ethers.getContractAt( DsaProviderModuleABI, hre.network.config.ProviderModuleDsa ); - // ===== Deploy Needed Contract ================== - - const PriceOracleResolver = await ethers.getContractFactory( - "PriceOracleResolver" - ); - priceOracleResolver = await PriceOracleResolver.deploy(); - await priceOracleResolver.deployed(); - - const ConditionMakerVaultUnsafe = await ethers.getContractFactory( + // ===== Get deployed contracts ================== + const priceOracleResolver = await ethers.getContract("PriceOracleResolver"); + const conditionMakerVaultUnsafe = await ethers.getContract( "ConditionMakerVaultUnsafe" ); - conditionMakerVaultUnsafe = await ConditionMakerVaultUnsafe.deploy(); - await conditionMakerVaultUnsafe.deployed(); - - const ConnectGelatoProviderPayment = await ethers.getContractFactory( + const connectGelatoProviderPayment = await ethers.getContract( "ConnectGelatoProviderPayment" ); - connectGelatoProviderPayment = await ConnectGelatoProviderPayment.deploy( - (await instaConnectors.connectorLength()).add(2), - providerAddress + const makerResolver = await ethers.getContract("MakerResolver"); + const connectGelatoDataForFullRefinance = await ethers.getContract( + "ConnectGelatoDataForFullRefinance" ); - await connectGelatoProviderPayment.deployed(); - - const MakerResolver = await ethers.getContractFactory("MakerResolver"); - const makerResolver = await MakerResolver.deploy(); - await makerResolver.deployed(); return { connectGelato, connectMaker, connectInstaPool, connectCompound, + connectGelatoDataForFullRefinance, instaIndex, instaList, instaMapping, @@ -154,8 +121,6 @@ async function getContracts(providerAddress) { connectGelatoProviderPayment, priceOracleResolver, dsa: ethers.constants.AddressZero, - connectGelatoData, - debtBridgeFromMakerForFullRefinance, makerResolver, dsaProviderModule, }; diff --git a/test/helpers/services/getWallets.js b/test/helpers/services/getWallets.js new file mode 100644 index 0000000..4e8705b --- /dev/null +++ b/test/helpers/services/getWallets.js @@ -0,0 +1,35 @@ +const {expect} = require("chai"); +const hre = require("hardhat"); +const {ethers} = hre; + +async function getWallets() { + let userWallet; + let userAddress; + let gelatoProviderWallet; + let gelatoProviderAddress; + let gelatoExecutorWallet; + let gelatoExecutorAddress; + + [ + userWallet, + gelatoProviderWallet, + gelatoExecutorWallet, + ] = await ethers.getSigners(); + userAddress = await userWallet.getAddress(); + gelatoProviderAddress = await gelatoProviderWallet.getAddress(); + gelatoExecutorAddress = await gelatoExecutorWallet.getAddress(); + + // Hardhat default wallets prefilled with 100 ETH + expect(await userWallet.getBalance()).to.be.gt(ethers.utils.parseEther("10")); + + return { + userWallet: userWallet, + userAddress: userAddress, + gelatoProviderWallet: gelatoProviderWallet, + gelatoProviderAddress: gelatoProviderAddress, + gelatoExecutorWallet: gelatoExecutorWallet, + gelatoExecutorAddress: gelatoExecutorAddress, + }; +} + +module.exports = getWallets; diff --git a/test/helpers/setups/Open-Deposit-Borrow-On-Maker-As-User.helper.js b/test/helpers/services/initializeMakerCdp.js similarity index 93% rename from test/helpers/setups/Open-Deposit-Borrow-On-Maker-As-User.helper.js rename to test/helpers/services/initializeMakerCdp.js index bdf9f8b..cc292b9 100644 --- a/test/helpers/setups/Open-Deposit-Borrow-On-Maker-As-User.helper.js +++ b/test/helpers/services/initializeMakerCdp.js @@ -3,7 +3,7 @@ const hre = require("hardhat"); const ConnectMaker = require("../../../pre-compiles/ConnectMaker.json"); -async function userOpenDepositBorrowOnMakerVault( +async function initializeMakerCdp( userAddress, DAI, dsa, @@ -63,4 +63,4 @@ async function userOpenDepositBorrowOnMakerVault( return vaultId; } -module.exports = userOpenDepositBorrowOnMakerVault; +module.exports = initializeMakerCdp; diff --git a/test/helpers/setups/Doing-Funding-As-Provider.helper.js b/test/helpers/services/provideFunds.js similarity index 63% rename from test/helpers/setups/Doing-Funding-As-Provider.helper.js rename to test/helpers/services/provideFunds.js index 50d6204..018ca25 100644 --- a/test/helpers/setups/Doing-Funding-As-Provider.helper.js +++ b/test/helpers/services/provideFunds.js @@ -1,7 +1,7 @@ const {expect} = require("chai"); -async function providerDoFunding( - providerWallet, +async function provideFunds( + gelatoProviderWallet, gelatoCore, gasLimit, gasPriceCeil @@ -13,7 +13,7 @@ async function providerDoFunding( // Provider. At each provider's task execution, some funds (approximatively // the gas cost value) will be transfered to the Executor stake. - let providerAddress = await providerWallet.getAddress(); + let gelatoProviderAddress = await gelatoProviderWallet.getAddress(); const TASK_AUTOMATION_FUNDS = await gelatoCore.minExecProviderFunds( gasLimit, @@ -21,16 +21,18 @@ async function providerDoFunding( ); await expect( - gelatoCore.connect(providerWallet).provideFunds(providerAddress, { - value: TASK_AUTOMATION_FUNDS, - }) + gelatoCore + .connect(gelatoProviderWallet) + .provideFunds(gelatoProviderAddress, { + value: TASK_AUTOMATION_FUNDS, + }) ).to.emit(gelatoCore, "LogFundsProvided"); - expect(await gelatoCore.providerFunds(providerAddress)).to.be.equal( + expect(await gelatoCore.providerFunds(gelatoProviderAddress)).to.be.equal( TASK_AUTOMATION_FUNDS ); //#endregion } -module.exports = providerDoFunding; +module.exports = provideFunds; diff --git a/test/helpers/services/providerAssignsExecutor.js b/test/helpers/services/providerAssignsExecutor.js new file mode 100644 index 0000000..e4638fc --- /dev/null +++ b/test/helpers/services/providerAssignsExecutor.js @@ -0,0 +1,28 @@ +const {expect} = require("chai"); + +async function providerAssignsExecutor( + gelatoProviderWallet, + gelatoExecutorAddress, + gelatoCore +) { + //#region Provider choose a executor + + // Provider choose a executor who will execute futur task + // for the provider, it will be compensated by the provider. + + const gelatoProviderAddress = await gelatoProviderWallet.getAddress(); + + await expect( + gelatoCore + .connect(gelatoProviderWallet) + .providerAssignsExecutor(gelatoExecutorAddress) + ).to.emit(gelatoCore, "LogProviderAssignedExecutor"); + + expect( + await gelatoCore.executorByProvider(gelatoProviderAddress) + ).to.be.equal(gelatoExecutorAddress); + + //#endregion +} + +module.exports = providerAssignsExecutor; diff --git a/test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js b/test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js new file mode 100644 index 0000000..88db46f --- /dev/null +++ b/test/helpers/services/providerWhiteListTaskForMakerETHAToMakerETHB.js @@ -0,0 +1,76 @@ +const {expect} = require("chai"); +const hre = require("hardhat"); +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 +) { + //#region Step 9 Provider should whitelist task + + // By WhiteList task, the provider can constrain the type + // of task the user can submitting. + + //#region Actions + + const spells = []; + + const debtBridgeCalculationForFullRefinance = new GelatoCoreLib.Action({ + addr: contracts.connectGelatoDataForFullRefinance.address, + data: await hre.run("abi-encode-withselector", { + abi: (await deployments.getArtifact("ConnectGelatoDataForFullRefinance")) + .abi, + functionname: "getDataAndCastForFromMakerToMaker", + inputs: [vaultId, constants.ETH, "ETH-B"], + }), + operation: GelatoCoreLib.Operation.Delegatecall, + }); + + spells.push(debtBridgeCalculationForFullRefinance); + + const gasPriceCeil = ethers.constants.MaxUint256; + + const connectGelatoFullDebtBridgeFromMakerTaskSpec = new GelatoCoreLib.TaskSpec( + { + conditions: [contracts.conditionMakerVaultUnsafe.address], + actions: spells, + gasPriceCeil, + } + ); + + await expect( + contracts.gelatoCore + .connect(wallets.gelatoProviderWallet) + .provideTaskSpecs([connectGelatoFullDebtBridgeFromMakerTaskSpec]) + ).to.emit(contracts.gelatoCore, "LogTaskSpecProvided"); + + expect( + await contracts.gelatoCore + .connect(wallets.gelatoProviderWallet) + .isTaskSpecProvided( + wallets.gelatoProviderAddress, + connectGelatoFullDebtBridgeFromMakerTaskSpec + ) + ).to.be.equal("OK"); + + expect( + await contracts.gelatoCore + .connect(wallets.gelatoProviderWallet) + .taskSpecGasPriceCeil( + wallets.gelatoProviderAddress, + await contracts.gelatoCore + .connect(wallets.gelatoProviderWallet) + .hashTaskSpec(connectGelatoFullDebtBridgeFromMakerTaskSpec) + ) + ).to.be.equal(gasPriceCeil); + + //#endregion + + return spells; +} + +module.exports = providerWhiteListTaskForMakerETHAToMakerETHB; diff --git a/test/helpers/services/providerWhiteListTaskForMakerToCompound.js b/test/helpers/services/providerWhiteListTaskForMakerToCompound.js new file mode 100644 index 0000000..409368d --- /dev/null +++ b/test/helpers/services/providerWhiteListTaskForMakerToCompound.js @@ -0,0 +1,76 @@ +const {expect} = require("chai"); +const hre = require("hardhat"); +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 +) { + //#region Step 9 Provider should whitelist task + + // By WhiteList task, the provider can constrain the type + // of task the user can submitting. + + //#region Actions + + const spells = []; + + const debtBridgeCalculationForFullRefinance = new GelatoCoreLib.Action({ + addr: contracts.connectGelatoDataForFullRefinance.address, + data: await hre.run("abi-encode-withselector", { + abi: (await deployments.getArtifact("ConnectGelatoDataForFullRefinance")) + .abi, + functionname: "getDataAndCastForFromMakerToCompound", + inputs: [vaultId, constants.ETH], + }), + operation: GelatoCoreLib.Operation.Delegatecall, + }); + + spells.push(debtBridgeCalculationForFullRefinance); + + const gasPriceCeil = ethers.constants.MaxUint256; + + const connectGelatoFullDebtBridgeFromMakerTaskSpec = new GelatoCoreLib.TaskSpec( + { + conditions: [contracts.conditionMakerVaultUnsafe.address], + actions: spells, + gasPriceCeil, + } + ); + + await expect( + contracts.gelatoCore + .connect(wallets.gelatoProviderWallet) + .provideTaskSpecs([connectGelatoFullDebtBridgeFromMakerTaskSpec]) + ).to.emit(contracts.gelatoCore, "LogTaskSpecProvided"); + + expect( + await contracts.gelatoCore + .connect(wallets.gelatoProviderWallet) + .isTaskSpecProvided( + wallets.gelatoProviderAddress, + connectGelatoFullDebtBridgeFromMakerTaskSpec + ) + ).to.be.equal("OK"); + + expect( + await contracts.gelatoCore + .connect(wallets.gelatoProviderWallet) + .taskSpecGasPriceCeil( + wallets.gelatoProviderAddress, + await contracts.gelatoCore + .connect(wallets.gelatoProviderWallet) + .hashTaskSpec(connectGelatoFullDebtBridgeFromMakerTaskSpec) + ) + ).to.be.equal(gasPriceCeil); + + //#endregion + + return spells; +} + +module.exports = providerWhiteListTaskForMakerToCompound; diff --git a/test/helpers/setups/Doing-Staking-As-Executor.helper.js b/test/helpers/services/stakeExecutor.js similarity index 60% rename from test/helpers/setups/Doing-Staking-As-Executor.helper.js rename to test/helpers/services/stakeExecutor.js index 9d09fed..05d0905 100644 --- a/test/helpers/setups/Doing-Staking-As-Executor.helper.js +++ b/test/helpers/services/stakeExecutor.js @@ -1,6 +1,6 @@ const {expect} = require("chai"); -async function executorDoStaking(executorWallet, gelatoCore) { +async function stakeExecutor(gelatoExecutorWallet, gelatoCore) { //#region Executor Stake on Gelato // For task execution provider will ask a executor to watch the @@ -10,15 +10,16 @@ async function executorDoStaking(executorWallet, gelatoCore) { // For safety measure Gelato ask the executor to stake a minimum // amount. - let executorAddress = await executorWallet.getAddress(); + const gelatoExecutorAddress = await gelatoExecutorWallet.getAddress(); - await gelatoCore.connect(executorWallet).stakeExecutor({ + await gelatoCore.connect(gelatoExecutorWallet).stakeExecutor({ value: await gelatoCore.minExecutorStake(), }); - expect(await gelatoCore.isExecutorMinStaked(executorAddress)).to.be.true; + expect(await gelatoCore.isExecutorMinStaked(gelatoExecutorAddress)).to.be + .true; //#endregion } -module.exports = executorDoStaking; +module.exports = stakeExecutor; diff --git a/test/helpers/setupFullRefinanceMakerToCompound.js b/test/helpers/setupFullRefinanceMakerToCompound.js new file mode 100644 index 0000000..e6f24d5 --- /dev/null +++ b/test/helpers/setupFullRefinanceMakerToCompound.js @@ -0,0 +1,70 @@ +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 providerWhiteListTaskForMakerToCompound = require("./services/providerWhiteListTaskForMakerToCompound"); +const getABI = require("./services/getABI"); + +async function setupFullRefinanceMakerToCompound() { + const wallets = await getWallets(); + const contracts = await getContracts(); + const constants = await getConstants(); + + // Gelato Testing environment setup. + await stakeExecutor(wallets.gelatoExecutorWallet, contracts.gelatoCore); + await provideFunds( + wallets.gelatoProviderWallet, + contracts.gelatoCore, + constants.GAS_LIMIT, + constants.GAS_PRICE_CEIL + ); + await providerAssignsExecutor( + wallets.gelatoProviderWallet, + wallets.gelatoExecutorAddress, + contracts.gelatoCore + ); + await addProviderModuleDSA( + wallets.gelatoProviderWallet, + contracts.gelatoCore, + contracts.dsaProviderModule.address + ); + contracts.dsa = await createDSA( + wallets.userAddress, + contracts.instaIndex, + contracts.instaList + ); + const vaultId = await initializeMakerCdp( + wallets.userAddress, + contracts.DAI, + contracts.dsa, + contracts.getCdps, + contracts.dssCdpManager, + constants.MAKER_INITIAL_ETH, + constants.MAKER_INITIAL_DEBT + ); + + const spells = await providerWhiteListTaskForMakerToCompound( + wallets, + contracts, + constants, + vaultId + ); + + const ABI = getABI(); + + return { + wallets, + contracts, + constants, + vaultId, + spells, + ABI, + }; +} + +module.exports = setupFullRefinanceMakerToCompound; diff --git a/test/helpers/setupFullRefinanceMakerToMaker.js b/test/helpers/setupFullRefinanceMakerToMaker.js new file mode 100644 index 0000000..a7df2d5 --- /dev/null +++ b/test/helpers/setupFullRefinanceMakerToMaker.js @@ -0,0 +1,76 @@ +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 providerWhiteListTaskForMakerETHAToMakerETHB = require("./services/providerWhiteListTaskForMakerETHAToMakerETHB"); +const getABI = require("./services/getABI"); + +async function setupFullRefinanceMakerToMaker() { + const wallets = await getWallets(); + const contracts = await getContracts(); + const constants = await getConstants(); + + // Gelato Testing environment setup. + await stakeExecutor(wallets.gelatoExecutorWallet, contracts.gelatoCore); + await provideFunds( + wallets.gelatoProviderWallet, + contracts.gelatoCore, + constants.GAS_LIMIT, + constants.GAS_PRICE_CEIL + ); + await providerAssignsExecutor( + wallets.gelatoProviderWallet, + wallets.gelatoExecutorAddress, + contracts.gelatoCore + ); + await addProviderModuleDSA( + wallets.gelatoProviderWallet, + contracts.gelatoCore, + contracts.dsaProviderModule.address + ); + contracts.dsa = await createDSA( + wallets.userAddress, + contracts.instaIndex, + contracts.instaList + ); + await addETHBGemJoinMapping( + wallets.userWallet, + contracts.instaMapping, + contracts.instaMaster + ); + const vaultAId = await initializeMakerCdp( + wallets.userAddress, + contracts.DAI, + contracts.dsa, + contracts.getCdps, + contracts.dssCdpManager, + constants.MAKER_INITIAL_ETH, + constants.MAKER_INITIAL_DEBT + ); + + const spells = await providerWhiteListTaskForMakerETHAToMakerETHB( + wallets, + contracts, + constants, + vaultAId + ); + + const ABI = getABI(); + + return { + wallets, + contracts, + constants, + vaultAId, + spells, + ABI, + }; +} + +module.exports = setupFullRefinanceMakerToMaker; diff --git a/test/helpers/setups/ABI.helper.js b/test/helpers/setups/ABI.helper.js deleted file mode 100644 index af8bf7a..0000000 --- a/test/helpers/setups/ABI.helper.js +++ /dev/null @@ -1,15 +0,0 @@ -const ConnectGelatoABI = require("./../../../pre-compiles/ConnectGelato.json") - .abi; -const PriceOracleResolverABI = require("./../../../artifacts/contracts/contracts/resolvers/PriceOracleResolver.sol/PriceOracleResolver.json") - .abi; -const ConnectAuthABI = require("./../../../pre-compiles/ConnectAuth.json").abi; - -async function getABI() { - return { - PriceOracleResolverABI: PriceOracleResolverABI, - ConnectGelatoABI: ConnectGelatoABI, - ConnectAuthABI: ConnectAuthABI, - }; -} - -module.exports = getABI; diff --git a/test/helpers/setups/Choosing-Executor-As-Provider.helper.js b/test/helpers/setups/Choosing-Executor-As-Provider.helper.js deleted file mode 100644 index a30ccb3..0000000 --- a/test/helpers/setups/Choosing-Executor-As-Provider.helper.js +++ /dev/null @@ -1,26 +0,0 @@ -const {expect} = require("chai"); - -async function providerChooseExecutor( - providerWallet, - executorAddress, - gelatoCore -) { - //#region Provider choose a executor - - // Provider choose a executor who will execute futur task - // for the provider, it will be compensated by the provider. - - let providerAddress = await providerWallet.getAddress(); - - await expect( - gelatoCore.connect(providerWallet).providerAssignsExecutor(executorAddress) - ).to.emit(gelatoCore, "LogProviderAssignedExecutor"); - - expect(await gelatoCore.executorByProvider(providerAddress)).to.be.equal( - executorAddress - ); - - //#endregion -} - -module.exports = providerChooseExecutor; diff --git a/test/helpers/setups/Contracts-For-Full-Refinancing.helper.js b/test/helpers/setups/Contracts-For-Full-Refinancing.helper.js deleted file mode 100644 index b82e1cd..0000000 --- a/test/helpers/setups/Contracts-For-Full-Refinancing.helper.js +++ /dev/null @@ -1,24 +0,0 @@ -const hre = require("hardhat"); -const {ethers} = hre; - -const getContracts = require("./Common-Contracts.helper"); - -async function getAllContracts(providerAddress) { - let connectGelatoData; - let contracts = await getContracts(providerAddress); - - const ConnectGelatoData = await ethers.getContractFactory( - "ConnectGelatoDataForFullRefinance" - ); - connectGelatoData = await ConnectGelatoData.deploy( - (await contracts.instaConnectors.connectorLength()).add(1), - contracts.connectGelatoProviderPayment.address - ); - await connectGelatoData.deployed(); - - contracts.connectGelatoData = connectGelatoData; - - return contracts; -} - -module.exports = getAllContracts; diff --git a/test/helpers/setups/Wallets.helper.js b/test/helpers/setups/Wallets.helper.js deleted file mode 100644 index d66cd30..0000000 --- a/test/helpers/setups/Wallets.helper.js +++ /dev/null @@ -1,31 +0,0 @@ -const {expect} = require("chai"); -const hre = require("hardhat"); -const {ethers} = hre; - -async function getWallets() { - let userWallet; - let userAddress; - let providerWallet; - let providerAddress; - let executorWallet; - let executorAddress; - - [userWallet, providerWallet, executorWallet] = await ethers.getSigners(); - userAddress = await userWallet.getAddress(); - providerAddress = await providerWallet.getAddress(); - executorAddress = await executorWallet.getAddress(); - - // Hardhat default wallets prefilled with 100 ETH - expect(await userWallet.getBalance()).to.be.gt(ethers.utils.parseEther("10")); - - return { - userWallet: userWallet, - userAddress: userAddress, - providerWallet: providerWallet, - providerAddress: providerAddress, - executorWallet: executorWallet, - executorAddress: executorAddress, - }; -} - -module.exports = getWallets; diff --git a/test/unit_tests/4_ConditionMakerVaultUnsafe.test.js b/test/unit_tests/0_ConditionMakerVaultUnsafe.test.js similarity index 91% rename from test/unit_tests/4_ConditionMakerVaultUnsafe.test.js rename to test/unit_tests/0_ConditionMakerVaultUnsafe.test.js index 0a83c6d..a8fe122 100644 --- a/test/unit_tests/4_ConditionMakerVaultUnsafe.test.js +++ b/test/unit_tests/0_ConditionMakerVaultUnsafe.test.js @@ -1,6 +1,6 @@ const {expect} = require("chai"); const hre = require("hardhat"); -const {ethers} = hre; +const {deployments, ethers} = hre; // #region Contracts ABI @@ -40,7 +40,10 @@ describe("ConditionMakerVaultUnsafe Unit Test", function () { let cdpId; let dsa; - before(async function () { + beforeEach(async function () { + // Deploy contract dependencies + await deployments.fixture(); + // Get Test Wallet for local testnet [userWallet] = await ethers.getSigners(); userAddress = await userWallet.getAddress(); @@ -69,21 +72,11 @@ describe("ConditionMakerVaultUnsafe Unit Test", function () { DAI = await ethers.getContractAt(IERC20.abi, hre.network.config.DAI); // ========== Test Setup ============ - - const PriceOracleResolver = await ethers.getContractFactory( - "PriceOracleResolver" - ); - - priceOracleResolver = await PriceOracleResolver.deploy(); - await priceOracleResolver.deployed(); - - const ConditionMakerVaultUnsafe = await ethers.getContractFactory( + priceOracleResolver = await ethers.getContract("PriceOracleResolver"); + conditionMakerVaultUnsafe = await ethers.getContract( "ConditionMakerVaultUnsafe" ); - conditionMakerVaultUnsafe = await ConditionMakerVaultUnsafe.deploy(); - await conditionMakerVaultUnsafe.deployed(); - // Create DeFi Smart Account const dsaAccountCount = await instaList.accounts(); diff --git a/test/unit_tests/5_ConnectGelatoProviderPayment.test.js b/test/unit_tests/1_ConnectGelatoProviderPayment.test.js similarity index 63% rename from test/unit_tests/5_ConnectGelatoProviderPayment.test.js rename to test/unit_tests/1_ConnectGelatoProviderPayment.test.js index 584947a..d528694 100644 --- a/test/unit_tests/5_ConnectGelatoProviderPayment.test.js +++ b/test/unit_tests/1_ConnectGelatoProviderPayment.test.js @@ -1,6 +1,6 @@ const {expect} = require("chai"); const hre = require("hardhat"); -const {ethers} = hre; +const {ethers, deployments} = hre; const GelatoCoreLib = require("@gelatonetwork/core"); @@ -14,7 +14,6 @@ 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 InstaConnector = require("../../pre-compiles/InstaConnectors.json"); // #endregion @@ -27,16 +26,14 @@ describe("ConnectGelatoProviderPayment Unit Test", function () { let userWallet; let userAddress; - let providerWallet; - let providerAddress; + let gelatoProviderWallet; + let gelatoProviderAddress; let gelatoCore; let instaList; let instaIndex; let DAI; - let instaConnectors; - let instaMaster; let connectBasic; let getCdps; let dssCdpManager; @@ -46,17 +43,14 @@ describe("ConnectGelatoProviderPayment Unit Test", function () { let dsa; let cdpId; - before(async function () { + beforeEach(async function () { + // Deploy dependencies + await deployments.fixture(); + // Get Test Wallet for local testnet - [userWallet] = await ethers.getSigners(); + [userWallet, gelatoProviderWallet] = await ethers.getSigners(); userAddress = await userWallet.getAddress(); - - [, providerWallet] = await ethers.getSigners(); - providerAddress = await providerWallet.getAddress(); - - instaMaster = await ethers.provider.getSigner( - hre.network.config.InstaMaster - ); + gelatoProviderAddress = await gelatoProviderWallet.getAddress(); gelatoCore = await ethers.getContractAt( GelatoCoreLib.GelatoCore.abi, @@ -80,10 +74,6 @@ describe("ConnectGelatoProviderPayment Unit Test", function () { ConnectBasic.abi, hre.network.config.ConnectBasic ); - instaConnectors = await ethers.getContractAt( - InstaConnector.abi, - hre.network.config.InstaConnectors - ); getCdps = await ethers.getContractAt( GetCdps.abi, hre.network.config.GetCdps @@ -95,41 +85,9 @@ describe("ConnectGelatoProviderPayment Unit Test", function () { DAI = await ethers.getContractAt(IERC20.abi, hre.network.config.DAI); // ========== Test Setup ============ - - const connectorLength = await instaConnectors.connectorLength(); - const connectorId = connectorLength.add(1); - - const ConnectGelatoProviderPayment = await ethers.getContractFactory( + connectGelatoProviderPayment = await ethers.getContract( "ConnectGelatoProviderPayment" ); - connectGelatoProviderPayment = await ConnectGelatoProviderPayment.deploy( - connectorId, - ethers.constants.AddressZero - ); - connectGelatoProviderPayment.deployed(); - - 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(connectGelatoProviderPayment.address); - - await hre.network.provider.request({ - method: "hardhat_stopImpersonatingAccount", - params: [await instaMaster.getAddress()], - }); - - expect( - await instaConnectors.isConnector([connectGelatoProviderPayment.address]) - ).to.be.true; // ========== Create DeFi Smart Account for User account ============ @@ -149,51 +107,28 @@ describe("ConnectGelatoProviderPayment Unit Test", function () { ); }); - it("#1: payProvider should return error message ConnectGelatoProviderPayment.payProvider:INVALIDADDESS when provider is Zero Address", async function () { + it("#1: ConnectGelatoProviderPayment should have been deployed with providerAddress", async function () { + expect(await connectGelatoProviderPayment.gelatoProvider()).to.be.eq( + gelatoProviderAddress + ); + }); + + it("#2: setProvider should revert for AddressZero", async function () { await expect( - dsa.cast( - [connectBasic.address, connectGelatoProviderPayment.address], - [ - await hre.run("abi-encode-withselector", { - abi: ConnectBasic.abi, - functionname: "deposit", - inputs: [ - "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", - ethers.utils.parseEther("1"), - 0, - "105", - ], - }), - await hre.run("abi-encode-withselector", { - abi: ( - await hre.artifacts.readArtifact("ConnectGelatoProviderPayment") - ).abi, - functionname: "payProvider", - inputs: ["0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", 0, "105", 0], - }), - ], - userAddress, - { - value: ethers.utils.parseEther("1"), - } - ) - ).to.be.revertedWith("ConnectGelatoProviderPayment.payProvider:!provider"); + connectGelatoProviderPayment.setProvider(ethers.constants.AddressZero) + ).to.be.revertedWith("ConnectGelatoProviderPayment.noAddressZeroProvider"); }); - it("#2: setProvider should change the provider address", async function () { - expect(await connectGelatoProviderPayment.gelatoProvider()).to.be.equal( - ethers.constants.AddressZero - ); - - await connectGelatoProviderPayment.setProvider(providerAddress); + it("#3: setProvider should change the provider address", async function () { + await connectGelatoProviderPayment.setProvider(userAddress); expect(await connectGelatoProviderPayment.gelatoProvider()).to.be.equal( - providerAddress + userAddress ); }); - it("#3: payProvider should pay to Provider 300 Dai", async function () { - const providerDAIBalanceBefore = await DAI.balanceOf(providerAddress); + it("#4: payProvider should pay to Provider 300 Dai", async function () { + const providerDAIBalanceBefore = await DAI.balanceOf(gelatoProviderAddress); await dsa.cast( [hre.network.config.ConnectMaker], @@ -256,14 +191,14 @@ describe("ConnectGelatoProviderPayment Unit Test", function () { userAddress ); - expect(await DAI.balanceOf(providerAddress)).to.be.equal( + expect(await DAI.balanceOf(gelatoProviderAddress)).to.be.equal( providerDAIBalanceBefore.add(ethers.utils.parseUnits("300", 18)) ); }); - it("#4: payProvider should pay to Provider 1 ether", async function () { + it("#5: payProvider should pay to Provider 1 ether", async function () { const providerBalanceOnGelatoCoreBefore = await gelatoCore.providerFunds( - providerAddress + gelatoProviderAddress ); await dsa.cast( @@ -293,7 +228,7 @@ describe("ConnectGelatoProviderPayment Unit Test", function () { } ); - expect(await gelatoCore.providerFunds(providerAddress)).to.be.equal( + expect(await gelatoCore.providerFunds(gelatoProviderAddress)).to.be.equal( providerBalanceOnGelatoCoreBefore.add(ethers.utils.parseEther("1")) ); }); diff --git a/test/unit_tests/6_DebtRefinanceMath.test.js b/test/unit_tests/2_DebtRefinanceMath.test.js similarity index 93% rename from test/unit_tests/6_DebtRefinanceMath.test.js rename to test/unit_tests/2_DebtRefinanceMath.test.js index a12fb0e..ef1e3ee 100644 --- a/test/unit_tests/6_DebtRefinanceMath.test.js +++ b/test/unit_tests/2_DebtRefinanceMath.test.js @@ -1,6 +1,6 @@ const {expect} = require("chai"); const hre = require("hardhat"); -const {ethers} = hre; +const {deployments, ethers} = hre; const WAD = ethers.utils.parseUnits("1", 18); @@ -24,15 +24,10 @@ describe("Debt Partial Refinance Math Unit Test", function () { } let fGelatoDebtBridgeMock; - before(async function () { - const FGelatoDebtBridgeMock = await ethers.getContractFactory( - "FGelatoDebtBridgeMock" - ); - fGelatoDebtBridgeMock = await FGelatoDebtBridgeMock - .deploy - //ethers.constants.AddressZero - (); - fGelatoDebtBridgeMock.deployed(); + beforeEach(async function () { + await deployments.fixture(); + + fGelatoDebtBridgeMock = await ethers.getContract("FGelatoDebtBridgeMock"); }); it("#1: wCalcCollateralToWithdraw should return the amount of collateral to withdraw on protocol 1 and to put on protocol 2", async function () { diff --git a/test/unit_tests/7_PriceOracleResolver.test.js b/test/unit_tests/3_PriceOracleResolver.test.js similarity index 80% rename from test/unit_tests/7_PriceOracleResolver.test.js rename to test/unit_tests/3_PriceOracleResolver.test.js index d3638b1..55f71eb 100644 --- a/test/unit_tests/7_PriceOracleResolver.test.js +++ b/test/unit_tests/3_PriceOracleResolver.test.js @@ -1,6 +1,6 @@ const {expect} = require("chai"); const hre = require("hardhat"); -const {ethers} = hre; +const {deployments, ethers} = hre; const ORACLE_MAKER_ETH_USD = "ETH/USD-Maker-v1"; const ORACLE_MAKER_ETH_USD_ADDR = "0x729D19f657BD0614b4985Cf1D82531c67569197B"; @@ -15,12 +15,9 @@ describe("PriceOracleResolver Unit Test", function () { let priceOracleResolver; - before(async function () { - const PriceOracleResolver = await ethers.getContractFactory( - "PriceOracleResolver" - ); - priceOracleResolver = await PriceOracleResolver.deploy(); - priceOracleResolver.deployed(); + beforeEach(async function () { + await deployments.fixture(); + priceOracleResolver = await ethers.getContract("PriceOracleResolver"); }); it("#1: addOracle should add a maker medianizer for a currencyPair", async function () { @@ -38,6 +35,11 @@ describe("PriceOracleResolver Unit Test", function () { }); it("#2: addOracle should revert when adding a maker medianizer and for this currency pair it was been already added", async function () { + await priceOracleResolver.addOracle( + ORACLE_MAKER_ETH_USD, + ORACLE_MAKER_ETH_USD_ADDR, + PRICE_ORACLE_MAKER_PAYLOAD + ); await expect( priceOracleResolver.addOracle( ORACLE_MAKER_ETH_USD, @@ -48,6 +50,11 @@ describe("PriceOracleResolver Unit Test", function () { }); it("#3: getPrice returns price", async function () { + await priceOracleResolver.addOracle( + ORACLE_MAKER_ETH_USD, + ORACLE_MAKER_ETH_USD_ADDR, + PRICE_ORACLE_MAKER_PAYLOAD + ); expect((await priceOracleResolver.getPrice(ORACLE_MAKER_ETH_USD)).isZero()) .to.be.false; }); diff --git a/test_temp/3_Partial-Refinance-External-Provider.test.js b/test_temp/3_Partial-Refinance-External-Provider.test.js index c758ec6..258670a 100644 --- a/test_temp/3_Partial-Refinance-External-Provider.test.js +++ b/test_temp/3_Partial-Refinance-External-Provider.test.js @@ -1,6 +1,6 @@ const {expect} = require("chai"); const hre = require("hardhat"); -const {ethers} = hre; +const {deployments, ethers} = hre; const GelatoCoreLib = require("@gelatonetwork/core"); // #region Contracts ABI and Constants @@ -23,9 +23,6 @@ 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 PriceOracleResolverABI = require("../artifacts/contracts/contracts/resolvers/PriceOracleResolver.sol/PriceOracleResolver.json") - .abi; - const ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; const GAS_LIMIT = "4000000"; const GAS_PRICE_CEIL = ethers.utils.parseUnits("1000", "gwei"); @@ -124,10 +121,10 @@ describe("Debt Bridge with External Provider", function () { // Wallet to use for local testing let userWallet; let userAddress; - let providerWallet; - let providerAddress; - let executorWallet; - let executorAddress; + let gelatoProviderWallet; + let gelatoProviderAddress; + let gelatoExecutorWallet; + let gelatoExecutorAddress; // Deployed instances let connectGelato; @@ -164,10 +161,14 @@ describe("Debt Bridge with External Provider", function () { before(async function () { // Get Test Wallet for local testnet - [userWallet, providerWallet, executorWallet] = await ethers.getSigners(); + [ + userWallet, + gelatoProviderWallet, + gelatoExecutorWallet, + ] = await ethers.getSigners(); userAddress = await userWallet.getAddress(); - providerAddress = await providerWallet.getAddress(); - executorAddress = await executorWallet.getAddress(); + gelatoProviderAddress = await gelatoProviderWallet.getAddress(); + gelatoExecutorAddress = await gelatoExecutorWallet.getAddress(); instaMaster = await ethers.provider.getSigner( hre.network.config.InstaMaster @@ -363,11 +364,13 @@ describe("Debt Bridge with External Provider", function () { // For safety measure Gelato ask the executor to stake a minimum // amount. - await gelatoCore.connect(executorWallet).stakeExecutor({ + await gelatoCore.connect(gelatoExecutorWallet).stakeExecutor({ value: await gelatoCore.minExecutorStake(), }); - expect(await gelatoCore.isExecutorMinStaked(executorAddress)).to.be.true; + expect( + await gelatoCore.isExecutorMinStaked(gelatoExecutorAddress) + ).to.be.true; //#endregion @@ -384,12 +387,14 @@ describe("Debt Bridge with External Provider", function () { ); await expect( - gelatoCore.connect(providerWallet).provideFunds(providerAddress, { - value: TASK_AUTOMATION_FUNDS, - }) + gelatoCore + .connect(gelatoProviderWallet) + .provideFunds(gelatoProviderAddress, { + value: TASK_AUTOMATION_FUNDS, + }) ).to.emit(gelatoCore, "LogFundsProvided"); - expect(await gelatoCore.providerFunds(providerAddress)).to.be.equal( + expect(await gelatoCore.providerFunds(gelatoProviderAddress)).to.be.equal( TASK_AUTOMATION_FUNDS ); @@ -402,13 +407,13 @@ describe("Debt Bridge with External Provider", function () { await expect( gelatoCore - .connect(providerWallet) - .providerAssignsExecutor(executorAddress) + .connect(gelatoProviderWallet) + .providerAssignsExecutor(gelatoExecutorAddress) ).to.emit(gelatoCore, "LogProviderAssignedExecutor"); - expect(await gelatoCore.executorByProvider(providerAddress)).to.be.equal( - executorAddress - ); + expect( + await gelatoCore.executorByProvider(gelatoProviderAddress) + ).to.be.equal(gelatoExecutorAddress); //#endregion @@ -420,14 +425,14 @@ describe("Debt Bridge with External Provider", function () { await expect( gelatoCore - .connect(providerWallet) + .connect(gelatoProviderWallet) .addProviderModules([dsaProviderModule.address]) ).to.emit(gelatoCore, "LogProviderModuleAdded"); expect( await gelatoCore - .connect(providerWallet) - .isModuleProvided(providerAddress, dsaProviderModule.address) + .connect(gelatoProviderWallet) + .isModuleProvided(gelatoProviderAddress, dsaProviderModule.address) ).to.be.true; //#endregion @@ -546,7 +551,7 @@ describe("Debt Bridge with External Provider", function () { MIN_COL_RATIO_B, priceOracleResolver.address, await hre.run("abi-encode-withselector", { - abi: PriceOracleResolverABI, + abi: (await deployments.getArtifcat("PriceOracleResolver")).abi, functionname: "getMockPrice", inputs: [userAddress], }), @@ -636,7 +641,7 @@ describe("Debt Bridge with External Provider", function () { data: await hre.run("abi-encode-withselector", { abi: ConnectGelatoProviderPaymentABI, functionname: "payProvider", - inputs: [providerAddress, ETH, 0, "605", 0], + inputs: [gelatoProviderAddress, ETH, 0, "605", 0], }), operation: GelatoCoreLib.Operation.Delegatecall, }); @@ -655,26 +660,26 @@ describe("Debt Bridge with External Provider", function () { await expect( gelatoCore - .connect(providerWallet) + .connect(gelatoProviderWallet) .provideTaskSpecs([connectGelatoFullDebtBridgeFromMakerTaskSpec]) ).to.emit(gelatoCore, "LogTaskSpecProvided"); expect( await gelatoCore - .connect(providerWallet) + .connect(gelatoProviderWallet) .isTaskSpecProvided( - providerAddress, + gelatoProviderAddress, connectGelatoFullDebtBridgeFromMakerTaskSpec ) ).to.be.equal("OK"); expect( await gelatoCore - .connect(providerWallet) + .connect(gelatoProviderWallet) .taskSpecGasPriceCeil( - providerAddress, + gelatoProviderAddress, await gelatoCore - .connect(providerWallet) + .connect(gelatoProviderWallet) .hashTaskSpec(connectGelatoFullDebtBridgeFromMakerTaskSpec) ) ).to.be.equal(gasPriceCeil); @@ -705,7 +710,7 @@ describe("Debt Bridge with External Provider", function () { vaultId, priceOracleResolver.address, await hre.run("abi-encode-withselector", { - abi: PriceOracleResolverABI, + abi: (await deployments.getArtifact("PriceOracleResolver")).abi, functionname: "getMockPrice", inputs: [userAddress], }), @@ -720,7 +725,7 @@ describe("Debt Bridge with External Provider", function () { }); const gelatoExternalProvider = new GelatoCoreLib.GelatoProvider({ - addr: providerAddress, + addr: gelatoProviderAddress, module: dsaProviderModule.address, }); @@ -769,7 +774,7 @@ describe("Debt Bridge with External Provider", function () { expect( await gelatoCore - .connect(executorWallet) + .connect(gelatoExecutorWallet) .canExec(taskReceipt, GAS_LIMIT, gelatoGasPrice) ).to.be.equal("ConditionNotOk:MakerVaultNotUnsafe"); @@ -778,7 +783,7 @@ describe("Debt Bridge with External Provider", function () { expect( await gelatoCore - .connect(executorWallet) + .connect(gelatoExecutorWallet) .canExec(taskReceipt, GAS_LIMIT, gelatoGasPrice) ).to.be.equal("OK"); @@ -826,10 +831,10 @@ describe("Debt Bridge with External Provider", function () { //console.log(String(wdiv(pricedCollateral.sub(wmul(expectedColWithdrawAmount, latestPrice).add(gasFeesPaidFromCol)),debt.sub(expectedBorAmountToPayBack)))); //#endregion - const providerBalanceBeforeExecution = await providerWallet.getBalance(); + const providerBalanceBeforeExecution = await gelatoProviderWallet.getBalance(); await expect( - gelatoCore.connect(executorWallet).exec(taskReceipt, { + gelatoCore.connect(gelatoExecutorWallet).exec(taskReceipt, { gasPrice: gelatoGasPrice, // Exectutor must use gelatoGasPrice (Chainlink fast gwei) gasLimit: GAS_LIMIT, }) @@ -837,7 +842,7 @@ describe("Debt Bridge with External Provider", function () { // 🚧 For Debugging: // const txResponse2 = await gelatoCore - // .connect(providerWallet) + // .connect(gelatoProviderWallet) // .exec(taskReceipt, { // gasPrice: gelatoGasPrice, // gasLimit: GAS_LIMIT, @@ -850,7 +855,7 @@ describe("Debt Bridge with External Provider", function () { // } // await GelatoCoreLib.sleep(10000); - expect(await providerWallet.getBalance()).to.be.gt( + expect(await gelatoProviderWallet.getBalance()).to.be.gt( providerBalanceBeforeExecution ); diff --git a/yarn.lock b/yarn.lock index f642763..c5d4bfd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4214,6 +4214,11 @@ har-validator@~5.1.3: ajv "^6.12.3" har-schema "^2.0.0" +hardhat-deploy-ethers@^0.3.0-beta.5: + version "0.3.0-beta.5" + resolved "https://registry.yarnpkg.com/hardhat-deploy-ethers/-/hardhat-deploy-ethers-0.3.0-beta.5.tgz#c365ebb5c29e7474b2b35912f27dc9699a0e63b3" + integrity sha512-KoUswkCPSuARGjZQIf8kItL5rPFt6srgVj+q0B9YTQ7Vyw/k2N0R8u7aYWq4Piy9mjDKcEGQTPBXhaX02BMkVw== + hardhat-deploy@0.7.0-beta.28: version "0.7.0-beta.28" resolved "https://registry.yarnpkg.com/hardhat-deploy/-/hardhat-deploy-0.7.0-beta.28.tgz#e71dc7d7a97903773c717818e8ecb7eef8f97a5c"