UnifiRouter.sol

Primary Uses - The uTrade V2 Router is the 'brain' of uTrade. The router finds the optimal path for exchanging one token for another. Whenever a trade is made, your wallet sends funds to the router address. The router will then carry out as many transactions as necessary to acquire the desired token. The router also handles adding liquidity to liquidity pools, and sending the corresponding LP tokens to liquidity providers.

Primary Uses - The uTrade V2 Router is the 'brain' of uTrade. The router finds the optimal path for exchanging one token for another. Whenever a trade is made, your wallet sends funds to the router address. The router will then carry out as many transactions as necessary to acquire the desired token. The router also handles adding liquidity to liquidity pools, and sending the corresponding LP tokens to liquidity providers.

uTrade V2 Router Code / Interfaces

uTrade V2 Router Contract Addresses

factory

function factory() external pure returns (address);

A call to the factory function returns the address of the current Factory used by uTrade v2. The current factory address for uTrade V2 on BTTC is 0xCAaB36C77841647dC9955B3b1D03710E9B9F127f.

WETH

function WETH() external pure returns (address);

A call to the WETH function returns the address of Wrapped BTT (WBTT) on BitTorrent Chain. As this address does not change, it will always return 0x8D193c6efa90BCFf940A98785d1Ce9D093d3DC8A.

quote

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

A call to the quote function returns the amount of tokenB that will be received for an amount of tokenA. This can be used to calculate the exchange rate between two tokens without factoring in slippage or fees. By entering the amount of tokenA, the total amount of reserves of tokenA as reserveA, and the total amount of reserves of tokenB as reserveB, the call will return the equivalent amount of tokenB.

getAmountOut

function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut, uint fee) external pure returns (uint amountOut);

A call to the getAmountOutfunction with the amount of the token being sent will return the maximum amount of a token to be received, accounting for fees and the total amount of reserves.

getAmountIn

function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut, uint fee) external pure returns (uint amountIn);

A call to the getAmountIn function with the amount of the token you wish to receive will return the minimum amount required of the token you wish to send, accounting for fees and the total amount of reserves.

getAmountsOut

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

A call to the getAmountsOut function with the amount of the token being sent will return the maximum amount to be received of multiple different tokens. By entering multiple LP addresses in the address array, the function will return the maximum amount of each token that will be received. A call to this function uses the getReserves function from UnifiPair.sol to determine the reserves of the liquidity pool. Then, it calls the getAmountOut function to determine the amount of each token in the array that will be received for the amountIn value of a token.

getAmountsIn

function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);

A call to the getAmountsIn function with the desired amount of the token to be received will return the minimum amount required to be sent of multiple tokens. By entering multiple LP addresses in the address array, the function will return the minimum amount of each token that will need to be sent to receive the desired amount of a token. A call to this function uses the getReserves function from UnifiPair.sol to determine the reserves of the liquidity pool. Then, it calls the getAmountIn function to determine the amount of each token in the array that will need to be sent for the amountOut value of a token.

State-Changing Functions - Liquidity

addLiquidity

function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);

The addLiquidity function adds the two tokens that make up the liquidity pool - tokenA and tokenB - at the proper ratio based on the reserves of the pool. For example, if the pool contains 100 USDT.t / 100,000 BTT, a user's liquidity will be added at the ratio of 1 USDT.t to 1000 BTT, provided there is no price movements in the pair between when the user broadcasts the transaction to when it is mined. In the event of a price movement, the amountADesired, amountBDesired, amountAMin, and amountBMin act as a security measure against an adverse price movement. After the liquidity is added, the function sends the corresponding LP tokens to the sender.

In the case of a pool not existing for the two assets, one will be created using UnifiFactory.sol at the ratio of the assets supplied.

Function Parameters Breakdown

Function Return Parameter Breakdown

addLiquidityETH

function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);

The addLiquidityEth function is similar to the addLiquidity function except it accounts for one token being a native asset. In the case of BitTorrent Chain, this native asset would be BTT. This function will convert BTT to WBTT, will pair that WBTT with the supplied other token, and add the liquidity to the pool. This function will add at the ideal ratio based on when the transaction is mined. In the case of a pool not existing for BTT and the token provided, one will be created using UnifiFactory.sol at the ratio of the assets supplied.

  • This function requires a msg.value with the amount of BTT to be added.

    • The msg.value acts as the amountETHDesired. As in, if the ratio between BTT and the token being paired with it change, this is the number of BTT that will be added to the pool.

    • Any leftover BTT is returned to the msg.sender address.

Function Parameter Breakdown

Function Return Parameter Breakdown

removeLiquidity

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

The removeLiquidity function removes the two tokens that make up liquidity from a pool. In other words, this function is used when the pool consists of two BRC-20 tokens.

  • In the event one of the assets is paired with BTT, BTT will have been wrapped and paired with WBTT (Wrapped BTT). If the user wishes to withdraw WBTT instead of withdrawing as BTT, this function should be used instead of removeLiquidityETH .

Function Parameter Breakdown

Function Return Parameter Breakdown

removeLiquidityETH

function removeLiquidityETH(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
) external returns (uint amountToken, uint amountETH);

The removeLiquidityETH function removes BTT as well as the corresponding paired token in the liquidity pool. In other words, this function is used when the pool consists of WBTT and a BRC-20 token.

  • In the event one of the assets is paired with BTT, BTT has been wrapped into WBTT(Wrapped BTT). This function will unwrap the WBTT as the liquidity removed. If the user wishes to withdraw WBTT instead of withdrawing as BTT, the removeLiquidity function should be used.

Function Parameter Breakdown

Function Return Parameter Breakdown

removeLiquidityWithPermit

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

The removeLiquidityWithPermit functions removes the two tokens that make up liquidity from a pool. In other words, this function is used when the pool consists of two BRC-20 tokens. This function operates similarly to the removeLiquidity function with the added benefit of not requiring pre-approvals using permit function from the BRC-20 contract.

  • In the event one of the assets is paired with BTT, BTT will have been wrapped and paired with WBTT (Wrapped BTT). If the user wishes to withdraw WBTT instead of withdrawing as BTT, this function should be used instead of removeLiquidityETHWithPermit .

Function Parameter Breakdown

Function Return Parameter Breakdown

removeLiquidityETHWithPermit

function removeLiquidityETHWithPermit(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);
    

The removeLiquidityETHWithPermit function removes BTT as well as the corresponding paired token in the liquidity pool. In other words, this function is used when the pool consists of WBTT and a BRC-20 token. This function operates similarly to the removeLiquidityETHfunction with the added benefit of not requiring pre-approvals using permit function from the BRC-20 contract.

  • In the event one of the assets is paired with BTT, BTT has been wrapped into WBTT(Wrapped BTT). This function will unwrap the WBTT as the liquidity removed. If the user wishes to withdraw WBTT instead of withdrawing as BTT, the removeLiquidityWithPermit function should be used.

Function Parameter Breakdown

Function Return Parameter Breakdown

removeLiquidityETHSupportingFeeOnTransferTokens

function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
) external returns (uint amountETH);

The removeLiquidityETHSupportingFeeOnTransferTokensfunction is similar to the removeLiquidityETH function, and contains the same call parameters. This function removes BTT as well as the corresponding paired token in the liquidity pool. In other words, this function is used when the pool consists of WBTT and a BRC-20 token.

However, this function allows for fee on transfer tokens, such as PAXG, to be used on uTrade.

Function Parameter Breakdown

Function Return Parameter Breakdown

removeLiquidityETHWithPermitSupportingFeeOnTransferTokens

function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountETH);

The removeLiquidityETHWithPermitSupportingFeeOnTransferTokensfunction is similar to the removeLiquidityETHWithPermitfunction. This function removes BTT as well as the corresponding paired token in the liquidity pool. In other words, this function is used when the pool consists of WBTT and a BRC-20 token. This function operates similarly to the removeLiquidityETHfunction with the added benefit of not requiring pre-approvals using permit function from the BRC-20 contract. In addition, this function allows for fee on transfer tokens, such as PAXG, to be used on uTrade.

Function Parameter Breakdown

Function Return Parameter Breakdown

State-Changing Functions - Swap

swapExactTokensForTokens

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

The swapExactTokensForTokens function sends an exact amount of tokens for the maximum amount of another token.

Function Parameter Breakdown

Function Return Parameter Breakdown

swapTokensForExactTokens

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

The swapTokensForExactTokens function sends the minimum amount of tokens for the exact amount of another token.

Function Parameter Breakdown

Function Return Parameter Breakdown

swapExactETHForTokens

function swapExactETHForTokens(
        uint amountOutMin, 
        address[] calldata path, 
        address to, 
        uint deadline)
        external
        payable
returns (uint[] memory amounts);

The swapExactETHForTokens function sends an exact amount of BTT for a desired token.

Function Parameter Breakdown

Function Return Parameter Breakdown

swapTokensForExactETH

function swapTokensForExactETH(
        uint amountOut, 
        uint amountInMax, 
        address[] calldata path, 
        address to, 
        uint deadline)
        external
        returns (uint[] memory amounts);

The swapTokensForExactETH function sends an amount of tokens for an exact amount of BTT.

Function Parameter Breakdown

Function Return Parameter Breakdown

swapExactTokensForETH

function swapExactTokensForETH(
        uint amountIn, 
        uint amountOutMin, 
        address[] calldata path, 
        address to, 
        uint deadline)
        external
        returns (uint[] memory amounts);

The swapExactTokensForETH function sends an exact amount of tokens for BTT.

Function Parameter Breakdown

Function Parameter Return Breakdown

swapETHForExactTokens

function swapETHForExactTokens(
        uint amountOut, 
        address[] calldata path, 
        address to, 
        uint deadline)
        external
        payable
        returns (uint[] memory amounts);

The swapETHForExactTokens function swaps an exact amount of BTT for an amount of the desired token.

Function Parameter Breakdown

Function Return Parameter Breakdown

swapExactTokensForTokensSupportingFeeOnTransferTokens

function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
) external;

The swapExactTokensForTokensSupportingFeeOnTransferTokens function is similar to swapExactTokensForTokens as it swaps one BRC-20 token for another BRC-20 token. In addition, this function allows for fee on transfer tokens, such as PAXG, to be used on uTrade.

Function Parameter Breakdown

swapExactETHForTokensSupportingFeeOnTransferTokens

function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
 ) external payable;

The swapExactETHForTokensSupportingFeeOnTransferTokensfunction is similar to swapExactETHForTokens function as it swaps an exact amount of BTT for BRC-20 tokens. In addition, this function allows for fee on transfer tokens, such as PAXG, to be used on uTrade.

Function Parameter Breakdown

swapExactTokensForETHSupportingFeeOnTransferTokens

function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
) external;

The function swapExactTokensForETHSupportingFeeOnTransferTokensis similar to the swapExactTokensForETHfunction, as it swaps an exact amount of BRC-20 tokens for BTT. In addition, this function allows for fee on transfer tokens, such as PAXG, to be used on uTrade.

Function Parameter Breakdown

Interface Code

interface IUnifiRouter01 {
    function factory() external pure returns (address);
    function WETH() external pure returns (address);

    function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB, uint liquidity);
    function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
    function removeLiquidity(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETH(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountToken, uint amountETH);
    function removeLiquidityWithPermit(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETHWithPermit(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountToken, uint amountETH);
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapTokensForExactTokens(
        uint amountOut,
        uint amountInMax,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);
    function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);

    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut, uint fee) external pure returns (uint amountOut);
    function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut, uint fee) external pure returns (uint amountIn);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
    function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}

interface IUnifiRouter02 is IUnifiRouter01 {
    function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountETH);
    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountETH);

    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable;
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
}

Last updated