Skip to content

Commit

Permalink
Adding WooFi DEX support
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmy-openlev committed Sep 15, 2022
1 parent db1297b commit 4908651
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Node.js CI

on:
push:
branches: [ main, tax-token-support, bsc ]
branches: [ main, woofi-integration ]
pull_request:
branches: [ main ]

Expand Down
52 changes: 52 additions & 0 deletions contracts/dex/IWooPP.sol
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);
}
30 changes: 20 additions & 10 deletions contracts/dex/bsc/BscDexAggregatorV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import "../../lib/Utils.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "../../DelegateInterface.sol";
import "../../Adminable.sol";
import "../IWooPP.sol";
import "./WoofiDexV1.sol";

/// @title Swap logic on BSC
/// @author OpenLeverage
/// @notice Use this contract to swap tokens.
/// @dev Routers for different swap requests.
contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterface, UniV2ClassDex {
contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterface, UniV2ClassDex, WoofiDexV1 {
using DexData for bytes;
using SafeMath for uint;

Expand All @@ -28,13 +30,16 @@ contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterf
//pancakeFactory: 0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73
function initialize(
IUniswapV2Factory _pancakeFactory,
address _unsedFactory
address _unsedFactory,
IWooPP _woo
) public {
require(msg.sender == admin, "Not admin");
// Shh - currently unused
_unsedFactory;
pancakeFactory = _pancakeFactory;
dexInfo[DexData.DEX_PANCAKE] = DexInfo(_pancakeFactory, 25);
woo = _woo;
quoteToken = woo.quoteToken();
}

/// @notice Save factories of the dex.
Expand All @@ -54,7 +59,7 @@ contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterf
openLev = _openLev;
}

/// @notice Sell tokens
/// @notice Sell tokens
/// @dev Sell exact amount of token with tax applied
/// @param buyToken Address of token transfer from Dex pair
/// @param sellToken Address of token transfer into Dex pair
Expand All @@ -64,10 +69,15 @@ contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterf
/// @return buyAmount Exact Amount bought
function sell(address buyToken, address sellToken, uint sellAmount, uint minBuyAmount, bytes memory data) external override returns (uint buyAmount){
address payer = msg.sender;
buyAmount = uniClassSell(dexInfo[data.toDex()], buyToken, sellToken, sellAmount, minBuyAmount, payer, payer);
uint8 dex = data.toDex();
if (dex != DexData.DEX_WOOFI) {
buyAmount = uniClassSell(dexInfo[data.toDex()], buyToken, sellToken, sellAmount, minBuyAmount, payer, payer);
} else {
buyAmount = wooSwap(sellAmount, minBuyAmount, buyToken, sellToken, openLev);
}
}

/// @notice Sell tokens
/// @notice Sell tokens
/// @dev Sell exact amount of token through path
/// @param sellAmount Exact amount to sell
/// @param minBuyAmount minmum amount of token to receive.
Expand All @@ -77,7 +87,7 @@ contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterf
buyAmount = uniClassSellMul(dexInfo[data.toDex()], sellAmount, minBuyAmount, data.toUniV2Path());
}

/// @notice Buy tokens
/// @notice Buy tokens
/// @dev Buy exact amount of token with tax applied
/// @param buyToken Address of token transfer from Dex pair
/// @param sellToken Address of token transfer into Dex pair
Expand All @@ -91,7 +101,7 @@ contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterf
sellAmount = uniClassBuy(dexInfo[data.toDex()], buyToken, sellToken, buyAmount, maxSellAmount, buyTax, sellTax);
}

/// @notice Calculate amount of token to buy
/// @notice Calculate amount of token to buy
/// @dev Calculate exact amount of token to buy with tax applied
/// @param buyToken Address of token transfer from Dex pair
/// @param sellToken Address of token transfer into Dex pair
Expand All @@ -106,7 +116,7 @@ contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterf
buyAmount = Utils.toAmountAfterTax(buyAmount, buyTax);
}

/// @notice Calculate amount of token to sell
/// @notice Calculate amount of token to sell
/// @dev Calculate exact amount of token to sell with tax applied
/// @param buyToken Address of token transfer from Dex pair
/// @param sellToken Address of token transfer into Dex pair
Expand All @@ -119,7 +129,7 @@ contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterf
sellAmount = uniClassCalSellAmount(dexInfo[data.toDex()], buyToken, sellToken, buyAmount, buyTax, sellTax);
}

/// @notice Get price
/// @notice Get price
/// @dev Get current price of desToken / quoteToken
/// @param desToken Token to be priced
/// @param quoteToken Token used for pricing
Expand Down Expand Up @@ -153,7 +163,7 @@ contract BscDexAggregatorV1 is DelegateInterface, Adminable, DexAggregatorInterf
/// @return cAvgPrice Current TWAP price
/// @return hAvgPrice Historical TWAP price
/// @return decimals Token price decimal
/// @return timestamp Last TWAP price update timestamp
/// @return timestamp Last TWAP price update timestamp
function getPriceCAvgPriceHAvgPrice(
address desToken,
address quoteToken,
Expand Down
110 changes: 110 additions & 0 deletions contracts/dex/bsc/WoofiDexV1.sol
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
}
}

3 changes: 2 additions & 1 deletion contracts/lib/DexData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ library DexData {
uint8 constant DEX_BABY = 12;
uint8 constant DEX_MOJITO = 13;
uint8 constant DEX_KU = 14;
uint8 constant DEX_BISWAP=15;
uint8 constant DEX_BISWAP = 15;
uint8 constant DEX_WOOFI = 16;

struct V3PoolData {
address tokenA;
Expand Down

0 comments on commit 4908651

Please sign in to comment.