From 891559c34bea1be6b2dbe12152e85caf090cae8d Mon Sep 17 00:00:00 2001 From: Samarendra Gouda Date: Sat, 22 Jun 2024 20:45:05 +0530 Subject: [PATCH] feat: paraswap v6.2 --- .../mainnet/connectors/paraswap/helpers.sol | 68 +++++++++++++++++++ .../mainnet/connectors/paraswap/main.sol | 55 +++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 contracts/mainnet/connectors/paraswap/helpers.sol create mode 100644 contracts/mainnet/connectors/paraswap/main.sol diff --git a/contracts/mainnet/connectors/paraswap/helpers.sol b/contracts/mainnet/connectors/paraswap/helpers.sol new file mode 100644 index 0000000..39b29d7 --- /dev/null +++ b/contracts/mainnet/connectors/paraswap/helpers.sol @@ -0,0 +1,68 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +import {DSMath} from "../../common/math.sol"; +import {Basic} from "../../common/basic.sol"; +import {TokenInterface} from "../../common/interfaces.sol"; + +abstract contract Helpers is DSMath, Basic { + struct SwapData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; + } + + address internal constant AUGUSTUS_V6 = + 0x6A000F20005980200259B80c5102003040001068; + + function _swapHelper( + SwapData memory swapData, + uint256 wethAmt + ) internal returns (uint256 buyAmt) { + TokenInterface buyToken = swapData.buyToken; + (uint256 _buyDec, uint256 _sellDec) = getTokensDec( + buyToken, + swapData.sellToken + ); + uint256 _sellAmt18 = convertTo18(_sellDec, swapData._sellAmt); + uint256 _slippageAmt = convert18ToDec( + _buyDec, + wmul(swapData.unitAmt, _sellAmt18) + ); + + uint256 initalBal = getTokenBal(buyToken); + + (bool success, ) = AUGUSTUS_V6.call{value: wethAmt}(swapData.callData); + if (!success) revert("paraswap-failed"); + + uint256 finalBal = getTokenBal(buyToken); + + buyAmt = sub(finalBal, initalBal); + + require(_slippageAmt <= buyAmt, "Too much slippage"); + } + + function _swap( + SwapData memory swapData, + uint256 setId + ) internal returns (SwapData memory) { + TokenInterface _sellAddr = swapData.sellToken; + + uint256 ethAmt; + + if (address(_sellAddr) == ethAddr) { + ethAmt = swapData._sellAmt; + } else { + approve(TokenInterface(_sellAddr), AUGUSTUS_V6, swapData._sellAmt); + } + + swapData._buyAmt = _swapHelper(swapData, ethAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} diff --git a/contracts/mainnet/connectors/paraswap/main.sol b/contracts/mainnet/connectors/paraswap/main.sol new file mode 100644 index 0000000..9535376 --- /dev/null +++ b/contracts/mainnet/connectors/paraswap/main.sol @@ -0,0 +1,55 @@ +//SPDX-License-Identifier: MIT +pragma solidity 0.8.19; + +import {TokenInterface} from "../../common/interfaces.sol"; +import {Stores} from "../../common/stores.sol"; +import {Helpers} from "./helpers.sol"; + +contract ParaswapResolver is Helpers { + /** + * @dev Sell ETH/ERC20_Token using ParaSwap. + * @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 paraswap API. + * @param setId ID stores the amount of token brought. + */ + function swap( + address buyAddr, + address sellAddr, + uint256 sellAmt, + uint256 unitAmt, + bytes calldata callData, + uint256 setId + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + Helpers.SwapData memory swapData = Helpers.SwapData({ + buyToken: TokenInterface(buyAddr), + sellToken: TokenInterface(sellAddr), + unitAmt: unitAmt, + callData: callData, + _sellAmt: sellAmt, + _buyAmt: 0 + }); + + swapData = _swap(swapData, setId); + + _eventName = "LogSwap(address,address,uint256,uint256,uint256)"; + _eventParam = abi.encode( + address(swapData.buyToken), + address(swapData.sellToken), + swapData._buyAmt, + swapData._sellAmt, + setId + ); + } +} + +contract ConnectV2ParaswapV6 is ParaswapResolver { + string public name = "Paraswap-v6.2"; +} \ No newline at end of file