From 72bbcfa8a3a95a7bfe623958c0554805f6ee3c53 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sat, 9 May 2020 06:10:44 +0530 Subject: [PATCH] Fixed stack too deep Error --- contracts/connectors/1inch.sol | 40 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/contracts/connectors/1inch.sol b/contracts/connectors/1inch.sol index 9cc64b5..a9f61d6 100644 --- a/contracts/connectors/1inch.sol +++ b/contracts/connectors/1inch.sol @@ -120,6 +120,29 @@ contract Resolver is OneHelpers { require(_slippageAmt <= buyAmt, "Too much slippage"); } + + function oneInchSwap( + TokenInterface _buyAddr, + TokenInterface _sellAddr, + bytes memory callData, + uint sellAmt, + uint unitAmt, + uint ethAmt + ) internal returns (uint buyAmt) { + (uint _buyDec, uint _sellDec) = getTokensDec(_buyAddr, _sellAddr); + uint _sellAmt18 = convertTo18(_sellDec, sellAmt); + uint _slippageAmt = convert18ToDec(_buyDec, wmul(unitAmt, _sellAmt18)); + uint initalBal = getTokenBal(_buyAddr); + + // solium-disable-next-line security/no-call-value + (bool success, ) = address(getOneInchAddress()).call.value(ethAmt)(callData); + if (!success) revert("1Inch-swap-failed"); + + uint finalBal = getTokenBal(_buyAddr); + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } } contract BasicResolver is Resolver { @@ -219,10 +242,6 @@ contract BasicResolver is Resolver { TokenInterface _buyAddr = TokenInterface(buyAddr); TokenInterface _sellAddr = TokenInterface(sellAddr); - (uint _buyDec, uint _sellDec) = getTokensDec(_buyAddr, _sellAddr); - uint _sellAmt18 = convertTo18(_sellDec, sellAmt); - uint _slippageAmt = convert18ToDec(_buyDec, wmul(unitAmt, _sellAmt18)); - uint ethAmt; if (address(_sellAddr) == getEthAddr()) { ethAmt = sellAmt; @@ -230,17 +249,8 @@ contract BasicResolver is Resolver { TokenInterface(_sellAddr).approve(getOneInchAddress(), sellAmt); } - uint initalBal = getTokenBal(_buyAddr); - - // solium-disable-next-line security/no-call-value - (bool success, ) = address(getOneInchAddress()).call.value(ethAmt)(callData); - if (!success) revert("1Inch-swap-failed"); - - uint finalBal = getTokenBal(_buyAddr); - uint buyAmt = sub(finalBal, initalBal); - - require(_slippageAmt <= buyAmt, "Too much slippage"); - + uint buyAmt = oneInchSwap(_buyAddr, _sellAddr, callData, sellAmt, unitAmt, sellAmt); + setUint(setId, buyAmt); emit LogSell(address(_buyAddr), address(_sellAddr), buyAmt, sellAmt, 0, setId); bytes32 _eventCode = keccak256("LogSell(address,address,uint256,uint256,uint256,uint256)");