From 84075c39b63c5277f63cca91018d997b10d3bad7 Mon Sep 17 00:00:00 2001 From: cryptoDev222 Date: Fri, 17 Sep 2021 14:44:09 -0500 Subject: [PATCH 1/3] feat: deploy connectors from cmd and minor fix on uniswap v3 connector --- .../mainnet/connectors/uniswap/v3/helpers.sol | 24 +++++- hardhat.config.js | 8 +- package.json | 1 + scripts/constant/cmdAssets.js | 12 +++ scripts/deployConnectorsFromCmd.js | 84 +++++++++++++++++++ test/uniswap/uniswap.test.js | 6 +- 6 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 scripts/constant/cmdAssets.js create mode 100644 scripts/deployConnectorsFromCmd.js diff --git a/contracts/mainnet/connectors/uniswap/v3/helpers.sol b/contracts/mainnet/connectors/uniswap/v3/helpers.sol index 2fc42f8b..08a3e303 100644 --- a/contracts/mainnet/connectors/uniswap/v3/helpers.sol +++ b/contracts/mainnet/connectors/uniswap/v3/helpers.sol @@ -53,6 +53,18 @@ abstract contract Helpers is DSMath, Basic { minAmt = convert18ToDec(token.decimals(), minAmt); } + function sortTokenAddress(address _token0, address _token1) + internal + view + returns (address token0, address token1) + { + if (_token0 > _token1) { + (token0, token1) = (_token1, _token0); + } else { + (token0, token1) = (_token0, _token1); + } + } + /** * @dev Mint function which interact with Uniswap v3 */ @@ -83,6 +95,17 @@ abstract contract Helpers is DSMath, Basic { approve(_token0, address(nftManager), _amount0); approve(_token1, address(nftManager), _amount1); + { + (address token0, ) = sortTokenAddress( + address(_token0), + address(_token1) + ); + + if (token0 != address(_token0)) { + (_token0, _token1) = (_token1, _token0); + (_amount0, _amount1) = (_amount1, _amount0); + } + } uint256 _minAmt0 = getMinAmount(_token0, _amount0, params.slippage); uint256 _minAmt1 = getMinAmount(_token1, _amount1, params.slippage); @@ -139,7 +162,6 @@ abstract contract Helpers is DSMath, Basic { uint256 _amount0, uint256 _amount1 ) internal { - bool isEth0 = _token0 == wethAddr; bool isEth1 = _token1 == wethAddr; convertEthToWeth(isEth0, TokenInterface(_token0), _amount0); diff --git a/hardhat.config.js b/hardhat.config.js index 44456a48..e6233d9c 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -11,6 +11,7 @@ const { utils } = require("ethers"); const PRIVATE_KEY = process.env.PRIVATE_KEY; const ALCHEMY_ID = process.env.ALCHEMY_ID; +const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY; if (!process.env.ALCHEMY_ID) { throw new Error("ENV Variable ALCHEMY_ID not set!"); @@ -54,6 +55,11 @@ module.exports = { timeout: 150000, gasPrice: parseInt(utils.parseUnits("30", "gwei")), }, + rinkeby: { + url: `https://eth-rinkeby.alchemyapi.io/v2/${ALCHEMY_ID}`, + accounts: [`0x${PRIVATE_KEY}`], + timeout: 150000, + }, hardhat: { forking: { url: `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_ID}`, @@ -70,7 +76,7 @@ module.exports = { }, }, etherscan: { - apiKey: process.env.ETHERSCAN_API_KEY, + apiKey: ETHERSCAN_API_KEY, }, tenderly: { project: process.env.TENDERLY_PROJECT, diff --git a/package.json b/package.json index 589d093e..dbb4e447 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "test": "hardhat test", "coverage": "./node_modules/.bin/solidity-coverage", "check-husky": "node status-checks/huskyCheck.js", + "deploy": "node scripts/deployConnectorsFromCmd.js", "build-contracts": "sol-merger \"./contracts/connectors/mock.sol\" ./contracts/build" }, "repository": { diff --git a/scripts/constant/cmdAssets.js b/scripts/constant/cmdAssets.js new file mode 100644 index 00000000..81b640e1 --- /dev/null +++ b/scripts/constant/cmdAssets.js @@ -0,0 +1,12 @@ +module.exports = { + connectors: { + uniswapV3: "ConnectV2UniswapV3", + "1inch": "ConnectV2OneInch", + "1proto": "ConnectV2OneProto", + aaveV1: "ConnectV2AaveV1", + aaveV2: "ConnectV2AaveV2", + authority: "ConnectV2Auth", + basic: "ConnectV2Basic", + comp: "ConnectV2COMP", + } + }; \ No newline at end of file diff --git a/scripts/deployConnectorsFromCmd.js b/scripts/deployConnectorsFromCmd.js new file mode 100644 index 00000000..290dee4d --- /dev/null +++ b/scripts/deployConnectorsFromCmd.js @@ -0,0 +1,84 @@ +const hre = require("hardhat"); +const { ethers } = hre; + +const { connectors, networks } = require("./constant/cmdAssets"); + +let args = process.argv; +args = args.splice(2, args.length); +let params = {}; + +for (let i = 0; i < args.length; i += 2) { + if (args[i][0] !== "-" || args[i][1] !== "-") { + console.log("Please add '--' for the key"); + process.exit(-1); + } + let key = args[i].slice(2, args[i].length); + params[key] = args[i + 1]; + if (key === "connector") { + params[key] = connectors[args[i + 1]]; + } +} + +if (!params.hasOwnProperty('connector')) { + console.error("Should include connector params") + process.exit(-1); +} + +if(params['connector'] === undefined || params['connector'] === null) { + console.error("Unsupported connector name"); + const keys = Object.keys(connectors); + console.log("Currently supported connector names are: ", keys.join(",")); + console.log("If you want to add, please edit scripts/constant/cmdAssets.js"); + process.exit(1); +} + +if (!params.hasOwnProperty('network')) { + console.error("Should include network params") + process.exit(-1); +} + +if (!params.hasOwnProperty('gas')) { + console.error("Should include gas params") + process.exit(-1); +} + +let privateKey = process.env.PRIVATE_KEY; +let provider = new ethers.providers.JsonRpcProvider(hre.config.networks[params['network']].url); +// let wallet = new ethers.Wallet("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", provider); +let wallet = new ethers.Wallet(privateKey, provider); + +const connectorName = params['connector']; + +hre.network.name = params['networkName']; +hre.network.config = hre.config.networks[params['networkName']]; +hre.network.provider = provider; + +const main = async () => { + const Connector = await ethers.getContractFactory(connectorName); + const connector = await Connector.connect(wallet).deploy({ gasPrice: ethers.utils.parseUnits(params['gas'], "gwei") }); + await connector.deployed(); + + console.log(`${connectorName} Deployed: ${connector.address}`); + try { + await hre.run("verify:verify", { + address: connector.address, + constructorArguments: [] + } + ) + } catch (error) { + console.log(`Failed to verify: ${connectorName}@${connector.address}`) + console.log(error) + } + + return connector.address +} + +main() + .then(() => { + console.log("Done successfully"); + process.exit(0) + }) + .catch(err => { + console.log("error:", err); + process.exit(1); + }) \ No newline at end of file diff --git a/test/uniswap/uniswap.test.js b/test/uniswap/uniswap.test.js index 63c74375..d7a2e407 100644 --- a/test/uniswap/uniswap.test.js +++ b/test/uniswap/uniswap.test.js @@ -16,7 +16,7 @@ const constants = require("../../scripts/constant/constant"); const tokens = require("../../scripts/constant/tokens"); const { abi: nftManagerAbi } = require("@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json") -const connectV2UniswapV3Artifacts = require("../../artifacts/contracts/mainnet/connectors/uniswapV3/main.sol/ConnectV2UniswapV3.json"); +const connectV2UniswapV3Artifacts = require("../../artifacts/contracts/mainnet/connectors/uniswap/v3/main.sol/ConnectV2UniswapV3.json"); const { eth } = require("../../scripts/constant/tokens"); const { BigNumber } = require("ethers"); @@ -112,13 +112,13 @@ describe("UniswapV3", function () { connector: connectorName, method: "mint", args: [ - DAI_ADDR, ethAddress, + DAI_ADDR, FeeAmount.MEDIUM, getMinTick(TICK_SPACINGS[FeeAmount.MEDIUM]), getMaxTick(TICK_SPACINGS[FeeAmount.MEDIUM]), - daiAmount, ethAmount, + daiAmount, "500000000000000000", getIds, setId From 99b672d78d74934b5f0e720d450864a42c1d3fff Mon Sep 17 00:00:00 2001 From: cryptoDev222 Date: Thu, 23 Sep 2021 09:02:54 -0500 Subject: [PATCH 2/3] parse contract name from folder structure --- scripts/constant/cmdAssets.js | 12 ------- scripts/deployConnectorsFromCmd.js | 57 ++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 30 deletions(-) delete mode 100644 scripts/constant/cmdAssets.js diff --git a/scripts/constant/cmdAssets.js b/scripts/constant/cmdAssets.js deleted file mode 100644 index 81b640e1..00000000 --- a/scripts/constant/cmdAssets.js +++ /dev/null @@ -1,12 +0,0 @@ -module.exports = { - connectors: { - uniswapV3: "ConnectV2UniswapV3", - "1inch": "ConnectV2OneInch", - "1proto": "ConnectV2OneProto", - aaveV1: "ConnectV2AaveV1", - aaveV2: "ConnectV2AaveV2", - authority: "ConnectV2Auth", - basic: "ConnectV2Basic", - comp: "ConnectV2COMP", - } - }; \ No newline at end of file diff --git a/scripts/deployConnectorsFromCmd.js b/scripts/deployConnectorsFromCmd.js index 290dee4d..f2584fa5 100644 --- a/scripts/deployConnectorsFromCmd.js +++ b/scripts/deployConnectorsFromCmd.js @@ -1,8 +1,7 @@ +const fs = require("fs"); const hre = require("hardhat"); const { ethers } = hre; -const { connectors, networks } = require("./constant/cmdAssets"); - let args = process.argv; args = args.splice(2, args.length); let params = {}; @@ -14,9 +13,6 @@ for (let i = 0; i < args.length; i += 2) { } let key = args[i].slice(2, args[i].length); params[key] = args[i + 1]; - if (key === "connector") { - params[key] = connectors[args[i + 1]]; - } } if (!params.hasOwnProperty('connector')) { @@ -24,38 +20,63 @@ if (!params.hasOwnProperty('connector')) { process.exit(-1); } -if(params['connector'] === undefined || params['connector'] === null) { - console.error("Unsupported connector name"); - const keys = Object.keys(connectors); - console.log("Currently supported connector names are: ", keys.join(",")); - console.log("If you want to add, please edit scripts/constant/cmdAssets.js"); - process.exit(1); -} - if (!params.hasOwnProperty('network')) { console.error("Should include network params") process.exit(-1); } -if (!params.hasOwnProperty('gas')) { +if (!params.hasOwnProperty('gasPrice')) { console.error("Should include gas params") process.exit(-1); } let privateKey = process.env.PRIVATE_KEY; let provider = new ethers.providers.JsonRpcProvider(hre.config.networks[params['network']].url); -// let wallet = new ethers.Wallet("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", provider); let wallet = new ethers.Wallet(privateKey, provider); -const connectorName = params['connector']; - hre.network.name = params['networkName']; hre.network.config = hre.config.networks[params['networkName']]; hre.network.provider = provider; +let contracts = []; + +const parseFile = async (filePath) => { + const data = fs.readFileSync(filePath, "utf-8"); + let parsedData = data.split("contract "); + parsedData = parsedData[parsedData.length - 1].split(" "); + parsedData = parsedData[0]; + return parsedData; +} + +const parseDir = async (root, basePath, addPath) => { + for(let i = 0; i < root.length; i++) { + addPath = "/" + root[i]; + const dir = fs.readdirSync(basePath + addPath); + if(dir.indexOf("main.sol") !== -1) { + const fileData = await parseFile(basePath + addPath + "/main.sol"); + contracts.push(fileData) + } else { + await parseDir(dir, basePath + addPath, ""); + } + } +} const main = async () => { + const mainnet = fs.readdirSync("./contracts/mainnet/connectors/"); + const polygon = fs.readdirSync("./contracts/polygon/connectors/"); + let basePathMainnet = "./contracts/mainnet/connectors/"; + let basePathPolygon = "./contracts/polygon/connectors/"; + + const connectorName = params['connector']; + + await parseDir(mainnet, basePathMainnet, ""); + await parseDir(polygon, basePathPolygon, ""); + + if(contracts.indexOf(connectorName) === -1) { + throw new Error("can not find the connector!\n" + "supported connector names are:\n" + contracts.join("\n")); + } + const Connector = await ethers.getContractFactory(connectorName); - const connector = await Connector.connect(wallet).deploy({ gasPrice: ethers.utils.parseUnits(params['gas'], "gwei") }); + const connector = await Connector.connect(wallet).deploy({ gasPrice: ethers.utils.parseUnits(params['gasPrice'], "gwei") }); await connector.deployed(); console.log(`${connectorName} Deployed: ${connector.address}`); From 48cf6a02feb7fd7e4162de50812c3e6408d712f8 Mon Sep 17 00:00:00 2001 From: cryptoDev222 Date: Wed, 29 Sep 2021 00:19:30 -0500 Subject: [PATCH 3/3] fix testcases --- hardhat.config.js | 5 +- test/aave/v1.test.js | 43 ++- test/aave/v2.test.js | 11 + test/b.protocol/b.compound.test.js | 13 +- test/b.protocol/b.liquity.test.js | 173 +++++----- test/b.protocol/b.maker.test.js | 449 +++++++++++++------------ test/basic-ERC1155/ERC1155-transfer.js | 13 +- test/basic-ERC721/ERC721-transfer.js | 13 +- test/compound/compound.test.js | 169 +++++----- test/instapool/instapool.test.js | 137 ++++---- test/liquity/liquity.test.js | 11 + test/mappings/mappings.test.js | 16 + test/uniswap/uniswap.test.js | 13 +- test/uniswapStake/uniswapStake.test.js | 11 + test/yearn/yearn.test.js | 11 + 15 files changed, 626 insertions(+), 462 deletions(-) diff --git a/hardhat.config.js b/hardhat.config.js index 896a5e96..7a07b081 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -27,7 +27,7 @@ module.exports = { version: "0.7.6", settings: { optimizer: { - enabled: false, + enabled: true, runs: 200, }, }, @@ -63,10 +63,9 @@ module.exports = { hardhat: { forking: { url: `https://eth-mainnet.alchemyapi.io/v2/${ALCHEMY_ID}`, - blockNumber: 12796965, + blockNumber: 12696000, }, blockGasLimit: 12000000, - gasPrice: parseInt(utils.parseUnits("300", "gwei")) }, matic: { url: "https://rpc-mainnet.maticvigil.com/", diff --git a/test/aave/v1.test.js b/test/aave/v1.test.js index aea2a222..970ef3e8 100644 --- a/test/aave/v1.test.js +++ b/test/aave/v1.test.js @@ -13,6 +13,8 @@ const constants = require("../../scripts/constant/constant"); const addLiquidity = require("../../scripts/addLiquidity"); const { ethers } = hre; +const ALCHEMY_ID = process.env.ALCHEMY_ID; + describe("Aave V1", function() { const connectorName = "AAVEV1-TEST-A"; @@ -23,19 +25,34 @@ describe("Aave V1", function() { let masterSigner; before(async () => { - [wallet0, wallet1] = await ethers.getSigners(); - masterSigner = await getMasterSigner(); - instaConnectorsV2 = await ethers.getContractAt( - abis.core.connectorsV2, - addresses.core.connectorsV2 - ); - connector = await deployAndEnableConnector({ - connectorName, - contractArtifact: ConnectV2AaveV1, - signer: masterSigner, - connectors: instaConnectorsV2, - }); - console.log("Connector address", connector.address); + try { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 12796965, + }, + }, + ], + }); + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2AaveV1, + signer: masterSigner, + connectors: instaConnectorsV2, + }); + console.log("Connector address", connector.address); + } catch (err) { + console.log("error", err); + } }); it("should have contracts deployed", async () => { diff --git a/test/aave/v2.test.js b/test/aave/v2.test.js index 09b023e6..67aac908 100644 --- a/test/aave/v2.test.js +++ b/test/aave/v2.test.js @@ -23,6 +23,17 @@ describe("Aave V2", function() { let masterSigner; before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 12796965, + }, + }, + ], + }); [wallet0, wallet1] = await ethers.getSigners(); masterSigner = await getMasterSigner(); instaConnectorsV2 = await ethers.getContractAt( diff --git a/test/b.protocol/b.compound.test.js b/test/b.protocol/b.compound.test.js index 5d08235d..582a01ca 100644 --- a/test/b.protocol/b.compound.test.js +++ b/test/b.protocol/b.compound.test.js @@ -13,7 +13,7 @@ const abis = require("../../scripts/constant/abis"); const constants = require("../../scripts/constant/constant"); const tokens = require("../../scripts/constant/tokens"); -const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/b.protocol/compound/main.sol/ConnectV1BCompound.json") +const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/b.protocol/compound/main.sol/ConnectV2BCompound.json") describe("B.Compound", function () { const connectorName = "B.COMPOUND-TEST-A" @@ -26,6 +26,17 @@ describe("B.Compound", function () { const wallets = provider.getWallets() const [wallet0, wallet1, wallet2, wallet3] = wallets before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3) instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); connector = await deployAndEnableConnector({ diff --git a/test/b.protocol/b.liquity.test.js b/test/b.protocol/b.liquity.test.js index 8a180345..c50f111c 100644 --- a/test/b.protocol/b.liquity.test.js +++ b/test/b.protocol/b.liquity.test.js @@ -20,43 +20,54 @@ const LUSD_WHALE = "0x66017D22b0f8556afDd19FC67041899Eb65a21bb" // stability poo const BAMM_ADDRESS = "0x0d3AbAA7E088C2c82f54B2f47613DA438ea8C598" describe("B.Liquity", function () { - const connectorName = "B.LIQUITY-TEST-A" - - let dsaWallet0; - let dsaWallet1; - let masterSigner; - let instaConnectorsV2; - let connector; - let manager; - let vat; - let lusd; - let bammToken; - let stabilityPool; - - const wallets = provider.getWallets() - const [wallet0, wallet1, wallet2, wallet3] = wallets - before(async () => { - masterSigner = await getMasterSigner(wallet3) - instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); - connector = await deployAndEnableConnector({ - connectorName, - contractArtifact: connectorLiquityArtifacts, - signer: masterSigner, - connectors: instaConnectorsV2 - }) + const connectorName = "B.LIQUITY-TEST-A" - lusd = await ethers.getContractAt("../artifacts/contracts/mainnet/common/interfaces.sol:TokenInterface", "0x5f98805A4E8be255a32880FDeC7F6728C6568bA0") - bammToken = await ethers.getContractAt("../artifacts/contracts/mainnet/connectors/b.protocol/liquity/interface.sol:BAMMLike", BAMM_ADDRESS) - stabilityPool = await ethers.getContractAt("../artifacts/contracts/mainnet/connectors/b.protocol/liquity/interface.sol:StabilityPoolLike", "0x66017D22b0f8556afDd19FC67041899Eb65a21bb") + let dsaWallet0; + let dsaWallet1; + let masterSigner; + let instaConnectorsV2; + let connector; + let manager; + let vat; + let lusd; + let bammToken; + let stabilityPool; - console.log("Connector address", connector.address) + const wallets = provider.getWallets() + const [wallet0, wallet1, wallet2, wallet3] = wallets + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 12996875, + }, + }, + ], + }); + masterSigner = await getMasterSigner(wallet3) + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: connectorLiquityArtifacts, + signer: masterSigner, + connectors: instaConnectorsV2 + }) + + lusd = await ethers.getContractAt("../artifacts/contracts/mainnet/common/interfaces.sol:TokenInterface", "0x5f98805A4E8be255a32880FDeC7F6728C6568bA0") + bammToken = await ethers.getContractAt("../artifacts/contracts/mainnet/connectors/b.protocol/liquity/interface.sol:BAMMLike", BAMM_ADDRESS) + stabilityPool = await ethers.getContractAt("../artifacts/contracts/mainnet/connectors/b.protocol/liquity/interface.sol:StabilityPoolLike", "0x66017D22b0f8556afDd19FC67041899Eb65a21bb") + + console.log("Connector address", connector.address) }) it("test veryClose.", async function () { expect(veryClose(1000001, 1000000)).to.be.true - expect(veryClose(1000000, 1000001)).to.be.true + expect(veryClose(1000000, 1000001)).to.be.true expect(veryClose(1003000, 1000001)).to.be.false - expect(veryClose(1000001, 1000300)).to.be.false + expect(veryClose(1000001, 1000300)).to.be.false }); it("Should have contracts deployed.", async function () { @@ -68,44 +79,44 @@ describe("B.Liquity", function () { describe("DSA wallet setup", function () { it("Should build DSA v2", async function () { - dsaWallet0 = await buildDSAv2(wallet0.address) - expect(!!dsaWallet0.address).to.be.true; + dsaWallet0 = await buildDSAv2(wallet0.address) + expect(!!dsaWallet0.address).to.be.true; - dsaWallet1 = await buildDSAv2(wallet1.address) - expect(!!dsaWallet1.address).to.be.true; + dsaWallet1 = await buildDSAv2(wallet1.address) + expect(!!dsaWallet1.address).to.be.true; }); it("Deposit LUSD into DSA wallet", async function () { - await hre.network.provider.request({ - method: "hardhat_impersonateAccount", - params: [LUSD_WHALE], - }); + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [LUSD_WHALE], + }); - const signer = await hre.ethers.provider.getSigner(LUSD_WHALE); - await lusd.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("100000")) + const signer = await hre.ethers.provider.getSigner(LUSD_WHALE); + await lusd.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("100000")) - expect(await lusd.balanceOf(dsaWallet0.address)).to.equal(ethers.utils.parseEther("100000")); + expect(await lusd.balanceOf(dsaWallet0.address)).to.equal(ethers.utils.parseEther("100000")); }); }); describe("Main", function () { it("should deposit 10k LUSD", async function () { - const totalSupplyBefore = await bammToken.totalSupply(); - const lusdBalanceBefore = await stabilityPool.getCompoundedLUSDDeposit(BAMM_ADDRESS); - const amount = ethers.utils.parseEther("10000"); - const spells = [ - { - connector: connectorName, - method: "deposit", - args: [amount, 0, 0, 0] - } - ] + const totalSupplyBefore = await bammToken.totalSupply(); + const lusdBalanceBefore = await stabilityPool.getCompoundedLUSDDeposit(BAMM_ADDRESS); + const amount = ethers.utils.parseEther("10000"); + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [amount, 0, 0, 0] + } + ] - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() - const expectedBalance = totalSupplyBefore.mul(amount).div(lusdBalanceBefore) - expect(veryClose(expectedBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true + const expectedBalance = totalSupplyBefore.mul(amount).div(lusdBalanceBefore) + expect(veryClose(expectedBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true }); it("should deposit all LUSD", async function () { @@ -115,11 +126,11 @@ describe("B.Liquity", function () { const balanceBefore = await bammToken.balanceOf(dsaWallet0.address) const spells = [ - { - connector: connectorName, - method: "deposit", - args: [amount, 0, 0, 0] - } + { + connector: connectorName, + method: "deposit", + args: [amount, 0, 0, 0] + } ] const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) @@ -128,54 +139,54 @@ describe("B.Liquity", function () { const expectedBalance = (totalSupplyBefore.mul(ethers.utils.parseEther("90000")).div(lusdBalanceBefore)).add(balanceBefore) expect(veryClose(expectedBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true }); - + it("should withdraw half of the shares", async function () { const balanceBefore = await bammToken.balanceOf(dsaWallet0.address) const halfBalance = balanceBefore.div("2") const spells = [ - { - connector: connectorName, - method: "withdraw", - args: [halfBalance, 0, 0, 0] - } + { + connector: connectorName, + method: "withdraw", + args: [halfBalance, 0, 0, 0] + } ] const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) const receipt = await tx.wait() expect(veryClose(halfBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true - expect(veryClose(ethers.utils.parseEther("50000"), await lusd.balanceOf(dsaWallet0.address))).to.be.true + expect(veryClose(ethers.utils.parseEther("50000"), await lusd.balanceOf(dsaWallet0.address))).to.be.true }); it("should withdraw all the shares", async function () { const amount = web3.utils.toBN("2").pow(web3.utils.toBN("256")).sub(web3.utils.toBN("1")); const spells = [ - { - connector: connectorName, - method: "withdraw", - args: [amount, 0, 0, 0] - } + { + connector: connectorName, + method: "withdraw", + args: [amount, 0, 0, 0] + } ] const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) const receipt = await tx.wait() - expect(veryClose(ethers.utils.parseEther("100000"), await lusd.balanceOf(dsaWallet0.address))).to.be.true - }); + expect(veryClose(ethers.utils.parseEther("100000"), await lusd.balanceOf(dsaWallet0.address))).to.be.true + }); }) }) function veryClose(n1, n2) { - n1 = web3.utils.toBN(n1) - n2 = web3.utils.toBN(n2) + n1 = web3.utils.toBN(n1) + n2 = web3.utils.toBN(n2) - _10000 = web3.utils.toBN(10000) - _9999 = web3.utils.toBN(9999) + _10000 = web3.utils.toBN(10000) + _9999 = web3.utils.toBN(9999) - if(n1.mul(_10000).lt(n2.mul(_9999))) return false - if(n2.mul(_10000).lt(n1.mul(_9999))) return false + if (n1.mul(_10000).lt(n2.mul(_9999))) return false + if (n2.mul(_10000).lt(n1.mul(_9999))) return false - return true + return true } diff --git a/test/b.protocol/b.maker.test.js b/test/b.protocol/b.maker.test.js index 57b687cd..5cc97daa 100644 --- a/test/b.protocol/b.maker.test.js +++ b/test/b.protocol/b.maker.test.js @@ -13,23 +13,34 @@ const abis = require("../../scripts/constant/abis"); const constants = require("../../scripts/constant/constant"); const tokens = require("../../scripts/constant/tokens"); -const connectorMakerArtifacts = require("../../artifacts/contracts/mainnet/connectors/b.protocol/makerdao/main.sol/ConnectV1BMakerDAO.json") +const connectorMakerArtifacts = require("../../artifacts/contracts/mainnet/connectors/b.protocol/makerdao/main.sol/ConnectV2BMakerDAO.json") describe("B.Maker", function () { const connectorName = "B.MAKER-TEST-A" - + let dsaWallet0; - let dsaWallet1; + let dsaWallet1; let masterSigner; let instaConnectorsV2; let connector; let manager; let vat; let dai; - + const wallets = provider.getWallets() const [wallet0, wallet1, wallet2, wallet3] = wallets before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 12696000, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3) instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); connector = await deployAndEnableConnector({ @@ -44,263 +55,263 @@ describe("B.Maker", function () { dai = await ethers.getContractAt("../artifacts/contracts/mainnet/common/interfaces.sol:TokenInterface", tokens.dai.address) console.log("Connector address", connector.address) - }) + }) - it("test veryClose.", async function () { - expect(veryClose(1000001, 1000000)).to.be.true - expect(veryClose(1000000, 1000001)).to.be.true - expect(veryClose(1003000, 1000001)).to.be.false - expect(veryClose(1000001, 1000300)).to.be.false - }); - - it("Should have contracts deployed.", async function () { - expect(!!instaConnectorsV2.address).to.be.true; - expect(!!connector.address).to.be.true; - expect(!!masterSigner.address).to.be.true; - expect(await connector.name()).to.be.equal("B.MakerDAO-v1.0"); - }); - - describe("DSA wallet setup", function () { - it("Should build DSA v2", async function () { - dsaWallet0 = await buildDSAv2(wallet0.address) - expect(!!dsaWallet0.address).to.be.true; - - dsaWallet1 = await buildDSAv2(wallet1.address) - expect(!!dsaWallet1.address).to.be.true; + it("test veryClose.", async function () { + expect(veryClose(1000001, 1000000)).to.be.true + expect(veryClose(1000000, 1000001)).to.be.true + expect(veryClose(1003000, 1000001)).to.be.false + expect(veryClose(1000001, 1000300)).to.be.false }); - it("Deposit ETH into DSA wallet", async function () { - await wallet0.sendTransaction({ - to: dsaWallet0.address, - value: ethers.utils.parseEther("10") + it("Should have contracts deployed.", async function () { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!masterSigner.address).to.be.true; + expect(await connector.name()).to.be.equal("B.MakerDAO-v1.0"); + }); + + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(wallet0.address) + expect(!!dsaWallet0.address).to.be.true; + + dsaWallet1 = await buildDSAv2(wallet1.address) + expect(!!dsaWallet1.address).to.be.true; }); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); - await wallet1.sendTransaction({ - to: dsaWallet1.address, - value: ethers.utils.parseEther("10") + it("Deposit ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + + await wallet1.sendTransaction({ + to: dsaWallet1.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("10")); }); - expect(await ethers.provider.getBalance(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("10")); - }); - }); - - describe("Main", function () { - let vault - let ilk - let urn - - it("Should open ETH-A vault Maker", async function () { - vault = Number(await manager.cdpi()) + 1 - const spells = [ - { - connector: connectorName, - method: "open", - args: ["ETH-A"] - } - ] - - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() - - expect(await manager.owns(vault)).to.be.equal(dsaWallet0.address) - - ilk = await manager.ilks(vault) - expect(ilk).to.be.equal("0x4554482d41000000000000000000000000000000000000000000000000000000") - - urn = await manager.urns(vault) }); - it("Should deposit", async function () { - const amount = ethers.utils.parseEther("7") // 7 ETH - const setId = "83478237" + describe("Main", function () { + let vault + let ilk + let urn - const spells = [ - { - connector: connectorName, - method: "deposit", - args: [vault, amount, 0, setId] - } - ] + it("Should open ETH-A vault Maker", async function () { + vault = Number(await manager.cdpi()) + 1 + const spells = [ + { + connector: connectorName, + method: "open", + args: ["ETH-A"] + } + ] - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("3")) + expect(await manager.owns(vault)).to.be.equal(dsaWallet0.address) - const urnData = await vat.urns(ilk, urn) - expect(urnData[0]).to.be.equal(amount) // ink - expect(urnData[1]).to.be.equal("0") // art + ilk = await manager.ilks(vault) + expect(ilk).to.be.equal("0x4554482d41000000000000000000000000000000000000000000000000000000") - }); + urn = await manager.urns(vault) + }); - it("Should withdraw", async function () { - const amount = ethers.utils.parseEther("1") // 1 ETH - const setId = "83478237" + it("Should deposit", async function () { + const amount = ethers.utils.parseEther("7") // 7 ETH + const setId = "83478237" - const spells = [ - { - connector: connectorName, - method: "withdraw", - args: [vault, amount, 0, setId] - } - ] + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [vault, amount, 0, setId] + } + ] - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("4")) + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("3")) - const urnData = await vat.urns(ilk, urn) - expect(urnData[0]).to.be.equal(ethers.utils.parseEther("6")) // ink - expect(urnData[1]).to.be.equal("0") // art + const urnData = await vat.urns(ilk, urn) + expect(urnData[0]).to.be.equal(amount) // ink + expect(urnData[1]).to.be.equal("0") // art - }); + }); - it("Should borrow", async function () { - const amount = ethers.utils.parseEther("6000") // 6000 dai - const setId = "83478237" + it("Should withdraw", async function () { + const amount = ethers.utils.parseEther("1") // 1 ETH + const setId = "83478237" - const spells = [ - { - connector: connectorName, - method: "borrow", - args: [vault, amount, 0, setId] - } - ] + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [vault, amount, 0, setId] + } + ] - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() - const urnData = await vat.urns(ilk, urn) - expect(urnData[0]).to.be.equal(ethers.utils.parseEther("6")) // ink - expect(urnData[1]).to.be.equal(await daiToArt(vat, ilk, amount)) // art - - expect(await dai.balanceOf(dsaWallet0.address)).to.be.equal(amount) - }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("4")) - it("Should repay", async function () { - const amount = ethers.utils.parseEther("500") // 500 dai - const setId = "83478237" + const urnData = await vat.urns(ilk, urn) + expect(urnData[0]).to.be.equal(ethers.utils.parseEther("6")) // ink + expect(urnData[1]).to.be.equal("0") // art - const spells = [ - { - connector: connectorName, - method: "payback", - args: [vault, amount, 0, setId] - } - ] + }); - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() + it("Should borrow", async function () { + const amount = ethers.utils.parseEther("6000") // 6000 dai + const setId = "83478237" - const urnData = await vat.urns(ilk, urn) - expect(urnData[0]).to.be.equal(ethers.utils.parseEther("6")) // ink - expect(urnData[1]).to.be.equal(await daiToArt(vat, ilk, ethers.utils.parseEther("5500"))) // art - expect(await dai.balanceOf(dsaWallet0.address)).to.be.equal(ethers.utils.parseEther("5500")) - }); + const spells = [ + { + connector: connectorName, + method: "borrow", + args: [vault, amount, 0, setId] + } + ] - it("Should depositAndBorrow", async function () { - const borrowAmount = ethers.utils.parseEther("1000") // 1000 dai - const depositAmount = ethers.utils.parseEther("1") // 1 dai - - const setId = "83478237" + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() - const spells = [ - { - connector: connectorName, - method: "depositAndBorrow", - args: [vault, depositAmount, borrowAmount, 0, 0, 0, 0] - } - ] + const urnData = await vat.urns(ilk, urn) + expect(urnData[0]).to.be.equal(ethers.utils.parseEther("6")) // ink + expect(urnData[1]).to.be.equal(await daiToArt(vat, ilk, amount)) // art - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() + expect(await dai.balanceOf(dsaWallet0.address)).to.be.equal(amount) + }); - const urnData = await vat.urns(ilk, urn) - expect(urnData[0]).to.be.equal(ethers.utils.parseEther("7")) // ink - expect(await dai.balanceOf(dsaWallet0.address)).to.be.equal(ethers.utils.parseEther("6500")) - // calculation is not precise as the jug was dripped - expect(veryClose(urnData[1], await daiToArt(vat, ilk, ethers.utils.parseEther("6500")))).to.be.true - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("1")) - }); + it("Should repay", async function () { + const amount = ethers.utils.parseEther("500") // 500 dai + const setId = "83478237" - it("Should close", async function () { - // open a new vault - const newVault = vault + 1 - let spells = [ - { - connector: connectorName, - method: "open", - args: ["ETH-A"] - } - ] + const spells = [ + { + connector: connectorName, + method: "payback", + args: [vault, amount, 0, setId] + } + ] - let tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) - let receipt = await tx.wait() - - expect(await manager.owns(newVault)).to.be.equal(dsaWallet1.address) + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() - ilk = await manager.ilks(newVault) - expect(ilk).to.be.equal("0x4554482d41000000000000000000000000000000000000000000000000000000") + const urnData = await vat.urns(ilk, urn) + expect(urnData[0]).to.be.equal(ethers.utils.parseEther("6")) // ink + expect(urnData[1]).to.be.equal(await daiToArt(vat, ilk, ethers.utils.parseEther("5500"))) // art + expect(await dai.balanceOf(dsaWallet0.address)).to.be.equal(ethers.utils.parseEther("5500")) + }); - urn = await manager.urns(newVault) + it("Should depositAndBorrow", async function () { + const borrowAmount = ethers.utils.parseEther("1000") // 1000 dai + const depositAmount = ethers.utils.parseEther("1") // 1 dai - // deposit and borrow - const borrowAmount = ethers.utils.parseEther("6000") // 6000 dai - const depositAmount = ethers.utils.parseEther("5") // 5 ETH - - spells = [ - { - connector: connectorName, - method: "depositAndBorrow", - args: [newVault, depositAmount, borrowAmount, 0, 0, 0, 0] - } - ] + const setId = "83478237" - tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) - receipt = await tx.wait() + const spells = [ + { + connector: connectorName, + method: "depositAndBorrow", + args: [vault, depositAmount, borrowAmount, 0, 0, 0, 0] + } + ] - const setId = 0 + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() - // repay borrow - spells = [ - { - connector: connectorName, - method: "payback", - args: [newVault, borrowAmount, 0, setId] - } - ] + const urnData = await vat.urns(ilk, urn) + expect(urnData[0]).to.be.equal(ethers.utils.parseEther("7")) // ink + expect(await dai.balanceOf(dsaWallet0.address)).to.be.equal(ethers.utils.parseEther("6500")) + // calculation is not precise as the jug was dripped + expect(veryClose(urnData[1], await daiToArt(vat, ilk, ethers.utils.parseEther("6500")))).to.be.true + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("1")) + }); - tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) - receipt = await tx.wait() + it("Should close", async function () { + // open a new vault + const newVault = vault + 1 + let spells = [ + { + connector: connectorName, + method: "open", + args: ["ETH-A"] + } + ] - // withdraw deposit - spells = [ - { - connector: connectorName, - method: "withdraw", - args: [newVault, depositAmount, 0, setId] - } - ] + let tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) + let receipt = await tx.wait() - tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) - receipt = await tx.wait() + expect(await manager.owns(newVault)).to.be.equal(dsaWallet1.address) - // close - spells = [ - { - connector: connectorName, - method: "close", - args: [newVault] - } - ] + ilk = await manager.ilks(newVault) + expect(ilk).to.be.equal("0x4554482d41000000000000000000000000000000000000000000000000000000") - tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) - receipt = await tx.wait() + urn = await manager.urns(newVault) - expect(await manager.owns(newVault)).not.to.be.equal(dsaWallet1.address) - }); - }) + // deposit and borrow + const borrowAmount = ethers.utils.parseEther("6000") // 6000 dai + const depositAmount = ethers.utils.parseEther("5") // 5 ETH + + spells = [ + { + connector: connectorName, + method: "depositAndBorrow", + args: [newVault, depositAmount, borrowAmount, 0, 0, 0, 0] + } + ] + + tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) + receipt = await tx.wait() + + const setId = 0 + + // repay borrow + spells = [ + { + connector: connectorName, + method: "payback", + args: [newVault, borrowAmount, 0, setId] + } + ] + + tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) + receipt = await tx.wait() + + // withdraw deposit + spells = [ + { + connector: connectorName, + method: "withdraw", + args: [newVault, depositAmount, 0, setId] + } + ] + + tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) + receipt = await tx.wait() + + // close + spells = [ + { + connector: connectorName, + method: "close", + args: [newVault] + } + ] + + tx = await dsaWallet1.connect(wallet1).cast(...encodeSpells(spells), wallet1.address) + receipt = await tx.wait() + + expect(await manager.owns(newVault)).not.to.be.equal(dsaWallet1.address) + }); + }) }) async function daiToArt(vat, ilk, dai) { @@ -317,10 +328,10 @@ function veryClose(n1, n2) { n2 = web3.utils.toBN(n2) _10000 = web3.utils.toBN(10000) - _9999 = web3.utils.toBN(9999) + _9999 = web3.utils.toBN(9999) - if(n1.mul(_10000).lt(n2.mul(_9999))) return false - if(n2.mul(_10000).lt(n1.mul(_9999))) return false + if (n1.mul(_10000).lt(n2.mul(_9999))) return false + if (n2.mul(_10000).lt(n1.mul(_9999))) return false return true } diff --git a/test/basic-ERC1155/ERC1155-transfer.js b/test/basic-ERC1155/ERC1155-transfer.js index cd2f2b12..a0835384 100644 --- a/test/basic-ERC1155/ERC1155-transfer.js +++ b/test/basic-ERC1155/ERC1155-transfer.js @@ -2,7 +2,7 @@ const { expect } = require("chai"); const hre = require("hardhat"); const { web3, deployments, waffle, ethers } = hre; const { provider, deployContract } = waffle -const {abi: implementationsABI} = require("../../scripts/constant/abi/core/InstaImplementations.json") +const { abi: implementationsABI } = require("../../scripts/constant/abi/core/InstaImplementations.json") const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js") const buildDSAv2 = require("../../scripts/buildDSAv2") @@ -38,6 +38,17 @@ describe("BASIC-ERC1155", function () { const wallets = provider.getWallets() const [wallet0, wallet1, wallet2, wallet3] = wallets before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: [TOKEN_OWNER_ADDR], diff --git a/test/basic-ERC721/ERC721-transfer.js b/test/basic-ERC721/ERC721-transfer.js index 01a5de0b..73dce8ca 100644 --- a/test/basic-ERC721/ERC721-transfer.js +++ b/test/basic-ERC721/ERC721-transfer.js @@ -2,7 +2,7 @@ const { expect } = require("chai"); const hre = require("hardhat"); const { web3, deployments, waffle, ethers } = hre; const { provider, deployContract } = waffle -const {abi: implementationsABI} = require("../../scripts/constant/abi/core/InstaImplementations.json") +const { abi: implementationsABI } = require("../../scripts/constant/abi/core/InstaImplementations.json") const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js") const buildDSAv2 = require("../../scripts/buildDSAv2") @@ -38,6 +38,17 @@ describe("BASIC-ERC721", function () { const wallets = provider.getWallets() const [wallet0, wallet1, wallet2, wallet3] = wallets before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: [TOKEN_OWNER_ADDR], diff --git a/test/compound/compound.test.js b/test/compound/compound.test.js index d1c77475..0719eb94 100644 --- a/test/compound/compound.test.js +++ b/test/compound/compound.test.js @@ -17,15 +17,26 @@ const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/co describe("Compound", function () { const connectorName = "COMPOUND-TEST-A" - + let dsaWallet0 let masterSigner; let instaConnectorsV2; let connector; - + const wallets = provider.getWallets() const [wallet0, wallet1, wallet2, wallet3] = wallets before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3) instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); connector = await deployAndEnableConnector({ @@ -35,93 +46,93 @@ describe("Compound", function () { connectors: instaConnectorsV2 }) console.log("Connector address", connector.address) - }) + }) - it("Should have contracts deployed.", async function () { - expect(!!instaConnectorsV2.address).to.be.true; - expect(!!connector.address).to.be.true; - expect(!!masterSigner.address).to.be.true; - }); - - describe("DSA wallet setup", function () { - it("Should build DSA v2", async function () { - dsaWallet0 = await buildDSAv2(wallet0.address) - expect(!!dsaWallet0.address).to.be.true; + it("Should have contracts deployed.", async function () { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!masterSigner.address).to.be.true; }); - it("Deposit ETH into DSA wallet", async function () { - await wallet0.sendTransaction({ - to: dsaWallet0.address, - value: ethers.utils.parseEther("10") + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(wallet0.address) + expect(!!dsaWallet0.address).to.be.true; }); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); - }); - }); - describe("Main", function () { - - it("Should deposit ETH in Compound", async function () { - const amount = ethers.utils.parseEther("1") // 1 ETH - const spells = [ - { - connector: connectorName, - method: "deposit", - args: ["ETH-A", amount, 0, 0] - } - ] - - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("9")); + it("Deposit ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + }); }); - it("Should borrow and payback DAI from Compound", async function () { - const amount = ethers.utils.parseEther("100") // 100 DAI - const setId = "83478237" - const spells = [ - { - connector: connectorName, - method: "borrow", - args: ["DAI-A", amount, 0, setId] - }, - { - connector: connectorName, - method: "payback", - args: ["DAI-A", 0, setId, 0] - } - ] + describe("Main", function () { - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("9")); - }); + it("Should deposit ETH in Compound", async function () { + const amount = ethers.utils.parseEther("1") // 1 ETH + const spells = [ + { + connector: connectorName, + method: "deposit", + args: ["ETH-A", amount, 0, 0] + } + ] - it("Should deposit all ETH in Compound", async function () { - const spells = [ - { - connector: connectorName, - method: "deposit", - args: ["ETH-A", constants.max_value, 0, 0] - } - ] + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("9")); + }); - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("0")); - }); + it("Should borrow and payback DAI from Compound", async function () { + const amount = ethers.utils.parseEther("100") // 100 DAI + const setId = "83478237" + const spells = [ + { + connector: connectorName, + method: "borrow", + args: ["DAI-A", amount, 0, setId] + }, + { + connector: connectorName, + method: "payback", + args: ["DAI-A", 0, setId, 0] + } + ] - it("Should withdraw all ETH from Compound", async function () { - const spells = [ - { - connector: connectorName, - method: "withdraw", - args: ["ETH-A", constants.max_value, 0, 0] - } - ] + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("9")); + }); - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) - const receipt = await tx.wait() - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); - }); - }) + it("Should deposit all ETH in Compound", async function () { + const spells = [ + { + connector: connectorName, + method: "deposit", + args: ["ETH-A", constants.max_value, 0, 0] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("0")); + }); + + it("Should withdraw all ETH from Compound", async function () { + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: ["ETH-A", constants.max_value, 0, 0] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + const receipt = await tx.wait() + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + }); + }) }) diff --git a/test/instapool/instapool.test.js b/test/instapool/instapool.test.js index fa211e8d..cbae67c2 100644 --- a/test/instapool/instapool.test.js +++ b/test/instapool/instapool.test.js @@ -17,25 +17,36 @@ const tokens = require("../../scripts/constant/tokens"); const connectV2CompoundArtifacts = require("../../artifacts/contracts/mainnet/connectors/compound/main.sol/ConnectV2Compound.json") describe("Instapool", function () { - const connectorName = "COMPOUND-TEST-A" - - let dsaWallet0 - let masterSigner; - let instaConnectorsV2; - let connector; - - const wallets = provider.getWallets() - const [wallet0, wallet1, wallet2, wallet3] = wallets - before(async () => { - masterSigner = await getMasterSigner(wallet3) - instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); - connector = await deployAndEnableConnector({ - connectorName, - contractArtifact: connectV2CompoundArtifacts, - signer: masterSigner, - connectors: instaConnectorsV2 - }) - console.log("Connector address", connector.address) + const connectorName = "COMPOUND-TEST-A" + + let dsaWallet0 + let masterSigner; + let instaConnectorsV2; + let connector; + + const wallets = provider.getWallets() + const [wallet0, wallet1, wallet2, wallet3] = wallets + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); + masterSigner = await getMasterSigner(wallet3) + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: connectV2CompoundArtifacts, + signer: masterSigner, + connectors: instaConnectorsV2 + }) + console.log("Connector address", connector.address) }) it("Should have contracts deployed.", async function () { @@ -46,64 +57,64 @@ describe("Instapool", function () { describe("DSA wallet setup", function () { it("Should build DSA v2", async function () { - dsaWallet0 = await buildDSAv2(wallet0.address) - expect(!!dsaWallet0.address).to.be.true; + dsaWallet0 = await buildDSAv2(wallet0.address) + expect(!!dsaWallet0.address).to.be.true; }); it("Deposit ETH into DSA wallet", async function () { - await wallet0.sendTransaction({ - to: dsaWallet0.address, - value: ethers.utils.parseEther("10") - }); - expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); }); }); describe("Main", function () { it("Should take 100 ETH flashloan from Instapool", async function () { - const amount = ethers.utils.parseEther("1") // 1 ETH - const flashloanAmount = ethers.utils.parseEther("100") // 100 ETH - const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + const amount = ethers.utils.parseEther("1") // 1 ETH + const flashloanAmount = ethers.utils.parseEther("100") // 100 ETH + const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" - const IdOne = "2878734423" - const IdTwo = "783243246" + const IdOne = "2878734423" + const IdTwo = "783243246" - const spells = [ - { - connector: connectorName, - method: "deposit", - args: ["ETH-A", flashloanAmount, 0, IdOne] - }, - { - connector: connectorName, - method: "withdraw", - args: ["ETH-A", amount, IdOne, IdTwo] - }, - { - connector: "INSTAPOOL-A", - method: "flashPayback", - args: [ethAddress, flashloanAmount, IdTwo, 0], - } - ] + const spells = [ + { + connector: connectorName, + method: "deposit", + args: ["ETH-A", flashloanAmount, 0, IdOne] + }, + { + connector: connectorName, + method: "withdraw", + args: ["ETH-A", amount, IdOne, IdTwo] + }, + { + connector: "INSTAPOOL-A", + method: "flashPayback", + args: [ethAddress, flashloanAmount, IdTwo, 0], + } + ] - const calldata = encodeFlashcastData(spells); + const calldata = encodeFlashcastData(spells); - const spells2 = [ - { - connector: "INSTAPOOL-A", - method: "flashBorrowAndCast", - args: [ - "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", - flashloanAmount, - 0, // route - calldata, - ], - } - ] + const spells2 = [ + { + connector: "INSTAPOOL-A", + method: "flashBorrowAndCast", + args: [ + "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + flashloanAmount, + 0, // route + calldata, + ], + } + ] - const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells2), wallet1.address) - const receipt = await tx.wait() + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells2), wallet1.address) + const receipt = await tx.wait() }); }) }) diff --git a/test/liquity/liquity.test.js b/test/liquity/liquity.test.js index 7b8dce39..e2acde15 100644 --- a/test/liquity/liquity.test.js +++ b/test/liquity/liquity.test.js @@ -21,6 +21,17 @@ describe("Liquity", () => { let liquity = null; before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); liquity = await helpers.deployAndConnect(contracts, true); expect(liquity.troveManager.address).to.exist; expect(liquity.borrowerOperations.address).to.exist; diff --git a/test/mappings/mappings.test.js b/test/mappings/mappings.test.js index 4702d54e..9a6546a3 100644 --- a/test/mappings/mappings.test.js +++ b/test/mappings/mappings.test.js @@ -20,6 +20,17 @@ describe("Test InstaMapping contract", () => { const testRoleAddress = "0x2971AdFa57b20E5a416aE5a708A8655A9c74f723"; before("get signers", async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 12796965, + }, + }, + ], + }); [account] = await ethers.getSigners(); const IndexContract = await ethers.getContractAt( @@ -33,6 +44,11 @@ describe("Test InstaMapping contract", () => { params: [masterAddress], }); + await network.provider.send("hardhat_setBalance", [ + masterAddress, + "0x1000000000000000000000000", + ]); + instaMaster = await ethers.getSigner(masterAddress); }); diff --git a/test/uniswap/uniswap.test.js b/test/uniswap/uniswap.test.js index d7a2e407..bd0efdad 100644 --- a/test/uniswap/uniswap.test.js +++ b/test/uniswap/uniswap.test.js @@ -51,6 +51,17 @@ describe("UniswapV3", function () { const wallets = provider.getWallets() const [wallet0, wallet1, wallet2, wallet3] = wallets before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13005785, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3) instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); nftManager = await ethers.getContractAt(nftManagerAbi, "0xC36442b4a4522E871399CD717aBDD847Ab11FE88"); @@ -184,7 +195,7 @@ describe("UniswapV3", function () { const data = await nftManager.positions(tokenIds[0]) expect(data.liquidity).to.be.equals(liquidities[0]); - }) + }).timeout(10000000000); it("Should deposit successfully", async function () { const daiAmount = ethers.utils.parseEther("400") // 1 ETH diff --git a/test/uniswapStake/uniswapStake.test.js b/test/uniswapStake/uniswapStake.test.js index 043bd07f..dee656ee 100644 --- a/test/uniswapStake/uniswapStake.test.js +++ b/test/uniswapStake/uniswapStake.test.js @@ -48,6 +48,17 @@ describe("UniswapV3", function () { const wallets = provider.getWallets() const [wallet0, wallet1, wallet2, wallet3] = wallets before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 13300000, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3) instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); nftManager = await ethers.getContractAt(nftManagerAbi, "0xC36442b4a4522E871399CD717aBDD847Ab11FE88"); diff --git a/test/yearn/yearn.test.js b/test/yearn/yearn.test.js index 409161b0..596029d3 100644 --- a/test/yearn/yearn.test.js +++ b/test/yearn/yearn.test.js @@ -32,6 +32,17 @@ describe("Yearn", function () { const wallets = provider.getWallets() const [wallet0, wallet1, wallet2, wallet3] = wallets before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 12996975, + }, + }, + ], + }); masterSigner = await getMasterSigner(wallet3) instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); connector = await deployAndEnableConnector({