MaverickV2Pool
Inherits: IMaverickV2Pool, IMaverickV2PoolAdmin
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.
State Variables
accessor
Address of Pool accessor. This is Zero address for permissionless pools.
kinds
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
protocolFeeA
Accumulated tokenA protocol fee.
protocolFeeB
Accumulated tokenB protocol fee.
tickSpacing
TickSpacing of pool where 1.0001^tickSpacing is the bin width.
tokenA
Pool tokenA. Address of tokenA is such that tokenA < tokenB.
tokenB
Pool tokenB.
lookback
Lookback period of pool in seconds.
factory
Deploying factory of the pool and also contract that has ability to set and collect protocol fees for the pool.
tokenAScale
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.
tokenBScale
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.
lendingFeeRateD18
bins
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().
ticks
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().
_state
Dynamic state variables of the pool. Takes up two storage slots.
_feeAIn
Fee rate for tokenA in swaps in 18-decimal format.
_feeBIn
Fee rate for tokenB in swaps in 18-decimal format.
Functions
constructor
Constructor is called by the deployer contract. In the constructor the pool calls back to the deployer to set the pool parameters.
addLiquidity
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
Returns
migrateBinUpStack
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
removeLiquidity
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
Returns
swap
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
setFee
Sets fee for permissioned pools. May only be called by the accessor.
flashLoan
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
adminAction
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
_lockPool
Internal function to lock the pool.
_unlockPool
Internal function to unlock the pool.
getState
External function to get the state of the pool.
getCurrentTwa
External function to get the current time-weighted average price.
fee
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.
binIdByTickKind
ID of bin at input tick position and kind.
getBin
Return state of Bin at input binId.
getTick
Return state of Tick at input tick position.
balanceOf
Retrieves the balance of a user within a bin.
Parameters
_kindSupportedByPool
Bool indicator of whether the input kind is supported by the pool.
_tickSqrtPriceAndLiquidity
Retrieves the square root price and liquidity of a tick.
Parameters
Returns
_tokenBalance
Get the balance of a given token.
Parameters
Returns
_checkTokenSolvency
_getOrCreateBin
Get or create a bin and return its ID and storage reference.
Parameters
Returns
_moveDirection
Merges bins and moves them to a new tick.
Parameters
_mergeAndDecommissionBin
Merges a given bin and removes its former tick so that the bin is no longer active.
Parameters
_mergeBinsInList
Merge bins in moveData.mergeBins into firstBin.
Parameters
_moveBinToNewTick
Move a bin from one tick to another.
Parameters
_getMovementBinsInRange
Finds bins of the same kind within the movement search window.
_moveBins
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
_swapTick
Swap tokens at a specific tick.
Parameters
Returns
_allocateSwapValuesToTick
Allocate swap values to bins based on token direction and tick.
Parameters
Structs
MoveData
Struct for holding data during bin movement.
Last updated