Merge pull request #28 from Instadapp/paraswap-connector

Add Paraswap connector
This commit is contained in:
Thrilok kumar 2021-04-22 02:05:03 +05:30 committed by GitHub
commit 06f626ba0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 107 additions and 1 deletions

View File

@ -14,7 +14,7 @@ abstract contract Helpers is DSMath, Basic {
* @dev Cream Mapping
*/
// TODO: wait for the cream mapping contract address
CreamMappingInterface internal constant creamMapping = CreamMappingInterface();
CreamMappingInterface internal constant creamMapping = CreamMappingInterface(address(0));
/**
* @dev enter cream market

View File

@ -0,0 +1,11 @@
pragma solidity ^0.7.0;
contract Events {
event LogSwap(
address buyToken,
address sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 setId
);
}

View File

@ -0,0 +1,55 @@
pragma solidity ^0.7.0;
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 paraswap = 0x90249ed4d69D70E709fFCd8beE2c5A566f65dADE;
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) == maticAddr) {
maticAmt = swapData._sellAmt;
} else {
TokenInterface(_sellAddr).approve(paraswap, swapData._sellAmt);
}
swapData._buyAmt = _swapHelper(swapData, maticAmt);
setUint(setId, swapData._buyAmt);
return swapData;
}
}

View File

@ -0,0 +1,40 @@
pragma solidity ^0.7.0;
import { TokenInterface } from "../../common/interfaces.sol";
import { Stores } from "../../common/stores.sol";
import { Helpers } from "./helpers.sol";
abstract contract ParaswapResolver is Helpers {
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 ConnectParaswap is ParaswapResolver {
string public name = "Paraswap-v1";
}