Skip to content

LBRouter ​

Git Source

Inherits:ILBRouter

Author:
Trader Joe

Main contract to interact with to swap and manage liquidity on Joe V2 exchange.

State Variables ​

_factory2_2 ​

solidity
ILBFactory private immutable _factory2_2;

_factory2_1 ​

solidity
ILBFactory private immutable _factory2_1;

_factoryV1 ​

solidity
IJoeFactory private immutable _factoryV1;

_legacyFactory ​

solidity
ILBLegacyFactory private immutable _legacyFactory;

_legacyRouter ​

solidity
ILBLegacyRouter private immutable _legacyRouter;

_wnative ​

solidity
IWNATIVE private immutable _wnative;

Functions ​

onlyFactoryOwner ​

solidity
modifier onlyFactoryOwner();

ensure ​

solidity
modifier ensure(uint256 deadline);

verifyPathValidity ​

solidity
modifier verifyPathValidity(Path memory path);

constructor ​

Constructor

solidity
constructor(
    ILBFactory factory2_2,
    IJoeFactory factoryV1,
    ILBLegacyFactory legacyFactory,
    ILBLegacyRouter legacyRouter,
    ILBFactory factory2_1,
    IWNATIVE wnative
);

Parameters

NameTypeDescription
factory2_2ILBFactoryAddress of Joe V2.2 factory
factoryV1IJoeFactoryAddress of Joe V1 factory
legacyFactoryILBLegacyFactoryAddress of Joe V2 factory
legacyRouterILBLegacyRouterAddress of Joe V2 router
factory2_1ILBFactoryAddress of Joe V2.1 factory
wnativeIWNATIVEAddress of WNATIVE

receive ​

Receive function that only accept NATIVE from the WNATIVE contract

solidity
receive() external payable;

getFactory ​

View function to get the factory V2.1 address

solidity
function getFactory() external view override returns (ILBFactory lbFactory);

Returns

NameTypeDescription
lbFactoryILBFactoryThe address of the factory V2.1

getFactoryV2_1 ​

View function to get the factory V2.1 address

solidity
function getFactoryV2_1() external view override returns (ILBFactory lbFactory);

Returns

NameTypeDescription
lbFactoryILBFactoryThe address of the factory V2.1

getLegacyFactory ​

View function to get the factory V2 address

solidity
function getLegacyFactory() external view override returns (ILBLegacyFactory legacyLBfactory);

Returns

NameTypeDescription
legacyLBfactoryILBLegacyFactoryThe address of the factory V2

getV1Factory ​

View function to get the factory V1 address

solidity
function getV1Factory() external view override returns (IJoeFactory factoryV1);

Returns

NameTypeDescription
factoryV1IJoeFactoryThe address of the factory V1

getLegacyRouter ​

View function to get the router V2 address

solidity
function getLegacyRouter() external view override returns (ILBLegacyRouter legacyRouter);

Returns

NameTypeDescription
legacyRouterILBLegacyRouterThe address of the router V2

getWNATIVE ​

View function to get the WNATIVE address

solidity
function getWNATIVE() external view override returns (IWNATIVE wnative);

Returns

NameTypeDescription
wnativeIWNATIVEThe address of WNATIVE

getIdFromPrice ​

Returns the approximate id corresponding to the inputted price.
Warning, the returned id may be inaccurate close to the start price of a bin

solidity
function getIdFromPrice(ILBPair pair, uint256 price) external view override returns (uint24);

Parameters

NameTypeDescription
pairILBPairThe address of the LBPair
priceuint256The price of y per x (multiplied by 1e36)

Returns

NameTypeDescription
<none>uint24The id corresponding to this price

getPriceFromId ​

Returns the price corresponding to the inputted id

solidity
function getPriceFromId(ILBPair pair, uint24 id) external view override returns (uint256);

Parameters

NameTypeDescription
pairILBPairThe address of the LBPair
iduint24The id

Returns

NameTypeDescription
<none>uint256The price corresponding to this id

getSwapIn ​

Simulate a swap in

solidity
function getSwapIn(ILBPair pair, uint128 amountOut, bool swapForY)
    public
    view
    override
    returns (uint128 amountIn, uint128 amountOutLeft, uint128 fee);

Parameters

NameTypeDescription
pairILBPairThe address of the LBPair
amountOutuint128The amount of token to receive
swapForYboolWhether you swap X for Y (true), or Y for X (false)

Returns

NameTypeDescription
amountInuint128The amount of token to send in order to receive amountOut token
amountOutLeftuint128The amount of token Out that can't be returned due to a lack of liquidity
feeuint128The amount of fees paid in token sent

getSwapOut ​

Simulate a swap out

solidity
function getSwapOut(ILBPair pair, uint128 amountIn, bool swapForY)
    external
    view
    override
    returns (uint128 amountInLeft, uint128 amountOut, uint128 fee);

Parameters

NameTypeDescription
pairILBPairThe address of the LBPair
amountInuint128The amount of token sent
swapForYboolWhether you swap X for Y (true), or Y for X (false)

Returns

NameTypeDescription
amountInLeftuint128The amount of token In that can't be swapped due to a lack of liquidity
amountOutuint128The amount of token received if amountIn tokenX are sent
feeuint128The amount of fees paid in token sent

createLBPair ​

Create a liquidity bin LBPair for tokenX and tokenY using the factory

solidity
function createLBPair(IERC20 tokenX, IERC20 tokenY, uint24 activeId, uint16 binStep)
    external
    override
    returns (ILBPair pair);

Parameters

NameTypeDescription
tokenXIERC20The address of the first token
tokenYIERC20The address of the second token
activeIduint24The active id of the pair
binStepuint16The bin step in basis point, used to calculate log(1 + binStep)

Returns

NameTypeDescription
pairILBPairThe address of the newly created LBPair

addLiquidity ​

Add liquidity while performing safety checks

This function is compliant with fee on transfer tokens

solidity
function addLiquidity(LiquidityParameters calldata liquidityParameters)
    external
    override
    returns (
        uint256 amountXAdded,
        uint256 amountYAdded,
        uint256 amountXLeft,
        uint256 amountYLeft,
        uint256[] memory depositIds,
        uint256[] memory liquidityMinted
    );

Parameters

NameTypeDescription
liquidityParametersLiquidityParametersThe liquidity parameters

Returns

NameTypeDescription
amountXAddeduint256The amount of token X added
amountYAddeduint256The amount of token Y added
amountXLeftuint256The amount of token X left (sent back to liquidityParameters.refundTo)
amountYLeftuint256The amount of token Y left (sent back to liquidityParameters.refundTo)
depositIdsuint256[]The ids of the deposits
liquidityMinteduint256[]The amount of liquidity minted

addLiquidityNATIVE ​

Add liquidity with NATIVE while performing safety checks

This function is compliant with fee on transfer tokens

solidity
function addLiquidityNATIVE(LiquidityParameters calldata liquidityParameters)
    external
    payable
    override
    returns (
        uint256 amountXAdded,
        uint256 amountYAdded,
        uint256 amountXLeft,
        uint256 amountYLeft,
        uint256[] memory depositIds,
        uint256[] memory liquidityMinted
    );

Parameters

NameTypeDescription
liquidityParametersLiquidityParametersThe liquidity parameters

Returns

NameTypeDescription
amountXAddeduint256The amount of token X added
amountYAddeduint256The amount of token Y added
amountXLeftuint256The amount of token X left (sent back to liquidityParameters.refundTo)
amountYLeftuint256The amount of token Y left (sent back to liquidityParameters.refundTo)
depositIdsuint256[]The ids of the deposits
liquidityMinteduint256[]The amount of liquidity minted

removeLiquidity ​

Remove liquidity while performing safety checks

This function is compliant with fee on transfer tokens

solidity
function removeLiquidity(
    IERC20 tokenX,
    IERC20 tokenY,
    uint16 binStep,
    uint256 amountXMin,
    uint256 amountYMin,
    uint256[] memory ids,
    uint256[] memory amounts,
    address to,
    uint256 deadline
) external override ensure(deadline) returns (uint256 amountX, uint256 amountY);

Parameters

NameTypeDescription
tokenXIERC20The address of token X
tokenYIERC20The address of token Y
binStepuint16The bin step of the LBPair
amountXMinuint256The min amount to receive of token X
amountYMinuint256The min amount to receive of token Y
idsuint256[]The list of ids to burn
amountsuint256[]The list of amounts to burn of each id in _ids
toaddressThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountXuint256Amount of token X returned
amountYuint256Amount of token Y returned

removeLiquidityNATIVE ​

Remove NATIVE liquidity while performing safety checks

This function is NOT compliant with fee on transfer tokens.
This is wanted as it would make users pays the fee on transfer twice,
use the removeLiquidity function to remove liquidity with fee on transfer tokens.

solidity
function removeLiquidityNATIVE(
    IERC20 token,
    uint16 binStep,
    uint256 amountTokenMin,
    uint256 amountNATIVEMin,
    uint256[] memory ids,
    uint256[] memory amounts,
    address payable to,
    uint256 deadline
) external override ensure(deadline) returns (uint256 amountToken, uint256 amountNATIVE);

Parameters

NameTypeDescription
tokenIERC20The address of token
binStepuint16The bin step of the LBPair
amountTokenMinuint256The min amount to receive of token
amountNATIVEMinuint256The min amount to receive of NATIVE
idsuint256[]The list of ids to burn
amountsuint256[]The list of amounts to burn of each id in _ids
toaddress payableThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountTokenuint256Amount of token returned
amountNATIVEuint256Amount of NATIVE returned

swapExactTokensForTokens ​

Swaps exact tokens for tokens while performing safety checks

solidity
function swapExactTokensForTokens(
    uint256 amountIn,
    uint256 amountOutMin,
    Path memory path,
    address to,
    uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);

Parameters

NameTypeDescription
amountInuint256The amount of token to send
amountOutMinuint256The min amount of token to receive
pathPathThe path of the swap
toaddressThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountOutuint256Output amount of the swap

swapExactTokensForNATIVE ​

Swaps exact tokens for NATIVE while performing safety checks

solidity
function swapExactTokensForNATIVE(
    uint256 amountIn,
    uint256 amountOutMinNATIVE,
    Path memory path,
    address payable to,
    uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);

Parameters

NameTypeDescription
amountInuint256The amount of token to send
amountOutMinNATIVEuint256The min amount of NATIVE to receive
pathPathThe path of the swap
toaddress payableThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountOutuint256Output amount of the swap

swapExactNATIVEForTokens ​

Swaps exact NATIVE for tokens while performing safety checks

solidity
function swapExactNATIVEForTokens(uint256 amountOutMin, Path memory path, address to, uint256 deadline)
    external
    payable
    override
    ensure(deadline)
    verifyPathValidity(path)
    returns (uint256 amountOut);

Parameters

NameTypeDescription
amountOutMinuint256The min amount of token to receive
pathPathThe path of the swap
toaddressThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountOutuint256Output amount of the swap

swapTokensForExactTokens ​

Swaps tokens for exact tokens while performing safety checks

solidity
function swapTokensForExactTokens(
    uint256 amountOut,
    uint256 amountInMax,
    Path memory path,
    address to,
    uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256[] memory amountsIn);

Parameters

NameTypeDescription
amountOutuint256The amount of token to receive
amountInMaxuint256The max amount of token to send
pathPathThe path of the swap
toaddressThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountsInuint256[]Input amounts of the swap

swapTokensForExactNATIVE ​

Swaps tokens for exact NATIVE while performing safety checks

solidity
function swapTokensForExactNATIVE(
    uint256 amountNATIVEOut,
    uint256 amountInMax,
    Path memory path,
    address payable to,
    uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256[] memory amountsIn);

Parameters

NameTypeDescription
amountNATIVEOutuint256The amount of NATIVE to receive
amountInMaxuint256The max amount of token to send
pathPathThe path of the swap
toaddress payableThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountsInuint256[]path amounts for every step of the swap

swapNATIVEForExactTokens ​

Swaps NATIVE for exact tokens while performing safety checks

Will refund any NATIVE amount sent in excess to msg.sender

solidity
function swapNATIVEForExactTokens(uint256 amountOut, Path memory path, address to, uint256 deadline)
    external
    payable
    override
    ensure(deadline)
    verifyPathValidity(path)
    returns (uint256[] memory amountsIn);

Parameters

NameTypeDescription
amountOutuint256The amount of tokens to receive
pathPathThe path of the swap
toaddressThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountsInuint256[]path amounts for every step of the swap

swapExactTokensForTokensSupportingFeeOnTransferTokens ​

Swaps exact tokens for tokens while performing safety checks supporting for fee on transfer tokens

solidity
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
    uint256 amountIn,
    uint256 amountOutMin,
    Path memory path,
    address to,
    uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);

Parameters

NameTypeDescription
amountInuint256The amount of token to send
amountOutMinuint256The min amount of token to receive
pathPathThe path of the swap
toaddressThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountOutuint256Output amount of the swap

swapExactTokensForNATIVESupportingFeeOnTransferTokens ​

Swaps exact tokens for NATIVE while performing safety checks supporting for fee on transfer tokens

solidity
function swapExactTokensForNATIVESupportingFeeOnTransferTokens(
    uint256 amountIn,
    uint256 amountOutMinNATIVE,
    Path memory path,
    address payable to,
    uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);

Parameters

NameTypeDescription
amountInuint256The amount of token to send
amountOutMinNATIVEuint256The min amount of NATIVE to receive
pathPathThe path of the swap
toaddress payableThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountOutuint256Output amount of the swap

swapExactNATIVEForTokensSupportingFeeOnTransferTokens ​

Swaps exact NATIVE for tokens while performing safety checks supporting for fee on transfer tokens

solidity
function swapExactNATIVEForTokensSupportingFeeOnTransferTokens(
    uint256 amountOutMin,
    Path memory path,
    address to,
    uint256 deadline
) external payable override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);

Parameters

NameTypeDescription
amountOutMinuint256The min amount of token to receive
pathPathThe path of the swap
toaddressThe address of the recipient
deadlineuint256The deadline of the tx

Returns

NameTypeDescription
amountOutuint256Output amount of the swap

sweep ​

Unstuck tokens that are sent to this contract by mistake

Only callable by the factory owner

solidity
function sweep(IERC20 token, address to, uint256 amount) external override onlyFactoryOwner;

Parameters

NameTypeDescription
tokenIERC20The address of the token
toaddressThe address of the user to send back the tokens
amountuint256The amount to send

sweepLBToken ​

Unstuck LBTokens that are sent to this contract by mistake

Only callable by the factory owner

solidity
function sweepLBToken(ILBToken lbToken, address to, uint256[] calldata ids, uint256[] calldata amounts)
    external
    override
    onlyFactoryOwner;

Parameters

NameTypeDescription
lbTokenILBTokenThe address of the LBToken
toaddressThe address of the user to send back the tokens
idsuint256[]The list of token ids
amountsuint256[]The list of amounts to send

_addLiquidity ​

Helper function to add liquidity

solidity
function _addLiquidity(LiquidityParameters calldata liq, ILBPair pair)
    private
    ensure(liq.deadline)
    returns (
        uint256 amountXAdded,
        uint256 amountYAdded,
        uint256 amountXLeft,
        uint256 amountYLeft,
        uint256[] memory depositIds,
        uint256[] memory liquidityMinted
    );

Parameters

NameTypeDescription
liqLiquidityParametersThe liquidity parameter
pairILBPairLBPair where liquidity is deposited

Returns

NameTypeDescription
amountXAddeduint256Amount of token X added
amountYAddeduint256Amount of token Y added
amountXLeftuint256Amount of token X left
amountYLeftuint256Amount of token Y left
depositIdsuint256[]The list of deposit ids
liquidityMinteduint256[]The list of liquidity minted

_getAmountsIn ​

Helper function to return the amounts in

solidity
function _getAmountsIn(Version[] memory versions, address[] memory pairs, IERC20[] memory tokenPath, uint256 amountOut)
    private
    view
    returns (uint256[] memory amountsIn);

Parameters

NameTypeDescription
versionsVersion[]The list of versions (V1, V2, V2_1 or V2_2)
pairsaddress[]The list of pairs
tokenPathIERC20[]The swap path
amountOutuint256The amount out

Returns

NameTypeDescription
amountsInuint256[]The list of amounts in

_removeLiquidity ​

Helper function to remove liquidity

solidity
function _removeLiquidity(
    ILBPair pair,
    uint256 amountXMin,
    uint256 amountYMin,
    uint256[] memory ids,
    uint256[] memory amounts,
    address to
) private returns (uint256 amountX, uint256 amountY);

Parameters

NameTypeDescription
pairILBPairThe address of the LBPair
amountXMinuint256The min amount to receive of token X
amountYMinuint256The min amount to receive of token Y
idsuint256[]The list of ids to burn
amountsuint256[]The list of amounts to burn of each id in _ids
toaddressThe address of the recipient

Returns

NameTypeDescription
amountXuint256The amount of token X sent by the pair
amountYuint256The amount of token Y sent by the pair

_swapExactTokensForTokens ​

Helper function to swap exact tokens for tokens

solidity
function _swapExactTokensForTokens(
    uint256 amountIn,
    address[] memory pairs,
    Version[] memory versions,
    IERC20[] memory tokenPath,
    address to
) private returns (uint256 amountOut);

Parameters

NameTypeDescription
amountInuint256The amount of token sent
pairsaddress[]The list of pairs
versionsVersion[]The list of versions (V1, V2, V2_1 or V2_2)
tokenPathIERC20[]The swap path using the binSteps following pairBinSteps
toaddressThe address of the recipient

Returns

NameTypeDescription
amountOutuint256The amount of token sent to to

_swapTokensForExactTokens ​

Helper function to swap tokens for exact tokens

solidity
function _swapTokensForExactTokens(
    address[] memory pairs,
    Version[] memory versions,
    IERC20[] memory tokenPath,
    uint256[] memory amountsIn,
    address to
) private returns (uint256 amountOut);

Parameters

NameTypeDescription
pairsaddress[]The array of pairs
versionsVersion[]The list of versions (V1, V2, V2_1 or V2_2)
tokenPathIERC20[]The swap path using the binSteps following pairBinSteps
amountsInuint256[]The list of amounts in
toaddressThe address of the recipient

Returns

NameTypeDescription
amountOutuint256The amount of token sent to to

_swapSupportingFeeOnTransferTokens ​

Helper function to swap exact tokens supporting for fee on transfer tokens

solidity
function _swapSupportingFeeOnTransferTokens(
    address[] memory pairs,
    Version[] memory versions,
    IERC20[] memory tokenPath,
    address to
) private;

Parameters

NameTypeDescription
pairsaddress[]The list of pairs
versionsVersion[]The list of versions (V1, V2, V2_1 or V2_2)
tokenPathIERC20[]The swap path using the binSteps following pairBinSteps
toaddressThe address of the recipient

_getLBPairInformation ​

Helper function to return the address of the LBPair

Revert if the pair is not created yet

solidity
function _getLBPairInformation(IERC20 tokenX, IERC20 tokenY, uint256 binStep, Version version)
    private
    view
    returns (address lbPair);

Parameters

NameTypeDescription
tokenXIERC20The address of the tokenX
tokenYIERC20The address of the tokenY
binStepuint256The bin step of the LBPair
versionVersionThe version of the LBPair

Returns

NameTypeDescription
lbPairaddressThe address of the LBPair

_getPair ​

Helper function to return the address of the pair (v1 or v2, according to binStep)

Revert if the pair is not created yet

solidity
function _getPair(IERC20 tokenX, IERC20 tokenY, uint256 binStep, Version version) private view returns (address pair);

Parameters

NameTypeDescription
tokenXIERC20The address of the tokenX
tokenYIERC20The address of the tokenY
binStepuint256The bin step of the LBPair
versionVersionThe version of the LBPair

Returns

NameTypeDescription
pairaddressThe address of the pair of binStep binStep

_getPairs ​

Helper function to return a list of pairs

solidity
function _getPairs(uint256[] memory pairBinSteps, Version[] memory versions, IERC20[] memory tokenPath)
    private
    view
    returns (address[] memory pairs);

Parameters

NameTypeDescription
pairBinStepsuint256[]The list of bin steps
versionsVersion[]The list of versions (V1, V2, V2_1 or V2_2)
tokenPathIERC20[]The swap path using the binSteps following pairBinSteps

Returns

NameTypeDescription
pairsaddress[]The list of pairs

_safeTransfer ​

Helper function to transfer tokens to to

solidity
function _safeTransfer(IERC20 token, address to, uint256 amount) private;

Parameters

NameTypeDescription
tokenIERC20The address of the token
toaddressThe address of the recipient
amountuint256The amount to send

_safeTransferFrom ​

Helper function to transfer tokens from from to to

solidity
function _safeTransferFrom(IERC20 token, address from, address to, uint256 amount) private;

Parameters

NameTypeDescription
tokenIERC20The address of the token
fromaddressThe address of the sender
toaddressThe address of the recipient
amountuint256The amount to send

_safeTransferNative ​

Helper function to transfer NATIVE to to

solidity
function _safeTransferNative(address to, uint256 amount) private;

Parameters

NameTypeDescription
toaddressThe address of the recipient
amountuint256The amount to send

_wNativeDepositAndTransfer ​

Helper function to deposit and transfer WNative to to

solidity
function _wNativeDepositAndTransfer(address to, uint256 amount) private;

Parameters

NameTypeDescription
toaddressThe address of the recipient
amountuint256The amount to deposit and transfer

_wNativeWithdrawAndTransfer ​

Helper function to withdraw and transfer WNative to to

solidity
function _wNativeWithdrawAndTransfer(address to, uint256 amount) private;

Parameters

NameTypeDescription
toaddressThe address of the recipient
amountuint256The amount to withdraw and transfer