2021-04-21 12:34:43 +00:00
|
|
|
pragma solidity ^0.7.0;
|
|
|
|
|
|
|
|
import { DSMath } from "../../common/math.sol";
|
|
|
|
import { Basic } from "../../common/basic.sol";
|
|
|
|
import { TokenInterface } from "../../common/interfaces.sol";
|
2021-05-03 16:50:26 +00:00
|
|
|
import { AugustusSwapperInterface } from "./interface.sol";
|
2021-04-21 12:34:43 +00:00
|
|
|
|
|
|
|
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 {
|
2021-05-03 16:50:26 +00:00
|
|
|
address tokenProxy = AugustusSwapperInterface(paraswap).getTokenTransferProxy();
|
2021-06-24 19:55:04 +00:00
|
|
|
approve(TokenInterface(_sellAddr), tokenProxy, swapData._sellAmt);
|
2021-04-21 12:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
swapData._buyAmt = _swapHelper(swapData, maticAmt);
|
|
|
|
|
|
|
|
setUint(setId, swapData._buyAmt);
|
|
|
|
|
|
|
|
return swapData;
|
|
|
|
}
|
|
|
|
}
|