diff --git a/contracts/polygon/connectors/quickswap/events.sol b/contracts/polygon/connectors/quickswap/events.sol index c8fc41d6..5f8c0f48 100644 --- a/contracts/polygon/connectors/quickswap/events.sol +++ b/contracts/polygon/connectors/quickswap/events.sol @@ -20,7 +20,11 @@ contract Events { uint256 getId, uint256[] setId ); +<<<<<<< HEAD +======= + +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 event LogBuy( address indexed buyToken, address indexed sellToken, @@ -38,4 +42,8 @@ contract Events { uint256 getId, uint256 setId ); +<<<<<<< HEAD } +======= +} +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 diff --git a/contracts/polygon/connectors/quickswap/interface.sol b/contracts/polygon/connectors/quickswap/interface.sol index 438d89c3..65c0482d 100644 --- a/contracts/polygon/connectors/quickswap/interface.sol +++ b/contracts/polygon/connectors/quickswap/interface.sol @@ -2,12 +2,16 @@ pragma solidity ^0.7.0; interface IQuickSwapRouter { function factory() external pure returns (address); +<<<<<<< HEAD +======= +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 function WETH() external pure returns (address); function addLiquidity( address tokenA, address tokenB, +<<<<<<< HEAD uint256 amountADesired, uint256 amountBDesired, uint256 amountAMin, @@ -94,4 +98,53 @@ interface IQuickSwapFactory { function createPair(address tokenA, address tokenB) external returns (address pair); +======= + uint amountADesired, + uint amountBDesired, + uint amountAMin, + uint amountBMin, + address to, + uint deadline + ) external returns (uint amountA, uint amountB, uint liquidity); + function removeLiquidity( + address tokenA, + address tokenB, + uint liquidity, + uint amountAMin, + uint amountBMin, + address to, + uint deadline + ) external returns (uint amountA, uint amountB); + function swapExactTokensForTokens( + uint amountIn, + uint amountOutMin, + address[] calldata path, + address to, + uint deadline + ) external returns (uint[] memory amounts); + function swapTokensForExactTokens( + uint amountOut, + uint amountInMax, + address[] calldata path, + address to, + uint deadline + ) external returns (uint[] memory amounts); + + function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB); + function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut); + function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn); + function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts); + function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts); +} + +interface IQuickSwapFactory { + function getPair(address tokenA, address tokenB) external view returns (address pair); + function allPairs(uint) external view returns (address pair); + function allPairsLength() external view returns (uint); + + function feeTo() external view returns (address); + function feeToSetter() external view returns (address); + + function createPair(address tokenA, address tokenB) external returns (address pair); +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 } diff --git a/contracts/polygon/connectors/quickswap/main.sol b/contracts/polygon/connectors/quickswap/main.sol index 2cc07049..de7a1926 100644 --- a/contracts/polygon/connectors/quickswap/main.sol +++ b/contracts/polygon/connectors/quickswap/main.sol @@ -5,9 +5,15 @@ pragma solidity ^0.7.0; * @dev Decentralized Exchange. */ +<<<<<<< HEAD import {TokenInterface} from "../../common/interfaces.sol"; import {Helpers} from "./helpers.sol"; import {Events} from "./events.sol"; +======= +import { TokenInterface } from "../../common/interfaces.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 abstract contract QuickpswapResolver is Helpers, Events { /** @@ -20,7 +26,11 @@ abstract contract QuickpswapResolver is Helpers, Events { * @param slippage Slippage amount. * @param getId ID to retrieve amtA. * @param setId ID stores the amount of pools tokens received. +<<<<<<< HEAD */ +======= + */ +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 function deposit( address tokenA, address tokenB, @@ -29,6 +39,7 @@ abstract contract QuickpswapResolver is Helpers, Events { uint256 slippage, uint256 getId, uint256 setId +<<<<<<< HEAD ) external payable @@ -55,6 +66,22 @@ abstract contract QuickpswapResolver is Helpers, Events { getId, setId ); +======= + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amtA); + + (uint _amtA, uint _amtB, uint _uniAmt) = _addLiquidity( + tokenA, + tokenB, + _amt, + unitAmt, + slippage + ); + setUint(setId, _uniAmt); + + _eventName = "LogDepositLiquidity(address,address,uint256,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(tokenA, tokenB, _amtA, _amtB, _uniAmt, getId, setId); +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 } /** @@ -67,7 +94,11 @@ abstract contract QuickpswapResolver is Helpers, Events { * @param unitAmtB The unit amount of amtB/uniAmt with slippage. * @param getId ID to retrieve uniAmt. * @param setIds Array of IDs to store the amount tokens received. +<<<<<<< HEAD */ +======= + */ +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 function withdraw( address tokenA, address tokenB, @@ -76,6 +107,7 @@ abstract contract QuickpswapResolver is Helpers, Events { uint256 unitAmtB, uint256 getId, uint256[] calldata setIds +<<<<<<< HEAD ) external payable @@ -84,6 +116,12 @@ abstract contract QuickpswapResolver is Helpers, Events { uint256 _amt = getUint(getId, uniAmt); (uint256 _amtA, uint256 _amtB, uint256 _uniAmt) = _removeLiquidity( +======= + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, uniAmt); + + (uint _amtA, uint _amtB, uint _uniAmt) = _removeLiquidity( +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 tokenA, tokenB, _amt, @@ -93,6 +131,7 @@ abstract contract QuickpswapResolver is Helpers, Events { setUint(setIds[0], _amtA); setUint(setIds[1], _amtB); +<<<<<<< HEAD _eventName = "LogWithdrawLiquidity(address,address,uint256,uint256,uint256,uint256,uint256[])"; _eventParam = abi.encode( @@ -104,6 +143,11 @@ abstract contract QuickpswapResolver is Helpers, Events { getId, setIds ); +======= + + _eventName = "LogWithdrawLiquidity(address,address,uint256,uint256,uint256,uint256,uint256[])"; + _eventParam = abi.encode(tokenA, tokenB, _amtA, _amtB, _uniAmt, getId, setIds); +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 } /** @@ -115,7 +159,11 @@ abstract contract QuickpswapResolver is Helpers, Events { * @param unitAmt The unit amount of sellAmt/buyAmt with slippage. * @param getId ID to retrieve buyAmt. * @param setId ID to store the amount of tokens sold. +<<<<<<< HEAD */ +======= + */ +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 function buy( address buyAddr, address sellAddr, @@ -123,6 +171,7 @@ abstract contract QuickpswapResolver is Helpers, Events { uint256 unitAmt, uint256 getId, uint256 setId +<<<<<<< HEAD ) external payable @@ -140,18 +189,34 @@ abstract contract QuickpswapResolver is Helpers, Events { uint256 _slippageAmt = convert18ToDec( _sellAddr.decimals(), +======= + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _buyAmt = getUint(getId, buyAmt); + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeMaticAddress(buyAddr, sellAddr); + address[] memory paths = getPaths(address(_buyAddr), address(_sellAddr)); + + uint _slippageAmt = convert18ToDec(_sellAddr.decimals(), +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 wmul(unitAmt, convertTo18(_buyAddr.decimals(), _buyAmt)) ); checkPair(paths); +<<<<<<< HEAD uint256 _expectedAmt = getExpectedSellAmt(paths, _buyAmt); +======= + uint _expectedAmt = getExpectedSellAmt(paths, _buyAmt); +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 require(_slippageAmt >= _expectedAmt, "Too much slippage"); bool isEth = address(_sellAddr) == wmaticAddr; convertMaticToWmatic(isEth, _sellAddr, _expectedAmt); approve(_sellAddr, address(router), _expectedAmt); +<<<<<<< HEAD uint256 _sellAmt = router.swapTokensForExactTokens( +======= + uint _sellAmt = router.swapTokensForExactTokens( +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 _buyAmt, _expectedAmt, paths, @@ -165,6 +230,7 @@ abstract contract QuickpswapResolver is Helpers, Events { setUint(setId, _sellAmt); _eventName = "LogBuy(address,address,uint256,uint256,uint256,uint256)"; +<<<<<<< HEAD _eventParam = abi.encode( buyAddr, sellAddr, @@ -173,6 +239,9 @@ abstract contract QuickpswapResolver is Helpers, Events { getId, setId ); +======= + _eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId); +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 } /** @@ -184,7 +253,11 @@ abstract contract QuickpswapResolver is Helpers, Events { * @param unitAmt The unit amount of buyAmt/sellAmt with slippage. * @param getId ID to retrieve sellAmt. * @param setId ID stores the amount of token brought. +<<<<<<< HEAD */ +======= + */ +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 function sell( address buyAddr, address sellAddr, @@ -192,6 +265,7 @@ abstract contract QuickpswapResolver is Helpers, Events { uint256 unitAmt, uint256 getId, uint256 setId +<<<<<<< HEAD ) external payable @@ -215,18 +289,40 @@ abstract contract QuickpswapResolver is Helpers, Events { uint256 _slippageAmt = convert18ToDec( _buyAddr.decimals(), +======= + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _sellAmt = getUint(getId, sellAmt); + (TokenInterface _buyAddr, TokenInterface _sellAddr) = changeMaticAddress(buyAddr, sellAddr); + address[] memory paths = getPaths(address(_buyAddr), address(_sellAddr)); + + if (_sellAmt == uint(-1)) { + _sellAmt = sellAddr == maticAddr ? + address(this).balance : + _sellAddr.balanceOf(address(this)); + } + + uint _slippageAmt = convert18ToDec(_buyAddr.decimals(), +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 wmul(unitAmt, convertTo18(_sellAddr.decimals(), _sellAmt)) ); checkPair(paths); +<<<<<<< HEAD uint256 _expectedAmt = getExpectedBuyAmt(paths, _sellAmt); +======= + uint _expectedAmt = getExpectedBuyAmt(paths, _sellAmt); +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 require(_slippageAmt <= _expectedAmt, "Too much slippage"); bool isEth = address(_sellAddr) == wmaticAddr; convertMaticToWmatic(isEth, _sellAddr, _sellAmt); approve(_sellAddr, address(router), _sellAmt); +<<<<<<< HEAD uint256 _buyAmt = router.swapExactTokensForTokens( +======= + uint _buyAmt = router.swapExactTokensForTokens( +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 _sellAmt, _expectedAmt, paths, @@ -240,6 +336,7 @@ abstract contract QuickpswapResolver is Helpers, Events { setUint(setId, _buyAmt); _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; +<<<<<<< HEAD _eventParam = abi.encode( buyAddr, sellAddr, @@ -248,6 +345,9 @@ abstract contract QuickpswapResolver is Helpers, Events { getId, setId ); +======= + _eventParam = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId); +>>>>>>> d9a7bfba85acceb302ac1d1dd854e9b380976557 } } diff --git a/test/quickswap-polygon/quickswap.test.js b/test/quickswap-polygon/quickswap.test.js new file mode 100644 index 00000000..e9d2d53b --- /dev/null +++ b/test/quickswap-polygon/quickswap.test.js @@ -0,0 +1,169 @@ +const { expect } = require("chai"); +const hre = require("hardhat"); +const { waffle, ethers } = hre; +const { provider } = waffle + +const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js") +const buildDSAv2 = require("../../scripts/buildDSAv2") +const encodeSpells = require("../../scripts/encodeSpells.js") +const getMasterSigner = require("../../scripts/getMasterSigner") +const addLiquidity = require("../../scripts/addLiquidity"); + +const addresses = require("../../scripts/constant/addresses"); +const abis = require("../../scripts/constant/abis"); + +const connectV2QuickswapArtifacts = require("../../artifacts/contracts/polygon/connectors/quickswap/main.sol/ConnectV2Quickswap.json"); + +const DAI_ADDR = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063" + +describe("Quickswap", function () { + const connectorName = "Quickpswap-v1.1" + + 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: 13005785, + // }, + // }, + // ], + // }); + masterSigner = await getMasterSigner(wallet3) + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: connectV2QuickswapArtifacts, + signer: masterSigner, + 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("Deposit ETH & DAI 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 addLiquidity("dai", dsaWallet0.address, ethers.utils.parseEther("100000")); + }); + + it("Deposit ETH & USDT 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 addLiquidity("usdt", dsaWallet0.address, ethers.utils.parseEther("100000")); + }); + }); + + describe("Main", function () { + it("Should deposit successfully", async function () { + const ethAmount = ethers.utils.parseEther("100") // 1 ETH + const daiUnitAmount = ethers.utils.parseUnits("4", 6) // 1 ETH + const usdtAmount = ethers.utils.parseEther("400") / Math.pow(10, 12) // 1 ETH + const ethAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE" + + const getId = "0" + const setId = "0" + + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [ + ethAddress, + DAI_ADDR, + ethAmount, + daiUnitAmount, + "500000000000000000", + getId, + setId + ], + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + let receipt = await tx.wait() + }).timeout(10000000000); + + it("Should withdraw successfully", async function () { + const ethAmount = ethers.utils.parseEther("0.1") // 1 ETH + const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + + const getId = "0" + const setIds = ["0", "0"] + + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [ + ethAddress, + DAI_ADDR, + ethAmount, + 0, + 0, + getId, + setIds + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + let receipt = await tx.wait() + }); + + it("Should buy successfully", async function () { + const ethAmount = ethers.utils.parseEther("0.1") // 1 ETH + const daiUnitAmount = ethers.utils.parseEther("4000") // 1 ETH + const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + + const getId = "0" + const setId = "0" + + const spells = [ + { + connector: connectorName, + method: "buy", + args: [ + ethAddress, + DAI_ADDR, + ethAmount, + daiUnitAmount, + getId, + setId + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + let receipt = await tx.wait() + }); + }); +}) diff --git a/test/sushiswap/sushiswap.test.js b/test/sushiswap/sushiswap.test.js new file mode 100644 index 00000000..baaf3876 --- /dev/null +++ b/test/sushiswap/sushiswap.test.js @@ -0,0 +1,170 @@ +const { expect } = require("chai"); +const hre = require("hardhat"); +const { waffle, ethers } = hre; +const { provider } = waffle + +const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js") +const buildDSAv2 = require("../../scripts/buildDSAv2") +const encodeSpells = require("../../scripts/encodeSpells.js") +const getMasterSigner = require("../../scripts/getMasterSigner") +const addLiquidity = require("../../scripts/addLiquidity"); + +const addresses = require("../../scripts/constant/addresses"); +const abis = require("../../scripts/constant/abis"); + +const connectV2SushiswapArtifacts = require("../../artifacts/contracts/mainnet/connectors/sushiswap/main.sol/ConnectV2Sushiswap.json"); + +const DAI_ADDR = "0x6b175474e89094c44da98b954eedeac495271d0f" + +describe("Sushiswap", function () { + const connectorName = "Sushiswap-v1" + + 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: 13005785, + }, + }, + ], + }); + masterSigner = await getMasterSigner(wallet3) + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: connectV2SushiswapArtifacts, + signer: masterSigner, + 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("Deposit ETH & DAI 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 addLiquidity("dai", dsaWallet0.address, ethers.utils.parseEther("100000")); + }); + + it("Deposit ETH & USDT 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 addLiquidity("usdt", dsaWallet0.address, ethers.utils.parseEther("100000")); + }); + }); + + describe("Main", function () { + + it("Should deposit successfully", async function () { + const ethAmount = ethers.utils.parseEther("0.1") // 1 ETH + const daiUnitAmount = ethers.utils.parseEther("4000") // 1 ETH + const usdtAmount = ethers.utils.parseEther("400") / Math.pow(10, 12) // 1 ETH + const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + + const getId = "0" + const setId = "0" + + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [ + ethAddress, + DAI_ADDR, + ethAmount, + daiUnitAmount, + "500000000000000000", + getId, + setId + ], + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + let receipt = await tx.wait() + }).timeout(10000000000); + + it("Should withdraw successfully", async function () { + const ethAmount = ethers.utils.parseEther("0.1") // 1 ETH + const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + + const getId = "0" + const setIds = ["0", "0"] + + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [ + ethAddress, + DAI_ADDR, + ethAmount, + 0, + 0, + getId, + setIds + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + let receipt = await tx.wait() + }); + + it("Should buy successfully", async function () { + const ethAmount = ethers.utils.parseEther("0.1") // 1 ETH + const daiUnitAmount = ethers.utils.parseEther("4000") // 1 ETH + const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + + const getId = "0" + const setId = "0" + + const spells = [ + { + connector: connectorName, + method: "buy", + args: [ + ethAddress, + DAI_ADDR, + ethAmount, + daiUnitAmount, + getId, + setId + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + let receipt = await tx.wait() + }); + }); +}) \ No newline at end of file diff --git a/test/sushiswapIncentive/sushiIncentive.test.js b/test/sushiswapIncentive/sushiIncentive.test.js new file mode 100644 index 00000000..1b2fd01b --- /dev/null +++ b/test/sushiswapIncentive/sushiIncentive.test.js @@ -0,0 +1,237 @@ +const { expect } = require("chai"); +const hre = require("hardhat"); +const { waffle, ethers } = hre; +const { provider } = waffle + +const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js") +const buildDSAv2 = require("../../scripts/buildDSAv2") +const encodeSpells = require("../../scripts/encodeSpells.js") +const getMasterSigner = require("../../scripts/getMasterSigner") +const addLiquidity = require("../../scripts/addLiquidity"); + +const addresses = require("../../scripts/constant/addresses"); +const abis = require("../../scripts/constant/abis"); + +const connectV2SushiswapArtifacts = require("../../artifacts/contracts/mainnet/connectors/sushiswap/main.sol/ConnectV2Sushiswap.json"); +const connectV2SushiswapIncentiveArtifacts = require("../../artifacts/contracts/mainnet/connectors/sushi-incentive/main.sol/ConnectV2SushiswapIncentive.json"); + +const DAI_ADDR = "0x6b175474e89094c44da98b954eedeac495271d0f" +const WETH_ADDR = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" + +describe("Sushiswap", function () { + const connectorName = "Sushiswap-v1" + const incentiveConnectorName = "Sushiswp-Incentive-v1" + + let dsaWallet0 + let masterSigner; + let instaConnectorsV2; + let connector, connectorIncentive; + + 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); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: connectV2SushiswapArtifacts, + signer: masterSigner, + connectors: instaConnectorsV2 + }) + console.log("Connector address", connector.address) + + connectorIncentive = await deployAndEnableConnector({ + connectorName: incentiveConnectorName, + contractArtifact: connectV2SushiswapIncentiveArtifacts, + signer: masterSigner, + connectors: instaConnectorsV2 + }) + console.log("Incentive Connector address", connectorIncentive.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("Deposit ETH & DAI 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 addLiquidity("dai", dsaWallet0.address, ethers.utils.parseEther("100000")); + }); + + it("Deposit ETH & USDT 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 addLiquidity("usdt", dsaWallet0.address, ethers.utils.parseEther("100000")); + }); + }); + + describe("Main", function () { + + it("Should deposit successfully", async function () { + const ethAmount = ethers.utils.parseEther("2") // 1 ETH + const daiUnitAmount = ethers.utils.parseEther("4000") // 1 ETH + const usdtAmount = ethers.utils.parseEther("400") / Math.pow(10, 12) // 1 ETH + const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + + const getId = "0" + const setId = "0" + + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [ + ethAddress, + DAI_ADDR, + ethAmount, + daiUnitAmount, + "500000000000000000", + getId, + setId + ], + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + await tx.wait() + + describe("Incentive", () => { + it("Should deposit successfully", async () => { + const getId = 0 + const setId = 0 + const spells = [ + { + connector: incentiveConnectorName, + method: "deposit", + args: [ + WETH_ADDR, + DAI_ADDR, + ethers.utils.parseEther("10"), + getId, + setId + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address) + await tx.wait(); + }) + + it("Should harvest successfully", async () => { + const setId = 0 + const spells = [ + { + connector: incentiveConnectorName, + method: "harvest", + args: [ + WETH_ADDR, + DAI_ADDR, + setId + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address) + await tx.wait(); + }) + + it("Should harvest and withdraw successfully", async () => { + const getId = 0 + const setId = 0 + const spells = [ + { + connector: incentiveConnectorName, + method: "withdrawAndHarvest", + args: [ + WETH_ADDR, + DAI_ADDR, + ethers.utils.parseEther("1"), + getId, + setId + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address) + await tx.wait(); + }) + + it("Should withdraw successfully", async () => { + const getId = 0 + const setId = 0 + const spells = [ + { + connector: incentiveConnectorName, + method: "withdraw", + args: [ + WETH_ADDR, + DAI_ADDR, + ethers.utils.parseEther("1"), + getId, + setId + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address) + await tx.wait(); + }) + }) + }).timeout(10000000000); + + it("Should buy successfully", async function () { + const ethAmount = ethers.utils.parseEther("0.1") // 1 ETH + const daiUnitAmount = ethers.utils.parseEther("4000") // 1 ETH + const ethAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" + + const getId = "0" + const setId = "0" + + const spells = [ + { + connector: connectorName, + method: "buy", + args: [ + ethAddress, + DAI_ADDR, + ethAmount, + daiUnitAmount, + getId, + setId + ] + } + ] + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address) + let receipt = await tx.wait() + }); + }); +}) \ No newline at end of file