add WooFi V2 Mixin (#644)
This commit is contained in:
parent
6fd269db34
commit
b613de1547
@ -24,29 +24,22 @@ import "@0x/contracts-utils/contracts/src/v06/LibSafeMathV06.sol";
|
||||
|
||||
/// @dev WooFI pool interface.
|
||||
interface IWooPP {
|
||||
function quoteToken() external view returns (address);
|
||||
|
||||
function sellBase(
|
||||
address baseToken,
|
||||
uint256 baseAmount,
|
||||
uint256 minQuoteAmount,
|
||||
/// @notice Swap `fromToken` to `toToken`.
|
||||
/// @param fromToken the from token
|
||||
/// @param toToken the to token
|
||||
/// @param fromAmount the amount of `fromToken` to swap
|
||||
/// @param minToAmount the minimum amount of `toToken` to receive
|
||||
/// @param to the destination address
|
||||
/// @param rebateTo the rebate address (optional, can be 0)
|
||||
/// @return realToAmount the amount of toToken to receive
|
||||
function swap(
|
||||
address fromToken,
|
||||
address toToken,
|
||||
uint256 fromAmount,
|
||||
uint256 minToAmount,
|
||||
address to,
|
||||
address rebateTo
|
||||
) external returns (uint256 quoteAmount);
|
||||
|
||||
function sellQuote(
|
||||
address baseToken,
|
||||
uint256 quoteAmount,
|
||||
uint256 minBaseAmount,
|
||||
address to,
|
||||
address rebateTo
|
||||
) external returns (uint256 baseAmount);
|
||||
|
||||
/// @dev Query the amount for selling the base token amount.
|
||||
/// @param baseToken the base token to sell
|
||||
/// @param baseAmount the amount to sell
|
||||
/// @return quoteAmount the swapped quote amount
|
||||
function querySellBase(address baseToken, uint256 baseAmount) external view returns (uint256 quoteAmount);
|
||||
) external payable returns (uint256 realToAmount);
|
||||
}
|
||||
|
||||
contract MixinWOOFi {
|
||||
@ -57,39 +50,17 @@ contract MixinWOOFi {
|
||||
// solhint-disable-next-line const-name-snakecase
|
||||
address constant rebateAddress = 0xBfdcBB4C05843163F491C24f9c0019c510786304;
|
||||
|
||||
// /// @dev Swaps an exact amount of input tokens for as many output tokens as possible.
|
||||
// /// @param _amountIn Amount of input tokens to send
|
||||
// /// @param _minAmountOut The minimum amount of output tokens that must be received for the transaction
|
||||
// /// not to revert
|
||||
// /// @param _tokenIn Input token
|
||||
// /// @param _tokenOut Output token
|
||||
// /// @param _to recipient of tokens
|
||||
// /// @param pool WOOFi pool where the swap will happen
|
||||
function _tradeWOOFi(
|
||||
IERC20TokenV06 sellToken,
|
||||
IERC20TokenV06 buyToken,
|
||||
uint256 sellAmount,
|
||||
bytes memory bridgeData
|
||||
) public returns (uint256 boughtAmount) {
|
||||
IWooPP _pool = abi.decode(bridgeData, (IWooPP));
|
||||
IWooPP _router = abi.decode(bridgeData, (IWooPP));
|
||||
uint256 beforeBalance = buyToken.balanceOf(address(this));
|
||||
|
||||
sellToken.approveIfBelow(address(_pool), sellAmount);
|
||||
sellToken.approveIfBelow(address(_router), sellAmount);
|
||||
|
||||
_swap(sellAmount, address(sellToken), address(buyToken), _pool);
|
||||
boughtAmount = buyToken.balanceOf(address(this)).safeSub(beforeBalance);
|
||||
}
|
||||
|
||||
function _swap(uint256 _amountIn, address _tokenIn, address _tokenOut, IWooPP pool) internal {
|
||||
address quoteToken = pool.quoteToken();
|
||||
if (_tokenIn == quoteToken) {
|
||||
pool.sellQuote(_tokenOut, _amountIn, 1, address(this), rebateAddress);
|
||||
} else if (_tokenOut == quoteToken) {
|
||||
pool.sellBase(_tokenIn, _amountIn, 1, address(this), rebateAddress);
|
||||
} else {
|
||||
uint256 quoteAmount = pool.sellBase(_tokenIn, _amountIn, 0, address(this), rebateAddress);
|
||||
IERC20TokenV06(pool.quoteToken()).approveIfBelow(address(pool), quoteAmount);
|
||||
pool.sellQuote(_tokenOut, quoteAmount, 1, address(this), rebateAddress);
|
||||
}
|
||||
boughtAmount = _router.swap(address(sellToken), address(buyToken), sellAmount, 0, address(this), rebateAddress);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user