From 84075c39b63c5277f63cca91018d997b10d3bad7 Mon Sep 17 00:00:00 2001 From: cryptoDev222 Date: Fri, 17 Sep 2021 14:44:09 -0500 Subject: [PATCH] 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