-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db1297b
commit 4908651
Showing
5 changed files
with
185 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
pragma solidity 0.7.6; | ||
|
||
interface IWooPP { | ||
|
||
/// @dev get the quote token address (immutable) | ||
/// @return address of quote token | ||
function quoteToken() external view returns (address); | ||
|
||
/// @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); | ||
|
||
/// @dev Query the amount for selling the quote token. | ||
/// @param baseToken the base token to receive (buy) | ||
/// @param quoteAmount the amount to sell | ||
/// @return baseAmount the swapped base token amount | ||
function querySellQuote(address baseToken, uint256 quoteAmount) external view returns (uint256 baseAmount); | ||
|
||
/// @dev Swap baseToken into quoteToken | ||
/// @param baseToken the base token | ||
/// @param baseAmount amount of baseToken that user want to swap | ||
/// @param minQuoteAmount minimum amount of quoteToken that user accept to receive | ||
/// @param to quoteToken receiver address | ||
/// @param rebateTo the wallet address for rebate | ||
/// @return quoteAmount the swapped amount of quote token | ||
function sellBase( | ||
address baseToken, | ||
uint256 baseAmount, | ||
uint256 minQuoteAmount, | ||
address to, | ||
address rebateTo | ||
) external returns (uint256 quoteAmount); | ||
|
||
/// @dev Swap quoteToken into baseToken | ||
/// @param baseToken the base token | ||
/// @param quoteAmount amount of quoteToken that user want to swap | ||
/// @param minBaseAmount minimum amount of baseToken that user accept to receive | ||
/// @param to baseToken receiver address | ||
/// @param rebateTo the wallet address for rebate | ||
/// @return baseAmount the swapped amount of base token | ||
function sellQuote( | ||
address baseToken, | ||
uint256 quoteAmount, | ||
uint256 minBaseAmount, | ||
address to, | ||
address rebateTo | ||
) external returns (uint256 baseAmount); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity 0.7.6; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "@openzeppelin/contracts/math/SafeMath.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import "../../lib/TransferHelper.sol"; | ||
import "../../lib/DexData.sol"; | ||
import "../../lib/Utils.sol"; | ||
import "../IWooPP.sol"; | ||
|
||
contract WoofiDexV1 { | ||
using SafeMath for uint; | ||
using Utils for uint; | ||
using TransferHelper for IERC20; | ||
|
||
address public quoteToken; | ||
IWooPP public woo; | ||
address rebateTo; | ||
|
||
function _approveIfNeeded( | ||
address _tokenIn, | ||
uint _amount | ||
) internal { | ||
uint allowance = IERC20(_tokenIn).allowance(address(this), address(woo)); | ||
if (allowance < _amount) { | ||
IERC20(_tokenIn).safeApprove(address(woo), _amount); | ||
} | ||
} | ||
|
||
function _safeQuery( | ||
function (address, uint) external view returns (uint) qFn, | ||
address _baseToken, | ||
uint _baseAmount | ||
) internal view returns (uint) { | ||
try qFn(_baseToken, _baseAmount) returns (uint amountOut) { | ||
return amountOut; | ||
} catch { | ||
return 0; | ||
} | ||
} | ||
|
||
|
||
function query( | ||
uint _amountIn, | ||
address _tokenIn, | ||
address _tokenOut | ||
) external view returns (uint256 amountOut) { | ||
if (_amountIn == 0) { | ||
return 0; | ||
} | ||
if (_tokenIn == quoteToken) { | ||
amountOut = woo.querySellQuote(_tokenOut, _amountIn); | ||
} else if (_tokenOut == quoteToken) { | ||
amountOut = woo.querySellBase(_tokenIn, _amountIn); | ||
} else { | ||
uint quoteAmount = woo.querySellBase(_tokenIn, _amountIn); | ||
amountOut = woo.querySellQuote(_tokenOut, quoteAmount); | ||
} | ||
} | ||
|
||
function wooSwap( | ||
uint _amountIn, | ||
uint _amountOut, | ||
address _tokenIn, | ||
address _tokenOut, | ||
address _to | ||
) internal returns (uint realToAmount) { | ||
// check parameters and approve the allowrance if needed | ||
|
||
if (_tokenIn == quoteToken) { | ||
// case 1: quoteToken --> baseToken | ||
realToAmount = woo.sellQuote( | ||
_tokenOut, | ||
_amountIn, | ||
_amountOut, | ||
_to, | ||
rebateTo | ||
); | ||
} else if (_tokenOut == quoteToken) { | ||
// case 2: fromToken --> quoteToken | ||
realToAmount = woo.sellBase( | ||
_tokenIn, | ||
_amountIn, | ||
_amountOut, | ||
_to, | ||
rebateTo | ||
); | ||
} else { | ||
// case 3: fromToken --> quoteToken --> toToken | ||
uint256 quoteAmount = woo.sellBase( | ||
_tokenIn, | ||
_amountIn, | ||
0, | ||
address(this), | ||
rebateTo | ||
); | ||
_approveIfNeeded(quoteToken, quoteAmount); | ||
realToAmount = woo.sellQuote( | ||
_tokenOut, | ||
quoteAmount, | ||
_amountOut, | ||
_to, | ||
rebateTo | ||
); | ||
} | ||
// emit events if needed | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters