From 9bb1e1587fe9e22b50776e49b09159cac324858b Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Wed, 6 Oct 2021 03:06:11 +0530 Subject: [PATCH] Added Paraswap avalanche connector --- .../avalanche/connectors/paraswap/events.sol | 11 ++++ .../avalanche/connectors/paraswap/helpers.sol | 57 +++++++++++++++++++ .../connectors/paraswap/interface.sol | 5 ++ .../avalanche/connectors/paraswap/main.sol | 55 ++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 contracts/avalanche/connectors/paraswap/events.sol create mode 100644 contracts/avalanche/connectors/paraswap/helpers.sol create mode 100644 contracts/avalanche/connectors/paraswap/interface.sol create mode 100644 contracts/avalanche/connectors/paraswap/main.sol diff --git a/contracts/avalanche/connectors/paraswap/events.sol b/contracts/avalanche/connectors/paraswap/events.sol new file mode 100644 index 00000000..0f9470dc --- /dev/null +++ b/contracts/avalanche/connectors/paraswap/events.sol @@ -0,0 +1,11 @@ +pragma solidity ^0.7.0; + +contract Events { + event LogSwap( + address buyToken, + address sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 setId + ); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/paraswap/helpers.sol b/contracts/avalanche/connectors/paraswap/helpers.sol new file mode 100644 index 00000000..ccee429b --- /dev/null +++ b/contracts/avalanche/connectors/paraswap/helpers.sol @@ -0,0 +1,57 @@ +pragma solidity ^0.7.0; + +import { DSMath } from "../../common/math.sol"; +import { Basic } from "../../common/basic.sol"; +import { TokenInterface } from "../../common/interfaces.sol"; +import { AugustusSwapperInterface } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + + struct SwapData { + TokenInterface sellToken; + TokenInterface buyToken; + uint256 _sellAmt; + uint256 _buyAmt; + uint256 unitAmt; + bytes callData; + } + + address internal constant paraswap = 0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57; + + function _swapHelper(SwapData memory swapData, uint256 wmaticAmt) 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, ) = paraswap.call{value: wmaticAmt}(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 maticAmt; + + if (address(_sellAddr) == avaxAddr) { + maticAmt = swapData._sellAmt; + } else { + address tokenProxy = AugustusSwapperInterface(paraswap).getTokenTransferProxy(); + approve(TokenInterface(_sellAddr), tokenProxy, swapData._sellAmt); + } + + swapData._buyAmt = _swapHelper(swapData, maticAmt); + + setUint(setId, swapData._buyAmt); + + return swapData; + } +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/paraswap/interface.sol b/contracts/avalanche/connectors/paraswap/interface.sol new file mode 100644 index 00000000..a63b4c77 --- /dev/null +++ b/contracts/avalanche/connectors/paraswap/interface.sol @@ -0,0 +1,5 @@ +pragma solidity ^0.7.0; + +interface AugustusSwapperInterface { + function getTokenTransferProxy() external view returns (address); +} \ No newline at end of file diff --git a/contracts/avalanche/connectors/paraswap/main.sol b/contracts/avalanche/connectors/paraswap/main.sol new file mode 100644 index 00000000..216e9c7c --- /dev/null +++ b/contracts/avalanche/connectors/paraswap/main.sol @@ -0,0 +1,55 @@ +pragma solidity ^0.7.0; + +/** + * @title Paraswap. + * @dev DEX Aggregator. + */ + +import { TokenInterface } from "../../common/interfaces.sol"; +import { Stores } from "../../common/stores.sol"; +import { Helpers } from "./helpers.sol"; + +abstract contract ParaswapResolver is Helpers { + /** + * @dev Sell AVAX/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 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 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 ConnectV2ParaswapAvalanche is ParaswapResolver { + string public constant name = "Paraswap-v1"; +} \ No newline at end of file