From 7b4d1a484201cd173b094f755a80cbf06375d15d Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Sat, 7 May 2022 20:33:57 +0530 Subject: [PATCH 1/6] hop polygon update --- contracts/polygon/connectors/hop/events.sol | 1 + contracts/polygon/connectors/hop/helpers.sol | 19 ++++++++++++++- .../polygon/connectors/hop/interface.sol | 2 +- contracts/polygon/connectors/hop/main.sol | 23 ++++++++++++------- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/contracts/polygon/connectors/hop/events.sol b/contracts/polygon/connectors/hop/events.sol index 9594f4a5..5f3f3ed5 100644 --- a/contracts/polygon/connectors/hop/events.sol +++ b/contracts/polygon/connectors/hop/events.sol @@ -4,6 +4,7 @@ pragma solidity ^0.7.0; contract Events { event LogBridge( address token, + bool isWrapped, uint256 chainId, address recipient, uint256 amount, diff --git a/contracts/polygon/connectors/hop/helpers.sol b/contracts/polygon/connectors/hop/helpers.sol index 9bb2f5f7..aaf0ba46 100644 --- a/contracts/polygon/connectors/hop/helpers.sol +++ b/contracts/polygon/connectors/hop/helpers.sol @@ -18,6 +18,7 @@ contract Helpers is DSMath, Basic { * @param sourceDeadline The deadline for the source chain transaction (Recommended - Date.now() + 604800 (1 week)) * @param destinationAmountOutMin minimum amount of token out for bridge on target chain, zero for L1 bridging * @param destinationDeadline The deadline for the target chain transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging + * @param isWrapped if the token to transfer if wrapped token of native chain token (ex. WETH) */ struct BridgeParams { address token; @@ -30,11 +31,27 @@ contract Helpers is DSMath, Basic { uint256 sourceDeadline; uint256 destinationAmountOutMin; uint256 destinationDeadline; + bool isWrapped; } - function _swapAndSend(BridgeParams memory params) internal { + function _swapAndSend(BridgeParams memory params, bool isNative) internal { IHopRouter router = IHopRouter(params.router); + if (isNative) { + router.swapAndSend{ value: params.amount }( + params.targetChainId, + params.recipient, + params.amount, + params.bonderFee, + params.sourceAmountOutMin, + params.sourceDeadline, + params.destinationAmountOutMin, + params.destinationDeadline + ); + + return; + } + TokenInterface tokenContract = TokenInterface(params.token); approve(tokenContract, params.router, params.amount); diff --git a/contracts/polygon/connectors/hop/interface.sol b/contracts/polygon/connectors/hop/interface.sol index db68c7ef..591056de 100644 --- a/contracts/polygon/connectors/hop/interface.sol +++ b/contracts/polygon/connectors/hop/interface.sol @@ -13,5 +13,5 @@ interface IHopRouter { uint256 deadline, uint256 destinationAmountOutMin, uint256 destinationDeadline - ) external; + ) external payable; } diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol index bb6ef7c1..20d9fa16 100644 --- a/contracts/polygon/connectors/hop/main.sol +++ b/contracts/polygon/connectors/hop/main.sol @@ -37,28 +37,35 @@ abstract contract Resolver is Helpers { } params.amount = getUint(getId, params.amount); - - bool isMatic = params.token == maticAddr; - params.token = params.token == maticAddr ? wmaticAddr : params.token; - TokenInterface tokenContract = TokenInterface(params.token); - if (isMatic) { + if (params.isWrapped) { + convertWmaticToMatic( + params.isWrapped, + tokenContract, + params.amount + ); + params.token = maticAddr; + } + + bool isNative = params.token == maticAddr; + + if (isNative) { params.amount = params.amount == uint256(-1) ? address(this).balance : params.amount; - convertMaticToWmatic(isMatic, tokenContract, params.amount); } else { params.amount = params.amount == uint256(-1) ? tokenContract.balanceOf(address(this)) : params.amount; } - _swapAndSend(params); + _swapAndSend(params, isNative); - _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventName = "LogBridge(address,bool,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( params.token, + params.isWrapped, params.targetChainId, params.recipient, params.amount, From 44e9968dac1950809a1c70b3f0ecde51c96cd475 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Tue, 10 May 2022 18:23:57 +0530 Subject: [PATCH 2/6] refactored code --- contracts/polygon/connectors/hop/events.sol | 1 - contracts/polygon/connectors/hop/helpers.sol | 20 ++------------------ contracts/polygon/connectors/hop/main.sol | 11 +++-------- 3 files changed, 5 insertions(+), 27 deletions(-) diff --git a/contracts/polygon/connectors/hop/events.sol b/contracts/polygon/connectors/hop/events.sol index 5f3f3ed5..9594f4a5 100644 --- a/contracts/polygon/connectors/hop/events.sol +++ b/contracts/polygon/connectors/hop/events.sol @@ -4,7 +4,6 @@ pragma solidity ^0.7.0; contract Events { event LogBridge( address token, - bool isWrapped, uint256 chainId, address recipient, uint256 amount, diff --git a/contracts/polygon/connectors/hop/helpers.sol b/contracts/polygon/connectors/hop/helpers.sol index aaf0ba46..5f3d306a 100644 --- a/contracts/polygon/connectors/hop/helpers.sol +++ b/contracts/polygon/connectors/hop/helpers.sol @@ -18,7 +18,6 @@ contract Helpers is DSMath, Basic { * @param sourceDeadline The deadline for the source chain transaction (Recommended - Date.now() + 604800 (1 week)) * @param destinationAmountOutMin minimum amount of token out for bridge on target chain, zero for L1 bridging * @param destinationDeadline The deadline for the target chain transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging - * @param isWrapped if the token to transfer if wrapped token of native chain token (ex. WETH) */ struct BridgeParams { address token; @@ -31,31 +30,16 @@ contract Helpers is DSMath, Basic { uint256 sourceDeadline; uint256 destinationAmountOutMin; uint256 destinationDeadline; - bool isWrapped; } function _swapAndSend(BridgeParams memory params, bool isNative) internal { IHopRouter router = IHopRouter(params.router); - if (isNative) { - router.swapAndSend{ value: params.amount }( - params.targetChainId, - params.recipient, - params.amount, - params.bonderFee, - params.sourceAmountOutMin, - params.sourceDeadline, - params.destinationAmountOutMin, - params.destinationDeadline - ); - - return; - } - + uint256 nativeTokenAmt = isNative ? params.amount : 0; TokenInterface tokenContract = TokenInterface(params.token); approve(tokenContract, params.router, params.amount); - router.swapAndSend( + router.swapAndSend{ value: nativeTokenAmt }( params.targetChainId, params.recipient, params.amount, diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol index 20d9fa16..e1c7ebc6 100644 --- a/contracts/polygon/connectors/hop/main.sol +++ b/contracts/polygon/connectors/hop/main.sol @@ -39,12 +39,8 @@ abstract contract Resolver is Helpers { params.amount = getUint(getId, params.amount); TokenInterface tokenContract = TokenInterface(params.token); - if (params.isWrapped) { - convertWmaticToMatic( - params.isWrapped, - tokenContract, - params.amount - ); + if (params.token == wmaticAddr) { + convertWmaticToMatic(true, tokenContract, params.amount); params.token = maticAddr; } @@ -62,10 +58,9 @@ abstract contract Resolver is Helpers { _swapAndSend(params, isNative); - _eventName = "LogBridge(address,bool,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( params.token, - params.isWrapped, params.targetChainId, params.recipient, params.amount, From aa1d2b1a7562df9a90886b7a38daf30ca94201bd Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Thu, 12 May 2022 01:02:55 +0530 Subject: [PATCH 3/6] fixed arbitrum connector --- contracts/arbitrum/connectors/hop/helpers.sol | 11 +++++++---- contracts/arbitrum/connectors/hop/interface.sol | 2 +- contracts/arbitrum/connectors/hop/main.sol | 12 +++++++----- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/contracts/arbitrum/connectors/hop/helpers.sol b/contracts/arbitrum/connectors/hop/helpers.sol index 9bb2f5f7..632b9488 100644 --- a/contracts/arbitrum/connectors/hop/helpers.sol +++ b/contracts/arbitrum/connectors/hop/helpers.sol @@ -32,13 +32,16 @@ contract Helpers is DSMath, Basic { uint256 destinationDeadline; } - function _swapAndSend(BridgeParams memory params) internal { + function _swapAndSend(BridgeParams memory params, bool isEth) internal { IHopRouter router = IHopRouter(params.router); - TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, params.router, params.amount); + uint256 nativeTokenAmt = isEth ? params.amount : 0; + if (!isEth) { + TokenInterface tokenContract = TokenInterface(params.token); + approve(tokenContract, params.router, params.amount); + } - router.swapAndSend( + router.swapAndSend{ value: nativeTokenAmt }( params.targetChainId, params.recipient, params.amount, diff --git a/contracts/arbitrum/connectors/hop/interface.sol b/contracts/arbitrum/connectors/hop/interface.sol index db68c7ef..591056de 100644 --- a/contracts/arbitrum/connectors/hop/interface.sol +++ b/contracts/arbitrum/connectors/hop/interface.sol @@ -13,5 +13,5 @@ interface IHopRouter { uint256 deadline, uint256 destinationAmountOutMin, uint256 destinationDeadline - ) external; + ) external payable; } diff --git a/contracts/arbitrum/connectors/hop/main.sol b/contracts/arbitrum/connectors/hop/main.sol index aca1aa29..9061f219 100644 --- a/contracts/arbitrum/connectors/hop/main.sol +++ b/contracts/arbitrum/connectors/hop/main.sol @@ -37,24 +37,26 @@ abstract contract Resolver is Helpers { } params.amount = getUint(getId, params.amount); + TokenInterface tokenContract = TokenInterface(params.token); + + if (params.token == wethAddr) { + convertWethToEth(true, tokenContract, params.amount); + params.token = ethAddr; + } bool isEth = params.token == ethAddr; - params.token = params.token == ethAddr ? wethAddr : params.token; - - TokenInterface tokenContract = TokenInterface(params.token); if (isEth) { params.amount = params.amount == uint256(-1) ? address(this).balance : params.amount; - convertEthToWeth(isEth, tokenContract, params.amount); } else { params.amount = params.amount == uint256(-1) ? tokenContract.balanceOf(address(this)) : params.amount; } - _swapAndSend(params); + _swapAndSend(params, isEth); _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( From 7b3b5035e399366532f4557a9a894be57ad035a0 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Thu, 12 May 2022 01:03:14 +0530 Subject: [PATCH 4/6] fixed optimism connector --- contracts/optimism/connectors/hop/helpers.sol | 11 +++++++---- contracts/optimism/connectors/hop/interface.sol | 2 +- contracts/optimism/connectors/hop/main.sol | 12 +++++++----- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/contracts/optimism/connectors/hop/helpers.sol b/contracts/optimism/connectors/hop/helpers.sol index 9bb2f5f7..632b9488 100644 --- a/contracts/optimism/connectors/hop/helpers.sol +++ b/contracts/optimism/connectors/hop/helpers.sol @@ -32,13 +32,16 @@ contract Helpers is DSMath, Basic { uint256 destinationDeadline; } - function _swapAndSend(BridgeParams memory params) internal { + function _swapAndSend(BridgeParams memory params, bool isEth) internal { IHopRouter router = IHopRouter(params.router); - TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, params.router, params.amount); + uint256 nativeTokenAmt = isEth ? params.amount : 0; + if (!isEth) { + TokenInterface tokenContract = TokenInterface(params.token); + approve(tokenContract, params.router, params.amount); + } - router.swapAndSend( + router.swapAndSend{ value: nativeTokenAmt }( params.targetChainId, params.recipient, params.amount, diff --git a/contracts/optimism/connectors/hop/interface.sol b/contracts/optimism/connectors/hop/interface.sol index db68c7ef..591056de 100644 --- a/contracts/optimism/connectors/hop/interface.sol +++ b/contracts/optimism/connectors/hop/interface.sol @@ -13,5 +13,5 @@ interface IHopRouter { uint256 deadline, uint256 destinationAmountOutMin, uint256 destinationDeadline - ) external; + ) external payable; } diff --git a/contracts/optimism/connectors/hop/main.sol b/contracts/optimism/connectors/hop/main.sol index 9f5f8594..87fc639f 100644 --- a/contracts/optimism/connectors/hop/main.sol +++ b/contracts/optimism/connectors/hop/main.sol @@ -37,24 +37,26 @@ abstract contract Resolver is Helpers { } params.amount = getUint(getId, params.amount); + TokenInterface tokenContract = TokenInterface(params.token); + + if (params.token == wethAddr) { + convertWethToEth(true, tokenContract, params.amount); + params.token = ethAddr; + } bool isEth = params.token == ethAddr; - params.token = params.token == ethAddr ? wethAddr : params.token; - - TokenInterface tokenContract = TokenInterface(params.token); if (isEth) { params.amount = params.amount == uint256(-1) ? address(this).balance : params.amount; - convertEthToWeth(isEth, tokenContract, params.amount); } else { params.amount = params.amount == uint256(-1) ? tokenContract.balanceOf(address(this)) : params.amount; } - _swapAndSend(params); + _swapAndSend(params, isEth); _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( From fc0c7f7910a475a87264bea8293633d05039d128 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Thu, 12 May 2022 01:03:33 +0530 Subject: [PATCH 5/6] minor fixing --- contracts/polygon/connectors/hop/helpers.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/contracts/polygon/connectors/hop/helpers.sol b/contracts/polygon/connectors/hop/helpers.sol index 5f3d306a..cc74905a 100644 --- a/contracts/polygon/connectors/hop/helpers.sol +++ b/contracts/polygon/connectors/hop/helpers.sol @@ -36,8 +36,10 @@ contract Helpers is DSMath, Basic { IHopRouter router = IHopRouter(params.router); uint256 nativeTokenAmt = isNative ? params.amount : 0; - TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, params.router, params.amount); + if (!isNative) { + TokenInterface tokenContract = TokenInterface(params.token); + approve(tokenContract, params.router, params.amount); + } router.swapAndSend{ value: nativeTokenAmt }( params.targetChainId, From f34513264e7c548699b9dac976c666a5a62aecb3 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Thu, 12 May 2022 01:03:59 +0530 Subject: [PATCH 6/6] added hop arb test cases --- test/arbitrum/hop/hop.test.ts | 142 ++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 test/arbitrum/hop/hop.test.ts diff --git a/test/arbitrum/hop/hop.test.ts b/test/arbitrum/hop/hop.test.ts new file mode 100644 index 00000000..5ccbddb0 --- /dev/null +++ b/test/arbitrum/hop/hop.test.ts @@ -0,0 +1,142 @@ +import { expect } from "chai"; +import hre, { ethers } from "hardhat"; +import type { Signer, Contract } from "ethers"; +import { BigNumber } from "bignumber.js"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { addresses } from "../../../scripts/tests/arbitrum/addresses"; +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { abis } from "../../../scripts/constant/abis"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { parseEther } from "ethers/lib/utils"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { ConnectV2HopArbitrum__factory, IERC20__factory } from "../../../typechain"; + +let account = "0xa067668661c84476afcdc6fa5d758c4c01c34352"; +const mnemonic = "test test test test test test test test test test test junk"; +const WETH = "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"; +const connectorName = "HOP-X"; +let signer: any, wallet0: any; + +describe("Hop connector", function () { + let dsaWallet0: any; + let masterSigner: Signer; + let instaConnectorsV2: Contract; + let connector: any; + + const wallet = ethers.Wallet.fromMnemonic(mnemonic); + const token = new ethers.Contract(WETH, IERC20__factory.abi); + + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + //@ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url + // blockNumber: 9333600 + } + } + ] + }); + masterSigner = await getMasterSigner(); + [wallet0] = await ethers.getSigners(); + + await hre.network.provider.send("hardhat_setBalance", [account, ethers.utils.parseEther("10").toHexString()]); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [account] + }); + + signer = await ethers.getSigner(account); + + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2HopArbitrum__factory, + signer: masterSigner, + connectors: instaConnectorsV2 + }); + }); + + describe("Deployment", async () => { + it("Should set correct name", async () => { + expect(await connector.name()).to.eq("Hop-v1.0"); + }); + }); + + describe("DSA wallet setup", async () => { + it("Should build DSA v2", async () => { + dsaWallet0 = await buildDSAv2(wallet.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("5") + }); + + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("5")); + }); + }); + + describe("Main", async () => { + it("should send ETH successfully", async () => { + const deadline = new BigNumber(Date.now()).dividedBy(1000).plus(604800).toFixed(0); + const bridgeParams = [ + "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", + "0x33ceb27b39d2Bb7D2e61F7564d3Df29344020417", + dsaWallet0.address, + "137", + parseEther("1"), + parseEther("0.01"), + parseEther("0.8"), + deadline, + parseEther("0.8"), + deadline + ]; + + const spells = [ + { + connector: connectorName, + method: "bridge", + args: [bridgeParams, "0"] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet.getAddress()); + await tx.wait(); + }); + + it("should send WETH successfully", async () => { + const deadline = new BigNumber(Date.now()).dividedBy(1000).plus(604800).toFixed(0); + await token.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("10")); + + const bridgeParams = [ + "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", + "0x33ceb27b39d2Bb7D2e61F7564d3Df29344020417", + dsaWallet0.address, + "137", + parseEther("1"), + parseEther("0.01"), + parseEther("0.8"), + deadline, + parseEther("0.8"), + deadline + ]; + + const spells = [ + { + connector: connectorName, + method: "bridge", + args: [bridgeParams, "0"] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet.getAddress()); + await tx.wait(); + }); + }); +});