LBPair
Last updated
Last updated
Inherits:, , ,
Author: Trader Joe
The Liquidity Book Pair contract is the core contract of the Liquidity Book protocol
Constructor for the Liquidity Book Pair contract that sets the Liquidity Book Factory
Parameters
factory_
ILBFactory
The Liquidity Book Factory
Initialize the Liquidity Book Pair fee parameters and active id
Can only be called by the Liquidity Book Factory
Parameters
baseFactor
uint16
The base factor for the static fee
filterPeriod
uint16
The filter period for the static fee
decayPeriod
uint16
The decay period for the static fee
reductionFactor
uint16
The reduction factor for the static fee
variableFeeControl
uint24
The variable fee control for the static fee
protocolShare
uint16
The protocol share for the static fee
maxVolatilityAccumulator
uint24
The max volatility accumulator for the static fee
activeId
uint24
The active id of the Liquidity Book Pair
Returns the Liquidity Book Factory
Returns
factory
ILBFactory
The Liquidity Book Factory
Returns the token X of the Liquidity Book Pair
Returns
tokenX
IERC20
The address of the token X
Returns the token Y of the Liquidity Book Pair
Returns
tokenY
IERC20
The address of the token Y
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.
Returns
<none>
uint16
binStep The bin step of the Liquidity Book Pair, in 10_000th
Returns the reserves of the Liquidity Book Pair This is the sum of the reserves of all bins, minus the protocol fees.
Returns
reserveX
uint128
The reserve of token X
reserveY
uint128
The reserve of token Y
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)
Returns
activeId
uint24
The active id of the Liquidity Book Pair
Returns the reserves of a bin
Parameters
id
uint24
The id of the bin
Returns
binReserveX
uint128
The reserve of token X in the bin
binReserveY
uint128
The reserve of token Y in the bin
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.
Parameters
swapForY
bool
Whether the swap is for token Y (true) or token X (false
id
uint24
The id of the bin
Returns
nextId
uint24
The id of the next non-empty bin
Returns the protocol fees of the Liquidity Book Pair
Returns
protocolFeeX
uint128
The protocol fees of token X
protocolFeeY
uint128
The protocol fees of token Y
Returns the static fee parameters of the Liquidity Book Pair
Returns
baseFactor
uint16
The base factor for the static fee
filterPeriod
uint16
The filter period for the static fee
decayPeriod
uint16
The decay period for the static fee
reductionFactor
uint16
The reduction factor for the static fee
variableFeeControl
uint24
The variable fee control for the static fee
protocolShare
uint16
The protocol share for the static fee
maxVolatilityAccumulator
uint24
The maximum volatility accumulator for the static fee
Gets the hooks parameters of the Liquidity Book Pair
Returns
<none>
bytes32
The hooks parameters of the Liquidity Book Pair
Returns the variable fee parameters of the Liquidity Book Pair
Returns
volatilityAccumulator
uint24
The volatility accumulator for the variable fee
volatilityReference
uint24
The volatility reference for the variable fee
idReference
uint24
The id reference for the variable fee
timeOfLastUpdate
uint40
The time of last update for the variable fee
Returns the oracle parameters of the Liquidity Book Pair
Returns
sampleLifetime
uint8
The sample lifetime for the oracle
size
uint16
The size of the oracle
activeSize
uint16
The active size of the oracle
lastUpdated
uint40
The last updated timestamp of the oracle
firstTimestamp
uint40
The first timestamp of the oracle, i.e. the timestamp of the oldest sample
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.
Parameters
lookupTimestamp
uint40
The timestamp at which to look up the cumulative values
Returns
cumulativeId
uint64
The cumulative id of the Liquidity Book Pair at the given timestamp
cumulativeVolatility
uint64
The cumulative volatility of the Liquidity Book Pair at the given timestamp
cumulativeBinCrossed
uint64
The cumulative bin crossed of the Liquidity Book Pair at the given timestamp
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
Parameters
id
uint24
The id of the bin
Returns
price
uint256
The price corresponding to this id
Returns the id corresponding to the given price
The id may be inaccurate due to rounding issues, always trust getPriceFromId rather than getIdFromPrice
Parameters
price
uint256
The price of y per x as a 128.128-binary fixed-point number
Returns
id
uint24
The id of the bin corresponding to this price
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
.
Parameters
amountOut
uint128
The amount of token X or Y to swap in
swapForY
bool
Whether the swap is for token Y (true) or token X (false)
Returns
amountIn
uint128
The amount of token X or Y that can be swapped in, including the fee
amountOutLeft
uint128
The amount of token Y or X that cannot be swapped out
fee
uint128
The fee of the swap
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
.
Parameters
amountIn
uint128
The amount of token X or Y to swap in
swapForY
bool
Whether the swap is for token Y (true) or token X (false)
Returns
amountInLeft
uint128
The amount of token X or Y that cannot be swapped in
amountOut
uint128
The amount of token Y or X that can be swapped out
fee
uint128
The fee of the 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.
Parameters
swapForY
bool
Whether you're swapping token X for token Y (true) or token Y for token X (false)
to
address
The address to send the tokens to
Returns
amountsOut
bytes32
The encoded amounts of token X and token Y sent to to
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.
Parameters
receiver
ILBFlashLoanCallback
The contract that will receive the tokens and execute the callback function
amounts
bytes32
The encoded amounts of token X and token Y to flash loan
data
bytes
Any data that will be passed to the callback function
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.
Parameters
to
address
The address that will receive the LB tokens
liquidityConfigs
bytes32[]
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.
refundTo
address
The address that will receive the excess amount of tokens
Returns
amountsReceived
bytes32
The amounts of token X and token Y received by the pool
amountsLeft
bytes32
The amounts of token X and token Y that were not added to the pool and were sent to to
liquidityMinted
uint256[]
The amounts of LB tokens minted for each bin
Burn Liquidity Book (LB) tokens and withdraw tokens from the pool. This function will burn the tokens directly from the caller
Parameters
from
address
The address that will burn the LB tokens
to
address
The address that will receive the tokens
ids
uint256[]
The ids of the bins from which to withdraw
amountsToBurn
uint256[]
The amounts of LB tokens to burn for each bin
Returns
amounts
bytes32[]
The amounts of token X and token Y received by the user
Collect the protocol fees from the pool.
Returns
collectedProtocolFees
bytes32
The amount of protocol fees collected
Increase the length of the oracle used by the pool
Parameters
newLength
uint16
The new length of the oracle
Sets the static fee parameters of the pool
Can only be called by the factory
Parameters
baseFactor
uint16
The base factor of the static fee
filterPeriod
uint16
The filter period of the static fee
decayPeriod
uint16
The decay period of the static fee
reductionFactor
uint16
The reduction factor of the static fee
variableFeeControl
uint24
The variable fee control of the static fee
protocolShare
uint16
The protocol share of the static fee
maxVolatilityAccumulator
uint24
The max volatility accumulator of the static fee
Sets the hooks parameter of the pool
Can only be called by the factory
Parameters
hooksParameters
bytes32
The hooks parameter
onHooksSetData
bytes
The data to be passed to the onHooksSet function of the hooks contract
Forces the decay of the volatility reference variables
Can only be called by the factory
Overrides the batch transfer function to call the hooks before and after the transfer
Parameters
from
address
The address to transfer from
to
address
The address to transfer to
ids
uint256[]
The ids of the tokens to transfer
amounts
uint256[]
The amounts of the tokens to transfer
Returns the address of the token X
Returns
<none>
IERC20
The address of the token X
Returns the address of the token Y
Returns
<none>
IERC20
The address of the token Y
Returns the bin step of the pool, in basis points
Returns
<none>
uint16
The bin step of the pool
Returns next non-empty bin
Parameters
swapForY
bool
Whether the swap is for Y
id
uint24
The id of the bin
Returns
<none>
uint24
The id of the next non-empty bin
Reverts if the caller is not the factory
Returns the encoded fees amounts for a flash loan
Parameters
amounts
bytes32
The amounts of the flash loan
Returns
<none>
bytes32
The encoded fees amounts
Sets the static fee parameters of the pair
Parameters
parameters
bytes32
The current parameters of the pair
baseFactor
uint16
The base factor of the static fee
filterPeriod
uint16
The filter period of the static fee
decayPeriod
uint16
The decay period of the static fee
reductionFactor
uint16
The reduction factor of the static fee
variableFeeControl
uint24
The variable fee control of the static fee
protocolShare
uint16
The protocol share of the static fee
maxVolatilityAccumulator
uint24
The max volatility accumulator of the static fee
Helper function to mint liquidity in each bin in the liquidity configurations
Parameters
liquidityConfigs
bytes32[]
The liquidity configurations
amountsReceived
bytes32
The amounts received
to
address
The address to mint the liquidity to
arrays
MintArrays
The arrays to store the results
Returns
amountsLeft
bytes32
The amounts left
Helper function to update a bin during minting
Parameters
binStep
uint16
The bin step of the pair
activeId
uint24
The id of the active bin
id
uint24
The id of the bin
maxAmountsInToBin
bytes32
The maximum amounts in to the bin
parameters
bytes32
The parameters of the pair
Returns
shares
uint256
The amount of shares minted
amountsIn
bytes32
The amounts in
amountsInToBin
bytes32
The amounts in to the bin