Two main contracts of WOOFi's swap function are as follows:
WooPP.sol: the main swap contract that handles the logic operation, including setting the token info, calculating slippage, calculating the exchange amount and executing trades. it stores all tokens that are supported for trading. In this contract, it defines quote token and base token. Quote token is the reference token (i.e. stablecoins) in the contract and there is only one quote token. The contract can have multiple base tokens and they can be added by the strategist.
WooRouter.sol: the router contract that frontend users interact with. It interacts with the lower layer WooPP.sol to execute the sell logic and send back users the desired amount of tokens. This contract also implements the logic to route user orders to 3rd party aggregator (e.g. 1inch) when WooPP.sol does not have sufficient liquidity.
More details about WOOFi smart contract architecture, implementation info, and addresses can be found in .
Supported assets
WooPP.sol currently supports the following tokens. Quote token is the stablecoin and the rest are base tokens.
Integrate WOOFi as a liquidity source
When integrating WOOFi as a liquidity source, you can either interact directly with WooPP.sol or throughWooRouter.sol.
Integrating WooPP.sol (recommended)
For aggregators or trading bots, we recommend integrate directly with WooPP.sol to be more gas efficient.
/// @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);
A simpler way is calling WooRouter.sol contract which provides the similar query and swap as above. This way costs slightly more gas than directly integrating with WooPP.sol.
/// @dev query the amount to swap fromToken -> toToken/// @param fromToken the from token/// @param toToken the to token/// @param fromAmount the amount of fromToken to swap/// @return toAmount the swapped amount to receivefunctionquerySwap(address fromToken,address toToken,uint256 fromAmount ) externalviewreturns (uint256 toAmount);
Sample code to retrieve quote on selling 1 BTC:
wooRouter.querySwap(0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c,// btcb token address0x55d398326f99059fF775485246999027B3197955,// usdt token address1000000000000000000); // btcb amount to swap in decimal 18
Swap function
/// @dev swap fromToken -> 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 required or the tx reverts/// @param to the destination address/// @param rebateTo the address to receive rebate (rebate rate is set to 0 now)/// @return realToAmount the amount of toToken to receivefunctionswap(address fromToken,address toToken,uint256 fromAmount,uint256 minToAmount,address payable to,address rebateTo ) externalpayablereturns (uint256 realToAmount);
Sample code to swap 1 BTC to USDT:
wooRouter.swap(0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c,// btcb token address0x55d398326f99059fF775485246999027B3197955,// usdt token address1000000000000000000,// btcb amount to swap in decimal 18990000000000000000,// min amount for 1% slippage0xd51062A4aF7B76ee0b2893Ef8b52aCC155393E3D,// the address to receive the swap fund0); // the rebate address
sPMM offchain simulation
For aggregators running offchain routing simulation, you can find the typescript implementation of WOOFi's sPMM algorithm in the github.