MaverickV2Pool
Last updated
Last updated
Inherits: ,
Pool contract for Maverick V2. There is no pool owner, but the Factory contract does have the ability to set and collect protocol fees. Users can add liquidity, remove liquidity, swap, and migrate bins up the merge stack. User liquidity is allocated to a recipient and can be assigned, by the sender, to a "subaccount". But liquidity is not transferable. For liquidity transfer and other manipulation capabilities, use the v2-supplemental Position (ERC-721 interfaces) or BoostedPosition (ERC-20 interfaces) contracts.
This contract does not support rebasing tokens. However, there are limited protections built in for tokens that have a negative rebase. On negative rebase, the pool's internal accounting of its balances will diverge from the token balance accounting. In order to interact with swap or add liquidity after a negative rebase, a user will have first make the pool solvent by transfering the pool enough asset so the two accounting systmes match. Post negative rebase, users will be able to remove their pro rata balance from the pool. Any positive rebase of the pool's balance will be accrued to the next add or swap user. That is, the proceeds of a positive rebase will not be distributed to all LPs.
Tokens such as stETH that have a mismatch between the amount transfered and the balanceOf of a user post-transfer are not supported by this contract. Such a mismatch between transfer and balanceOf will result in a revert when attempting to add liquidity.
Address of Pool accessor. This is Zero address for permissionless pools.
1-15 number to represent the active kinds. 0b0001 = static; 0b0010 = right; 0b0100 = left; 0b1000 = both; E.g. a pool with all 4 modes will have kinds = b1111 = 15
Accumulated tokenA protocol fee.
Accumulated tokenB protocol fee.
TickSpacing of pool where 1.0001^tickSpacing is the bin width.
Pool tokenA. Address of tokenA is such that tokenA < tokenB.
Pool tokenB.
Lookback period of pool in seconds.
Deploying factory of the pool and also contract that has ability to set and collect protocol fees for the pool.
Most significant bit of scale value is a flag to indicate whether tokenA has more or less than 18 decimals. Scale is used in conjuction with Math.toScale/Math.fromScale functions to convert from token amounts to D18 scale internal pool accounting.
Most significant bit of scale value is a flag to indicate whether tokenA has more or less than 18 decimals. Scale is used in conjuction with Math.toScale/Math.fromScale functions to convert from token amounts to D18 scale internal pool accounting.
Mapping of Bin objects for the pool. Each Bin is indexed by an incrementing uint32 ID. There are at most 4 bins per tick. Bin state is viewable with getBin().
Mapping of Tick objects for the pool. There are at most 4 bins per tick. Ticks are indexed by their position in the log price domain. The number of ticks is limited by Constants.sol/MAX_TICK. Tick state is viewable with getTick().
Dynamic state variables of the pool. Takes up two storage slots.
Fee rate for tokenA in swaps in 18-decimal format.
Fee rate for tokenB in swaps in 18-decimal format.
Constructor is called by the deployer contract. In the constructor the pool calls back to the deployer to set the pool parameters.
Add liquidity to a pool. This function allows users to deposit tokens into a liquidity pool.
This function will call maverickV2AddLiquidityCallback
on the calling contract to collect the tokenA/tokenB payment.
Parameters
recipient
address
The account that will receive credit for the added liquidity.
subaccount
uint256
The account that will receive credit for the added liquidity.
params
AddLiquidityParams
Parameters containing the details for adding liquidity, such as token types and amounts.
data
bytes
Bytes information that gets passed to the callback.
Returns
tokenAAmount
uint256
The amount of token A added to the pool.
tokenBAmount
uint256
The amount of token B added to the pool.
binIds
uint32[]
An array of bin IDs where the liquidity is stored.
Migrate bins up the linked list of merged bins so that its mergeId is the currrent active bin.
Liquidy can only be removed from a bin that is either unmerged or has a mergeId of an unmerged bin. If a bin is merged more than one level deep, it must be migrated up the merge stack to the root bin before liquidity removal.
Parameters
binId
uint32
The ID of the bin to migrate.
maxRecursion
uint32
The maximum recursion depth for the migration.
Removes liquidity from the pool.
Liquidy can only be removed from a bin that is either unmerged or has a mergeId of an unmerged bin. If a bin is merged more than one level deep, it must be migrated up the merge stack to the root bin before liquidity removal.
Parameters
recipient
address
The address to receive the tokens.
subaccount
uint256
The subaccount for the recipient.
params
RemoveLiquidityParams
The parameters for removing liquidity.
Returns
tokenAOut
uint256
The amount of token A received.
tokenBOut
uint256
The amount of token B received.
Swap tokenA/tokenB assets in the pool. The swap user has two options for funding their swap.
The user can push the input token amount to the pool before calling the swap function. In order to avoid having the pool call the callback, the user should pass a zero-length data
bytes object with the swap call.
The user can send the input token amount to the pool when the pool calls the maverickV2SwapCallback
function on the calling contract. That callback has input parameters that specify the token address of the input token, the input and output amounts, and the bytes data sent to the swap function.
If the users elects to do a callback-based swap, the output assets will be sent before the callback is called, allowing the user to execute flash swaps. However, the pool does have reentrancy protection, so a swapper will not be able to interact with the same pool again while they are in the callback function.
Parameters
recipient
address
The address to receive the output tokens.
params
SwapParams
Parameters containing the details of the swap
data
bytes
Bytes information that gets passed to the callback.
Sets fee for permissioned pools. May only be called by the accessor.
Loan tokenA/tokenB assets from the pool to recipient. The fee rate of a loan is determined by lendingFeeRateD18
, which is set at the protocol level by the factory. This function calls maverickV2FlashLoanCallback
on the calling contract. At the end of the callback, the caller must pay back the loan with fee (if there is a fee).
Parameters
recipient
address
The address to receive the loaned tokens.
amountA
uint256
amountB
uint256
Loan amount of tokenA sent to recipient.
data
bytes
Bytes information that gets passed to the callback.
Perform pool admin action; this function can only be called by the pool factory contract. When called by other callers, this function will revert.
Parameters
action
AdminAction
Selector of admin action from AdminAction enum.
value
uint256
Applicable for "setting" admin actions and is the new value of the parameter being set.
Internal function to lock the pool.
Internal function to unlock the pool.
External function to get the state of the pool.
External function to get the current time-weighted average price.
Pool swap fee for the given direction (A-in or B-in swap) in 18-decimal format. E.g. 0.01e18 is a 1% swap fee.
ID of bin at input tick position and kind.
Return state of Bin at input binId.
Return state of Tick at input tick position.
Retrieves the balance of a user within a bin.
Parameters
user
address
The user's address.
subaccount
uint256
The subaccount for the user.
binId
uint32
The ID of the bin.
Bool indicator of whether the input kind is supported by the pool.
Retrieves the square root price and liquidity of a tick.
Parameters
tick
int32
The tick value.
Returns
sqrtLowerTickPrice
uint256
The square root of the lower tick price.
sqrtUpperTickPrice
uint256
The square root of the upper tick price.
sqrtPrice
uint256
The square root price.
output
TickData
The tick data.
Get the balance of a given token.
Parameters
token
IERC20
The IERC20 token to check the balance of.
Returns
<none>
uint128
The balance of the token.
Get or create a bin and return its ID and storage reference.
Parameters
currentState
State
The State struct containing the current state data.
kind
uint8
The uint8 value representing the kind of bin.
tick
int32
The int32 value representing the tick.
Returns
binId
uint32
The ID of the bin.
bin
Bin.Instance
The storage reference to the bin.
Merges bins and moves them to a new tick.
Parameters
moveData
MoveData
The MoveData struct containing the necessary data for the move.
Merges a given bin and removes its former tick so that the bin is no longer active.
Parameters
binIdToBeMerged
uint32
binId of the bin being merged.
parentBinId
uint32
binId of the parent bin where the merging bin's liquidity is being sent.
parentBin
Bin.Instance
Bin object of parent.
parentBinTickState
IMaverickV2Pool.TickState
Tick object of the parent bin's tick.
kind
uint8
Kind of the bins.
Merge bins in moveData.mergeBins into firstBin.
Parameters
firstBin
Bin.Instance
The storage reference to the first bin.
firstBinTickState
IMaverickV2Pool.TickState
The storage reference to the first bin's TickState.
moveData
MoveData
The MoveData struct containing the necessary data.
Move a bin from one tick to another.
Parameters
firstBin
Bin.Instance
The storage reference to the first bin.
startingTickState
IMaverickV2Pool.TickState
The storage reference to the starting tick's TickState.
endingTickState
IMaverickV2Pool.TickState
The storage reference to the ending tick's TickState.
moveData
MoveData
The MoveData struct containing the necessary data.
Finds bins of the same kind within the movement search window.
Move bins based on TWAP conditions.
Logic looks left and right of the active tick for movement bins. If there is more than one movement bin of the same kind in this multi-tick window, then these bins will get moved to the TWAP tick and merged into to the Bin with the lowest binId.
Parameters
startingActiveTick
int32
Active tick at the beginning of the swap.
activeTick
int32
Active tick at the end of the swap.
lastTwapD8
int64
TWAP at beginning of the swap.
newTwapD8
int64
TWAP at end of the swap.
boundary
int64
Distance into a tick that a swap has to move in order to cause a bin movement.
Swap tokens at a specific tick.
Parameters
currentState
State
The State struct containing the current state data.
delta
Delta.Instance
The Delta.Instance struct containing swap data.
Returns
newDelta
Delta.Instance
The updated Delta.Instance struct.
Allocate swap values to bins based on token direction and tick.
Parameters
delta
Delta.Instance
The Delta.Instance struct containing swap data.
tokenAIn
bool
A boolean indicating whether token A is being swapped in.
tick
int32
The int32 value representing the tick.
Struct for holding data during bin movement.