Pool
This documentation covers functionality of v3 pool.
Code#
[AlgebraPool.sol]
Address#
See Pool Addresses.
Events#
Initialize#
Emitted exactly once by a pool when #initialize is first called on the pool. Mint/Burn/Swap cannot be emitted by the pool before Initialize
Params
priceThe initial sqrt price of the pool, as a Q64.96tickThe initial tick of the pool, i.e. log base 1.0001 of the starting price of the pool
Mint#
Emitted when liquidity is minted for a given position
Params
senderThe address that minted the liquidityownerThe owner of the position and recipient of any minted liquiditybottomTickThe lower tick of the positiontopTickThe upper tick of the positionliquidityAmountThe amount of liquidity minted to the position rangeamount0How much token0 was required for the minted liquidityamount1How much token1 was required for the minted liquidity
Collect#
Emitted when fees are collected by the owner of a position. Collect events may be emitted with zero amount0 and amount1 when the caller chooses not to collect fees
Params
ownerThe owner of the position for which fees are collectedrecipientThe address that received feesbottomTickThe lower tick of the positiontopTickThe upper tick of the positionamount0The amount of token0 fees collectedamount1The amount of token1 fees collected
Burn#
Emitted when a position's liquidity is removed. Does not withdraw any fees earned by the liquidity position, which must be withdrawn via collect
Params
ownerThe owner of the position for which liquidity is removedbottomTickThe lower tick of the positiontopTickThe upper tick of the positionliquidityAmountThe amount of liquidity to removeamount0The amount of token0 withdrawnamount1The amount of token1 withdrawn
Swap#
Emitted by the pool for any swaps between token0 and token1
Params
senderThe address that initiated the swap call, and that received the callbackrecipientThe address that received the output of the swapamount0The delta of the token0 balance of the poolamount1The delta of the token1 balance of the poolpriceThe sqrt(price) of the pool after the swap, as a Q64.96liquidityThe liquidity of the pool after the swaptickThe log base 1.0001 of price of the pool after the swap
Flash#
Emitted by the pool for any flashes of token0/token1
Params
senderThe address that initiated the swap call, and that received the callbackrecipientThe address that received the tokens from flashamount0The amount of token0 that was flashedamount1The amount of token1 that was flashedpaid0The amount of token0 paid for the flash, which can exceed the amount0 plus the feepaid1The amount of token1 paid for the flash, which can exceed the amount1 plus the fee
CommunityFee#
Emitted when the community fee is changed by the pool
Params
communityFee0NewThe updated value of the token0 community fee percentcommunityFee1NewThe updated value of the token1 community fee percent
Incentive#
Emitted when new activeIncentive is set
Params
virtualPoolAddressThe address of a virtual pool associated with the current active incentive
Fee#
Params
feeThe value of the token fee
LiquidityCooldown#
Params
liquidityCooldownThe value of locktime for added liquidity
Read-Only Functions#
dataStorageOperator#
The contract that stores all the timepoints and can perform actions with them Returns the operator address
factory#
The contract that deployed the pool, which must adhere to the IAlgebraFactory interface Returns the contract address
token0#
The first of the two tokens of the pool, sorted by address Returns the token contract address
token1#
The second of the two tokens of the pool, sorted by address Returns the token contract address
tickSpacing#
The pool tick spacing. Ticks can only be used at multiples of this value. e.g.: a tickSpacing of 60 means ticks can be initialized every 60th tick, i.e., ..., -120, -60, 0, 60, 120, ... This value is an int24 to avoid casting even though it is always positive. Returns the tick spacing
maxLiquidityPerTick#
The maximum amount of position liquidity that can use any tick in the range. This parameter is enforced per tick to prevent liquidity from overflowing a uint128 at any point, and also prevents out-of-range liquidity from being used to prevent adding in-range liquidity to a pool. Returns the max amount of liquidity per tick
globalState#
The globalState structure in the pool stores many values but requires only one slot and is exposed as a single method to save gas when accessed externally.
Returns
priceThe current price of the pool as a sqrt(token1/token0) Q64.96 valuetickThe current tick of the pool, i.e. according to the last tick transition that was run. Returns This value may not always be equal to SqrtTickMath.getTickAtSqrtRatio(price) if the price is on a tick boundaryfeeThe last pool fee value in hundredths of a bip, i.e. 1e-6timepointIndexThe index of the last written timepointcommunityFeeToken0The community fee percentage of the swap fee in thousandths (1e-3) for token0communityFeeToken1The community fee percentage of the swap fee in thousandths (1e-3) for token1unlockedWhether the pool is currently locked to reentrancy
totalFeeGrowth0Token#
The fee growth as a Q128.128 fees of token0 collected per unit of liquidity for the entire life of the pool. This value can overflow the uint256
totalFeeGrowth1Token#
The fee growth as a Q128.128 fees of token1 collected per unit of liquidity for the entire life of the pool. This value can overflow the uint256
liquidity#
The currently in range liquidity available to the pool. This value has no relationship to the total liquidity across all ticks. Returned value cannot exceed type(uint128).max
ticks#
Look up information about a specific tick in the pool. This is a public structure, so the return natspec tags are omitted.
Params
tickThe tick to look up
Return
liquidityTotalthe total amount of position liquidity that uses the pool either as tick lower or tick upperliquidityDeltahow much liquidity changes when the pool price crosses the tickouterFeeGrowth0Tokenthe fee growth on the other side of the tick from the current tick in token0outerFeeGrowth1Tokenthe fee growth on the other side of the tick from the current tick in token1outerTickCumulativethe cumulative tick value on the other side of the tick from the current tickouterSecondsPerLiquiditythe seconds spent per liquidity on the other side of the tick from the current tickouterSecondsSpentthe seconds spent on the other side of the tick from the current tickinitializedSet to true if the tick is initialized, i.e. liquidityTotal is greater than 0 otherwise equal to false. Outside values can only be used if the tick is initialized In addition, these values are only relative and must be used only in comparison to previous snapshots for a specific position.
tickTable#
Returns 256 packed tick initialized boolean values. See [TickTable] for more information
positions#
Returns the information about a position by the position's key. This is a public mapping of structures, so the return natspec tags are omitted.
Params
keyThe position's key is a hash of a preimage composed by the owner, bottomTick and topTick
Return
liquidityAmountThe amount of liquidity in the positionlastLiquidityAddTimestampTimestamp of last adding of liquidityinnerFeeGrowth0TokenFee growth of token0 inside the tick range as of the last mint/burn/pokeinnerFeeGrowth1TokenFee growth of token1 inside the tick range as of the last mint/burn/pokefees0The computed amount of token0 owed to the position as of the last mint/burn/pokefees1The computed amount of token1 owed to the position as of the last mint/burn/poke
timepoints#
Returns data about a specific timepoint index. You most likely want to use getTimepoints() instead of this method to get an timepoint as of some amount of time ago, rather than at a specific index in the array. This is a public mapping of structures, so the return natspec tags are omitted.
Params
indexThe element of the timepoints array to fetch
Return
initializedwhether the timepoint has been initialized and the values are safe to useblockTimestampThe timestamp of the timepointtickCumulativethe tick multiplied by seconds elapsed for the life of the pool as of the timepoint timestampsecondsPerLiquidityCumulativethe seconds per in range liquidity for the life of the pool as of the timepoint timestampvolatilityCumulativeCumulative standard deviation for the life of the pool as of the timepoint timestampaverageTickTime-weighted average tickvolumePerLiquidityCumulativeCumulative swap volume per liquidity for the life of the pool as of the timepoint timestamp
activeIncentive#
Returns the information about active incentive. If there is no active incentive at the moment, virtualPool,endTimestamp,startTimestamp would be equal to 0.
Return
virtualPoolThe address of a virtual pool associated with the current active incentive
liquidityCooldown#
Returns the lock time for added liquidity.
getTimepoints#
Returns the cumulative tick and liquidity as of each timestamp secondsAgo from the current block timestamp. To get a time weighted average tick or liquidity-in-range, you must call this with two values, one representing the beginning of the period and another for the end of the period. E.g., to get the last hour time-weighted average tick, you must call it with secondsAgos = [3600, 0]. The time weighted average tick represents the geometric time weighted average price of the pool, in log base sqrt(1.0001) of token1 / token0. The TickMath library can be used to go from a tick value to a ratio.
Params
secondsAgosFrom how long ago each cumulative tick and liquidity value should be returned
Return
tickCumulativesCumulative tick values as of eachsecondsAgosfrom the current block timestampsecondsPerLiquidityCumulativesCumulative seconds per liquidity-in-range value as of eachsecondsAgosfrom the current block timestampvolatilityCumulativesCumulative standard deviation as of eachsecondsAgosvolumePerAvgLiquiditysCumulative swap volume per liquidity as of eachsecondsAgos
getInnerCumulatives#
Returns a snapshot of the tick cumulative, seconds per liquidity and seconds inside a tick range. Snapshots must only be compared to other snapshots, taken over a period for which a position existed. I.e., snapshots cannot be compared if a position is not held for the entire period between when the first snapshot is taken and the second snapshot is taken.
Params
bottomTickThe lower tick of the rangetopTickThe upper tick of the range
Return
innerTickCumulativeThe snapshot of the tick accumulator for the rangeinnerSecondsSpentPerLiquidityThe snapshot of seconds per liquidity for the rangeinnerSecondsSpentThe snapshot of the number of seconds during which the price was in this range
State-Changing Functions#
initialize#
Sets the initial price for the pool. Price is represented as a sqrt(amountToken1/amountToken0) Q64.96 value
Params
pricethe initial sqrt price of the pool as a Q64.96
mint#
Adds liquidity for the given recipient/bottomTick/topTick position. The caller of this method receives a callback in the form of IAlgebraMintCallback# AlgebraMintCallback in which they must pay any token0 or token1 owed for the liquidity. The amount of token0/token1 due depends on bottomTick, topTick, the amount of liquidity, and the current price.
Params
senderThe address which will receive potential surplus of paid tokensrecipientThe address for which the liquidity will be createdbottomTickThe lower tick of the position in which to add liquiditytopTickThe upper tick of the position in which to add liquidityamountThe desired amount of liquidity to mintdataAny data that should be passed through to the callback
Return
amount0The amount of token0 that was paid to mint the given amount of liquidity. Matches the value in the callbackamount1The amount of token1 that was paid to mint the given amount of liquidity. Matches the value in the callbackliquidityActualThe actual minted amount of liquidity
collect#
Collects tokens owed to a position. Does not recompute fees earned, which must be done either via mint or burn of any amount of liquidity. Collect must be called by the position owner. To withdraw only token0 or only token1, amount0Requested or amount1Requested may be set to zero. To withdraw all tokens owed, caller may pass any value greater than the actual tokens owed, e.g. type(uint128).max. Tokens owed may be from accumulated swap fees or burned liquidity.
Params
recipientThe address which should receive the fees collectedbottomTickThe lower tick of the position for which to collect feestopTickThe upper tick of the position for which to collect feesamount0RequestedHow much token0 should be withdrawn from the fees owedamount1RequestedHow much token1 should be withdrawn from the fees owed
Return
amount0The amount of fees collected in token0amount1The amount of fees collected in token1
burn#
Burn liquidity from the sender and account tokens owed for the liquidity to the position. Can be used to trigger a recalculation of fees owed to a position by calling with an amount of 0. Fees must be collected separately via a call to collect
Params
bottomTickThe lower tick of the position for which to burn liquiditytopTickThe upper tick of the position for which to burn liquidityamountHow much liquidity to burn
Return
amount0The amount of token0 sent to the recipientamount1The amount of token1 sent to the recipient
swap#
Swap token0 for token1, or token1 for token0. The caller of this method receives a callback in the form of IAlgebraSwapCallback# AlgebraSwapCallback
Params
recipientThe address to receive the output of the swapzeroToOneThe direction of the swap, true for token0 to token1, false for token1 to token0amountSpecifiedThe amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)limitSqrtPriceThe Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swapdataAny data to be passed through to the callback. If using the Router it should contain SwapRouter#SwapCallbackData
Return
amount0The delta of the balance of token0 of the pool, exact when negative, minimum when positiveamount1The delta of the balance of token1 of the pool, exact when negative, minimum when positive
swapSupportingFeeOnInputTokens#
Swap token0 for token1, or token1 for token0 (tokens that have fee on transfer). The caller of this method receives a callback in the form of IAlgebraSwapCallback# AlgebraSwapCallback
Params
senderThe address called this function (Comes from the Router)recipientThe address to receive the output of the swapzeroToOneThe direction of the swap, true for token0 to token1, false for token1 to token0amountSpecifiedThe amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)limitSqrtPriceThe Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swapdataAny data to be passed through to the callback. If using the Router it should contain SwapRouter#SwapCallbackData
Return
amount0The delta of the balance of token0 of the pool, exact when negative, minimum when positiveamount1The delta of the balance of token1 of the pool, exact when negative, minimum when positive
flash#
Receive token0 and/or token1 and pay it back, plus a fee, in the callback. The caller of this method receives a callback in the form of IAlgebraFlashCallback# AlgebraFlashCallback. All excess tokens paid in the callback are distributed to liquidity providers as an additional fee. So this method can be used to donate underlying tokens to currently in-range liquidity providers by calling with 0 amount{0,1} and sending the donation amount(s) from the callback
Params
recipientThe address which will receive the token0 and token1 amountsamount0The amount of token0 to sendamount1The amount of token1 to senddataAny data to be passed through to the callback
setCommunityFee#
Set the community's % share of the fees. Cannot exceed 25% (250)
Params
communityFee0new community fee percent for token0 of the pool in thousandths (1e-3)communityFee1new community fee percent for token1 of the pool in thousandths (1e-3)
setIncentive#
Sets an active incentive
Params
virtualPoolAddressThe address of a virtual pool associated with the incentive
setLiquidityCooldown#
Sets new lock time for added liquidity
Params
newLiquidityCooldownThe time in seconds