From 243326c7b22844dce710d841b371f303271a4663 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 4 Apr 2022 00:18:53 +0530 Subject: [PATCH 01/14] Added HOP connector: polygon --- contracts/polygon/connectors/hop/events.sol | 30 ++++ contracts/polygon/connectors/hop/helpers.sol | 63 ++++++++ .../polygon/connectors/hop/interface.sol | 17 +++ contracts/polygon/connectors/hop/main.sol | 143 ++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 contracts/polygon/connectors/hop/events.sol create mode 100644 contracts/polygon/connectors/hop/helpers.sol create mode 100644 contracts/polygon/connectors/hop/interface.sol create mode 100644 contracts/polygon/connectors/hop/main.sol diff --git a/contracts/polygon/connectors/hop/events.sol b/contracts/polygon/connectors/hop/events.sol new file mode 100644 index 00000000..5d1221cd --- /dev/null +++ b/contracts/polygon/connectors/hop/events.sol @@ -0,0 +1,30 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogSendToL1( + address token, + uint256 chainId, + address recipient, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline, + uint256 getId + ); + + event LogSendToL2( + address token, + uint256 chainId, + address recipient, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline, + uint256 getId + ); +} diff --git a/contracts/polygon/connectors/hop/helpers.sol b/contracts/polygon/connectors/hop/helpers.sol new file mode 100644 index 00000000..c612d05b --- /dev/null +++ b/contracts/polygon/connectors/hop/helpers.sol @@ -0,0 +1,63 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import "./interface.sol"; + +contract Helpers is DSMath, Basic { + function _swapAndSend( + address token, + uint256 chainId, + address recipient, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline + ) internal { + IHopRouter router = _getRouter(token); + + TokenInterface tokenContract = TokenInterface(token); + approve(tokenContract, address(router), amount); + + router.swapAndSend( + chainId, + recipient, + amount, + bonderFee, + amountOutMin, + deadline, + destinationAmountOutMin, + destinationDeadline + ); + } + + function _getRouter(address token_) + internal + pure + returns (IHopRouter router) + { + if (token_ == 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) + //USDC l2AmmWrapper + router = IHopRouter(0x76b22b8C1079A44F1211D867D68b1eda76a635A7); + else if (token_ == 0xc2132D05D31c914a87C6611C10748AEb04B58e8F) + //USDT l2AmmWrapper + router = IHopRouter(0x8741Ba6225A6BF91f9D73531A98A89807857a2B3); + else if (token_ == 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270) + //WMATIC l2AmmWrapper + router = IHopRouter(0x884d1Aa15F9957E1aEAA86a82a72e49Bc2bfCbe3); + else if (token_ == 0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063) + //DAI l2AmmWrapper + router = IHopRouter(0x28529fec439cfF6d7D1D5917e956dEE62Cd3BE5c); + else if (token_ == 0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619) + //WETH l2AmmWrapper + router = IHopRouter(0xc315239cFb05F1E130E7E28E603CEa4C014c57f0); + else if (token_ == 0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6) + //WBTC l2AmmWrapper + router = IHopRouter(0xCd1d7AEfA8055e020db0d0e98bbF3FeD1A16aad6); + else revert("Invalid token migration"); + } +} diff --git a/contracts/polygon/connectors/hop/interface.sol b/contracts/polygon/connectors/hop/interface.sol new file mode 100644 index 00000000..db68c7ef --- /dev/null +++ b/contracts/polygon/connectors/hop/interface.sol @@ -0,0 +1,17 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + +interface IHopRouter { + function swapAndSend( + uint256 chainId, + address recipient, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline + ) external; +} diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol new file mode 100644 index 00000000..d49e25dd --- /dev/null +++ b/contracts/polygon/connectors/hop/main.sol @@ -0,0 +1,143 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Hop. + * @dev Cross chain Bridge. + */ + +import { TokenInterface, MemoryInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import "./interface.sol"; +import "./helpers.sol"; +import "./events.sol"; + +abstract contract Resolver is Helpers { + function sendToL1( + address token, + uint256 chainId, + address recipientOnL1, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline, + uint256 getId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amount); + + bool isMatic = token == maticAddr; + address _token = isMatic ? wmaticAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + if (isMatic) { + _amt = _amt == uint256(-1) ? address(this).balance : _amt; + convertMaticToWmatic(isMatic, tokenContract, _amt); + } else { + _amt = _amt == uint256(-1) + ? tokenContract.balanceOf(address(this)) + : _amt; + } + + require( + destinationAmountOutMin == 0, + "destinationAmountOutMin != 0, sending to L1" + ); + require( + destinationDeadline == 0, + "destinationDeadline != 0, sending to L1" + ); + + _swapAndSend( + _token, + chainId, + recipientOnL1, + _amt, + bonderFee, + amountOutMin, + deadline, + destinationAmountOutMin, + destinationDeadline + ); + + _eventName = "LogSendToL1(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _token, + chainId, + recipientOnL1, + _amt, + bonderFee, + amountOutMin, + deadline, + destinationAmountOutMin, + destinationDeadline, + getId + ); + } + + function sendToL2( + address token, + uint256 chainId, + address recipientOnL2, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline, + uint256 getId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amount); + + bool isMatic = token == maticAddr; + address _token = isMatic ? wmaticAddr : token; + + TokenInterface tokenContract = TokenInterface(_token); + + if (isMatic) { + _amt = _amt == uint256(-1) ? address(this).balance : _amt; + convertMaticToWmatic(isMatic, tokenContract, _amt); + } else { + _amt = _amt == uint256(-1) + ? tokenContract.balanceOf(address(this)) + : _amt; + } + + _swapAndSend( + _token, + chainId, + recipientOnL2, + _amt, + bonderFee, + amountOutMin, + deadline, + destinationAmountOutMin, + destinationDeadline + ); + + _eventName = "LogSendToL2(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + _token, + chainId, + recipientOnL2, + _amt, + bonderFee, + amountOutMin, + deadline, + destinationAmountOutMin, + destinationDeadline, + getId + ); + } +} From b833ab13924bcf9d52179722b8d5deb1345b7f7d Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 4 Apr 2022 00:19:45 +0530 Subject: [PATCH 02/14] minor fix --- contracts/polygon/connectors/hop/main.sol | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol index d49e25dd..572ff9bd 100644 --- a/contracts/polygon/connectors/hop/main.sol +++ b/contracts/polygon/connectors/hop/main.sol @@ -141,3 +141,7 @@ abstract contract Resolver is Helpers { ); } } + +contract ConnectV2HopPolygon is Resolver { + string public constant name = "Hop-v1.0"; +} From 9efdf4239c3f0d2c1d945fc6776aa78cf0cf0336 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 4 Apr 2022 00:26:27 +0530 Subject: [PATCH 03/14] added natspec comments --- contracts/polygon/connectors/hop/main.sol | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol index 572ff9bd..c1caed68 100644 --- a/contracts/polygon/connectors/hop/main.sol +++ b/contracts/polygon/connectors/hop/main.sol @@ -14,6 +14,20 @@ import "./helpers.sol"; import "./events.sol"; abstract contract Resolver is Helpers { + /** + * @dev Bridge Token. + * @notice Bridge Token on HOP. + * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) + * @param chainId The Id of the destination chain.(For MAINNET : 1) + * @param recipientOnL1 The address to recieve the token on destination chain (Layer 1). + * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). + * @param bonderFee The fee to be recieved by bonder at destination chain. + * @param amountOutMin minimum amount of token out for swap + * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) + * @param destinationAmountOutMin minimum amount of token out for bridge + * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) + * @param getId ID to retrieve amtA. + */ function sendToL1( address token, uint256 chainId, @@ -82,6 +96,20 @@ abstract contract Resolver is Helpers { ); } + /** + * @dev Send to L2 . + * @notice Bridge Token on HOP. + * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) + * @param chainId The Id of the destination chain.(For MAINNET : 1) + * @param recipientOnL2 The address to recieve the token on destination chain (Layer 2). + * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). + * @param bonderFee The fee to be recieved by bonder at destination chain. + * @param amountOutMin minimum amount of token out for swap + * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) + * @param destinationAmountOutMin minimum amount of token out for bridge + * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) + * @param getId ID to retrieve amtA. + */ function sendToL2( address token, uint256 chainId, From ba6edcd1d48370add89d14c8e97095eeb34cf13e Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 4 Apr 2022 00:41:30 +0530 Subject: [PATCH 04/14] refactored code --- contracts/polygon/connectors/hop/events.sol | 15 +- contracts/polygon/connectors/hop/helpers.sol | 57 ++++--- contracts/polygon/connectors/hop/main.sol | 163 ++++--------------- 3 files changed, 71 insertions(+), 164 deletions(-) diff --git a/contracts/polygon/connectors/hop/events.sol b/contracts/polygon/connectors/hop/events.sol index 5d1221cd..9594f4a5 100644 --- a/contracts/polygon/connectors/hop/events.sol +++ b/contracts/polygon/connectors/hop/events.sol @@ -2,20 +2,7 @@ pragma solidity ^0.7.0; contract Events { - event LogSendToL1( - address token, - uint256 chainId, - address recipient, - uint256 amount, - uint256 bonderFee, - uint256 amountOutMin, - uint256 deadline, - uint256 destinationAmountOutMin, - uint256 destinationDeadline, - uint256 getId - ); - - event LogSendToL2( + event LogBridge( address token, uint256 chainId, address recipient, diff --git a/contracts/polygon/connectors/hop/helpers.sol b/contracts/polygon/connectors/hop/helpers.sol index c612d05b..73000389 100644 --- a/contracts/polygon/connectors/hop/helpers.sol +++ b/contracts/polygon/connectors/hop/helpers.sol @@ -7,31 +7,44 @@ import { Basic } from "../../common/basic.sol"; import "./interface.sol"; contract Helpers is DSMath, Basic { - function _swapAndSend( - address token, - uint256 chainId, - address recipient, - uint256 amount, - uint256 bonderFee, - uint256 amountOutMin, - uint256 deadline, - uint256 destinationAmountOutMin, - uint256 destinationDeadline - ) internal { - IHopRouter router = _getRouter(token); + /** + * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) + * @param chainId The Id of the destination chain.(For MAINNET : 1) + * @param recipient The address to recieve the token on destination chain. + * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). + * @param bonderFee The fee to be recieved by bonder at destination chain. + * @param amountOutMin minimum amount of token out for swap + * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) + * @param destinationAmountOutMin minimum amount of token out for bridge, zero for L1 bridging + * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging + */ + struct BridgeParams { + address token; + uint256 chainId; + address recipient; + uint256 amount; + uint256 bonderFee; + uint256 amountOutMin; + uint256 deadline; + uint256 destinationAmountOutMin; + uint256 destinationDeadline; + } - TokenInterface tokenContract = TokenInterface(token); - approve(tokenContract, address(router), amount); + function _swapAndSend(BridgeParams memory params) internal { + IHopRouter router = _getRouter(params.token); + + TokenInterface tokenContract = TokenInterface(params.token); + approve(tokenContract, address(router), params.amount); router.swapAndSend( - chainId, - recipient, - amount, - bonderFee, - amountOutMin, - deadline, - destinationAmountOutMin, - destinationDeadline + params.chainId, + params.recipient, + params.amount, + params.bonderFee, + params.amountOutMin, + params.deadline, + params.destinationAmountOutMin, + params.destinationDeadline ); } diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol index c1caed68..619bc791 100644 --- a/contracts/polygon/connectors/hop/main.sol +++ b/contracts/polygon/connectors/hop/main.sol @@ -17,154 +17,61 @@ abstract contract Resolver is Helpers { /** * @dev Bridge Token. * @notice Bridge Token on HOP. - * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) - * @param chainId The Id of the destination chain.(For MAINNET : 1) - * @param recipientOnL1 The address to recieve the token on destination chain (Layer 1). - * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). - * @param bonderFee The fee to be recieved by bonder at destination chain. - * @param amountOutMin minimum amount of token out for swap - * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) - * @param destinationAmountOutMin minimum amount of token out for bridge - * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) - * @param getId ID to retrieve amtA. + * @param params BridgeParams struct for bridging + * @param bridgeToL1 bool to check which layer to migrate to + * @param getId ID to retrieve amount from last spell. */ function sendToL1( - address token, - uint256 chainId, - address recipientOnL1, - uint256 amount, - uint256 bonderFee, - uint256 amountOutMin, - uint256 deadline, - uint256 destinationAmountOutMin, - uint256 destinationDeadline, + BridgeParams memory params, + bool bridgeToL1, uint256 getId ) external payable returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(getId, amount); - - bool isMatic = token == maticAddr; - address _token = isMatic ? wmaticAddr : token; - - TokenInterface tokenContract = TokenInterface(_token); - - if (isMatic) { - _amt = _amt == uint256(-1) ? address(this).balance : _amt; - convertMaticToWmatic(isMatic, tokenContract, _amt); - } else { - _amt = _amt == uint256(-1) - ? tokenContract.balanceOf(address(this)) - : _amt; + if (bridgeToL1) { + require( + params.destinationAmountOutMin == 0, + "destinationAmountOutMin != 0, sending to L1" + ); + require( + params.destinationDeadline == 0, + "destinationDeadline != 0, sending to L1" + ); } - require( - destinationAmountOutMin == 0, - "destinationAmountOutMin != 0, sending to L1" - ); - require( - destinationDeadline == 0, - "destinationDeadline != 0, sending to L1" - ); + params.amount = getUint(getId, params.amount); - _swapAndSend( - _token, - chainId, - recipientOnL1, - _amt, - bonderFee, - amountOutMin, - deadline, - destinationAmountOutMin, - destinationDeadline - ); + bool isMatic = params.token == maticAddr; + params.token = params.token == maticAddr ? wmaticAddr : params.token; - _eventName = "LogSendToL1(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode( - _token, - chainId, - recipientOnL1, - _amt, - bonderFee, - amountOutMin, - deadline, - destinationAmountOutMin, - destinationDeadline, - getId - ); - } - - /** - * @dev Send to L2 . - * @notice Bridge Token on HOP. - * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) - * @param chainId The Id of the destination chain.(For MAINNET : 1) - * @param recipientOnL2 The address to recieve the token on destination chain (Layer 2). - * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). - * @param bonderFee The fee to be recieved by bonder at destination chain. - * @param amountOutMin minimum amount of token out for swap - * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) - * @param destinationAmountOutMin minimum amount of token out for bridge - * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) - * @param getId ID to retrieve amtA. - */ - function sendToL2( - address token, - uint256 chainId, - address recipientOnL2, - uint256 amount, - uint256 bonderFee, - uint256 amountOutMin, - uint256 deadline, - uint256 destinationAmountOutMin, - uint256 destinationDeadline, - uint256 getId - ) - external - payable - returns (string memory _eventName, bytes memory _eventParam) - { - uint256 _amt = getUint(getId, amount); - - bool isMatic = token == maticAddr; - address _token = isMatic ? wmaticAddr : token; - - TokenInterface tokenContract = TokenInterface(_token); + TokenInterface tokenContract = TokenInterface(params.token); if (isMatic) { - _amt = _amt == uint256(-1) ? address(this).balance : _amt; - convertMaticToWmatic(isMatic, tokenContract, _amt); + params.amount = params.amount == uint256(-1) + ? address(this).balance + : params.amount; + convertMaticToWmatic(isMatic, tokenContract, params.amount); } else { - _amt = _amt == uint256(-1) + params.amount = params.amount == uint256(-1) ? tokenContract.balanceOf(address(this)) - : _amt; + : params.amount; } - _swapAndSend( - _token, - chainId, - recipientOnL2, - _amt, - bonderFee, - amountOutMin, - deadline, - destinationAmountOutMin, - destinationDeadline - ); + _swapAndSend(params); - _eventName = "LogSendToL2(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( - _token, - chainId, - recipientOnL2, - _amt, - bonderFee, - amountOutMin, - deadline, - destinationAmountOutMin, - destinationDeadline, + params.token, + params.chainId, + params.recipient, + params.amount, + params.bonderFee, + params.amountOutMin, + params.deadline, + params.destinationAmountOutMin, + params.destinationDeadline, getId ); } From 533fee6ebab66c56dff1523bbb73ca7cb03d8854 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 4 Apr 2022 00:42:50 +0530 Subject: [PATCH 05/14] changed function name --- contracts/polygon/connectors/hop/main.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol index 619bc791..a36d8ca1 100644 --- a/contracts/polygon/connectors/hop/main.sol +++ b/contracts/polygon/connectors/hop/main.sol @@ -21,7 +21,7 @@ abstract contract Resolver is Helpers { * @param bridgeToL1 bool to check which layer to migrate to * @param getId ID to retrieve amount from last spell. */ - function sendToL1( + function bridge( BridgeParams memory params, bool bridgeToL1, uint256 getId From ca34079520d0082fd0153d5a15c908b75fe36946 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 11 Apr 2022 17:24:54 +0530 Subject: [PATCH 06/14] added hop mainnet --- contracts/mainnet/connectors/hop/events.sol | 14 ++++ contracts/mainnet/connectors/hop/helpers.sol | 69 +++++++++++++++++++ .../mainnet/connectors/hop/interface.sol | 16 +++++ contracts/mainnet/connectors/hop/main.sol | 63 +++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 contracts/mainnet/connectors/hop/events.sol create mode 100644 contracts/mainnet/connectors/hop/helpers.sol create mode 100644 contracts/mainnet/connectors/hop/interface.sol create mode 100644 contracts/mainnet/connectors/hop/main.sol diff --git a/contracts/mainnet/connectors/hop/events.sol b/contracts/mainnet/connectors/hop/events.sol new file mode 100644 index 00000000..12442b65 --- /dev/null +++ b/contracts/mainnet/connectors/hop/events.sol @@ -0,0 +1,14 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogBridge( + address token, + uint256 chainId, + address recipient, + uint256 amount, + uint256 amountOutMin, + uint256 deadline, + uint256 getId + ); +} diff --git a/contracts/mainnet/connectors/hop/helpers.sol b/contracts/mainnet/connectors/hop/helpers.sol new file mode 100644 index 00000000..9bff9a0a --- /dev/null +++ b/contracts/mainnet/connectors/hop/helpers.sol @@ -0,0 +1,69 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import "./interface.sol"; + +contract Helpers is DSMath, Basic { + /** + * @param token The address of token to be bridged.(For USDC: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48) + * @param chainId The Id of the destination chain.(For POLYGON : 137) + * @param recipient The address to recieve the token on destination chain. + * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). + * @param amountOutMin minimum amount of token out for swap + * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) + */ + struct BridgeParams { + address token; + uint256 chainId; + address recipient; + uint256 amount; + uint256 amountOutMin; + uint256 deadline; + } + + function _sendToL2(BridgeParams memory params) internal { + IHopRouter router = _getRouter(params.token); + + TokenInterface tokenContract = TokenInterface(params.token); + approve(tokenContract, address(router), params.amount); + + router.sendToL2( + params.chainId, + params.recipient, + params.amount, + params.amountOutMin, + params.deadline, + address(0), // relayer address + 0 // relayer fee + ); + } + + function _getRouter(address token_) + internal + pure + returns (IHopRouter router) + { + if (token_ == 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48) + //USDC l1Bridge + router = IHopRouter(0x3666f603Cc164936C1b87e207F36BEBa4AC5f18a); + else if (token_ == 0xdAC17F958D2ee523a2206206994597C13D831ec7) + //USDT l1Bridge + router = IHopRouter(0x3E4a3a4796d16c0Cd582C382691998f7c06420B6); + else if (token_ == 0x7c9f4C87d911613Fe9ca58b579f737911AAD2D43) + //WMATIC l1Bridge + router = IHopRouter(0x22B1Cbb8D98a01a3B71D034BB899775A76Eb1cc2); + else if (token_ == 0x6B175474E89094C44Da98b954EedeAC495271d0F) + //DAI l1Bridge + router = IHopRouter(0x3d4Cc8A61c7528Fd86C55cfe061a78dCBA48EDd1); + else if (token_ == 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2) + //WETH l1Bridge + router = IHopRouter(0xb8901acB165ed027E32754E0FFe830802919727f); + else if (token_ == 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599) + //WBTC l1Bridge + router = IHopRouter(0xb98454270065A31D71Bf635F6F7Ee6A518dFb849); + else revert("Invalid token migration"); + } +} diff --git a/contracts/mainnet/connectors/hop/interface.sol b/contracts/mainnet/connectors/hop/interface.sol new file mode 100644 index 00000000..09b8997a --- /dev/null +++ b/contracts/mainnet/connectors/hop/interface.sol @@ -0,0 +1,16 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + +interface IHopRouter { + function sendToL2( + uint256 chainId, + address recipient, + uint256 amount, + uint256 amountOutMin, + uint256 deadline, + address relayer, + uint256 relayerFee + ) external; +} diff --git a/contracts/mainnet/connectors/hop/main.sol b/contracts/mainnet/connectors/hop/main.sol new file mode 100644 index 00000000..0cd42411 --- /dev/null +++ b/contracts/mainnet/connectors/hop/main.sol @@ -0,0 +1,63 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Hop. + * @dev Cross chain Bridge. + */ + +import { TokenInterface, MemoryInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import "./interface.sol"; +import "./helpers.sol"; +import "./events.sol"; + +abstract contract Resolver is Helpers { + /** + * @dev Bridge Token. + * @notice Bridge Token on HOP. + * @param params BridgeParams struct for bridging + * @param getId ID to retrieve amount from last spell. + */ + function bridge(BridgeParams memory params, uint256 getId) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + params.amount = getUint(getId, params.amount); + + 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; + } + + _sendToL2(params); + + _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + params.token, + params.chainId, + params.recipient, + params.amount, + params.amountOutMin, + params.deadline, + getId + ); + } +} + +contract ConnectV2Hop is Resolver { + string public constant name = "Hop-v1.0"; +} From 227004ab79bbf6081c510a3d7289df4dafad8350 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 11 Apr 2022 17:32:58 +0530 Subject: [PATCH 07/14] added hop optimism --- contracts/optimism/connectors/hop/events.sol | 17 ++++ contracts/optimism/connectors/hop/helpers.sol | 73 +++++++++++++++++ .../optimism/connectors/hop/interface.sol | 17 ++++ contracts/optimism/connectors/hop/main.sol | 82 +++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 contracts/optimism/connectors/hop/events.sol create mode 100644 contracts/optimism/connectors/hop/helpers.sol create mode 100644 contracts/optimism/connectors/hop/interface.sol create mode 100644 contracts/optimism/connectors/hop/main.sol diff --git a/contracts/optimism/connectors/hop/events.sol b/contracts/optimism/connectors/hop/events.sol new file mode 100644 index 00000000..9594f4a5 --- /dev/null +++ b/contracts/optimism/connectors/hop/events.sol @@ -0,0 +1,17 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogBridge( + address token, + uint256 chainId, + address recipient, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline, + uint256 getId + ); +} diff --git a/contracts/optimism/connectors/hop/helpers.sol b/contracts/optimism/connectors/hop/helpers.sol new file mode 100644 index 00000000..48561498 --- /dev/null +++ b/contracts/optimism/connectors/hop/helpers.sol @@ -0,0 +1,73 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import "./interface.sol"; + +contract Helpers is DSMath, Basic { + /** + * @param token The address of token to be bridged.(For USDC: 0x7f5c764cbc14f9669b88837ca1490cca17c31607) + * @param chainId The Id of the destination chain.(For MAINNET : 1) + * @param recipient The address to recieve the token on destination chain. + * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). + * @param bonderFee The fee to be recieved by bonder at destination chain. + * @param amountOutMin minimum amount of token out for swap + * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) + * @param destinationAmountOutMin minimum amount of token out for bridge, zero for L1 bridging + * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging + */ + struct BridgeParams { + address token; + uint256 chainId; + address recipient; + uint256 amount; + uint256 bonderFee; + uint256 amountOutMin; + uint256 deadline; + uint256 destinationAmountOutMin; + uint256 destinationDeadline; + } + + function _swapAndSend(BridgeParams memory params) internal { + IHopRouter router = _getRouter(params.token); + + TokenInterface tokenContract = TokenInterface(params.token); + approve(tokenContract, address(router), params.amount); + + router.swapAndSend( + params.chainId, + params.recipient, + params.amount, + params.bonderFee, + params.amountOutMin, + params.deadline, + params.destinationAmountOutMin, + params.destinationDeadline + ); + } + + function _getRouter(address token_) + internal + pure + returns (IHopRouter router) + { + if (token_ == 0x7F5c764cBc14f9669B88837ca1490cCa17c31607) + //USDC l2AmmWrapper + router = IHopRouter(0x2ad09850b0CA4c7c1B33f5AcD6cBAbCaB5d6e796); + else if (token_ == 0x94b008aA00579c1307B0EF2c499aD98a8ce58e58) + //USDT l2AmmWrapper + router = IHopRouter(0x7D269D3E0d61A05a0bA976b7DBF8805bF844AF3F); + else if (token_ == 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1) + //DAI l2AmmWrapper + router = IHopRouter(0xb3C68a491608952Cb1257FC9909a537a0173b63B); + else if (token_ == 0x4200000000000000000000000000000000000006) + //WETH l2AmmWrapper + router = IHopRouter(0x86cA30bEF97fB651b8d866D45503684b90cb3312); + else if (token_ == 0x68f180fcCe6836688e9084f035309E29Bf0A2095) + //WBTC l2AmmWrapper + router = IHopRouter(0x2A11a98e2fCF4674F30934B5166645fE6CA35F56); + else revert("Invalid token migration"); + } +} diff --git a/contracts/optimism/connectors/hop/interface.sol b/contracts/optimism/connectors/hop/interface.sol new file mode 100644 index 00000000..db68c7ef --- /dev/null +++ b/contracts/optimism/connectors/hop/interface.sol @@ -0,0 +1,17 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + +interface IHopRouter { + function swapAndSend( + uint256 chainId, + address recipient, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline + ) external; +} diff --git a/contracts/optimism/connectors/hop/main.sol b/contracts/optimism/connectors/hop/main.sol new file mode 100644 index 00000000..aa3b4798 --- /dev/null +++ b/contracts/optimism/connectors/hop/main.sol @@ -0,0 +1,82 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Hop. + * @dev Cross chain Bridge. + */ + +import { TokenInterface, MemoryInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import "./interface.sol"; +import "./helpers.sol"; +import "./events.sol"; + +abstract contract Resolver is Helpers { + /** + * @dev Bridge Token. + * @notice Bridge Token on HOP. + * @param params BridgeParams struct for bridging + * @param bridgeToL1 bool to check which layer to migrate to + * @param getId ID to retrieve amount from last spell. + */ + function bridge( + BridgeParams memory params, + bool bridgeToL1, + uint256 getId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + if (bridgeToL1) { + require( + params.destinationAmountOutMin == 0, + "destinationAmountOutMin != 0, sending to L1" + ); + require( + params.destinationDeadline == 0, + "destinationDeadline != 0, sending to L1" + ); + } + + params.amount = getUint(getId, params.amount); + + 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); + + _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + params.token, + params.chainId, + params.recipient, + params.amount, + params.bonderFee, + params.amountOutMin, + params.deadline, + params.destinationAmountOutMin, + params.destinationDeadline, + getId + ); + } +} + +contract ConnectV2HopOptimism is Resolver { + string public constant name = "Hop-v1.0"; +} From 5952962977fc22dee8956bdeea1feb1a3ab292cb Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 11 Apr 2022 17:40:07 +0530 Subject: [PATCH 08/14] added hop arbitrum --- contracts/arbitrum/connectors/hop/events.sol | 17 ++++ contracts/arbitrum/connectors/hop/helpers.sol | 52 ++++++++++++ .../arbitrum/connectors/hop/interface.sol | 17 ++++ contracts/arbitrum/connectors/hop/main.sol | 82 +++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 contracts/arbitrum/connectors/hop/events.sol create mode 100644 contracts/arbitrum/connectors/hop/helpers.sol create mode 100644 contracts/arbitrum/connectors/hop/interface.sol create mode 100644 contracts/arbitrum/connectors/hop/main.sol diff --git a/contracts/arbitrum/connectors/hop/events.sol b/contracts/arbitrum/connectors/hop/events.sol new file mode 100644 index 00000000..9594f4a5 --- /dev/null +++ b/contracts/arbitrum/connectors/hop/events.sol @@ -0,0 +1,17 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogBridge( + address token, + uint256 chainId, + address recipient, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline, + uint256 getId + ); +} diff --git a/contracts/arbitrum/connectors/hop/helpers.sol b/contracts/arbitrum/connectors/hop/helpers.sol new file mode 100644 index 00000000..c8667bbc --- /dev/null +++ b/contracts/arbitrum/connectors/hop/helpers.sol @@ -0,0 +1,52 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import "./interface.sol"; + +contract Helpers is DSMath, Basic { + /** + * @param token The address of token to be bridged.(For USDC: 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8) + * @param hopRouter The address of hop l2AmmWrapper. + * @param chainId The Id of the destination chain.(For MAINNET : 1) + * @param recipient The address to recieve the token on destination chain. + * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). + * @param bonderFee The fee to be recieved by bonder at destination chain. + * @param amountOutMin minimum amount of token out for swap + * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) + * @param destinationAmountOutMin minimum amount of token out for bridge, zero for L1 bridging + * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging + */ + struct BridgeParams { + address token; + address recipient; + address hopRouter; + uint256 chainId; + uint256 amount; + uint256 bonderFee; + uint256 amountOutMin; + uint256 deadline; + uint256 destinationAmountOutMin; + uint256 destinationDeadline; + } + + function _swapAndSend(BridgeParams memory params) internal { + IHopRouter router = IHopRouter(params.hopRouter); + + TokenInterface tokenContract = TokenInterface(params.token); + approve(tokenContract, params.hopRouter, params.amount); + + router.swapAndSend( + params.chainId, + params.recipient, + params.amount, + params.bonderFee, + params.amountOutMin, + params.deadline, + params.destinationAmountOutMin, + params.destinationDeadline + ); + } +} diff --git a/contracts/arbitrum/connectors/hop/interface.sol b/contracts/arbitrum/connectors/hop/interface.sol new file mode 100644 index 00000000..db68c7ef --- /dev/null +++ b/contracts/arbitrum/connectors/hop/interface.sol @@ -0,0 +1,17 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + +interface IHopRouter { + function swapAndSend( + uint256 chainId, + address recipient, + uint256 amount, + uint256 bonderFee, + uint256 amountOutMin, + uint256 deadline, + uint256 destinationAmountOutMin, + uint256 destinationDeadline + ) external; +} diff --git a/contracts/arbitrum/connectors/hop/main.sol b/contracts/arbitrum/connectors/hop/main.sol new file mode 100644 index 00000000..bf7e0204 --- /dev/null +++ b/contracts/arbitrum/connectors/hop/main.sol @@ -0,0 +1,82 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Hop. + * @dev Cross chain Bridge. + */ + +import { TokenInterface, MemoryInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import "./interface.sol"; +import "./helpers.sol"; +import "./events.sol"; + +abstract contract Resolver is Helpers { + /** + * @dev Bridge Token. + * @notice Bridge Token on HOP. + * @param params BridgeParams struct for bridging + * @param bridgeToL1 bool to check which layer to migrate to + * @param getId ID to retrieve amount from last spell. + */ + function bridge( + BridgeParams memory params, + bool bridgeToL1, + uint256 getId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + if (bridgeToL1) { + require( + params.destinationAmountOutMin == 0, + "destinationAmountOutMin != 0, sending to L1" + ); + require( + params.destinationDeadline == 0, + "destinationDeadline != 0, sending to L1" + ); + } + + params.amount = getUint(getId, params.amount); + + 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); + + _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + params.token, + params.chainId, + params.recipient, + params.amount, + params.bonderFee, + params.amountOutMin, + params.deadline, + params.destinationAmountOutMin, + params.destinationDeadline, + getId + ); + } +} + +contract ConnectV2HopArbitrum is Resolver { + string public constant name = "Hop-v1.0"; +} From 595e4fb62020ec8ab8283ebec2bc46dd0a90d7e9 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Mon, 11 Apr 2022 17:45:54 +0530 Subject: [PATCH 09/14] added l2AmmWrapper and l1Bridge addresses as a param --- contracts/mainnet/connectors/hop/helpers.sol | 34 +++---------------- contracts/optimism/connectors/hop/helpers.sol | 31 +++-------------- contracts/polygon/connectors/hop/helpers.sol | 34 +++---------------- 3 files changed, 15 insertions(+), 84 deletions(-) diff --git a/contracts/mainnet/connectors/hop/helpers.sol b/contracts/mainnet/connectors/hop/helpers.sol index 9bff9a0a..27f669d8 100644 --- a/contracts/mainnet/connectors/hop/helpers.sol +++ b/contracts/mainnet/connectors/hop/helpers.sol @@ -9,6 +9,7 @@ import "./interface.sol"; contract Helpers is DSMath, Basic { /** * @param token The address of token to be bridged.(For USDC: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48) + * @param hopRouter The address of hop l1Bridge. * @param chainId The Id of the destination chain.(For POLYGON : 137) * @param recipient The address to recieve the token on destination chain. * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). @@ -17,18 +18,19 @@ contract Helpers is DSMath, Basic { */ struct BridgeParams { address token; - uint256 chainId; address recipient; + address hopRouter; + uint256 chainId; uint256 amount; uint256 amountOutMin; uint256 deadline; } function _sendToL2(BridgeParams memory params) internal { - IHopRouter router = _getRouter(params.token); + IHopRouter router = IHopRouter(params.hopRouter); TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, address(router), params.amount); + approve(tokenContract, params.hopRouter, params.amount); router.sendToL2( params.chainId, @@ -40,30 +42,4 @@ contract Helpers is DSMath, Basic { 0 // relayer fee ); } - - function _getRouter(address token_) - internal - pure - returns (IHopRouter router) - { - if (token_ == 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48) - //USDC l1Bridge - router = IHopRouter(0x3666f603Cc164936C1b87e207F36BEBa4AC5f18a); - else if (token_ == 0xdAC17F958D2ee523a2206206994597C13D831ec7) - //USDT l1Bridge - router = IHopRouter(0x3E4a3a4796d16c0Cd582C382691998f7c06420B6); - else if (token_ == 0x7c9f4C87d911613Fe9ca58b579f737911AAD2D43) - //WMATIC l1Bridge - router = IHopRouter(0x22B1Cbb8D98a01a3B71D034BB899775A76Eb1cc2); - else if (token_ == 0x6B175474E89094C44Da98b954EedeAC495271d0F) - //DAI l1Bridge - router = IHopRouter(0x3d4Cc8A61c7528Fd86C55cfe061a78dCBA48EDd1); - else if (token_ == 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2) - //WETH l1Bridge - router = IHopRouter(0xb8901acB165ed027E32754E0FFe830802919727f); - else if (token_ == 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599) - //WBTC l1Bridge - router = IHopRouter(0xb98454270065A31D71Bf635F6F7Ee6A518dFb849); - else revert("Invalid token migration"); - } } diff --git a/contracts/optimism/connectors/hop/helpers.sol b/contracts/optimism/connectors/hop/helpers.sol index 48561498..8df6f08d 100644 --- a/contracts/optimism/connectors/hop/helpers.sol +++ b/contracts/optimism/connectors/hop/helpers.sol @@ -10,6 +10,7 @@ contract Helpers is DSMath, Basic { /** * @param token The address of token to be bridged.(For USDC: 0x7f5c764cbc14f9669b88837ca1490cca17c31607) * @param chainId The Id of the destination chain.(For MAINNET : 1) + * @param hopRouter The address of hop l2AmmWrapper. * @param recipient The address to recieve the token on destination chain. * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). * @param bonderFee The fee to be recieved by bonder at destination chain. @@ -20,8 +21,9 @@ contract Helpers is DSMath, Basic { */ struct BridgeParams { address token; - uint256 chainId; address recipient; + address hopRouter; + uint256 chainId; uint256 amount; uint256 bonderFee; uint256 amountOutMin; @@ -31,10 +33,10 @@ contract Helpers is DSMath, Basic { } function _swapAndSend(BridgeParams memory params) internal { - IHopRouter router = _getRouter(params.token); + IHopRouter router = IHopRouter(params.hopRouter); TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, address(router), params.amount); + approve(tokenContract, params.hopRouter, params.amount); router.swapAndSend( params.chainId, @@ -47,27 +49,4 @@ contract Helpers is DSMath, Basic { params.destinationDeadline ); } - - function _getRouter(address token_) - internal - pure - returns (IHopRouter router) - { - if (token_ == 0x7F5c764cBc14f9669B88837ca1490cCa17c31607) - //USDC l2AmmWrapper - router = IHopRouter(0x2ad09850b0CA4c7c1B33f5AcD6cBAbCaB5d6e796); - else if (token_ == 0x94b008aA00579c1307B0EF2c499aD98a8ce58e58) - //USDT l2AmmWrapper - router = IHopRouter(0x7D269D3E0d61A05a0bA976b7DBF8805bF844AF3F); - else if (token_ == 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1) - //DAI l2AmmWrapper - router = IHopRouter(0xb3C68a491608952Cb1257FC9909a537a0173b63B); - else if (token_ == 0x4200000000000000000000000000000000000006) - //WETH l2AmmWrapper - router = IHopRouter(0x86cA30bEF97fB651b8d866D45503684b90cb3312); - else if (token_ == 0x68f180fcCe6836688e9084f035309E29Bf0A2095) - //WBTC l2AmmWrapper - router = IHopRouter(0x2A11a98e2fCF4674F30934B5166645fE6CA35F56); - else revert("Invalid token migration"); - } } diff --git a/contracts/polygon/connectors/hop/helpers.sol b/contracts/polygon/connectors/hop/helpers.sol index 73000389..e32b5082 100644 --- a/contracts/polygon/connectors/hop/helpers.sol +++ b/contracts/polygon/connectors/hop/helpers.sol @@ -10,6 +10,7 @@ contract Helpers is DSMath, Basic { /** * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) * @param chainId The Id of the destination chain.(For MAINNET : 1) + * @param hopRouter The address of hop l2AmmWrapper. * @param recipient The address to recieve the token on destination chain. * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). * @param bonderFee The fee to be recieved by bonder at destination chain. @@ -20,8 +21,9 @@ contract Helpers is DSMath, Basic { */ struct BridgeParams { address token; - uint256 chainId; + address hopRouter; address recipient; + uint256 chainId; uint256 amount; uint256 bonderFee; uint256 amountOutMin; @@ -31,10 +33,10 @@ contract Helpers is DSMath, Basic { } function _swapAndSend(BridgeParams memory params) internal { - IHopRouter router = _getRouter(params.token); + IHopRouter router = IHopRouter(params.hopRouter); TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, address(router), params.amount); + approve(tokenContract, params.hopRouter, params.amount); router.swapAndSend( params.chainId, @@ -47,30 +49,4 @@ contract Helpers is DSMath, Basic { params.destinationDeadline ); } - - function _getRouter(address token_) - internal - pure - returns (IHopRouter router) - { - if (token_ == 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) - //USDC l2AmmWrapper - router = IHopRouter(0x76b22b8C1079A44F1211D867D68b1eda76a635A7); - else if (token_ == 0xc2132D05D31c914a87C6611C10748AEb04B58e8F) - //USDT l2AmmWrapper - router = IHopRouter(0x8741Ba6225A6BF91f9D73531A98A89807857a2B3); - else if (token_ == 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270) - //WMATIC l2AmmWrapper - router = IHopRouter(0x884d1Aa15F9957E1aEAA86a82a72e49Bc2bfCbe3); - else if (token_ == 0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063) - //DAI l2AmmWrapper - router = IHopRouter(0x28529fec439cfF6d7D1D5917e956dEE62Cd3BE5c); - else if (token_ == 0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619) - //WETH l2AmmWrapper - router = IHopRouter(0xc315239cFb05F1E130E7E28E603CEa4C014c57f0); - else if (token_ == 0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6) - //WBTC l2AmmWrapper - router = IHopRouter(0xCd1d7AEfA8055e020db0d0e98bbF3FeD1A16aad6); - else revert("Invalid token migration"); - } } From 382366e006fc4f8d67120393d3bb15a4d0bb6eb7 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Tue, 12 Apr 2022 21:53:55 +0530 Subject: [PATCH 10/14] used params.chainId instead of bool to check --- contracts/arbitrum/connectors/hop/main.sol | 9 ++------- contracts/optimism/connectors/hop/main.sol | 9 ++------- contracts/polygon/connectors/hop/main.sol | 9 ++------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/contracts/arbitrum/connectors/hop/main.sol b/contracts/arbitrum/connectors/hop/main.sol index bf7e0204..88ae2f7d 100644 --- a/contracts/arbitrum/connectors/hop/main.sol +++ b/contracts/arbitrum/connectors/hop/main.sol @@ -18,19 +18,14 @@ abstract contract Resolver is Helpers { * @dev Bridge Token. * @notice Bridge Token on HOP. * @param params BridgeParams struct for bridging - * @param bridgeToL1 bool to check which layer to migrate to * @param getId ID to retrieve amount from last spell. */ - function bridge( - BridgeParams memory params, - bool bridgeToL1, - uint256 getId - ) + function bridge(BridgeParams memory params, uint256 getId) external payable returns (string memory _eventName, bytes memory _eventParam) { - if (bridgeToL1) { + if (params.chainId == 1) { require( params.destinationAmountOutMin == 0, "destinationAmountOutMin != 0, sending to L1" diff --git a/contracts/optimism/connectors/hop/main.sol b/contracts/optimism/connectors/hop/main.sol index aa3b4798..c44b29d5 100644 --- a/contracts/optimism/connectors/hop/main.sol +++ b/contracts/optimism/connectors/hop/main.sol @@ -18,19 +18,14 @@ abstract contract Resolver is Helpers { * @dev Bridge Token. * @notice Bridge Token on HOP. * @param params BridgeParams struct for bridging - * @param bridgeToL1 bool to check which layer to migrate to * @param getId ID to retrieve amount from last spell. */ - function bridge( - BridgeParams memory params, - bool bridgeToL1, - uint256 getId - ) + function bridge(BridgeParams memory params, uint256 getId) external payable returns (string memory _eventName, bytes memory _eventParam) { - if (bridgeToL1) { + if (params.chainId == 1) { require( params.destinationAmountOutMin == 0, "destinationAmountOutMin != 0, sending to L1" diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol index a36d8ca1..d0a432e2 100644 --- a/contracts/polygon/connectors/hop/main.sol +++ b/contracts/polygon/connectors/hop/main.sol @@ -18,19 +18,14 @@ abstract contract Resolver is Helpers { * @dev Bridge Token. * @notice Bridge Token on HOP. * @param params BridgeParams struct for bridging - * @param bridgeToL1 bool to check which layer to migrate to * @param getId ID to retrieve amount from last spell. */ - function bridge( - BridgeParams memory params, - bool bridgeToL1, - uint256 getId - ) + function bridge(BridgeParams memory params, uint256 getId) external payable returns (string memory _eventName, bytes memory _eventParam) { - if (bridgeToL1) { + if (params.chainId == 1) { require( params.destinationAmountOutMin == 0, "destinationAmountOutMin != 0, sending to L1" From dbb08a2b12aa4ec320746c104e0f4bf0ec7a43c1 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Tue, 12 Apr 2022 23:21:46 +0530 Subject: [PATCH 11/14] added hop polygon tests --- test/polygon/hop/hop.test.ts | 147 +++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 test/polygon/hop/hop.test.ts diff --git a/test/polygon/hop/hop.test.ts b/test/polygon/hop/hop.test.ts new file mode 100644 index 00000000..bbcac02b --- /dev/null +++ b/test/polygon/hop/hop.test.ts @@ -0,0 +1,147 @@ +import { expect } from "chai"; +import hre from "hardhat"; +const { waffle, ethers } = hre; +const { provider } = waffle; + +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { addLiquidity } from "../../../scripts/tests/addLiquidity"; +import { addresses } from "../../../scripts/tests/polygon/addresses"; +import { abis } from "../../../scripts/constant/abis"; +import { ConnectV2HopPolygon__factory } from "../../../typechain"; +import { Signer, Contract } from "ethers"; +import BigNumber from "bignumber.js"; + +describe("Hop Connector", function () { + const connectorName = "HOP-A"; + + let dsaWallet0: Contract; + let masterSigner: Signer; + let instaConnectorsV2: Contract; + let connector: Contract; + let latestBlock: any; + + const wallets = provider.getWallets(); + const [wallet0, wallet1, wallet2, wallet3] = wallets; + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + // @ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + blockNumber: 27054896 + } + } + ] + }); + + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2HopPolygon__factory, + 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(!!(await masterSigner.getAddress())).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 MATIC & 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("10000")); + }); + }); + + describe("Main", function () { + it("should migrate from L2 to L1", async function () { + const amount = ethers.utils.parseEther("10"); + const DAI_ADDR = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063"; + const l2AmmWrapper = "0x28529fec439cfF6d7D1D5917e956dEE62Cd3BE5c"; + const bonderFee = new BigNumber(100 * 1e9) + .multipliedBy(150000) + .multipliedBy(1.5) + .plus(new BigNumber(amount.toString()).multipliedBy(0.72)); + const deadline = Date.now() + 604800; + const getId = "0"; + + const token = new ethers.Contract(DAI_ADDR, abis.basic.erc20); + await token.connect(wallet0).approve(l2AmmWrapper, amount.toString()); + + const params: any = [ + DAI_ADDR, + l2AmmWrapper, + wallet0.address, + 1, + amount.toString(), + bonderFee.toString(), + "0", + deadline, + "0", + "0" + ]; + + const spells = [ + { + connector: connectorName, + method: "bridge", + args: [params, getId] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + let receipt = await tx.wait(); + latestBlock = receipt.blockNumber; + }); + + it("Should fetch transferId from TransferSent Event", async function () { + const l2Bridge = "0xEcf268Be00308980B5b3fcd0975D47C4C8e1382a"; + const ABI = [ + { + anonymous: false, + inputs: [ + { indexed: true, internalType: "bytes32", name: "transferId", type: "bytes32" }, + { indexed: true, internalType: "uint256", name: "chainId", type: "uint256" }, + { indexed: true, internalType: "address", name: "recipient", type: "address" }, + { indexed: false, internalType: "uint256", name: "amount", type: "uint256" }, + { indexed: false, internalType: "bytes32", name: "transferNonce", type: "bytes32" }, + { indexed: false, internalType: "uint256", name: "bonderFee", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "index", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "deadline", type: "uint256" } + ], + name: "TransferSent", + type: "event" + } + ]; + const l2BridgeInstance = new ethers.Contract(l2Bridge, ABI); + const filter = l2BridgeInstance.connect(wallet0).filters.TransferSent(); + const events = await l2BridgeInstance.connect(wallet0).queryFilter(filter, 27054896, latestBlock); + + const transferSentEvent: any = events[0].args; + + expect(transferSentEvent[2].toLowerCase()).to.be.equals(wallet0.address.toLowerCase()); + console.log("TransferId", transferSentEvent[0]); + }); + }); +}); From 4af97282efc22ddf374736a857850eba7f8c0b78 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Tue, 12 Apr 2022 23:41:30 +0530 Subject: [PATCH 12/14] added l2-l2 migration tests --- test/polygon/hop/hop.test.ts | 100 +++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/test/polygon/hop/hop.test.ts b/test/polygon/hop/hop.test.ts index bbcac02b..a96c591a 100644 --- a/test/polygon/hop/hop.test.ts +++ b/test/polygon/hop/hop.test.ts @@ -143,5 +143,105 @@ describe("Hop Connector", function () { expect(transferSentEvent[2].toLowerCase()).to.be.equals(wallet0.address.toLowerCase()); console.log("TransferId", transferSentEvent[0]); }); + + it("should migrate from L2 to L2", async function () { + const amount = ethers.utils.parseEther("10"); + const DAI_ADDR = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063"; + const hDai = "0xb8901acB165ed027E32754E0FFe830802919727f"; + const l2AmmWrapper = "0x28529fec439cfF6d7D1D5917e956dEE62Cd3BE5c"; + const ABI = [ + { + inputs: [{ internalType: "address", name: "tokenAddress", type: "address" }], + name: "getTokenIndex", + outputs: [{ internalType: "uint8", name: "", type: "uint8" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "uint8", name: "tokenIndexFrom", type: "uint8" }, + { internalType: "uint8", name: "tokenIndexTo", type: "uint8" }, + { internalType: "uint256", name: "dx", type: "uint256" } + ], + name: "calculateSwap", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + } + ]; + + const saddleSwap = new ethers.Contract("0x25FB92E505F752F730cAD0Bd4fa17ecE4A384266", ABI); + const hDaiIdx = await saddleSwap.connect(wallet0).getTokenIndex(hDai); + const daiIdx = await saddleSwap.connect(wallet0).getTokenIndex(DAI_ADDR); + const amountWithSlippage = new BigNumber(amount.toString()).multipliedBy(0.99); + const destinationAmountOutMin = await saddleSwap + .connect(wallet0) + .calculateSwap(daiIdx, hDaiIdx, amountWithSlippage.toString()); + + const bonderFee = new BigNumber(1e9) + .multipliedBy(150000) + .multipliedBy(1.5) + .plus(new BigNumber(amount.toString()).multipliedBy(0.72)); + const deadline = Date.now() + 604800; + const getId = "0"; + + const token = new ethers.Contract(DAI_ADDR, abis.basic.erc20); + await token.connect(wallet0).approve(l2AmmWrapper, amount.toString()); + + const params: any = [ + DAI_ADDR, + l2AmmWrapper, + wallet0.address, + 10, + amount.toString(), + bonderFee.toString(), + "0", + deadline, + destinationAmountOutMin.toString(), + deadline + ]; + + const spells = [ + { + connector: connectorName, + method: "bridge", + args: [params, getId] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + let receipt = await tx.wait(); + latestBlock = receipt.blockNumber; + }); + + it("Should fetch transferId from TransferSent Event", async function () { + const l2Bridge = "0xEcf268Be00308980B5b3fcd0975D47C4C8e1382a"; + const ABI = [ + { + anonymous: false, + inputs: [ + { indexed: true, internalType: "bytes32", name: "transferId", type: "bytes32" }, + { indexed: true, internalType: "uint256", name: "chainId", type: "uint256" }, + { indexed: true, internalType: "address", name: "recipient", type: "address" }, + { indexed: false, internalType: "uint256", name: "amount", type: "uint256" }, + { indexed: false, internalType: "bytes32", name: "transferNonce", type: "bytes32" }, + { indexed: false, internalType: "uint256", name: "bonderFee", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "index", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "deadline", type: "uint256" } + ], + name: "TransferSent", + type: "event" + } + ]; + const l2BridgeInstance = new ethers.Contract(l2Bridge, ABI); + const filter = l2BridgeInstance.connect(wallet0).filters.TransferSent(); + const events = await l2BridgeInstance.connect(wallet0).queryFilter(filter, 27054896, latestBlock); + + const transferSentEvent: any = events[1].args; + + expect(transferSentEvent[2].toLowerCase()).to.be.equals(wallet0.address.toLowerCase()); + console.log("TransferId", transferSentEvent[0]); + }); }); }); From a5edeee567f1f2a6d21e625dbcb70836d75fd592 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Wed, 13 Apr 2022 00:32:47 +0530 Subject: [PATCH 13/14] refactored test code --- test/polygon/hop/hop.test.ts | 149 +++++++++++++++++------------------ 1 file changed, 71 insertions(+), 78 deletions(-) diff --git a/test/polygon/hop/hop.test.ts b/test/polygon/hop/hop.test.ts index a96c591a..c7fc3294 100644 --- a/test/polygon/hop/hop.test.ts +++ b/test/polygon/hop/hop.test.ts @@ -25,6 +25,57 @@ describe("Hop Connector", function () { const wallets = provider.getWallets(); const [wallet0, wallet1, wallet2, wallet3] = wallets; + + const saddleSwapABI = [ + { + inputs: [{ internalType: "address", name: "tokenAddress", type: "address" }], + name: "getTokenIndex", + outputs: [{ internalType: "uint8", name: "", type: "uint8" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "uint8", name: "tokenIndexFrom", type: "uint8" }, + { internalType: "uint8", name: "tokenIndexTo", type: "uint8" }, + { internalType: "uint256", name: "dx", type: "uint256" } + ], + name: "calculateSwap", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + } + ]; + + const l2BridgeABI = [ + { + anonymous: false, + inputs: [ + { indexed: true, internalType: "bytes32", name: "transferId", type: "bytes32" }, + { indexed: true, internalType: "uint256", name: "chainId", type: "uint256" }, + { indexed: true, internalType: "address", name: "recipient", type: "address" }, + { indexed: false, internalType: "uint256", name: "amount", type: "uint256" }, + { indexed: false, internalType: "bytes32", name: "transferNonce", type: "bytes32" }, + { indexed: false, internalType: "uint256", name: "bonderFee", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "index", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "amountOutMin", type: "uint256" }, + { indexed: false, internalType: "uint256", name: "deadline", type: "uint256" } + ], + name: "TransferSent", + type: "event" + } + ]; + + const DAI_ADDR = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063"; + const hDai = "0xb8901acB165ed027E32754E0FFe830802919727f"; + const l2AmmWrapper = "0x28529fec439cfF6d7D1D5917e956dEE62Cd3BE5c"; + const l2Bridge = "0xEcf268Be00308980B5b3fcd0975D47C4C8e1382a"; + const saddleSwap = "0x25FB92E505F752F730cAD0Bd4fa17ecE4A384266"; + const saddleSwapInstance = new ethers.Contract(saddleSwap, saddleSwapABI); + + const token = new ethers.Contract(DAI_ADDR, abis.basic.erc20); + const l2BridgeInstance = new ethers.Contract(l2Bridge, l2BridgeABI); + before(async () => { await hre.network.provider.request({ method: "hardhat_reset", @@ -76,18 +127,21 @@ describe("Hop Connector", function () { describe("Main", function () { it("should migrate from L2 to L1", async function () { const amount = ethers.utils.parseEther("10"); - const DAI_ADDR = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063"; - const l2AmmWrapper = "0x28529fec439cfF6d7D1D5917e956dEE62Cd3BE5c"; const bonderFee = new BigNumber(100 * 1e9) .multipliedBy(150000) .multipliedBy(1.5) - .plus(new BigNumber(amount.toString()).multipliedBy(0.72)); + .plus(new BigNumber(amount.toString()).multipliedBy(0.18)); const deadline = Date.now() + 604800; const getId = "0"; - const token = new ethers.Contract(DAI_ADDR, abis.basic.erc20); await token.connect(wallet0).approve(l2AmmWrapper, amount.toString()); + const hDaiIdx = await saddleSwapInstance.connect(wallet0).getTokenIndex(hDai); + const daiIdx = await saddleSwapInstance.connect(wallet0).getTokenIndex(DAI_ADDR); + + let amountOutMin = await saddleSwapInstance.connect(wallet0).calculateSwap(daiIdx, hDaiIdx, amount.toString()); + amountOutMin = new BigNumber(amountOutMin.toString()).multipliedBy(0.99); + const params: any = [ DAI_ADDR, l2AmmWrapper, @@ -95,7 +149,7 @@ describe("Hop Connector", function () { 1, amount.toString(), bonderFee.toString(), - "0", + amountOutMin.toFixed(0), deadline, "0", "0" @@ -115,26 +169,6 @@ describe("Hop Connector", function () { }); it("Should fetch transferId from TransferSent Event", async function () { - const l2Bridge = "0xEcf268Be00308980B5b3fcd0975D47C4C8e1382a"; - const ABI = [ - { - anonymous: false, - inputs: [ - { indexed: true, internalType: "bytes32", name: "transferId", type: "bytes32" }, - { indexed: true, internalType: "uint256", name: "chainId", type: "uint256" }, - { indexed: true, internalType: "address", name: "recipient", type: "address" }, - { indexed: false, internalType: "uint256", name: "amount", type: "uint256" }, - { indexed: false, internalType: "bytes32", name: "transferNonce", type: "bytes32" }, - { indexed: false, internalType: "uint256", name: "bonderFee", type: "uint256" }, - { indexed: false, internalType: "uint256", name: "index", type: "uint256" }, - { indexed: false, internalType: "uint256", name: "amountOutMin", type: "uint256" }, - { indexed: false, internalType: "uint256", name: "deadline", type: "uint256" } - ], - name: "TransferSent", - type: "event" - } - ]; - const l2BridgeInstance = new ethers.Contract(l2Bridge, ABI); const filter = l2BridgeInstance.connect(wallet0).filters.TransferSent(); const events = await l2BridgeInstance.connect(wallet0).queryFilter(filter, 27054896, latestBlock); @@ -146,46 +180,25 @@ describe("Hop Connector", function () { it("should migrate from L2 to L2", async function () { const amount = ethers.utils.parseEther("10"); - const DAI_ADDR = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063"; - const hDai = "0xb8901acB165ed027E32754E0FFe830802919727f"; - const l2AmmWrapper = "0x28529fec439cfF6d7D1D5917e956dEE62Cd3BE5c"; - const ABI = [ - { - inputs: [{ internalType: "address", name: "tokenAddress", type: "address" }], - name: "getTokenIndex", - outputs: [{ internalType: "uint8", name: "", type: "uint8" }], - stateMutability: "view", - type: "function" - }, - { - inputs: [ - { internalType: "uint8", name: "tokenIndexFrom", type: "uint8" }, - { internalType: "uint8", name: "tokenIndexTo", type: "uint8" }, - { internalType: "uint256", name: "dx", type: "uint256" } - ], - name: "calculateSwap", - outputs: [{ internalType: "uint256", name: "", type: "uint256" }], - stateMutability: "view", - type: "function" - } - ]; - const saddleSwap = new ethers.Contract("0x25FB92E505F752F730cAD0Bd4fa17ecE4A384266", ABI); - const hDaiIdx = await saddleSwap.connect(wallet0).getTokenIndex(hDai); - const daiIdx = await saddleSwap.connect(wallet0).getTokenIndex(DAI_ADDR); - const amountWithSlippage = new BigNumber(amount.toString()).multipliedBy(0.99); - const destinationAmountOutMin = await saddleSwap + const hDaiIdx = await saddleSwapInstance.connect(wallet0).getTokenIndex(hDai); + const daiIdx = await saddleSwapInstance.connect(wallet0).getTokenIndex(DAI_ADDR); + + let destinationAmountOutMin = await saddleSwapInstance .connect(wallet0) - .calculateSwap(daiIdx, hDaiIdx, amountWithSlippage.toString()); + .calculateSwap(hDaiIdx, daiIdx, amount.toString()); + destinationAmountOutMin = new BigNumber(destinationAmountOutMin.toString()).multipliedBy(0.99); + + let amountOutMin = await saddleSwapInstance.connect(wallet0).calculateSwap(daiIdx, hDaiIdx, amount.toString()); + amountOutMin = new BigNumber(amountOutMin.toString()).multipliedBy(0.99); const bonderFee = new BigNumber(1e9) .multipliedBy(150000) .multipliedBy(1.5) - .plus(new BigNumber(amount.toString()).multipliedBy(0.72)); + .plus(new BigNumber(amount.toString()).multipliedBy(0.18)); const deadline = Date.now() + 604800; const getId = "0"; - const token = new ethers.Contract(DAI_ADDR, abis.basic.erc20); await token.connect(wallet0).approve(l2AmmWrapper, amount.toString()); const params: any = [ @@ -195,9 +208,9 @@ describe("Hop Connector", function () { 10, amount.toString(), bonderFee.toString(), - "0", + amountOutMin.toFixed(0), deadline, - destinationAmountOutMin.toString(), + destinationAmountOutMin.toFixed(0), deadline ]; @@ -215,26 +228,6 @@ describe("Hop Connector", function () { }); it("Should fetch transferId from TransferSent Event", async function () { - const l2Bridge = "0xEcf268Be00308980B5b3fcd0975D47C4C8e1382a"; - const ABI = [ - { - anonymous: false, - inputs: [ - { indexed: true, internalType: "bytes32", name: "transferId", type: "bytes32" }, - { indexed: true, internalType: "uint256", name: "chainId", type: "uint256" }, - { indexed: true, internalType: "address", name: "recipient", type: "address" }, - { indexed: false, internalType: "uint256", name: "amount", type: "uint256" }, - { indexed: false, internalType: "bytes32", name: "transferNonce", type: "bytes32" }, - { indexed: false, internalType: "uint256", name: "bonderFee", type: "uint256" }, - { indexed: false, internalType: "uint256", name: "index", type: "uint256" }, - { indexed: false, internalType: "uint256", name: "amountOutMin", type: "uint256" }, - { indexed: false, internalType: "uint256", name: "deadline", type: "uint256" } - ], - name: "TransferSent", - type: "event" - } - ]; - const l2BridgeInstance = new ethers.Contract(l2Bridge, ABI); const filter = l2BridgeInstance.connect(wallet0).filters.TransferSent(); const events = await l2BridgeInstance.connect(wallet0).queryFilter(filter, 27054896, latestBlock); From 8f6c5092ac70fdfb0a07257afaa28600cf36ebe8 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Sat, 16 Apr 2022 22:54:24 +0530 Subject: [PATCH 14/14] updated parama --- contracts/arbitrum/connectors/hop/helpers.sol | 32 +++++++++---------- contracts/arbitrum/connectors/hop/main.sol | 8 ++--- contracts/mainnet/connectors/hop/helpers.sol | 18 +++++------ contracts/mainnet/connectors/hop/main.sol | 6 ++-- contracts/optimism/connectors/hop/helpers.sol | 32 +++++++++---------- contracts/optimism/connectors/hop/main.sol | 8 ++--- contracts/polygon/connectors/hop/helpers.sol | 30 ++++++++--------- contracts/polygon/connectors/hop/main.sol | 8 ++--- 8 files changed, 71 insertions(+), 71 deletions(-) diff --git a/contracts/arbitrum/connectors/hop/helpers.sol b/contracts/arbitrum/connectors/hop/helpers.sol index c8667bbc..9bb2f5f7 100644 --- a/contracts/arbitrum/connectors/hop/helpers.sol +++ b/contracts/arbitrum/connectors/hop/helpers.sol @@ -8,43 +8,43 @@ import "./interface.sol"; contract Helpers is DSMath, Basic { /** - * @param token The address of token to be bridged.(For USDC: 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8) - * @param hopRouter The address of hop l2AmmWrapper. - * @param chainId The Id of the destination chain.(For MAINNET : 1) + * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) + * @param targetChainId The Id of the destination chain.(For MAINNET : 1) + * @param router The address of hop router. * @param recipient The address to recieve the token on destination chain. * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). * @param bonderFee The fee to be recieved by bonder at destination chain. - * @param amountOutMin minimum amount of token out for swap - * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) - * @param destinationAmountOutMin minimum amount of token out for bridge, zero for L1 bridging - * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging + * @param sourceAmountOutMin minimum amount of token out for swap on source chain. + * @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 */ struct BridgeParams { address token; + address router; address recipient; - address hopRouter; - uint256 chainId; + uint256 targetChainId; uint256 amount; uint256 bonderFee; - uint256 amountOutMin; - uint256 deadline; + uint256 sourceAmountOutMin; + uint256 sourceDeadline; uint256 destinationAmountOutMin; uint256 destinationDeadline; } function _swapAndSend(BridgeParams memory params) internal { - IHopRouter router = IHopRouter(params.hopRouter); + IHopRouter router = IHopRouter(params.router); TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, params.hopRouter, params.amount); + approve(tokenContract, params.router, params.amount); router.swapAndSend( - params.chainId, + params.targetChainId, params.recipient, params.amount, params.bonderFee, - params.amountOutMin, - params.deadline, + params.sourceAmountOutMin, + params.sourceDeadline, params.destinationAmountOutMin, params.destinationDeadline ); diff --git a/contracts/arbitrum/connectors/hop/main.sol b/contracts/arbitrum/connectors/hop/main.sol index 88ae2f7d..aca1aa29 100644 --- a/contracts/arbitrum/connectors/hop/main.sol +++ b/contracts/arbitrum/connectors/hop/main.sol @@ -25,7 +25,7 @@ abstract contract Resolver is Helpers { payable returns (string memory _eventName, bytes memory _eventParam) { - if (params.chainId == 1) { + if (params.targetChainId == 1) { require( params.destinationAmountOutMin == 0, "destinationAmountOutMin != 0, sending to L1" @@ -59,12 +59,12 @@ abstract contract Resolver is Helpers { _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( params.token, - params.chainId, + params.targetChainId, params.recipient, params.amount, params.bonderFee, - params.amountOutMin, - params.deadline, + params.sourceAmountOutMin, + params.sourceDeadline, params.destinationAmountOutMin, params.destinationDeadline, getId diff --git a/contracts/mainnet/connectors/hop/helpers.sol b/contracts/mainnet/connectors/hop/helpers.sol index 27f669d8..9b8b099c 100644 --- a/contracts/mainnet/connectors/hop/helpers.sol +++ b/contracts/mainnet/connectors/hop/helpers.sol @@ -19,25 +19,25 @@ contract Helpers is DSMath, Basic { struct BridgeParams { address token; address recipient; - address hopRouter; - uint256 chainId; + address router; + uint256 targetChainId; uint256 amount; - uint256 amountOutMin; - uint256 deadline; + uint256 destinationAmountOutMin; + uint256 destinationDeadline; } function _sendToL2(BridgeParams memory params) internal { - IHopRouter router = IHopRouter(params.hopRouter); + IHopRouter router = IHopRouter(params.router); TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, params.hopRouter, params.amount); + approve(tokenContract, params.router, params.amount); router.sendToL2( - params.chainId, + params.targetChainId, params.recipient, params.amount, - params.amountOutMin, - params.deadline, + params.destinationAmountOutMin, + params.destinationDeadline, address(0), // relayer address 0 // relayer fee ); diff --git a/contracts/mainnet/connectors/hop/main.sol b/contracts/mainnet/connectors/hop/main.sol index 0cd42411..32481914 100644 --- a/contracts/mainnet/connectors/hop/main.sol +++ b/contracts/mainnet/connectors/hop/main.sol @@ -48,11 +48,11 @@ abstract contract Resolver is Helpers { _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( params.token, - params.chainId, + params.targetChainId, params.recipient, params.amount, - params.amountOutMin, - params.deadline, + params.destinationAmountOutMin, + params.destinationDeadline, getId ); } diff --git a/contracts/optimism/connectors/hop/helpers.sol b/contracts/optimism/connectors/hop/helpers.sol index 8df6f08d..9bb2f5f7 100644 --- a/contracts/optimism/connectors/hop/helpers.sol +++ b/contracts/optimism/connectors/hop/helpers.sol @@ -8,43 +8,43 @@ import "./interface.sol"; contract Helpers is DSMath, Basic { /** - * @param token The address of token to be bridged.(For USDC: 0x7f5c764cbc14f9669b88837ca1490cca17c31607) - * @param chainId The Id of the destination chain.(For MAINNET : 1) - * @param hopRouter The address of hop l2AmmWrapper. + * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) + * @param targetChainId The Id of the destination chain.(For MAINNET : 1) + * @param router The address of hop router. * @param recipient The address to recieve the token on destination chain. * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). * @param bonderFee The fee to be recieved by bonder at destination chain. - * @param amountOutMin minimum amount of token out for swap - * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) - * @param destinationAmountOutMin minimum amount of token out for bridge, zero for L1 bridging - * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging + * @param sourceAmountOutMin minimum amount of token out for swap on source chain. + * @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 */ struct BridgeParams { address token; + address router; address recipient; - address hopRouter; - uint256 chainId; + uint256 targetChainId; uint256 amount; uint256 bonderFee; - uint256 amountOutMin; - uint256 deadline; + uint256 sourceAmountOutMin; + uint256 sourceDeadline; uint256 destinationAmountOutMin; uint256 destinationDeadline; } function _swapAndSend(BridgeParams memory params) internal { - IHopRouter router = IHopRouter(params.hopRouter); + IHopRouter router = IHopRouter(params.router); TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, params.hopRouter, params.amount); + approve(tokenContract, params.router, params.amount); router.swapAndSend( - params.chainId, + params.targetChainId, params.recipient, params.amount, params.bonderFee, - params.amountOutMin, - params.deadline, + params.sourceAmountOutMin, + params.sourceDeadline, params.destinationAmountOutMin, params.destinationDeadline ); diff --git a/contracts/optimism/connectors/hop/main.sol b/contracts/optimism/connectors/hop/main.sol index c44b29d5..9f5f8594 100644 --- a/contracts/optimism/connectors/hop/main.sol +++ b/contracts/optimism/connectors/hop/main.sol @@ -25,7 +25,7 @@ abstract contract Resolver is Helpers { payable returns (string memory _eventName, bytes memory _eventParam) { - if (params.chainId == 1) { + if (params.targetChainId == 1) { require( params.destinationAmountOutMin == 0, "destinationAmountOutMin != 0, sending to L1" @@ -59,12 +59,12 @@ abstract contract Resolver is Helpers { _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( params.token, - params.chainId, + params.targetChainId, params.recipient, params.amount, params.bonderFee, - params.amountOutMin, - params.deadline, + params.sourceAmountOutMin, + params.sourceDeadline, params.destinationAmountOutMin, params.destinationDeadline, getId diff --git a/contracts/polygon/connectors/hop/helpers.sol b/contracts/polygon/connectors/hop/helpers.sol index e32b5082..9bb2f5f7 100644 --- a/contracts/polygon/connectors/hop/helpers.sol +++ b/contracts/polygon/connectors/hop/helpers.sol @@ -9,42 +9,42 @@ import "./interface.sol"; contract Helpers is DSMath, Basic { /** * @param token The address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174) - * @param chainId The Id of the destination chain.(For MAINNET : 1) - * @param hopRouter The address of hop l2AmmWrapper. + * @param targetChainId The Id of the destination chain.(For MAINNET : 1) + * @param router The address of hop router. * @param recipient The address to recieve the token on destination chain. * @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost). * @param bonderFee The fee to be recieved by bonder at destination chain. - * @param amountOutMin minimum amount of token out for swap - * @param deadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)) - * @param destinationAmountOutMin minimum amount of token out for bridge, zero for L1 bridging - * @param destinationDeadline The deadline for the transaction (Recommended - Date.now() + 604800 (1 week)), zero for L1 bridging + * @param sourceAmountOutMin minimum amount of token out for swap on source chain. + * @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 */ struct BridgeParams { address token; - address hopRouter; + address router; address recipient; - uint256 chainId; + uint256 targetChainId; uint256 amount; uint256 bonderFee; - uint256 amountOutMin; - uint256 deadline; + uint256 sourceAmountOutMin; + uint256 sourceDeadline; uint256 destinationAmountOutMin; uint256 destinationDeadline; } function _swapAndSend(BridgeParams memory params) internal { - IHopRouter router = IHopRouter(params.hopRouter); + IHopRouter router = IHopRouter(params.router); TokenInterface tokenContract = TokenInterface(params.token); - approve(tokenContract, params.hopRouter, params.amount); + approve(tokenContract, params.router, params.amount); router.swapAndSend( - params.chainId, + params.targetChainId, params.recipient, params.amount, params.bonderFee, - params.amountOutMin, - params.deadline, + params.sourceAmountOutMin, + params.sourceDeadline, params.destinationAmountOutMin, params.destinationDeadline ); diff --git a/contracts/polygon/connectors/hop/main.sol b/contracts/polygon/connectors/hop/main.sol index d0a432e2..bb6ef7c1 100644 --- a/contracts/polygon/connectors/hop/main.sol +++ b/contracts/polygon/connectors/hop/main.sol @@ -25,7 +25,7 @@ abstract contract Resolver is Helpers { payable returns (string memory _eventName, bytes memory _eventParam) { - if (params.chainId == 1) { + if (params.targetChainId == 1) { require( params.destinationAmountOutMin == 0, "destinationAmountOutMin != 0, sending to L1" @@ -59,12 +59,12 @@ abstract contract Resolver is Helpers { _eventName = "LogBridge(address,uint256,address,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( params.token, - params.chainId, + params.targetChainId, params.recipient, params.amount, params.bonderFee, - params.amountOutMin, - params.deadline, + params.sourceAmountOutMin, + params.sourceDeadline, params.destinationAmountOutMin, params.destinationDeadline, getId