From 9255d771404706f2bf950a253d05e7c6af39e240 Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Fri, 17 Jun 2022 12:13:33 +0530 Subject: [PATCH 1/2] added 1inch ftm --- contracts/fantom/connectors/1inch/events.sol | 13 ++ contracts/fantom/connectors/1inch/helpers.sol | 14 ++ .../fantom/connectors/1inch/interface.sol | 28 ++++ contracts/fantom/connectors/1inch/main.sol | 132 ++++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 contracts/fantom/connectors/1inch/events.sol create mode 100644 contracts/fantom/connectors/1inch/helpers.sol create mode 100644 contracts/fantom/connectors/1inch/interface.sol create mode 100644 contracts/fantom/connectors/1inch/main.sol diff --git a/contracts/fantom/connectors/1inch/events.sol b/contracts/fantom/connectors/1inch/events.sol new file mode 100644 index 00000000..14cd1873 --- /dev/null +++ b/contracts/fantom/connectors/1inch/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/helpers.sol b/contracts/fantom/connectors/1inch/helpers.sol new file mode 100644 index 00000000..316a247a --- /dev/null +++ b/contracts/fantom/connectors/1inch/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 Address + */ + address internal constant oneInchAddr = + 0x1111111254fb6c44bAC0beD2854e76F90643097d; +} diff --git a/contracts/fantom/connectors/1inch/interface.sol b/contracts/fantom/connectors/1inch/interface.sol new file mode 100644 index 00000000..be10db0e --- /dev/null +++ b/contracts/fantom/connectors/1inch/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/main.sol b/contracts/fantom/connectors/1inch/main.sol new file mode 100644 index 00000000..ef7ac3fb --- /dev/null +++ b/contracts/fantom/connectors/1inch/main.sol @@ -0,0 +1,132 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title 1InchV4. + * @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 - Eth 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 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 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 ConnectV2OneInchV4Fantom is OneInch { + string public name = "1Inch-v4"; +} From 630f1c3d87f5af6deb57490da4b02c4a9a2b6e4e Mon Sep 17 00:00:00 2001 From: pradyuman-verma Date: Fri, 17 Jun 2022 12:19:01 +0530 Subject: [PATCH 2/2] comment fix --- contracts/fantom/connectors/1inch/main.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/fantom/connectors/1inch/main.sol b/contracts/fantom/connectors/1inch/main.sol index ef7ac3fb..c2ca69c5 100644 --- a/contracts/fantom/connectors/1inch/main.sol +++ b/contracts/fantom/connectors/1inch/main.sol @@ -18,7 +18,7 @@ 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 - Eth to swap for .value() + * @param ftmAmt - Ftm to swap for .value() */ function oneInchSwap(OneInchData memory oneInchData, uint256 ftmAmt) internal @@ -83,7 +83,7 @@ abstract contract OneInchResolverHelpers is OneInchResolver { abstract contract OneInch is OneInchResolverHelpers { /** - * @dev Sell ETH/ERC20_Token using 1Inch. + * @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)