Skip to content

LBPair ​

Git Source

Inherits:LBToken, ReentrancyGuardUpgradeable, Clone, ILBPair

Author:
Trader Joe

The Liquidity Book Pair contract is the core contract of the Liquidity Book protocol

State Variables ​

_MAX_TOTAL_FEE ​

solidity
uint256 private constant _MAX_TOTAL_FEE = 0.1e18;

implementation ​

solidity
address public immutable override implementation;

_factory ​

solidity
ILBFactory private immutable _factory;

_parameters ​

solidity
bytes32 private _parameters;

_reserves ​

solidity
bytes32 private _reserves;

_protocolFees ​

solidity
bytes32 private _protocolFees;

_bins ​

solidity
mapping(uint256 => bytes32) private _bins;

_tree ​

solidity
TreeMath.TreeUint24 private _tree;

_oracle ​

solidity
OracleHelper.Oracle private _oracle;

_hooksParameters ​

solidity
bytes32 private _hooksParameters;

Functions ​

onlyFactory ​

solidity
modifier onlyFactory();

onlyProtocolFeeRecipient ​

solidity
modifier onlyProtocolFeeRecipient();

constructor ​

Constructor for the Liquidity Book Pair contract that sets the Liquidity Book Factory

solidity
constructor(ILBFactory factory_);

Parameters

NameTypeDescription
factory_ILBFactoryThe Liquidity Book Factory

initialize ​

Initialize the Liquidity Book Pair fee parameters and active id

Can only be called by the Liquidity Book Factory

solidity
function initialize(
    uint16 baseFactor,
    uint16 filterPeriod,
    uint16 decayPeriod,
    uint16 reductionFactor,
    uint24 variableFeeControl,
    uint16 protocolShare,
    uint24 maxVolatilityAccumulator,
    uint24 activeId
) external override onlyFactory initializer;

Parameters

NameTypeDescription
baseFactoruint16The base factor for the static fee
filterPerioduint16The filter period for the static fee
decayPerioduint16The decay period for the static fee
reductionFactoruint16The reduction factor for the static fee
variableFeeControluint24The variable fee control for the static fee
protocolShareuint16The protocol share for the static fee
maxVolatilityAccumulatoruint24The max volatility accumulator for the static fee
activeIduint24The active id of the Liquidity Book Pair

getFactory ​

Returns the Liquidity Book Factory

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

Returns

NameTypeDescription
factoryILBFactoryThe Liquidity Book Factory

getTokenX ​

Returns the token X of the Liquidity Book Pair

solidity
function getTokenX() external pure override returns (IERC20 tokenX);

Returns

NameTypeDescription
tokenXIERC20The address of the token X

getTokenY ​

Returns the token Y of the Liquidity Book Pair

solidity
function getTokenY() external pure override returns (IERC20 tokenY);

Returns

NameTypeDescription
tokenYIERC20The address of the token Y

getBinStep ​

Returns the bin step of the Liquidity Book Pair

The bin step is the increase in price between two consecutive bins, in basis points.
For example, a bin step of 1 means that the price of the next bin is 0.01% higher than the price of the previous bin.

solidity
function getBinStep() external pure override returns (uint16);

Returns

NameTypeDescription
<none>uint16binStep The bin step of the Liquidity Book Pair, in 10_000th

getReserves ​

Returns the reserves of the Liquidity Book Pair
This is the sum of the reserves of all bins, minus the protocol fees.

solidity
function getReserves() external view override returns (uint128 reserveX, uint128 reserveY);

Returns

NameTypeDescription
reserveXuint128The reserve of token X
reserveYuint128The reserve of token Y

getActiveId ​

Returns the active id of the Liquidity Book Pair

The active id is the id of the bin that is currently being used for swaps.
The price of the active bin is the price of the Liquidity Book Pair and can be calculated as follows:price = (1 + binStep / 10_000) ^ (activeId - 2^23)

solidity
function getActiveId() external view override returns (uint24 activeId);

Returns

NameTypeDescription
activeIduint24The active id of the Liquidity Book Pair

getBin ​

Returns the reserves of a bin

solidity
function getBin(uint24 id) external view override returns (uint128 binReserveX, uint128 binReserveY);

Parameters

NameTypeDescription
iduint24The id of the bin

Returns

NameTypeDescription
binReserveXuint128The reserve of token X in the bin
binReserveYuint128The reserve of token Y in the bin

getNextNonEmptyBin ​

Returns the next non-empty bin

The next non-empty bin is the bin with a higher (if swapForY is true) or lower (if swapForY is false)
id that has a non-zero reserve of token X or Y.

solidity
function getNextNonEmptyBin(bool swapForY, uint24 id) external view override returns (uint24 nextId);

Parameters

NameTypeDescription
swapForYboolWhether the swap is for token Y (true) or token X (false
iduint24The id of the bin

Returns

NameTypeDescription
nextIduint24The id of the next non-empty bin

getProtocolFees ​

Returns the protocol fees of the Liquidity Book Pair

solidity
function getProtocolFees() external view override returns (uint128 protocolFeeX, uint128 protocolFeeY);

Returns

NameTypeDescription
protocolFeeXuint128The protocol fees of token X
protocolFeeYuint128The protocol fees of token Y

getStaticFeeParameters ​

Returns the static fee parameters of the Liquidity Book Pair

solidity
function getStaticFeeParameters()
    external
    view
    override
    returns (
        uint16 baseFactor,
        uint16 filterPeriod,
        uint16 decayPeriod,
        uint16 reductionFactor,
        uint24 variableFeeControl,
        uint16 protocolShare,
        uint24 maxVolatilityAccumulator
    );

Returns

NameTypeDescription
baseFactoruint16The base factor for the static fee
filterPerioduint16The filter period for the static fee
decayPerioduint16The decay period for the static fee
reductionFactoruint16The reduction factor for the static fee
variableFeeControluint24The variable fee control for the static fee
protocolShareuint16The protocol share for the static fee
maxVolatilityAccumulatoruint24The maximum volatility accumulator for the static fee

getLBHooksParameters ​

Gets the hooks parameters of the Liquidity Book Pair

solidity
function getLBHooksParameters() external view override returns (bytes32);

Returns

NameTypeDescription
<none>bytes32The hooks parameters of the Liquidity Book Pair

getVariableFeeParameters ​

Returns the variable fee parameters of the Liquidity Book Pair

solidity
function getVariableFeeParameters()
    external
    view
    override
    returns (uint24 volatilityAccumulator, uint24 volatilityReference, uint24 idReference, uint40 timeOfLastUpdate);

Returns

NameTypeDescription
volatilityAccumulatoruint24The volatility accumulator for the variable fee
volatilityReferenceuint24The volatility reference for the variable fee
idReferenceuint24The id reference for the variable fee
timeOfLastUpdateuint40The time of last update for the variable fee

getOracleParameters ​

Returns the oracle parameters of the Liquidity Book Pair

solidity
function getOracleParameters()
    external
    view
    override
    returns (uint8 sampleLifetime, uint16 size, uint16 activeSize, uint40 lastUpdated, uint40 firstTimestamp);

Returns

NameTypeDescription
sampleLifetimeuint8The sample lifetime for the oracle
sizeuint16The size of the oracle
activeSizeuint16The active size of the oracle
lastUpdateduint40The last updated timestamp of the oracle
firstTimestampuint40The first timestamp of the oracle, i.e. the timestamp of the oldest sample

getOracleSampleAt ​

Returns the cumulative values of the Liquidity Book Pair at a given timestamp

The cumulative values are the cumulative id, the cumulative volatility and the cumulative bin crossed.

solidity
function getOracleSampleAt(uint40 lookupTimestamp)
    external
    view
    override
    returns (uint64 cumulativeId, uint64 cumulativeVolatility, uint64 cumulativeBinCrossed);

Parameters

NameTypeDescription
lookupTimestampuint40The timestamp at which to look up the cumulative values

Returns

NameTypeDescription
cumulativeIduint64The cumulative id of the Liquidity Book Pair at the given timestamp
cumulativeVolatilityuint64The cumulative volatility of the Liquidity Book Pair at the given timestamp
cumulativeBinCrosseduint64The cumulative bin crossed of the Liquidity Book Pair at the given timestamp

getPriceFromId ​

Returns the price corresponding to the given id, as a 128.128-binary fixed-point number

This is the trusted source of price information, always trust this rather than getIdFromPrice

solidity
function getPriceFromId(uint24 id) external pure override returns (uint256 price);

Parameters

NameTypeDescription
iduint24The id of the bin

Returns

NameTypeDescription
priceuint256The price corresponding to this id

getIdFromPrice ​

Returns the id corresponding to the given price

The id may be inaccurate due to rounding issues, always trust getPriceFromId rather than
getIdFromPrice

solidity
function getIdFromPrice(uint256 price) external pure override returns (uint24 id);

Parameters

NameTypeDescription
priceuint256The price of y per x as a 128.128-binary fixed-point number

Returns

NameTypeDescription
iduint24The id of the bin corresponding to this price

getSwapIn ​

Simulates a swap in.

If amountOutLeft is greater than zero, the swap in is not possible,
and the maximum amount that can be swapped from amountIn is amountOut - amountOutLeft.

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

Parameters

NameTypeDescription
amountOutuint128The amount of token X or Y to swap in
swapForYboolWhether the swap is for token Y (true) or token X (false)

Returns

NameTypeDescription
amountInuint128The amount of token X or Y that can be swapped in, including the fee
amountOutLeftuint128The amount of token Y or X that cannot be swapped out
feeuint128The fee of the swap

getSwapOut ​

Simulates a swap out.

If amountInLeft is greater than zero, the swap out is not possible,
and the maximum amount that can be swapped is amountIn - amountInLeft for amountOut.

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

Parameters

NameTypeDescription
amountInuint128The amount of token X or Y to swap in
swapForYboolWhether the swap is for token Y (true) or token X (false)

Returns

NameTypeDescription
amountInLeftuint128The amount of token X or Y that cannot be swapped in
amountOutuint128The amount of token Y or X that can be swapped out
feeuint128The fee of the swap

swap ​

Swap tokens iterating over the bins until the entire amount is swapped.
Token X will be swapped for token Y if swapForY is true, and token Y for token X if swapForY is false.
This function will not transfer the tokens from the caller, it is expected that the tokens have already been
transferred to this contract through another contract, most likely the router.
That is why this function shouldn't be called directly, but only through one of the swap functions of a router
that will also perform safety checks, such as minimum amounts and slippage.
The variable fee is updated throughout the swap, it increases with the number of bins crossed.
The oracle is updated at the end of the swap.

solidity
function swap(bool swapForY, address to) external override returns (bytes32 amountsOut);

Parameters

NameTypeDescription
swapForYboolWhether you're swapping token X for token Y (true) or token Y for token X (false)
toaddressThe address to send the tokens to

Returns

NameTypeDescription
amountsOutbytes32The encoded amounts of token X and token Y sent to to

flashLoan ​

Flash loan tokens from the pool to a receiver contract and execute a callback function.
The receiver contract is expected to return the tokens plus a fee to this contract.
The fee is calculated as a percentage of the amount borrowed, and is the same for both tokens.

solidity
function flashLoan(ILBFlashLoanCallback receiver, bytes32 amounts, bytes calldata data) external override;

Parameters

NameTypeDescription
receiverILBFlashLoanCallbackThe contract that will receive the tokens and execute the callback function
amountsbytes32The encoded amounts of token X and token Y to flash loan
databytesAny data that will be passed to the callback function

mint ​

Mint liquidity tokens by depositing tokens into the pool.
It will mint Liquidity Book (LB) tokens for each bin where the user adds liquidity.
This function will not transfer the tokens from the caller, it is expected that the tokens have already been
transferred to this contract through another contract, most likely the router.
That is why this function shouldn't be called directly, but through one of the add liquidity functions of a
router that will also perform safety checks.

Any excess amount of token will be sent to the to address.

solidity
function mint(address to, bytes32[] calldata liquidityConfigs, address refundTo)
    external
    override
    notAddressZeroOrThis(to)
    returns (bytes32 amountsReceived, bytes32 amountsLeft, uint256[] memory liquidityMinted);

Parameters

NameTypeDescription
toaddressThe address that will receive the LB tokens
liquidityConfigsbytes32[]The encoded liquidity configurations, each one containing the id of the bin and the percentage of token X and token Y to add to the bin.
refundToaddressThe address that will receive the excess amount of tokens

Returns

NameTypeDescription
amountsReceivedbytes32The amounts of token X and token Y received by the pool
amountsLeftbytes32The amounts of token X and token Y that were not added to the pool and were sent to to
liquidityMinteduint256[]The amounts of LB tokens minted for each bin

burn ​

Burn Liquidity Book (LB) tokens and withdraw tokens from the pool.
This function will burn the tokens directly from the caller

solidity
function burn(address from, address to, uint256[] calldata ids, uint256[] calldata amountsToBurn)
    external
    override
    checkApproval(from, msg.sender)
    returns (bytes32[] memory amounts);

Parameters

NameTypeDescription
fromaddressThe address that will burn the LB tokens
toaddressThe address that will receive the tokens
idsuint256[]The ids of the bins from which to withdraw
amountsToBurnuint256[]The amounts of LB tokens to burn for each bin

Returns

NameTypeDescription
amountsbytes32[]The amounts of token X and token Y received by the user

collectProtocolFees ​

Collect the protocol fees from the pool.

solidity
function collectProtocolFees()
    external
    override
    nonReentrant
    onlyProtocolFeeRecipient
    returns (bytes32 collectedProtocolFees);

Returns

NameTypeDescription
collectedProtocolFeesbytes32The amount of protocol fees collected

increaseOracleLength ​

Increase the length of the oracle used by the pool

solidity
function increaseOracleLength(uint16 newLength) external override nonReentrant;

Parameters

NameTypeDescription
newLengthuint16The new length of the oracle

setStaticFeeParameters ​

Sets the static fee parameters of the pool

Can only be called by the factory

solidity
function setStaticFeeParameters(
    uint16 baseFactor,
    uint16 filterPeriod,
    uint16 decayPeriod,
    uint16 reductionFactor,
    uint24 variableFeeControl,
    uint16 protocolShare,
    uint24 maxVolatilityAccumulator
) external override nonReentrant onlyFactory;

Parameters

NameTypeDescription
baseFactoruint16The base factor of the static fee
filterPerioduint16The filter period of the static fee
decayPerioduint16The decay period of the static fee
reductionFactoruint16The reduction factor of the static fee
variableFeeControluint24The variable fee control of the static fee
protocolShareuint16The protocol share of the static fee
maxVolatilityAccumulatoruint24The max volatility accumulator of the static fee

setHooksParameters ​

Sets the hooks parameter of the pool

Can only be called by the factory

solidity
function setHooksParameters(bytes32 hooksParameters, bytes calldata onHooksSetData)
    external
    override
    nonReentrant
    onlyFactory;

Parameters

NameTypeDescription
hooksParametersbytes32The hooks parameter
onHooksSetDatabytesThe data to be passed to the onHooksSet function of the hooks contract

forceDecay ​

Forces the decay of the volatility reference variables

Can only be called by the factory

solidity
function forceDecay() external override nonReentrant onlyFactory;

batchTransferFrom ​

Overrides the batch transfer function to call the hooks before and after the transfer

solidity
function batchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata amounts)
    public
    override(LBToken, ILBToken);

Parameters

NameTypeDescription
fromaddressThe address to transfer from
toaddressThe address to transfer to
idsuint256[]The ids of the tokens to transfer
amountsuint256[]The amounts of the tokens to transfer

_tokenX ​

Returns the address of the token X

solidity
function _tokenX() internal pure returns (IERC20);

Returns

NameTypeDescription
<none>IERC20The address of the token X

_tokenY ​

Returns the address of the token Y

solidity
function _tokenY() internal pure returns (IERC20);

Returns

NameTypeDescription
<none>IERC20The address of the token Y

_binStep ​

Returns the bin step of the pool, in basis points

solidity
function _binStep() internal pure returns (uint16);

Returns

NameTypeDescription
<none>uint16The bin step of the pool

_getNextNonEmptyBin ​

Returns next non-empty bin

solidity
function _getNextNonEmptyBin(bool swapForY, uint24 id) internal view returns (uint24);

Parameters

NameTypeDescription
swapForYboolWhether the swap is for Y
iduint24The id of the bin

Returns

NameTypeDescription
<none>uint24The id of the next non-empty bin

_onlyFactory ​

Reverts if the caller is not the factory

solidity
function _onlyFactory() private view;

_getFlashLoanFees ​

Returns the encoded fees amounts for a flash loan

solidity
function _getFlashLoanFees(bytes32 amounts) private view returns (bytes32);

Parameters

NameTypeDescription
amountsbytes32The amounts of the flash loan

Returns

NameTypeDescription
<none>bytes32The encoded fees amounts

_setStaticFeeParameters ​

Sets the static fee parameters of the pair

solidity
function _setStaticFeeParameters(
    bytes32 parameters,
    uint16 baseFactor,
    uint16 filterPeriod,
    uint16 decayPeriod,
    uint16 reductionFactor,
    uint24 variableFeeControl,
    uint16 protocolShare,
    uint24 maxVolatilityAccumulator
) internal;

Parameters

NameTypeDescription
parametersbytes32The current parameters of the pair
baseFactoruint16The base factor of the static fee
filterPerioduint16The filter period of the static fee
decayPerioduint16The decay period of the static fee
reductionFactoruint16The reduction factor of the static fee
variableFeeControluint24The variable fee control of the static fee
protocolShareuint16The protocol share of the static fee
maxVolatilityAccumulatoruint24The max volatility accumulator of the static fee

_mintBins ​

Helper function to mint liquidity in each bin in the liquidity configurations

solidity
function _mintBins(bytes32[] calldata liquidityConfigs, bytes32 amountsReceived, address to, MintArrays memory arrays)
    private
    returns (bytes32 amountsLeft);

Parameters

NameTypeDescription
liquidityConfigsbytes32[]The liquidity configurations
amountsReceivedbytes32The amounts received
toaddressThe address to mint the liquidity to
arraysMintArraysThe arrays to store the results

Returns

NameTypeDescription
amountsLeftbytes32The amounts left

_updateBin ​

Helper function to update a bin during minting

solidity
function _updateBin(uint16 binStep, uint24 activeId, uint24 id, bytes32 maxAmountsInToBin, bytes32 parameters)
    internal
    returns (uint256 shares, bytes32 amountsIn, bytes32 amountsInToBin);

Parameters

NameTypeDescription
binStepuint16The bin step of the pair
activeIduint24The id of the active bin
iduint24The id of the bin
maxAmountsInToBinbytes32The maximum amounts in to the bin
parametersbytes32The parameters of the pair

Returns

NameTypeDescription
sharesuint256The amount of shares minted
amountsInbytes32The amounts in
amountsInToBinbytes32The amounts in to the bin