diff --git a/contracts/arbitrum/connectors/1inch/events.sol b/contracts/arbitrum/connectors/1inch/1inch-v3/events.sol similarity index 100% rename from contracts/arbitrum/connectors/1inch/events.sol rename to contracts/arbitrum/connectors/1inch/1inch-v3/events.sol diff --git a/contracts/mainnet/connectors/1inch/helpers.sol b/contracts/arbitrum/connectors/1inch/1inch-v3/helpers.sol similarity index 71% rename from contracts/mainnet/connectors/1inch/helpers.sol rename to contracts/arbitrum/connectors/1inch/1inch-v3/helpers.sol index 41cc4b0d..f52848e6 100644 --- a/contracts/mainnet/connectors/1inch/helpers.sol +++ b/contracts/arbitrum/connectors/1inch/1inch-v3/helpers.sol @@ -1,8 +1,8 @@ pragma solidity ^0.7.0; -import { TokenInterface } from "../../common/interfaces.sol"; -import { DSMath } from "../../common/math.sol"; -import { Basic } from "../../common/basic.sol"; +import { TokenInterface } from "../../../common/interfaces.sol"; +import { DSMath } from "../../../common/math.sol"; +import { Basic } from "../../../common/basic.sol"; abstract contract Helpers is DSMath, Basic { diff --git a/contracts/polygon/connectors/1inch/interface.sol b/contracts/arbitrum/connectors/1inch/1inch-v3/interface.sol similarity index 90% rename from contracts/polygon/connectors/1inch/interface.sol rename to contracts/arbitrum/connectors/1inch/1inch-v3/interface.sol index f35b9277..d2d790b7 100644 --- a/contracts/polygon/connectors/1inch/interface.sol +++ b/contracts/arbitrum/connectors/1inch/1inch-v3/interface.sol @@ -1,6 +1,6 @@ pragma solidity ^0.7.0; -import { TokenInterface } from "../../common/interfaces.sol"; +import { TokenInterface } from "../../../common/interfaces.sol"; interface OneInchInterace { function swap( diff --git a/contracts/arbitrum/connectors/1inch/main.sol b/contracts/arbitrum/connectors/1inch/1inch-v3/main.sol similarity index 95% rename from contracts/arbitrum/connectors/1inch/main.sol rename to contracts/arbitrum/connectors/1inch/1inch-v3/main.sol index 7adef93c..4db92ebe 100644 --- a/contracts/arbitrum/connectors/1inch/main.sol +++ b/contracts/arbitrum/connectors/1inch/1inch-v3/main.sol @@ -2,13 +2,13 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; /** - * @title 1Inch. + * @title 1InchV3. * @dev On-chain DEX Aggregator. */ // import files from common directory -import { TokenInterface , MemoryInterface } from "../../common/interfaces.sol"; -import { Stores } from "../../common/stores.sol"; +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"; @@ -123,6 +123,6 @@ abstract contract OneInch is OneInchResolverHelpers { } } -contract ConnectV2OneInchArbitrum is OneInch { +contract ConnectV2OneInchV3Arbitrum is OneInch { string public name = "1Inch-v1.0"; } diff --git a/contracts/mainnet/connectors/1inch/events.sol b/contracts/arbitrum/connectors/1inch/1inch-v4/events.sol similarity index 100% rename from contracts/mainnet/connectors/1inch/events.sol rename to contracts/arbitrum/connectors/1inch/1inch-v4/events.sol diff --git a/contracts/arbitrum/connectors/1inch/1inch-v4/helpers.sol b/contracts/arbitrum/connectors/1inch/1inch-v4/helpers.sol new file mode 100644 index 00000000..0a8f1fda --- /dev/null +++ b/contracts/arbitrum/connectors/1inch/1inch-v4/helpers.sol @@ -0,0 +1,13 @@ +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; +} \ No newline at end of file diff --git a/contracts/arbitrum/connectors/1inch/interface.sol b/contracts/arbitrum/connectors/1inch/1inch-v4/interface.sol similarity index 90% rename from contracts/arbitrum/connectors/1inch/interface.sol rename to contracts/arbitrum/connectors/1inch/1inch-v4/interface.sol index f35b9277..d2d790b7 100644 --- a/contracts/arbitrum/connectors/1inch/interface.sol +++ b/contracts/arbitrum/connectors/1inch/1inch-v4/interface.sol @@ -1,6 +1,6 @@ pragma solidity ^0.7.0; -import { TokenInterface } from "../../common/interfaces.sol"; +import { TokenInterface } from "../../../common/interfaces.sol"; interface OneInchInterace { function swap( diff --git a/contracts/arbitrum/connectors/1inch/1inch-v4/main.sol b/contracts/arbitrum/connectors/1inch/1inch-v4/main.sol new file mode 100644 index 00000000..65e733a8 --- /dev/null +++ b/contracts/arbitrum/connectors/1inch/1inch-v4/main.sol @@ -0,0 +1,112 @@ +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 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 ConnectV2OneInchV4Arbitrum is OneInch { + string public name = "1Inch-v4"; +} diff --git a/contracts/polygon/connectors/1inch/events.sol b/contracts/mainnet/connectors/1inch/1inch-v3/events.sol similarity index 100% rename from contracts/polygon/connectors/1inch/events.sol rename to contracts/mainnet/connectors/1inch/1inch-v3/events.sol diff --git a/contracts/arbitrum/connectors/1inch/helpers.sol b/contracts/mainnet/connectors/1inch/1inch-v3/helpers.sol similarity index 71% rename from contracts/arbitrum/connectors/1inch/helpers.sol rename to contracts/mainnet/connectors/1inch/1inch-v3/helpers.sol index 41cc4b0d..f52848e6 100644 --- a/contracts/arbitrum/connectors/1inch/helpers.sol +++ b/contracts/mainnet/connectors/1inch/1inch-v3/helpers.sol @@ -1,8 +1,8 @@ pragma solidity ^0.7.0; -import { TokenInterface } from "../../common/interfaces.sol"; -import { DSMath } from "../../common/math.sol"; -import { Basic } from "../../common/basic.sol"; +import { TokenInterface } from "../../../common/interfaces.sol"; +import { DSMath } from "../../../common/math.sol"; +import { Basic } from "../../../common/basic.sol"; abstract contract Helpers is DSMath, Basic { diff --git a/contracts/mainnet/connectors/1inch/interface.sol b/contracts/mainnet/connectors/1inch/1inch-v3/interface.sol similarity index 90% rename from contracts/mainnet/connectors/1inch/interface.sol rename to contracts/mainnet/connectors/1inch/1inch-v3/interface.sol index f35b9277..d2d790b7 100644 --- a/contracts/mainnet/connectors/1inch/interface.sol +++ b/contracts/mainnet/connectors/1inch/1inch-v3/interface.sol @@ -1,6 +1,6 @@ pragma solidity ^0.7.0; -import { TokenInterface } from "../../common/interfaces.sol"; +import { TokenInterface } from "../../../common/interfaces.sol"; interface OneInchInterace { function swap( diff --git a/contracts/mainnet/connectors/1inch/main.sol b/contracts/mainnet/connectors/1inch/1inch-v3/main.sol similarity index 95% rename from contracts/mainnet/connectors/1inch/main.sol rename to contracts/mainnet/connectors/1inch/1inch-v3/main.sol index 7f1622b6..c4a95203 100644 --- a/contracts/mainnet/connectors/1inch/main.sol +++ b/contracts/mainnet/connectors/1inch/1inch-v3/main.sol @@ -2,13 +2,13 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; /** - * @title 1Inch. + * @title 1InchV3. * @dev On-chain DEX Aggregator. */ // import files from common directory -import { TokenInterface , MemoryInterface } from "../../common/interfaces.sol"; -import { Stores } from "../../common/stores.sol"; +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"; @@ -123,6 +123,6 @@ abstract contract OneInch is OneInchResolverHelpers { } } -contract ConnectV2OneInch is OneInch { +contract ConnectV2OneInchV3 is OneInch { string public name = "1Inch-v1.2"; } diff --git a/contracts/mainnet/connectors/1inch/1inch-v4/events.sol b/contracts/mainnet/connectors/1inch/1inch-v4/events.sol new file mode 100644 index 00000000..bec3b27a --- /dev/null +++ b/contracts/mainnet/connectors/1inch/1inch-v4/events.sol @@ -0,0 +1,12 @@ +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-v4/helpers.sol b/contracts/mainnet/connectors/1inch/1inch-v4/helpers.sol new file mode 100644 index 00000000..b569d2d6 --- /dev/null +++ b/contracts/mainnet/connectors/1inch/1inch-v4/helpers.sol @@ -0,0 +1,13 @@ +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; +} \ No newline at end of file diff --git a/contracts/mainnet/connectors/1inch/1inch-v4/interface.sol b/contracts/mainnet/connectors/1inch/1inch-v4/interface.sol new file mode 100644 index 00000000..d2d790b7 --- /dev/null +++ b/contracts/mainnet/connectors/1inch/1inch-v4/interface.sol @@ -0,0 +1,30 @@ +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-v4/main.sol b/contracts/mainnet/connectors/1inch/1inch-v4/main.sol new file mode 100644 index 00000000..a7b1d929 --- /dev/null +++ b/contracts/mainnet/connectors/1inch/1inch-v4/main.sol @@ -0,0 +1,111 @@ +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 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 ConnectV2OneInchV4 is OneInch { + string public name = "1Inch-v4"; +} diff --git a/contracts/polygon/connectors/1inch/1inch-v3/events.sol b/contracts/polygon/connectors/1inch/1inch-v3/events.sol new file mode 100644 index 00000000..bec3b27a --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v3/events.sol @@ -0,0 +1,12 @@ +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/helpers.sol b/contracts/polygon/connectors/1inch/1inch-v3/helpers.sol similarity index 71% rename from contracts/polygon/connectors/1inch/helpers.sol rename to contracts/polygon/connectors/1inch/1inch-v3/helpers.sol index 41cc4b0d..f52848e6 100644 --- a/contracts/polygon/connectors/1inch/helpers.sol +++ b/contracts/polygon/connectors/1inch/1inch-v3/helpers.sol @@ -1,8 +1,8 @@ pragma solidity ^0.7.0; -import { TokenInterface } from "../../common/interfaces.sol"; -import { DSMath } from "../../common/math.sol"; -import { Basic } from "../../common/basic.sol"; +import { TokenInterface } from "../../../common/interfaces.sol"; +import { DSMath } from "../../../common/math.sol"; +import { Basic } from "../../../common/basic.sol"; abstract contract Helpers is DSMath, Basic { diff --git a/contracts/polygon/connectors/1inch/1inch-v3/interface.sol b/contracts/polygon/connectors/1inch/1inch-v3/interface.sol new file mode 100644 index 00000000..d2d790b7 --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v3/interface.sol @@ -0,0 +1,30 @@ +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/main.sol b/contracts/polygon/connectors/1inch/1inch-v3/main.sol similarity index 95% rename from contracts/polygon/connectors/1inch/main.sol rename to contracts/polygon/connectors/1inch/1inch-v3/main.sol index 38a720c0..086bb191 100644 --- a/contracts/polygon/connectors/1inch/main.sol +++ b/contracts/polygon/connectors/1inch/1inch-v3/main.sol @@ -2,13 +2,13 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; /** - * @title 1Inch. + * @title 1InchV3. * @dev On-chain DEX Aggregator. */ // import files from common directory -import { TokenInterface , MemoryInterface } from "../../common/interfaces.sol"; -import { Stores } from "../../common/stores.sol"; +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"; @@ -123,6 +123,6 @@ abstract contract OneInch is OneInchResolverHelpers { } } -contract ConnectV2OneInchPolygon is OneInch { +contract ConnectV2OneInchV3Polygon is OneInch { string public name = "1Inch-v1"; } diff --git a/contracts/polygon/connectors/1inch/1inch-v4/events.sol b/contracts/polygon/connectors/1inch/1inch-v4/events.sol new file mode 100644 index 00000000..bec3b27a --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v4/events.sol @@ -0,0 +1,12 @@ +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-v4/helpers.sol b/contracts/polygon/connectors/1inch/1inch-v4/helpers.sol new file mode 100644 index 00000000..b569d2d6 --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v4/helpers.sol @@ -0,0 +1,13 @@ +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; +} \ No newline at end of file diff --git a/contracts/polygon/connectors/1inch/1inch-v4/interface.sol b/contracts/polygon/connectors/1inch/1inch-v4/interface.sol new file mode 100644 index 00000000..d2d790b7 --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v4/interface.sol @@ -0,0 +1,30 @@ +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-v4/main.sol b/contracts/polygon/connectors/1inch/1inch-v4/main.sol new file mode 100644 index 00000000..40db7077 --- /dev/null +++ b/contracts/polygon/connectors/1inch/1inch-v4/main.sol @@ -0,0 +1,115 @@ +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 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 ConnectV2OneInchV4Polygon is OneInch { + string public name = "1Inch-v4-v1"; +} diff --git a/hardhat.config.ts b/hardhat.config.ts index a8e617e1..8c10d28f 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -50,7 +50,6 @@ function createConfig(network: string) { return { url: getNetworkUrl(network), accounts: !!PRIVATE_KEY ? [`0x${PRIVATE_KEY}`] : { mnemonic }, - gasPrice: utils.parseUnits(networkGasPriceConfig[network], "gwei").toNumber(), }; } @@ -118,8 +117,8 @@ const config: HardhatUserConfig = { sources: "./contracts", tests: "./test", }, - etherscan: { - apiKey: getScanApiKey(String(process.env.networkType)), + etherscan: { + apiKey: getScanApiKey(getNetworkUrl(String(process.env.networkType))), }, typechain: { outDir: "typechain", diff --git a/scripts/deployment/deploy.ts b/scripts/deployment/deploy.ts index c32788fd..4bcbd756 100644 --- a/scripts/deployment/deploy.ts +++ b/scripts/deployment/deploy.ts @@ -5,7 +5,7 @@ async function main() { const accounts = await ethers.getSigners(); const connectMapping: Record<string, string> = { - "1INCH-A": "ConnectV2OneInch", + "1INCH-A": "ConnectV2OneInch", "1INCH-B": "ConnectV2OneProto", "AAVE-V1-A": "ConnectV2AaveV1", "AAVE-V2-A": "ConnectV2AaveV2", @@ -18,9 +18,10 @@ async function main() { "GELATO-A": "ConnectV2Gelato", "MAKERDAO-A": "ConnectV2Maker", "UNISWAP-A": "ConnectV2UniswapV2", - "QUICKSWAP-A": "ConnectV2QuickswapPolygon" + "QUICKSWAP-A": "ConnectV2QuickswapPolygon", "UniswapV3-v1" : "ConnectV2UniswapV3Polygon", - "Uniswap-V3-Staker-v1.1" : "ConnectV2UniswapV3StakerPolygon" + "Uniswap-V3-Staker-v1.1" : "ConnectV2UniswapV3StakerPolygon", + "1INCH-V4" : "ConnectV2OneInchV4Polygon", }; const addressMapping: Record<string, string> = {};