From c61d69682ddc257c9d951bf953d2fd1ad447e708 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 15 Jun 2022 20:54:05 +0530 Subject: [PATCH] contracts-opt,arb,ftm --- contracts/arbitrum/common/interfaces.sol | 5 +++ contracts/arbitrum/connectors/swap/events.sol | 12 +++++ .../arbitrum/connectors/swap/helpers.sol | 44 +++++++++++++++++++ contracts/arbitrum/connectors/swap/main.sol | 44 +++++++++++++++++++ contracts/fantom/common/interfaces.sol | 7 +++ contracts/fantom/connectors/swap/events.sol | 12 +++++ contracts/fantom/connectors/swap/helpers.sol | 44 +++++++++++++++++++ contracts/fantom/connectors/swap/main.sol | 44 +++++++++++++++++++ contracts/optimism/common/interfaces.sol | 5 +++ contracts/optimism/connectors/swap/events.sol | 12 +++++ .../optimism/connectors/swap/helpers.sol | 44 +++++++++++++++++++ contracts/optimism/connectors/swap/main.sol | 44 +++++++++++++++++++ 12 files changed, 317 insertions(+) create mode 100644 contracts/arbitrum/connectors/swap/events.sol create mode 100644 contracts/arbitrum/connectors/swap/helpers.sol create mode 100644 contracts/arbitrum/connectors/swap/main.sol create mode 100644 contracts/fantom/connectors/swap/events.sol create mode 100644 contracts/fantom/connectors/swap/helpers.sol create mode 100644 contracts/fantom/connectors/swap/main.sol create mode 100644 contracts/optimism/connectors/swap/events.sol create mode 100644 contracts/optimism/connectors/swap/helpers.sol create mode 100644 contracts/optimism/connectors/swap/main.sol diff --git a/contracts/arbitrum/common/interfaces.sol b/contracts/arbitrum/common/interfaces.sol index f843d5fb..5fbd1b6d 100644 --- a/contracts/arbitrum/common/interfaces.sol +++ b/contracts/arbitrum/common/interfaces.sol @@ -1,5 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; +pragma abicoder v2; interface TokenInterface { function approve(address, uint256) external; @@ -26,3 +27,7 @@ interface AccountInterface { function disable(address) external; function isAuth(address) external view returns (bool); } + +interface InstaConnectors { + function isConnectors(string[] calldata) external returns (bool, address[] memory); +} diff --git a/contracts/arbitrum/connectors/swap/events.sol b/contracts/arbitrum/connectors/swap/events.sol new file mode 100644 index 00000000..45e71522 --- /dev/null +++ b/contracts/arbitrum/connectors/swap/events.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +contract Events { + event LogSwapAggregator( + string[] connectors, + string connectorName, + string eventName, + bytes eventParam + ); +} diff --git a/contracts/arbitrum/connectors/swap/helpers.sol b/contracts/arbitrum/connectors/swap/helpers.sol new file mode 100644 index 00000000..e35584c2 --- /dev/null +++ b/contracts/arbitrum/connectors/swap/helpers.sol @@ -0,0 +1,44 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +import { InstaConnectors } from "../../common/interfaces.sol"; + +contract SwapHelpers { + /** + * @dev Instadapp Connectors Registry + */ + InstaConnectors internal constant instaConnectors = + InstaConnectors(0x67fCE99Dd6d8d659eea2a1ac1b8881c57eb6592B); + + /** + *@dev Swap using the dex aggregators. + *@param _connectors name of the connectors in preference order. + *@param _datas data for the swap cast. + */ + function _swap(string[] memory _connectors, bytes[] memory _datas) + internal + returns ( + bool success, + bytes memory returnData, + string memory connector + ) + { + uint256 _length = _connectors.length; + require(_length > 0, "zero-length-not-allowed"); + require(_datas.length == _length, "calldata-length-invalid"); + + (bool isOk, address[] memory connectors) = instaConnectors.isConnectors( + _connectors + ); + require(isOk, "connector-names-invalid"); + + for (uint256 i = 0; i < _length; i++) { + (success, returnData) = connectors[i].delegatecall(_datas[i]); + if (success) { + connector = _connectors[i]; + break; + } + } + } +} diff --git a/contracts/arbitrum/connectors/swap/main.sol b/contracts/arbitrum/connectors/swap/main.sol new file mode 100644 index 00000000..cf6014c9 --- /dev/null +++ b/contracts/arbitrum/connectors/swap/main.sol @@ -0,0 +1,44 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Swap. + * @dev Swap integration for DEX Aggregators. + */ + +// import files +import { SwapHelpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract Swap is SwapHelpers, Events { + /** + * @dev Swap ETH/ERC20_Token using dex aggregators. + * @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain. + * @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority. + * @param _datas Encoded function call data including function selector encoded with parameters. + */ + function swap(string[] memory _connectors, bytes[] memory _datas) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + (bool success, bytes memory returnData, string memory connector) = _swap( + _connectors, + _datas + ); + + require(success, "swap-Aggregator-failed"); + (string memory eventName, bytes memory eventParam) = abi.decode( + returnData, + (string, bytes) + ); + + _eventName = "LogSwapAggregator(string[],string,string,bytes)"; + _eventParam = abi.encode(_connectors, connector, eventName, eventParam); + } +} + +contract ConnectV2SwapAggregatorArbitrum is Swap { + string public name = "Swap-Aggregator-v1"; +} diff --git a/contracts/fantom/common/interfaces.sol b/contracts/fantom/common/interfaces.sol index 93042689..cf1abfee 100644 --- a/contracts/fantom/common/interfaces.sol +++ b/contracts/fantom/common/interfaces.sol @@ -1,5 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; +pragma abicoder v2; interface TokenInterface { function approve(address, uint256) external; @@ -34,3 +35,9 @@ interface AccountInterface { function isAuth(address) external view returns (bool); } + +interface InstaConnectors { + function isConnectors(string[] calldata) + external + returns (bool, address[] memory); +} diff --git a/contracts/fantom/connectors/swap/events.sol b/contracts/fantom/connectors/swap/events.sol new file mode 100644 index 00000000..45e71522 --- /dev/null +++ b/contracts/fantom/connectors/swap/events.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +contract Events { + event LogSwapAggregator( + string[] connectors, + string connectorName, + string eventName, + bytes eventParam + ); +} diff --git a/contracts/fantom/connectors/swap/helpers.sol b/contracts/fantom/connectors/swap/helpers.sol new file mode 100644 index 00000000..c6c07ad3 --- /dev/null +++ b/contracts/fantom/connectors/swap/helpers.sol @@ -0,0 +1,44 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +import { InstaConnectors } from "../../common/interfaces.sol"; + +contract SwapHelpers { + /** + * @dev Instadapp Connectors Registry + */ + InstaConnectors internal constant instaConnectors = + InstaConnectors(0x819910794a030403F69247E1e5C0bBfF1593B968); + + /** + *@dev Swap using the dex aggregators. + *@param _connectors name of the connectors in preference order. + *@param _datas data for the swap cast. + */ + function _swap(string[] memory _connectors, bytes[] memory _datas) + internal + returns ( + bool success, + bytes memory returnData, + string memory connector + ) + { + uint256 _length = _connectors.length; + require(_length > 0, "zero-length-not-allowed"); + require(_datas.length == _length, "calldata-length-invalid"); + + (bool isOk, address[] memory connectors) = instaConnectors.isConnectors( + _connectors + ); + require(isOk, "connector-names-invalid"); + + for (uint256 i = 0; i < _length; i++) { + (success, returnData) = connectors[i].delegatecall(_datas[i]); + if (success) { + connector = _connectors[i]; + break; + } + } + } +} diff --git a/contracts/fantom/connectors/swap/main.sol b/contracts/fantom/connectors/swap/main.sol new file mode 100644 index 00000000..e8784945 --- /dev/null +++ b/contracts/fantom/connectors/swap/main.sol @@ -0,0 +1,44 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Swap. + * @dev Swap integration for DEX Aggregators. + */ + +// import files +import { SwapHelpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract Swap is SwapHelpers, Events { + /** + * @dev Swap ETH/ERC20_Token using dex aggregators. + * @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain. + * @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority. + * @param _datas Encoded function call data including function selector encoded with parameters. + */ + function swap(string[] memory _connectors, bytes[] memory _datas) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + (bool success, bytes memory returnData, string memory connector) = _swap( + _connectors, + _datas + ); + + require(success, "swap-Aggregator-failed"); + (string memory eventName, bytes memory eventParam) = abi.decode( + returnData, + (string, bytes) + ); + + _eventName = "LogSwapAggregator(string[],string,string,bytes)"; + _eventParam = abi.encode(_connectors, connector, eventName, eventParam); + } +} + +contract ConnectV2SwapAggregatorFantom is Swap { + string public name = "Swap-Aggregator-v1"; +} diff --git a/contracts/optimism/common/interfaces.sol b/contracts/optimism/common/interfaces.sol index 03211b06..797bce15 100644 --- a/contracts/optimism/common/interfaces.sol +++ b/contracts/optimism/common/interfaces.sol @@ -1,5 +1,6 @@ //SPDX-License-Identifier: MIT pragma solidity ^0.7.0; +pragma abicoder v2; interface TokenInterface { function approve(address, uint256) external; @@ -23,3 +24,7 @@ interface AccountInterface { function disable(address) external; function isAuth(address) external view returns (bool); } + +interface InstaConnectors { + function isConnectors(string[] calldata) external returns (bool, address[] memory); +} \ No newline at end of file diff --git a/contracts/optimism/connectors/swap/events.sol b/contracts/optimism/connectors/swap/events.sol new file mode 100644 index 00000000..45e71522 --- /dev/null +++ b/contracts/optimism/connectors/swap/events.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +contract Events { + event LogSwapAggregator( + string[] connectors, + string connectorName, + string eventName, + bytes eventParam + ); +} diff --git a/contracts/optimism/connectors/swap/helpers.sol b/contracts/optimism/connectors/swap/helpers.sol new file mode 100644 index 00000000..b9207a26 --- /dev/null +++ b/contracts/optimism/connectors/swap/helpers.sol @@ -0,0 +1,44 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +import { InstaConnectors } from "../../common/interfaces.sol"; + +contract SwapHelpers { + /** + * @dev Instadapp Connectors Registry + */ + InstaConnectors internal constant instaConnectors = + InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14); + + /** + *@dev Swap using the dex aggregators. + *@param _connectors name of the connectors in preference order. + *@param _datas data for the swap cast. + */ + function _swap(string[] memory _connectors, bytes[] memory _datas) + internal + returns ( + bool success, + bytes memory returnData, + string memory connector + ) + { + uint256 _length = _connectors.length; + require(_length > 0, "zero-length-not-allowed"); + require(_datas.length == _length, "calldata-length-invalid"); + + (bool isOk, address[] memory connectors) = instaConnectors.isConnectors( + _connectors + ); + require(isOk, "connector-names-invalid"); + + for (uint256 i = 0; i < _length; i++) { + (success, returnData) = connectors[i].delegatecall(_datas[i]); + if (success) { + connector = _connectors[i]; + break; + } + } + } +} diff --git a/contracts/optimism/connectors/swap/main.sol b/contracts/optimism/connectors/swap/main.sol new file mode 100644 index 00000000..008fa4b9 --- /dev/null +++ b/contracts/optimism/connectors/swap/main.sol @@ -0,0 +1,44 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Swap. + * @dev Swap integration for DEX Aggregators. + */ + +// import files +import { SwapHelpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract Swap is SwapHelpers, Events { + /** + * @dev Swap ETH/ERC20_Token using dex aggregators. + * @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain. + * @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority. + * @param _datas Encoded function call data including function selector encoded with parameters. + */ + function swap(string[] memory _connectors, bytes[] memory _datas) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + (bool success, bytes memory returnData, string memory connector) = _swap( + _connectors, + _datas + ); + + require(success, "swap-Aggregator-failed"); + (string memory eventName, bytes memory eventParam) = abi.decode( + returnData, + (string, bytes) + ); + + _eventName = "LogSwapAggregator(string[],string,string,bytes)"; + _eventParam = abi.encode(_connectors, connector, eventName, eventParam); + } +} + +contract ConnectV2SwapAggregatorOptimism is Swap { + string public name = "Swap-Aggregator-v1"; +}