Skip to main content

Core Contracts

Factory

Handles deployment of LyfeblocDMM v2 pools and where administrative configurations are held, such as the whitelisting of NFT position managers, and government fee settings.

parameters

Used by the Pool's constructor to fetch the parameters. The reason why they are not passed directly is for the automatic contract verification on Etherscan.

FieldTypeExplanation
factoryaddressthis contract's address
token0addressfirst pool token by address sort order
token1addresssecond pool token by address sort order
swapFeeBpsuint16fee to be collected upon every swap in the pool, in basis points
_tickDistanceint24Minimum number of ticks between initialized ticks

poolInitHash

TypeExplanation
bytes32keccak256 hash of the pool's creation code. This is accessed by periphery contracts for the
deterministic computation of a pool's address

configMaster

TypeExplanation
addressThe address that is able to modify different settings and parameters

whitelistDisabled

TypeExplanation
boolIf true, anyone can directly mint liquidity from the pool. Otherwise, only whitelisted addresses are
able to do so

vestingPeriod

TypeExplanation
uint32The maximum time duration for which LP fees are proportionally burnt upon LP removals

feeAmountTickDistance()

Returns the tick distance for a specified fee. Once added, cannot be updated or removed.

FieldTypeExplanation
Input, swapFeeBpsuint16swap fee, in basis points
Outputint24configured tick distance

feeConfiguration()

FieldTypeExplanation
_feeToaddressrecipient of government fees
_governmentFeeBpsuint16current government fee charged in basis points. Taken out of swap fee

getPool()

Returns the pool address for a given pair of tokens and a swap fee. Note that the token order does not matter.

Input

FieldTypeExplanation
tokenAaddresscontract address of either token0 or token1
tokenBaddresscontract address of the other token
swapFeeBpsuint16swap fee, in basis points

Output

FieldTypeExplanation
pooladdressThe pool address. Returns null address if it does not exist

createPool()

Creates a pool for the given two tokens and fee. The token order does not matter.

The call reverts under the following conditions:

  1. Pool already exists
  2. Invalid swap fee (tickDistance is zero)
  3. Invalid token arguments

Input

FieldTypeExplanation
tokenAaddresscontract address of either token0 or token1
tokenBaddresscontract address of the other token
swapFeeBpsuint16swap fee, in basis points

Output

FieldTypeExplanation
pooladdresscreated pool address

isWhitelistedNFTManager()

Checks if an address is a whitelisted NFT manager. Returns true if it is, false if it is not.

FieldTypeExplanation
senderaddressaddress to be checked

getWhitelistedNFTManagers()

Returns an array of addresses of all whitelisted NFT managers.

updateConfigMaster()

To update the address of configMaster. Can only be changed by configMaster.

FieldTypeExplanation
_configMasteraddressnew config master address

enableWhitelist()

Sets whitelistDisabled to false, meaning, only whitelisted addresses are able to call the Pool's mint method. Can only be called by configMaster.

disableWhitelist()

Sets whitelistDisabled to true. Will allow anyone to call the Pool's mint method. Can only be called by configMaster.

addNFTManager()

Whitelists an NFT manager. Returns true if addition was successful, that is if it was not already present. Can only be called by configMaster.

FieldTypeExplanation
_nftManageraddressaddress to be whitelisted

removeNFTManager()

Removes a whitelisted NFT manager. Returns true if removal was successful, that is if it was not already present. Can only be called by configMaster.

FieldTypeExplanation
_nftManageraddresswhitelisted address to be removed

updateVestingPeriod()

Updates the value of vestingPeriod. Can only be called by configMaster.

enableSwapFee()

Enables a fee amount with the given tickDistance. The value cannot be updated or removed after setting. Can only be called by configMaster. | Field | Type | Explanation | | ---- | ----------- | ------ | | swapFeeBps | uint16 | swap fee, in basis points | | tickDistance | int24 | desired tick distance |

updateFeeConfiguration()

Updates the address receiving government fees and government fee to be charged (taken out of swap fee). | Field | Type | Explanation | | ---- | ----------- | ------ | | feeTo | address | recipient of government fees| | governmentFeeBps | uint16 | government fee charged in basis points |


Pool

Primarily contains the implementation of actionable items by users, such as adding or removing liquidity, executing swaps or flash loans, and burning reinvestment tokens in exchange for fees collected.

unlockPool()

Provides initial liquidity and sets initial price for the pool. All other actions cannot be performed prior to the execution of this function.

Note that this function should also be called at most once.

Input

FieldTypeExplanation
initialSqrtPuint160Initial sqrt price, multiplied by $2^{96}$
databytesData, if any, to be passed into the callback function

Output

FieldTypeExplanation
qty0uint256token0 quantity permanently locked in the pool
qty1uint256token1 quantity permanently locked in the pool

mint()

Adds liquidity to a specified recipient/tickLower/tickUpper position. Any token0 or token1 owed for the liquidity provision have to be paid for in the callback function.

Also sends reinvestment tokens (fees) to the recipient for any fees collected by the position. Reinvestment tokens have to be burnt via burnRTokens in exchange for token0 and token1.

Input

FieldTypeExplanation
recipientaddressaddress for which the added liquidity is credited to. Equivalent to position owner
tickLowerint24position's lower tick
tickUpperint24position's upper tick
ticksPreviousint24[2]an array containing 2 values tickLowerPrevious and tickUpperPrevious which
are expected to be the nearest initialized tick <= tickLower and tickUpper respectively
qtyuint128Liquidity quantity to mint
databytesData, if any, to be passed into the callback function

Output

FieldTypeExplanation
qty0uint256token0 quantity sent to the pool in exchange for specified liquidity quantity
qty1uint256token1 quantity sent to the pool in exchange for specified liquidity quantity
feeGrowthInsideuint256position's updated feeGrowthInside value

burn()

Removes liquidity from the caller. In other words, the caller is treated as the position owner.

Also sends reinvestment tokens (fees) to the caller for any fees collected by the position. Reinvestment tokens have to be burnt via burnRTokens in exchange for token0 and token1.

Input

FieldTypeExplanation
tickLowerint24position's lower tick
tickUpperint24position's upper tick
qtyuint128Liquidity quantity to burn

Output

FieldTypeExplanation
qty0uint256token0 quantity sent to the caller
qty1uint256token1 quantity sent to the caller
feeGrowthInsideuint256position's updated feeGrowthInside value

burnRTokens()

Burns reinvestment tokens in exchange to receive the fees collected in token0 and token1.

Input

FieldTypeExplanation
qtyuint256Reinvestment token quantity to burn
boolisLogicalBurnfalse = burning tokens without receiving any fees in exchange. true = fees
should be calculated and sent

The use-case for burning tokens whilst leaving the collected fees in the pool is the anti-snipping attack mechanism.

Output

FieldTypeExplanation
qty0uint256token0 quantity sent to the caller
qty1uint256token1 quantity sent to the caller

swap()

Swap token0 -> token1, or vice versa. Note that swaps will either fully use up the specified swap quantity, or swap up to the specified price limit, depending on whichever condition is satisfied first.

Input

FieldTypeExplanation
recipientaddressaddress to receive the swap output
swapQtyint256swap quantity, which implicitly configures the swap as exact input (>0), or exact
output (<0)
isToken0boolwhether the swapQty is specified in token0 (true) or token1 (false)
limitSqrtPuint160sqrt price limit to reach
databytesData, if any, to be passed into the callback function
Note

To specify an unlimited price limit for a swap, use the following values.

isToken0swapQtyValue
true> 0MIN_SQRT_RATIO + 1
true< 0MAX_SQRT_RATIO - 1
false> 0MAX_SQRT_RATIO - 1
false< 0MIN_SQRT_RATIO + 1

Output

FieldTypeExplanation
qty0int256exact token0 qty sent to recipient if < 0. Minimally received quantity if > 0
qty1int256exact token1 qty sent to recipient if < 0. Minimally received quantity if > 0

flash()

Request for token0 and/or token1 and pay it back in the same transaction, plus a fee, in the callback. Fees collected are distributed to all rToken holders since no rTokens are minted from it.

Input

FieldTypeExplanation
recipientaddressaddress which will receive the token0 and token1 quantities
qty0uint256token0 quantity to be loaned to the recipient
qty1uint256token1 quantity to be loaned to the recipient
databytesData, if any, to be passed into the callback function

PoolStorage

Contains all variables and getter methods to be used by the Pool contract. Inherited by [PoolTickState] (#PoolTicksState).

factory

TypeExplanation
IFactoryCanonical factory address fetched upon pool creation

token0

TypeExplanation
IERC20First of the two tokens of the pool, sorted by address

token1

TypeExplanation
IERC20Second of the two tokens of the pool, sorted by address

maxTickLiquidity

TypeExplanation
uint128Maximum gross liquidity that an initialized tick can have. This is to prevent overflow the pool's
active base liquidity (uint128)

swapFeeBps

TypeExplanation
uint16fee to be charged for a swap in basis points

tickDistance

TypeExplanation
int24Ticks can only be initialized and used at multiples of this value

For instance, a tickDistance of 5 means ticks can be initialized every 5th tick, i.e., ..., -10, -5, 0, 5, 10, ...

ticks()

Retrieve information about a specified int24 tick.

Output FieldTypeExplanation
liquidityGrossuint128total liquidity amount from positions that uses this tick as a lower or upper
tick
liquidityNetint128how much liquidity changes when the pool tick crosses up this tick
feeGrowthOutsideuint256fee growth on the other side of the tick relative to the current tick. <br/
> Appendix-A
secondsPerLiquidityOutsideuint128seconds spent on the other side of the tick relative to the
current tick.
Appendix-A

initializedTicks()

Returns the previous and next initialized ticks from the specified tick.

Note

If the specified tick is uninitialized, the returned values are zero.

Output FieldTypeExplanation
previousint24Next initalized tick below the specified tick
nextint24Next initalized tick above the specified tick

getPositions()

Returns the information about a position.

Inputs

FieldTypeExplanation
owneraddressposition owner
tickLowerint24position's lower tick
tickUpperint24position's upper tick

Outputs

FieldTypeExplanation
liquidityuint128position's liquidity amount
feeGrowthInsideLastuint256last cached fee growth inside the position as of the last action
performed

getPoolState()

Primarily returns the pool's current price and ticks, and whether it is locked.

FieldTypeExplanation
sqrtPuint160current sqrt price, multiplied by $2^{96}$
currentTickint24current tick that closely reflects sqrtP
nearestCurrentTickint24nearest initialized tick that is <= currentTick
lockedbooltrue if pool is locked, false otherwise

getLiquidityState()

Fetches the pool's liquidity values.

FieldTypeExplanation
baseLuint128total liquidity provided by active positions
reinvestLuint128liquidity is reinvested into the pool
reinvestLLastuint128last cached value of reinvestL, used for calculating reinvestment token qty

getFeeGrowthGlobal()

Returns the all-time fee growth per unit of liquidity of the pool. More information can be found [here] (##Global-value).

getSecondsPerLiquidityData()

FieldTypeExplanation
secondsPerLiquidityGlobaluint128all-time seconds per unit of liquidity of the pool. More
information can be found here
lastUpdateTimeuint32timestamp of last performed updated to secondsPerLiquidityGlobal

getSecondsPerLiquidityInside()

Calculates and returns the active time per unit of liquidity until up to the current timestamp.

Input

FieldTypeExplanation
tickLowerint24a lower tick
tickUpperint24an upper tick

Output

FieldTypeExplanation
secondsPerLiquidityInsideuint128active time between tickLower and tickUpper. Note that its
value is multiplied by $2^{96}$. More information can be found here

_initPoolStorage()

Used by the pool to initialize relevant pool variables.

_positionKey()

Calculates the key to the positions mapping. It is simply the keccak256 hash of a specified address and lower and upper ticks.


PoolTicksState

Inherits PoolStorage. Contains functions for updating a tick's, position's or pool's state.

_updatePosition()

Calls _updateTick on a position's lower and upper tick, calculates the fee growth inside between the 2 ticks and updates the position by calling _updatePositionData().

Returns the calculated reinvestment tokens to be minted for the position's accumulated fees.

_updateLiquidityAndCrossTick()

Called by the pool when crossing a tick. Updates the tick's outside values (read more here), and applies any change in liquidity to the pool's base liquidity.

Returns the new base liquidity and next tick values.

_updatePoolData()

Called after a pool swap's calculations are completed, and new values are to be assigned to the pool's price, liquidity and tick values.

_getInitialSwapData()

Returns the stored pool's price, liquidity and tick values before swap calculations are to be performed.

_updatePositionData()

Updates a position's liquidity and feeGrowthInside value. Also calculates the reinvestment tokens to be minted for the position's accumulated fees.

_updateTick()

Initializes / updates / deletes a tick's state (liquidity, outside values) and if required, inserts and removes the tick from the linked list by calling _updateTickList().

_updateTickList()

Add / remove a specified tick to / from the linked list. No changes are applied if the specified tick is either the MIN_TICK or MAX_TICK.