diff --git a/contracts/arbitrum/connectors/1inch/1inch-v6/events.sol b/contracts/arbitrum/connectors/1inch/1inch-v6/events.sol new file mode 100644 index 00000000..14cd1873 --- /dev/null +++ b/contracts/arbitrum/connectors/1inch/1inch-v6/events.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/arbitrum/connectors/1inch/1inch-v6/helpers.sol b/contracts/arbitrum/connectors/1inch/1inch-v6/helpers.sol new file mode 100644 index 00000000..db64434c --- /dev/null +++ b/contracts/arbitrum/connectors/1inch/1inch-v6/helpers.sol @@ -0,0 +1,14 @@ +//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"; + + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 1Inch Router v6 Address + */ + address internal constant oneInchAddr = 0x111111125421cA6dc452d289314280a0f8842A65; +} \ No newline at end of file diff --git a/contracts/arbitrum/connectors/1inch/1inch-v6/interface.sol b/contracts/arbitrum/connectors/1inch/1inch-v6/interface.sol new file mode 100644 index 00000000..f63b509b --- /dev/null +++ b/contracts/arbitrum/connectors/1inch/1inch-v6/interface.sol @@ -0,0 +1,31 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../../common/interfaces.sol"; + +interface OneInchInterace { + function swap( + TokenInterface fromToken, + TokenInterface toToken, + uint256 fromTokenAmount, + uint256 minReturnAmount, + uint256 guaranteedAmount, + address payable referrer, + address[] calldata callAddresses, + bytes calldata callDataConcat, + uint256[] calldata starts, + uint256[] calldata gasLimitsAndValues + ) + external + payable + returns (uint256 returnAmount); +} + +struct OneInchData { + TokenInterface sellToken; + TokenInterface buyToken; + uint _sellAmt; + uint _buyAmt; + uint unitAmt; + bytes callData; +} \ No newline at end of file diff --git a/contracts/arbitrum/connectors/1inch/1inch-v6/main.sol b/contracts/arbitrum/connectors/1inch/1inch-v6/main.sol new file mode 100644 index 00000000..0e6ea124 --- /dev/null +++ b/contracts/arbitrum/connectors/1inch/1inch-v6/main.sol @@ -0,0 +1,113 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title 1InchV6. + * @dev On-chain DEX Aggregator. + */ + +// import files from common directory +import { TokenInterface , MemoryInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { OneInchInterace, OneInchData } from "./interface.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract OneInchResolver is Helpers, Events { + + + /** + * @dev 1inch API swap handler + * @param oneInchData - contains data returned from 1inch API. Struct defined in interfaces.sol + * @param ethAmt - Eth to swap for .value() + */ + function oneInchSwap( + OneInchData memory oneInchData, + uint ethAmt + ) internal returns (uint buyAmt) { + TokenInterface buyToken = oneInchData.buyToken; + (uint _buyDec, uint _sellDec) = getTokensDec(buyToken, oneInchData.sellToken); + uint _sellAmt18 = convertTo18(_sellDec, oneInchData._sellAmt); + uint _slippageAmt = convert18ToDec(_buyDec, wmul(oneInchData.unitAmt, _sellAmt18)); + + uint initalBal = getTokenBal(buyToken); + + // solium-disable-next-line security/no-call-value + (bool success, ) = oneInchAddr.call{value: ethAmt}(oneInchData.callData); + if (!success) revert("1Inch-swap-failed"); + + uint finalBal = getTokenBal(buyToken); + + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } + +} + +abstract contract OneInchResolverHelpers is OneInchResolver { + + /** + * @dev Gets the swapping data from 1inch's API. + * @param oneInchData Struct with multiple swap data defined in interfaces.sol + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function _sell( + OneInchData memory oneInchData, + uint setId + ) internal returns (OneInchData memory) { + TokenInterface _sellAddr = oneInchData.sellToken; + + uint ethAmt; + if (address(_sellAddr) == ethAddr) { + ethAmt = oneInchData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), oneInchAddr, oneInchData._sellAmt); + } + + oneInchData._buyAmt = oneInchSwap(oneInchData, ethAmt); + setUint(setId, oneInchData._buyAmt); + + return oneInchData; + } +} + +abstract contract OneInch is OneInchResolverHelpers { + /** + * @dev Sell ETH/ERC20_Token using 1Inch. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from 1inch API. + * @param setId ID stores the amount of token brought. + */ + function sell( + address buyAddr, + address sellAddr, + uint sellAmt, + uint unitAmt, + bytes calldata callData, + uint setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + OneInchData memory oneInchData = OneInchData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + oneInchData = _sell(oneInchData, setId); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(buyAddr, sellAddr, oneInchData._buyAmt, oneInchData._sellAmt, 0, setId); + } +} + +contract ConnectV2OneInchV6Arbitrum is OneInch { + string public name = "1Inch-v6"; +} diff --git a/contracts/avalanche/connectors/1inch-v6/events.sol b/contracts/avalanche/connectors/1inch-v6/events.sol new file mode 100644 index 00000000..14cd1873 --- /dev/null +++ b/contracts/avalanche/connectors/1inch-v6/events.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/1inch-v6/helpers.sol b/contracts/avalanche/connectors/1inch-v6/helpers.sol new file mode 100644 index 00000000..3fb5bef4 --- /dev/null +++ b/contracts/avalanche/connectors/1inch-v6/helpers.sol @@ -0,0 +1,14 @@ +//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"; + + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 1Inch Router v6 Address + */ + address internal constant oneInchAddr = 0x111111125421cA6dc452d289314280a0f8842A65; +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/1inch-v6/interface.sol b/contracts/avalanche/connectors/1inch-v6/interface.sol new file mode 100644 index 00000000..2575866e --- /dev/null +++ b/contracts/avalanche/connectors/1inch-v6/interface.sol @@ -0,0 +1,31 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + +interface OneInchInterace { + function swap( + TokenInterface fromToken, + TokenInterface toToken, + uint256 fromTokenAmount, + uint256 minReturnAmount, + uint256 guaranteedAmount, + address payable referrer, + address[] calldata callAddresses, + bytes calldata callDataConcat, + uint256[] calldata starts, + uint256[] calldata gasLimitsAndValues + ) + external + payable + returns (uint256 returnAmount); +} + +struct OneInchData { + TokenInterface sellToken; + TokenInterface buyToken; + uint _sellAmt; + uint _buyAmt; + uint unitAmt; + bytes callData; +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/1inch-v6/main.sol b/contracts/avalanche/connectors/1inch-v6/main.sol new file mode 100644 index 00000000..410fa85a --- /dev/null +++ b/contracts/avalanche/connectors/1inch-v6/main.sol @@ -0,0 +1,112 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title 1InchV6. + * @dev On-chain DEX Aggregator. + */ + +// import files from common directory +import { TokenInterface , MemoryInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { OneInchInterace, OneInchData } from "./interface.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract OneInchResolver is Helpers, Events { + /** + * @dev 1inch API swap handler + * @param oneInchData - contains data returned from 1inch API. Struct defined in interfaces.sol + * @param avaxAmt - Avax to swap for .value() + */ + function oneInchSwap( + OneInchData memory oneInchData, + uint avaxAmt + ) internal returns (uint buyAmt) { + TokenInterface buyToken = oneInchData.buyToken; + (uint _buyDec, uint _sellDec) = getTokensDec(buyToken, oneInchData.sellToken); + uint _sellAmt18 = convertTo18(_sellDec, oneInchData._sellAmt); + uint _slippageAmt = convert18ToDec(_buyDec, wmul(oneInchData.unitAmt, _sellAmt18)); + + uint initalBal = getTokenBal(buyToken); + + // solium-disable-next-line security/no-call-value + (bool success, ) = oneInchAddr.call{value: avaxAmt}(oneInchData.callData); + if (!success) revert("1Inch-swap-failed"); + + uint finalBal = getTokenBal(buyToken); + + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } + +} + +abstract contract OneInchResolverHelpers is OneInchResolver { + + /** + * @dev Gets the swapping data from 1inch's API. + * @param oneInchData Struct with multiple swap data defined in interfaces.sol + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function _sell( + OneInchData memory oneInchData, + uint setId + ) internal returns (OneInchData memory) { + TokenInterface _sellAddr = oneInchData.sellToken; + + uint avaxAmt; + if (address(_sellAddr) == avaxAddr) { + avaxAmt = oneInchData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), oneInchAddr, oneInchData._sellAmt); + } + + oneInchData._buyAmt = oneInchSwap(oneInchData, avaxAmt); + setUint(setId, oneInchData._buyAmt); + + return oneInchData; + + } +} + +abstract contract OneInch is OneInchResolverHelpers { + /** + * @dev Sell Avax/ERC20_Token using 1Inch. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For Avax: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from 1inch API. + * @param setId ID stores the amount of token brought. + */ + function sell( + address buyAddr, + address sellAddr, + uint sellAmt, + uint unitAmt, + bytes calldata callData, + uint setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + OneInchData memory oneInchData = OneInchData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + oneInchData = _sell(oneInchData, setId); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(buyAddr, sellAddr, oneInchData._buyAmt, oneInchData._sellAmt, 0, setId); + } +} + +contract ConnectV2OneInchV6Avalanche is OneInch { + string public name = "1Inch-v6"; +} diff --git a/contracts/fantom/connectors/1inch-v6/events.sol b/contracts/fantom/connectors/1inch-v6/events.sol new file mode 100644 index 00000000..14cd1873 --- /dev/null +++ b/contracts/fantom/connectors/1inch-v6/events.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/fantom/connectors/1inch-v6/helpers.sol b/contracts/fantom/connectors/1inch-v6/helpers.sol new file mode 100644 index 00000000..4a23a925 --- /dev/null +++ b/contracts/fantom/connectors/1inch-v6/helpers.sol @@ -0,0 +1,14 @@ +//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"; + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 1Inch Router v6 Address + */ + address internal constant oneInchAddr = + 0x111111125421cA6dc452d289314280a0f8842A65; +} diff --git a/contracts/fantom/connectors/1inch-v6/interface.sol b/contracts/fantom/connectors/1inch-v6/interface.sol new file mode 100644 index 00000000..be10db0e --- /dev/null +++ b/contracts/fantom/connectors/1inch-v6/interface.sol @@ -0,0 +1,28 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../common/interfaces.sol"; + +interface OneInchInterace { + function swap( + TokenInterface fromToken, + TokenInterface toToken, + uint256 fromTokenAmount, + uint256 minReturnAmount, + uint256 guaranteedAmount, + address payable referrer, + address[] calldata callAddresses, + bytes calldata callDataConcat, + uint256[] calldata starts, + uint256[] calldata gasLimitsAndValues + ) external payable returns (uint256 returnAmount); +} + +struct OneInchData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; +} diff --git a/contracts/fantom/connectors/1inch-v6/main.sol b/contracts/fantom/connectors/1inch-v6/main.sol new file mode 100644 index 00000000..2be6fe65 --- /dev/null +++ b/contracts/fantom/connectors/1inch-v6/main.sol @@ -0,0 +1,132 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title 1InchV6. + * @dev On-chain DEX Aggregator. + */ + +// import files from common directory +import { TokenInterface, MemoryInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { OneInchInterace, OneInchData } from "./interface.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract OneInchResolver is Helpers, Events { + /** + * @dev 1inch API swap handler + * @param oneInchData - contains data returned from 1inch API. Struct defined in interfaces.sol + * @param ftmAmt - Ftm to swap for .value() + */ + function oneInchSwap(OneInchData memory oneInchData, uint256 ftmAmt) + internal + returns (uint256 buyAmt) + { + TokenInterface buyToken = oneInchData.buyToken; + (uint256 _buyDec, uint256 _sellDec) = getTokensDec( + buyToken, + oneInchData.sellToken + ); + uint256 _sellAmt18 = convertTo18(_sellDec, oneInchData._sellAmt); + uint256 _slippageAmt = convert18ToDec( + _buyDec, + wmul(oneInchData.unitAmt, _sellAmt18) + ); + + uint256 initalBal = getTokenBal(buyToken); + + // solium-disable-next-line security/no-call-value + (bool success, ) = oneInchAddr.call{ value: ftmAmt }( + oneInchData.callData + ); + if (!success) revert("1Inch-swap-failed"); + + uint256 finalBal = getTokenBal(buyToken); + + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } +} + +abstract contract OneInchResolverHelpers is OneInchResolver { + /** + * @dev Gets the swapping data from 1inch's API. + * @param oneInchData Struct with multiple swap data defined in interfaces.sol + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function _sell(OneInchData memory oneInchData, uint256 setId) + internal + returns (OneInchData memory) + { + TokenInterface _sellAddr = oneInchData.sellToken; + + uint256 ftmAmt; + if (address(_sellAddr) == ftmAddr) { + ftmAmt = oneInchData._sellAmt; + } else { + approve( + TokenInterface(_sellAddr), + oneInchAddr, + oneInchData._sellAmt + ); + } + + oneInchData._buyAmt = oneInchSwap(oneInchData, ftmAmt); + setUint(setId, oneInchData._buyAmt); + + return oneInchData; + } +} + +abstract contract OneInch is OneInchResolverHelpers { + /** + * @dev Sell FTM/ERC20_Token using 1Inch. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For FTM: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For FTM: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from 1inch API. + * @param setId ID stores the amount of token brought. + */ + function sell( + address buyAddr, + address sellAddr, + uint256 sellAmt, + uint256 unitAmt, + bytes calldata callData, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + OneInchData memory oneInchData = OneInchData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + oneInchData = _sell(oneInchData, setId); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode( + buyAddr, + sellAddr, + oneInchData._buyAmt, + oneInchData._sellAmt, + 0, + setId + ); + } +} + +contract ConnectV2OneInchV6Fantom is OneInch { + string public name = "1Inch-v6"; +} diff --git a/contracts/mainnet/connectors/1inch/1inch-v6/events.sol b/contracts/mainnet/connectors/1inch/1inch-v6/events.sol new file mode 100644 index 00000000..14cd1873 --- /dev/null +++ b/contracts/mainnet/connectors/1inch/1inch-v6/events.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/1inch/1inch-v6/helpers.sol b/contracts/mainnet/connectors/1inch/1inch-v6/helpers.sol new file mode 100644 index 00000000..b2025497 --- /dev/null +++ b/contracts/mainnet/connectors/1inch/1inch-v6/helpers.sol @@ -0,0 +1,14 @@ +//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"; + + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 1Inch Router v6 Address + */ + address internal constant oneInchAddr = 0x111111125421cA6dc452d289314280a0f8842A65; +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/1inch/1inch-v6/interface.sol b/contracts/mainnet/connectors/1inch/1inch-v6/interface.sol new file mode 100644 index 00000000..f63b509b --- /dev/null +++ b/contracts/mainnet/connectors/1inch/1inch-v6/interface.sol @@ -0,0 +1,31 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../../common/interfaces.sol"; + +interface OneInchInterace { + function swap( + TokenInterface fromToken, + TokenInterface toToken, + uint256 fromTokenAmount, + uint256 minReturnAmount, + uint256 guaranteedAmount, + address payable referrer, + address[] calldata callAddresses, + bytes calldata callDataConcat, + uint256[] calldata starts, + uint256[] calldata gasLimitsAndValues + ) + external + payable + returns (uint256 returnAmount); +} + +struct OneInchData { + TokenInterface sellToken; + TokenInterface buyToken; + uint _sellAmt; + uint _buyAmt; + uint unitAmt; + bytes callData; +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/1inch/1inch-v6/main.sol b/contracts/mainnet/connectors/1inch/1inch-v6/main.sol new file mode 100644 index 00000000..43f73148 --- /dev/null +++ b/contracts/mainnet/connectors/1inch/1inch-v6/main.sol @@ -0,0 +1,112 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title 1InchV6. + * @dev On-chain DEX Aggregator. + */ + +// import files from common directory +import { TokenInterface , MemoryInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { OneInchInterace, OneInchData } from "./interface.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract OneInchResolver is Helpers, Events { + /** + * @dev 1inch API swap handler + * @param oneInchData - contains data returned from 1inch API. Struct defined in interfaces.sol + * @param ethAmt - Eth to swap for .value() + */ + function oneInchSwap( + OneInchData memory oneInchData, + uint ethAmt + ) internal returns (uint buyAmt) { + TokenInterface buyToken = oneInchData.buyToken; + (uint _buyDec, uint _sellDec) = getTokensDec(buyToken, oneInchData.sellToken); + uint _sellAmt18 = convertTo18(_sellDec, oneInchData._sellAmt); + uint _slippageAmt = convert18ToDec(_buyDec, wmul(oneInchData.unitAmt, _sellAmt18)); + + uint initalBal = getTokenBal(buyToken); + + // solium-disable-next-line security/no-call-value + (bool success, ) = oneInchAddr.call{value: ethAmt}(oneInchData.callData); + if (!success) revert("1Inch-swap-failed"); + + uint finalBal = getTokenBal(buyToken); + + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } + +} + +abstract contract OneInchResolverHelpers is OneInchResolver { + + /** + * @dev Gets the swapping data from 1inch's API. + * @param oneInchData Struct with multiple swap data defined in interfaces.sol + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function _sell( + OneInchData memory oneInchData, + uint setId + ) internal returns (OneInchData memory) { + TokenInterface _sellAddr = oneInchData.sellToken; + + uint ethAmt; + if (address(_sellAddr) == ethAddr) { + ethAmt = oneInchData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), oneInchAddr, oneInchData._sellAmt); + } + + oneInchData._buyAmt = oneInchSwap(oneInchData, ethAmt); + setUint(setId, oneInchData._buyAmt); + + return oneInchData; + + } +} + +abstract contract OneInch is OneInchResolverHelpers { + /** + * @dev Sell ETH/ERC20_Token using 1Inch. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from 1inch API. + * @param setId ID stores the amount of token brought. + */ + function sell( + address buyAddr, + address sellAddr, + uint sellAmt, + uint unitAmt, + bytes calldata callData, + uint setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + OneInchData memory oneInchData = OneInchData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + oneInchData = _sell(oneInchData, setId); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(buyAddr, sellAddr, oneInchData._buyAmt, oneInchData._sellAmt, 0, setId); + } +} + +contract ConnectV2OneInchV6 is OneInch { + string public name = "1Inch-v6"; +} diff --git a/contracts/optimism/connectors/1inch/1inch-v6/events.sol b/contracts/optimism/connectors/1inch/1inch-v6/events.sol new file mode 100644 index 00000000..14cd1873 --- /dev/null +++ b/contracts/optimism/connectors/1inch/1inch-v6/events.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/optimism/connectors/1inch/1inch-v6/helpers.sol b/contracts/optimism/connectors/1inch/1inch-v6/helpers.sol new file mode 100644 index 00000000..b2025497 --- /dev/null +++ b/contracts/optimism/connectors/1inch/1inch-v6/helpers.sol @@ -0,0 +1,14 @@ +//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"; + + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 1Inch Router v6 Address + */ + address internal constant oneInchAddr = 0x111111125421cA6dc452d289314280a0f8842A65; +} \ No newline at end of file diff --git a/contracts/optimism/connectors/1inch/1inch-v6/interface.sol b/contracts/optimism/connectors/1inch/1inch-v6/interface.sol new file mode 100644 index 00000000..f63b509b --- /dev/null +++ b/contracts/optimism/connectors/1inch/1inch-v6/interface.sol @@ -0,0 +1,31 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../../common/interfaces.sol"; + +interface OneInchInterace { + function swap( + TokenInterface fromToken, + TokenInterface toToken, + uint256 fromTokenAmount, + uint256 minReturnAmount, + uint256 guaranteedAmount, + address payable referrer, + address[] calldata callAddresses, + bytes calldata callDataConcat, + uint256[] calldata starts, + uint256[] calldata gasLimitsAndValues + ) + external + payable + returns (uint256 returnAmount); +} + +struct OneInchData { + TokenInterface sellToken; + TokenInterface buyToken; + uint _sellAmt; + uint _buyAmt; + uint unitAmt; + bytes callData; +} \ No newline at end of file diff --git a/contracts/optimism/connectors/1inch/1inch-v6/main.sol b/contracts/optimism/connectors/1inch/1inch-v6/main.sol new file mode 100644 index 00000000..a118ec6d --- /dev/null +++ b/contracts/optimism/connectors/1inch/1inch-v6/main.sol @@ -0,0 +1,112 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title 1InchV6. + * @dev On-chain DEX Aggregator. + */ + +// import files from common directory +import { TokenInterface , MemoryInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { OneInchInterace, OneInchData } from "./interface.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract OneInchResolver is Helpers, Events { + /** + * @dev 1inch API swap handler + * @param oneInchData - contains data returned from 1inch API. Struct defined in interfaces.sol + * @param ethAmt - Eth to swap for .value() + */ + function oneInchSwap( + OneInchData memory oneInchData, + uint ethAmt + ) internal returns (uint buyAmt) { + TokenInterface buyToken = oneInchData.buyToken; + (uint _buyDec, uint _sellDec) = getTokensDec(buyToken, oneInchData.sellToken); + uint _sellAmt18 = convertTo18(_sellDec, oneInchData._sellAmt); + uint _slippageAmt = convert18ToDec(_buyDec, wmul(oneInchData.unitAmt, _sellAmt18)); + + uint initalBal = getTokenBal(buyToken); + + // solium-disable-next-line security/no-call-value + (bool success, ) = oneInchAddr.call{value: ethAmt}(oneInchData.callData); + if (!success) revert("1Inch-swap-failed"); + + uint finalBal = getTokenBal(buyToken); + + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } + +} + +abstract contract OneInchResolverHelpers is OneInchResolver { + + /** + * @dev Gets the swapping data from 1inch's API. + * @param oneInchData Struct with multiple swap data defined in interfaces.sol + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function _sell( + OneInchData memory oneInchData, + uint setId + ) internal returns (OneInchData memory) { + TokenInterface _sellAddr = oneInchData.sellToken; + + uint ethAmt; + if (address(_sellAddr) == ethAddr) { + ethAmt = oneInchData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), oneInchAddr, oneInchData._sellAmt); + } + + oneInchData._buyAmt = oneInchSwap(oneInchData, ethAmt); + setUint(setId, oneInchData._buyAmt); + + return oneInchData; + + } +} + +abstract contract OneInch is OneInchResolverHelpers { + /** + * @dev Sell ETH/ERC20_Token using 1Inch. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from 1inch API. + * @param setId ID stores the amount of token brought. + */ + function sell( + address buyAddr, + address sellAddr, + uint sellAmt, + uint unitAmt, + bytes calldata callData, + uint setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + OneInchData memory oneInchData = OneInchData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + oneInchData = _sell(oneInchData, setId); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(buyAddr, sellAddr, oneInchData._buyAmt, oneInchData._sellAmt, 0, setId); + } +} + +contract ConnectV2OneInch6Optimism is OneInch { + string public name = "1Inch-v6"; +} diff --git a/contracts/polygon/connectors/1inch/1inch-v6/events.sol b/contracts/polygon/connectors/1inch/1inch-v6/events.sol new file mode 100644 index 00000000..14cd1873 --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v6/events.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/polygon/connectors/1inch/1inch-v6/helpers.sol b/contracts/polygon/connectors/1inch/1inch-v6/helpers.sol new file mode 100644 index 00000000..b2025497 --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v6/helpers.sol @@ -0,0 +1,14 @@ +//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"; + + +abstract contract Helpers is DSMath, Basic { + /** + * @dev 1Inch Router v6 Address + */ + address internal constant oneInchAddr = 0x111111125421cA6dc452d289314280a0f8842A65; +} \ No newline at end of file diff --git a/contracts/polygon/connectors/1inch/1inch-v6/interface.sol b/contracts/polygon/connectors/1inch/1inch-v6/interface.sol new file mode 100644 index 00000000..f63b509b --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v6/interface.sol @@ -0,0 +1,31 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +import { TokenInterface } from "../../../common/interfaces.sol"; + +interface OneInchInterace { + function swap( + TokenInterface fromToken, + TokenInterface toToken, + uint256 fromTokenAmount, + uint256 minReturnAmount, + uint256 guaranteedAmount, + address payable referrer, + address[] calldata callAddresses, + bytes calldata callDataConcat, + uint256[] calldata starts, + uint256[] calldata gasLimitsAndValues + ) + external + payable + returns (uint256 returnAmount); +} + +struct OneInchData { + TokenInterface sellToken; + TokenInterface buyToken; + uint _sellAmt; + uint _buyAmt; + uint unitAmt; + bytes callData; +} \ No newline at end of file diff --git a/contracts/polygon/connectors/1inch/1inch-v6/main.sol b/contracts/polygon/connectors/1inch/1inch-v6/main.sol new file mode 100644 index 00000000..8852fa7d --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v6/main.sol @@ -0,0 +1,116 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title 1InchV6. + * @dev On-chain DEX Aggregator. + */ + +// import files from common directory +import { TokenInterface , MemoryInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { OneInchInterace, OneInchData } from "./interface.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract OneInchResolver is Helpers, Events { + + + /** + * @dev 1inch API swap handler + * @param oneInchData - contains data returned from 1inch API. Struct defined in interfaces.sol + * @param ethAmt - Eth to swap for .value() + */ + function oneInchSwap( + OneInchData memory oneInchData, + uint ethAmt + ) internal returns (uint buyAmt) { + TokenInterface buyToken = oneInchData.buyToken; + (uint _buyDec, uint _sellDec) = getTokensDec(buyToken, oneInchData.sellToken); + uint _sellAmt18 = convertTo18(_sellDec, oneInchData._sellAmt); + uint _slippageAmt = convert18ToDec(_buyDec, wmul(oneInchData.unitAmt, _sellAmt18)); + + uint initalBal = getTokenBal(buyToken); + + // solium-disable-next-line security/no-call-value + (bool success, ) = oneInchAddr.call{value: ethAmt}(oneInchData.callData); + if (!success) revert("1Inch-swap-failed"); + + uint finalBal = getTokenBal(buyToken); + + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } + +} + +abstract contract OneInchResolverHelpers is OneInchResolver { + + /** + * @dev Gets the swapping data from 1inch's API. + * @param oneInchData Struct with multiple swap data defined in interfaces.sol + * @param setId Set token amount at this ID in `InstaMemory` Contract. + */ + function _sell( + OneInchData memory oneInchData, + uint setId + ) internal returns (OneInchData memory) { + TokenInterface _sellAddr = oneInchData.sellToken; + + uint ethAmt; + if (address(_sellAddr) == maticAddr) { + ethAmt = oneInchData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), oneInchAddr, oneInchData._sellAmt); + } + + + oneInchData._buyAmt = oneInchSwap(oneInchData, ethAmt); + setUint(setId, oneInchData._buyAmt); + + return oneInchData; + + + } +} + +abstract contract OneInch is OneInchResolverHelpers { + /** + * @dev Sell ETH/ERC20_Token using 1Inch. + * @notice Swap tokens from exchanges like kyber, 0x etc, with calculation done off-chain. + * @param buyAddr The address of the token to buy.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) + * @param sellAmt The amount of the token to sell. + * @param unitAmt The amount of buyAmt/sellAmt with slippage. + * @param callData Data from 1inch API. + * @param setId ID stores the amount of token brought. + */ + function sell( + address buyAddr, + address sellAddr, + uint sellAmt, + uint unitAmt, + bytes calldata callData, + uint setId + ) external payable returns (string memory _eventName, bytes memory _eventParam) { + OneInchData memory oneInchData = OneInchData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + oneInchData = _sell(oneInchData, setId); + + _eventName = "LogSell(address,address,uint256,uint256,uint256,uint256)"; + _eventParam = abi.encode(buyAddr, sellAddr, oneInchData._buyAmt, oneInchData._sellAmt, 0, setId); + } +} + +contract ConnectV2OneInchV6Polygon is OneInch { + string public name = "1Inch-v6"; +} diff --git a/package.json b/package.json index 3d426aa0..d8d62f2b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "dsa-connectors", "version": "1.0.0", - "description": "", + "description": " ", "directories": {}, "scripts": { "test": "hardhat run scripts/tests/global-test.ts",