LBRouter
Inherits:ILBRouter
Author: Trader Joe
Main contract to interact with to swap and manage liquidity on Joe V2 exchange.
State Variables
_factory2_2
ILBFactory private immutable _factory2_2;
_factory2_1
ILBFactory private immutable _factory2_1;
_factoryV1
IJoeFactory private immutable _factoryV1;
_legacyFactory
ILBLegacyFactory private immutable _legacyFactory;
_legacyRouter
ILBLegacyRouter private immutable _legacyRouter;
_wnative
IWNATIVE private immutable _wnative;
Functions
onlyFactoryOwner
modifier onlyFactoryOwner();
ensure
modifier ensure(uint256 deadline);
verifyPathValidity
modifier verifyPathValidity(Path memory path);
constructor
Constructor
constructor(
ILBFactory factory2_2,
IJoeFactory factoryV1,
ILBLegacyFactory legacyFactory,
ILBLegacyRouter legacyRouter,
ILBFactory factory2_1,
IWNATIVE wnative
);
Parameters
factory2_2
ILBFactory
Address of Joe V2.2 factory
factoryV1
IJoeFactory
Address of Joe V1 factory
legacyFactory
ILBLegacyFactory
Address of Joe V2 factory
legacyRouter
ILBLegacyRouter
Address of Joe V2 router
factory2_1
ILBFactory
Address of Joe V2.1 factory
wnative
IWNATIVE
Address of WNATIVE
receive
Receive function that only accept NATIVE from the WNATIVE contract
receive() external payable;
getFactory
View function to get the factory V2.1 address
function getFactory() external view override returns (ILBFactory lbFactory);
Returns
lbFactory
ILBFactory
The address of the factory V2.1
getFactoryV2_1
View function to get the factory V2.1 address
function getFactoryV2_1() external view override returns (ILBFactory lbFactory);
Returns
lbFactory
ILBFactory
The address of the factory V2.1
getLegacyFactory
View function to get the factory V2 address
function getLegacyFactory() external view override returns (ILBLegacyFactory legacyLBfactory);
Returns
legacyLBfactory
ILBLegacyFactory
The address of the factory V2
getV1Factory
View function to get the factory V1 address
function getV1Factory() external view override returns (IJoeFactory factoryV1);
Returns
factoryV1
IJoeFactory
The address of the factory V1
getLegacyRouter
View function to get the router V2 address
function getLegacyRouter() external view override returns (ILBLegacyRouter legacyRouter);
Returns
legacyRouter
ILBLegacyRouter
The address of the router V2
getWNATIVE
View function to get the WNATIVE address
function getWNATIVE() external view override returns (IWNATIVE wnative);
Returns
wnative
IWNATIVE
The 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
function getIdFromPrice(ILBPair pair, uint256 price) external view override returns (uint24);
Parameters
pair
ILBPair
The address of the LBPair
price
uint256
The price of y per x (multiplied by 1e36)
Returns
<none>
uint24
The id corresponding to this price
getPriceFromId
Returns the price corresponding to the inputted id
function getPriceFromId(ILBPair pair, uint24 id) external view override returns (uint256);
Parameters
pair
ILBPair
The address of the LBPair
id
uint24
The id
Returns
<none>
uint256
The price corresponding to this id
getSwapIn
Simulate a swap in
function getSwapIn(ILBPair pair, uint128 amountOut, bool swapForY)
public
view
override
returns (uint128 amountIn, uint128 amountOutLeft, uint128 fee);
Parameters
pair
ILBPair
The address of the LBPair
amountOut
uint128
The amount of token to receive
swapForY
bool
Whether you swap X for Y (true), or Y for X (false)
Returns
amountIn
uint128
The amount of token to send in order to receive amountOut token
amountOutLeft
uint128
The amount of token Out that can't be returned due to a lack of liquidity
fee
uint128
The amount of fees paid in token sent
getSwapOut
Simulate a swap out
function getSwapOut(ILBPair pair, uint128 amountIn, bool swapForY)
external
view
override
returns (uint128 amountInLeft, uint128 amountOut, uint128 fee);
Parameters
pair
ILBPair
The address of the LBPair
amountIn
uint128
The amount of token sent
swapForY
bool
Whether you swap X for Y (true), or Y for X (false)
Returns
amountInLeft
uint128
The amount of token In that can't be swapped due to a lack of liquidity
amountOut
uint128
The amount of token received if amountIn tokenX are sent
fee
uint128
The amount of fees paid in token sent
createLBPair
Create a liquidity bin LBPair for tokenX and tokenY using the factory
function createLBPair(IERC20 tokenX, IERC20 tokenY, uint24 activeId, uint16 binStep)
external
override
returns (ILBPair pair);
Parameters
tokenX
IERC20
The address of the first token
tokenY
IERC20
The address of the second token
activeId
uint24
The active id of the pair
binStep
uint16
The bin step in basis point, used to calculate log(1 + binStep)
Returns
pair
ILBPair
The address of the newly created LBPair
addLiquidity
Add liquidity while performing safety checks
This function is compliant with fee on transfer tokens
function addLiquidity(LiquidityParameters calldata liquidityParameters)
external
override
returns (
uint256 amountXAdded,
uint256 amountYAdded,
uint256 amountXLeft,
uint256 amountYLeft,
uint256[] memory depositIds,
uint256[] memory liquidityMinted
);
Parameters
liquidityParameters
LiquidityParameters
The liquidity parameters
Returns
amountXAdded
uint256
The amount of token X added
amountYAdded
uint256
The amount of token Y added
amountXLeft
uint256
The amount of token X left (sent back to liquidityParameters.refundTo)
amountYLeft
uint256
The amount of token Y left (sent back to liquidityParameters.refundTo)
depositIds
uint256[]
The ids of the deposits
liquidityMinted
uint256[]
The amount of liquidity minted
addLiquidityNATIVE
Add liquidity with NATIVE while performing safety checks
This function is compliant with fee on transfer tokens
function addLiquidityNATIVE(LiquidityParameters calldata liquidityParameters)
external
payable
override
returns (
uint256 amountXAdded,
uint256 amountYAdded,
uint256 amountXLeft,
uint256 amountYLeft,
uint256[] memory depositIds,
uint256[] memory liquidityMinted
);
Parameters
liquidityParameters
LiquidityParameters
The liquidity parameters
Returns
amountXAdded
uint256
The amount of token X added
amountYAdded
uint256
The amount of token Y added
amountXLeft
uint256
The amount of token X left (sent back to liquidityParameters.refundTo)
amountYLeft
uint256
The amount of token Y left (sent back to liquidityParameters.refundTo)
depositIds
uint256[]
The ids of the deposits
liquidityMinted
uint256[]
The amount of liquidity minted
removeLiquidity
Remove liquidity while performing safety checks
This function is compliant with fee on transfer tokens
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
tokenX
IERC20
The address of token X
tokenY
IERC20
The address of token Y
binStep
uint16
The bin step of the LBPair
amountXMin
uint256
The min amount to receive of token X
amountYMin
uint256
The min amount to receive of token Y
ids
uint256[]
The list of ids to burn
amounts
uint256[]
The list of amounts to burn of each id in _ids
to
address
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountX
uint256
Amount of token X returned
amountY
uint256
Amount 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.
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
token
IERC20
The address of token
binStep
uint16
The bin step of the LBPair
amountTokenMin
uint256
The min amount to receive of token
amountNATIVEMin
uint256
The min amount to receive of NATIVE
ids
uint256[]
The list of ids to burn
amounts
uint256[]
The list of amounts to burn of each id in _ids
to
address payable
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountToken
uint256
Amount of token returned
amountNATIVE
uint256
Amount of NATIVE returned
swapExactTokensForTokens
Swaps exact tokens for tokens while performing safety checks
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
Path memory path,
address to,
uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);
Parameters
amountIn
uint256
The amount of token to send
amountOutMin
uint256
The min amount of token to receive
path
Path
The path of the swap
to
address
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountOut
uint256
Output amount of the swap
swapExactTokensForNATIVE
Swaps exact tokens for NATIVE while performing safety checks
function swapExactTokensForNATIVE(
uint256 amountIn,
uint256 amountOutMinNATIVE,
Path memory path,
address payable to,
uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);
Parameters
amountIn
uint256
The amount of token to send
amountOutMinNATIVE
uint256
The min amount of NATIVE to receive
path
Path
The path of the swap
to
address payable
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountOut
uint256
Output amount of the swap
swapExactNATIVEForTokens
Swaps exact NATIVE for tokens while performing safety checks
function swapExactNATIVEForTokens(uint256 amountOutMin, Path memory path, address to, uint256 deadline)
external
payable
override
ensure(deadline)
verifyPathValidity(path)
returns (uint256 amountOut);
Parameters
amountOutMin
uint256
The min amount of token to receive
path
Path
The path of the swap
to
address
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountOut
uint256
Output amount of the swap
swapTokensForExactTokens
Swaps tokens for exact tokens while performing safety checks
function swapTokensForExactTokens(
uint256 amountOut,
uint256 amountInMax,
Path memory path,
address to,
uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256[] memory amountsIn);
Parameters
amountOut
uint256
The amount of token to receive
amountInMax
uint256
The max amount of token to send
path
Path
The path of the swap
to
address
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountsIn
uint256[]
Input amounts of the swap
swapTokensForExactNATIVE
Swaps tokens for exact NATIVE while performing safety checks
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
amountNATIVEOut
uint256
The amount of NATIVE to receive
amountInMax
uint256
The max amount of token to send
path
Path
The path of the swap
to
address payable
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountsIn
uint256[]
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
function swapNATIVEForExactTokens(uint256 amountOut, Path memory path, address to, uint256 deadline)
external
payable
override
ensure(deadline)
verifyPathValidity(path)
returns (uint256[] memory amountsIn);
Parameters
amountOut
uint256
The amount of tokens to receive
path
Path
The path of the swap
to
address
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountsIn
uint256[]
path amounts for every step of the swap
swapExactTokensForTokensSupportingFeeOnTransferTokens
Swaps exact tokens for tokens while performing safety checks supporting for fee on transfer tokens
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
Path memory path,
address to,
uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);
Parameters
amountIn
uint256
The amount of token to send
amountOutMin
uint256
The min amount of token to receive
path
Path
The path of the swap
to
address
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountOut
uint256
Output amount of the swap
swapExactTokensForNATIVESupportingFeeOnTransferTokens
Swaps exact tokens for NATIVE while performing safety checks supporting for fee on transfer tokens
function swapExactTokensForNATIVESupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMinNATIVE,
Path memory path,
address payable to,
uint256 deadline
) external override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);
Parameters
amountIn
uint256
The amount of token to send
amountOutMinNATIVE
uint256
The min amount of NATIVE to receive
path
Path
The path of the swap
to
address payable
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountOut
uint256
Output amount of the swap
swapExactNATIVEForTokensSupportingFeeOnTransferTokens
Swaps exact NATIVE for tokens while performing safety checks supporting for fee on transfer tokens
function swapExactNATIVEForTokensSupportingFeeOnTransferTokens(
uint256 amountOutMin,
Path memory path,
address to,
uint256 deadline
) external payable override ensure(deadline) verifyPathValidity(path) returns (uint256 amountOut);
Parameters
amountOutMin
uint256
The min amount of token to receive
path
Path
The path of the swap
to
address
The address of the recipient
deadline
uint256
The deadline of the tx
Returns
amountOut
uint256
Output amount of the swap
sweep
Unstuck tokens that are sent to this contract by mistake
Only callable by the factory owner
function sweep(IERC20 token, address to, uint256 amount) external override onlyFactoryOwner;
Parameters
token
IERC20
The address of the token
to
address
The address of the user to send back the tokens
amount
uint256
The amount to send
sweepLBToken
Unstuck LBTokens that are sent to this contract by mistake
Only callable by the factory owner
function sweepLBToken(ILBToken lbToken, address to, uint256[] calldata ids, uint256[] calldata amounts)
external
override
onlyFactoryOwner;
Parameters
lbToken
ILBToken
The address of the LBToken
to
address
The address of the user to send back the tokens
ids
uint256[]
The list of token ids
amounts
uint256[]
The list of amounts to send
_addLiquidity
Helper function to add liquidity
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
liq
LiquidityParameters
The liquidity parameter
pair
ILBPair
LBPair where liquidity is deposited
Returns
amountXAdded
uint256
Amount of token X added
amountYAdded
uint256
Amount of token Y added
amountXLeft
uint256
Amount of token X left
amountYLeft
uint256
Amount of token Y left
depositIds
uint256[]
The list of deposit ids
liquidityMinted
uint256[]
The list of liquidity minted
_getAmountsIn
Helper function to return the amounts in
function _getAmountsIn(Version[] memory versions, address[] memory pairs, IERC20[] memory tokenPath, uint256 amountOut)
private
view
returns (uint256[] memory amountsIn);
Parameters
versions
Version[]
The list of versions (V1, V2, V2_1 or V2_2)
pairs
address[]
The list of pairs
tokenPath
IERC20[]
The swap path
amountOut
uint256
The amount out
Returns
amountsIn
uint256[]
The list of amounts in
_removeLiquidity
Helper function to remove liquidity
function _removeLiquidity(
ILBPair pair,
uint256 amountXMin,
uint256 amountYMin,
uint256[] memory ids,
uint256[] memory amounts,
address to
) private returns (uint256 amountX, uint256 amountY);
Parameters
pair
ILBPair
The address of the LBPair
amountXMin
uint256
The min amount to receive of token X
amountYMin
uint256
The min amount to receive of token Y
ids
uint256[]
The list of ids to burn
amounts
uint256[]
The list of amounts to burn of each id in _ids
to
address
The address of the recipient
Returns
amountX
uint256
The amount of token X sent by the pair
amountY
uint256
The amount of token Y sent by the pair
_swapExactTokensForTokens
Helper function to swap exact tokens for tokens
function _swapExactTokensForTokens(
uint256 amountIn,
address[] memory pairs,
Version[] memory versions,
IERC20[] memory tokenPath,
address to
) private returns (uint256 amountOut);
Parameters
amountIn
uint256
The amount of token sent
pairs
address[]
The list of pairs
versions
Version[]
The list of versions (V1, V2, V2_1 or V2_2)
tokenPath
IERC20[]
The swap path using the binSteps following pairBinSteps
to
address
The address of the recipient
Returns
amountOut
uint256
The amount of token sent to to
_swapTokensForExactTokens
Helper function to swap tokens for exact tokens
function _swapTokensForExactTokens(
address[] memory pairs,
Version[] memory versions,
IERC20[] memory tokenPath,
uint256[] memory amountsIn,
address to
) private returns (uint256 amountOut);
Parameters
pairs
address[]
The array of pairs
versions
Version[]
The list of versions (V1, V2, V2_1 or V2_2)
tokenPath
IERC20[]
The swap path using the binSteps following pairBinSteps
amountsIn
uint256[]
The list of amounts in
to
address
The address of the recipient
Returns
amountOut
uint256
The amount of token sent to to
_swapSupportingFeeOnTransferTokens
Helper function to swap exact tokens supporting for fee on transfer tokens
function _swapSupportingFeeOnTransferTokens(
address[] memory pairs,
Version[] memory versions,
IERC20[] memory tokenPath,
address to
) private;
Parameters
pairs
address[]
The list of pairs
versions
Version[]
The list of versions (V1, V2, V2_1 or V2_2)
tokenPath
IERC20[]
The swap path using the binSteps following pairBinSteps
to
address
The address of the recipient
_getLBPairInformation
Helper function to return the address of the LBPair
Revert if the pair is not created yet
function _getLBPairInformation(IERC20 tokenX, IERC20 tokenY, uint256 binStep, Version version)
private
view
returns (address lbPair);
Parameters
tokenX
IERC20
The address of the tokenX
tokenY
IERC20
The address of the tokenY
binStep
uint256
The bin step of the LBPair
version
Version
The version of the LBPair
Returns
lbPair
address
The 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
function _getPair(IERC20 tokenX, IERC20 tokenY, uint256 binStep, Version version) private view returns (address pair);
Parameters
tokenX
IERC20
The address of the tokenX
tokenY
IERC20
The address of the tokenY
binStep
uint256
The bin step of the LBPair
version
Version
The version of the LBPair
Returns
pair
address
The address of the pair of binStep binStep
_getPairs
Helper function to return a list of pairs
function _getPairs(uint256[] memory pairBinSteps, Version[] memory versions, IERC20[] memory tokenPath)
private
view
returns (address[] memory pairs);
Parameters
pairBinSteps
uint256[]
The list of bin steps
versions
Version[]
The list of versions (V1, V2, V2_1 or V2_2)
tokenPath
IERC20[]
The swap path using the binSteps following pairBinSteps
Returns
pairs
address[]
The list of pairs
_safeTransfer
Helper function to transfer tokens to to
function _safeTransfer(IERC20 token, address to, uint256 amount) private;
Parameters
token
IERC20
The address of the token
to
address
The address of the recipient
amount
uint256
The amount to send
_safeTransferFrom
Helper function to transfer tokens from from
to to
function _safeTransferFrom(IERC20 token, address from, address to, uint256 amount) private;
Parameters
token
IERC20
The address of the token
from
address
The address of the sender
to
address
The address of the recipient
amount
uint256
The amount to send
_safeTransferNative
Helper function to transfer NATIVE to to
function _safeTransferNative(address to, uint256 amount) private;
Parameters
to
address
The address of the recipient
amount
uint256
The amount to send
_wNativeDepositAndTransfer
Helper function to deposit and transfer WNative to to
function _wNativeDepositAndTransfer(address to, uint256 amount) private;
Parameters
to
address
The address of the recipient
amount
uint256
The amount to deposit and transfer
_wNativeWithdrawAndTransfer
Helper function to withdraw and transfer WNative to to
function _wNativeWithdrawAndTransfer(address to, uint256 amount) private;
Parameters
to
address
The address of the recipient
amount
uint256
The amount to withdraw and transfer
Last updated