BuniCornRouter02

Code

BuniCornRouter02.sol

Address

BuniCornRouter02 is deployed at 0x689E37aFb4236A5cF808cA05a8D1c564200fA0E4 on the Binance SmartChain mainnet.

Read-Only Functions

factory

function factory() external pure returns (address);

Returns the factory address.

quote

function quote(uint256 amountA, uint256 reserveA, uint256 reserveB) external pure returns (uint256 amountB);

Given some asset amount and reserves, returns an amount of the other asset representing equivalent value.

getAmountsOut

function getAmountsOut(uint256 amountIn, address[] memory poolsPath, IERC20[] memory path) external view returns (uint256[] memory amounts);

First calls verifyPoolsPathSwap to verify the validity of the poolsPath and path variables.

Then, given an input asset amount and an array of token and corresponding pool addresses, calculates all subsequent maximum output token amounts by calling getTradeInfo for each pair of token addresses in the path in turn, and using these to call getAmountOut.

getAmountsIn

function getAmountsIn(uint256 amountOut, address[] memory poolsPath, IERC20[] memory path) external view returns (uint256[] memory amounts);

First calls verifyPoolsPathSwap to verify the validity of the poolsPath and path variables.

Then, given an output asset amount and an array of token addresses, calculates all preceding minimum input token amounts by calling getTradeInfo for each pair of token addresses in the path in turn, and using these to call getAmountIn.

State-Changing Functions

addLiquidity

function addLiquidity(
  IERC20 tokenA,
  IERC20 tokenB,
  address pool,
  uint256 amountADesired,
  uint256 amountBDesired,
  uint256 amountAMin,
  uint256 amountBMin,
  uint256[2] memory vReserveRatioBounds,
  address to,
  uint256 deadline
) external returns (uint256 amountA, uint256 amountB, uint256 liquidity);

Adds liquidity to an existing stable pool.

  • pool should already be an existing pool of the token pair of tokenA and tokenB. Otherwise, the transaction will revert.

  • msg.sender should have already given the router an allowance of at least amountADesired/amountBDesired on tokenA/tokenB.

  • Always adds assets at the ideal ratio, according to the price when the transaction is executed.

removeLiquidity

function removeLiquidity(
  IERC20 tokenA,
  IERC20 tokenB,
  address pool,
  uint256 liquidity,
  uint256 amountAMin,
  uint256 amountBMin,
  address to,
  uint256 deadline
) external returns (uint256 amountA, uint256 amountB);

Removes liquidity from a specified stable pool.

  • msg.sender should have already given the router an allowance of at least liquidity on the pool.

removeLiquidityWithPermit

function removeLiquidityWithPermit(
  IERC20 tokenA,
  IERC20 tokenB,
  address pool,
  uint256 liquidity,
  uint256 amountAMin,
  uint256 amountBMin,
  address to,
  uint256 deadline,
  bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint256 amountA, uint256 amountB);

Removes liquidity from an ERC-20⇄ERC-20 pool without pre-approval, thanks to permit.

swapExactTokensForTokens

function swapExactTokensForTokens(
  uint256 amountIn,
  uint256 amountOutMin,
  address[] calldata poolsPath,
  IERC20[] calldata path,
  address to,
  uint256 deadline
) external returns (uint256[] memory amounts);

Swaps an exact amount of input tokens for as many output tokens as possible. The token and pool routes are specified by the poolsPath and path variables respectively.

The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist). The nth element of poolsPath is the first pool to be used for the nth and (n+1)th elements of path. It therefore is a requirement for poolsPath to be a size smaller than path, ie. poolsPath.length = path.length - 1.

  • msg.sender should have already given the router an allowance of at least amountIn on the input token.

swapTokensForExactTokens

function swapTokensForExactTokens(
  uint256 amountOut,
  uint256 amountInMax,
  address[] calldata poolsPath,
  IERC20[] calldata path,
  address to,
  uint256 deadline
) external returns (uint256[] memory amounts);

Receive an exact amount of output tokens for as few input tokens as possible. The token and pool routes are specified by the poolsPath and path variables respectively.

The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist). The nth element of poolsPath is the first pool to be used for the nth and (n+1)th elements of path. It therefore is a requirement for poolsPath to be a size smaller than path, ie. poolsPath.length = path.length - 1.

  • msg.sender should have already given the router an allowance of at least amountInMax on the input token.

Interface

pragma solidity 0.6.12;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/// @dev an simple interface for integration dApp to swap
interface IBuniCornExchangeRouter {
    function swapExactTokensForTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata poolsPath,
        IERC20[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);

    function swapTokensForExactTokens(
        uint256 amountOut,
        uint256 amountInMax,
        address[] calldata poolsPath,
        IERC20[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);

    function getAmountsOut(
        uint256 amountIn,
        address[] calldata poolsPath,
        IERC20[] calldata path
    ) external view returns (uint256[] memory amounts);

    function getAmountsIn(
        uint256 amountOut,
        address[] calldata poolsPath,
        IERC20[] calldata path
    ) external view returns (uint256[] memory amounts);
}

/// @dev an simple interface for integration dApp to contribute liquidity
interface IBuniCornLiquidityRouter {
    /**
     * @param tokenA address of token in the pool
     * @param tokenB address of token in the pool
     * @param pool the address of the pool
     * @param amountADesired the amount of tokenA users want to add to the pool
     * @param amountBDesired the amount of tokenB users want to add to the pool
     * @param amountAMin bounds to the extents to which amountB/amountA can go up
     * @param amountBMin bounds to the extents to which amountB/amountA can go down
     * @param vReserveRatioBounds bounds to the extents to which vReserveB/vReserveA can go (precision: 2 ** 112)
     * @param to Recipient of the liquidity tokens.
     * @param deadline Unix timestamp after which the transaction will revert.
     */
    function addLiquidity(
        IERC20 tokenA,
        IERC20 tokenB,
        address pool,
        uint256 amountADesired,
        uint256 amountBDesired,
        uint256 amountAMin,
        uint256 amountBMin,
        uint256[2] calldata vReserveRatioBounds,
        address to,
        uint256 deadline
    )
        external
        returns (
            uint256 amountA,
            uint256 amountB,
            uint256 liquidity
        );

    function addLiquidityNewPool(
        IERC20 tokenA,
        IERC20 tokenB,
        uint32 ampBps,
        uint256 amountADesired,
        uint256 amountBDesired,
        uint256 amountAMin,
        uint256 amountBMin,
        address to,
        uint256 deadline
    )
        external
        returns (
            uint256 amountA,
            uint256 amountB,
            uint256 liquidity
        );

    /**
     * @param tokenA address of token in the pool
     * @param tokenB address of token in the pool
     * @param pool the address of the pool
     * @param liquidity the amount of lp token users want to burn
     * @param amountAMin the minimum token retuned after burning
     * @param amountBMin the minimum token retuned after burning
     * @param to Recipient of the returned tokens.
     * @param deadline Unix timestamp after which the transaction will revert.
     */
    function removeLiquidity(
        IERC20 tokenA,
        IERC20 tokenB,
        address pool,
        uint256 liquidity,
        uint256 amountAMin,
        uint256 amountBMin,
        address to,
        uint256 deadline
    ) external returns (uint256 amountA, uint256 amountB);

    /**
     * @param tokenA address of token in the pool
     * @param tokenB address of token in the pool
     * @param pool the address of the pool
     * @param liquidity the amount of lp token users want to burn
     * @param amountAMin the minimum token retuned after burning
     * @param amountBMin the minimum token retuned after burning
     * @param to Recipient of the returned tokens.
     * @param deadline Unix timestamp after which the transaction will revert.
     * @param approveMax whether users permit the router spending max lp token or not.
     * @param r s v Signature of user to permit the router spending lp token
     */
    function removeLiquidityWithPermit(
        IERC20 tokenA,
        IERC20 tokenB,
        address pool,
        uint256 liquidity,
        uint256 amountAMin,
        uint256 amountBMin,
        address to,
        uint256 deadline,
        bool approveMax,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external returns (uint256 amountA, uint256 amountB);

    /**
     * @param amountA amount of 1 side token added to the pool
     * @param reserveA current reserve of the pool
     * @param reserveB current reserve of the pool
     * @return amountB amount of the other token added to the pool
     */
    function quote(
        uint256 amountA,
        uint256 reserveA,
        uint256 reserveB
    ) external pure returns (uint256 amountB);
}

/// @dev full interface for router
interface IBuniCornRouter01 is IBuniCornExchangeRouter, IBuniCornLiquidityRouter {
    function factory() external pure returns (address);
}

Last updated