UniswapV3PoolUpgradeable
UniswapV3PoolUpgradeable.sol
NFTX AMM pool contract. Uses BeaconProxy for deployments.
Table of Contents
Variables
factory
Address of the NFTX AMM's UniswapV3FactoryUpgradeable
contract.
token0
Address of "token0" (either WETH or vToken).
token1
Address of "token1" (either WETH or vToken).
fee
AMM swap fee tier.
tickSpacing
Tick spacing of the pool.
maxLiquidityPerTick
Maximum liquidity amount per tick.
slot0
Pool data.
feeGrowthGlobal0X128
Represents the accumulated fees of token0 that have been earned per unit of liquidity since the contract was deployed, stored as a fixed-point number.
feeGrowthGlobal1X128
Represents the accumulated fees of token1 that have been earned per unit of liquidity since the contract was deployed, stored as a fixed-point number.
protocolFees
Accumulated fees owed to the protocol (for example, to the governance treasury).
liquidity
The total liquidity that's currently active in the pool.
ticks
Tick data.
tickBitmap
A mapping that represents a sub-interval of ticks to help with gas-efficient calculations related to liquidity.
positions
A mapping that keeps track of individual liquidity provider positions. The key to this mapping is a bytes32 value derived from the liquidity provider's address and the tick range in which they've provided liquidity.
observations
An array used for calculating the time-weighted average price (TWAP).
Events
Initialize
Emitted by initialize()
.
Parameters | Type | Description |
---|---|---|
sqrtPriceX96 |
| Starting price. |
tick |
| Closest tick representing price. |
Mint
Emitted by mint()
.
Parameters | Type | Description |
---|---|---|
sender |
| The address that called the mint function to add liquidity. |
owner |
| The address that will own the liquidity position and can collect fees on it. |
tickLower |
| The lower price tick of the price range for which the liquidity is being provided. |
tickUpper |
| The upper price tick of the price range for which the liquidity is being provided. |
amount |
| The amount of liquidity that was added. |
amount0 |
| The amount of token0 that was added as liquidity. |
amount1 |
| The amount of token1 that was added as liquidity. |
Collect
Emitted by collect()
.
Parameters | Type | Description |
---|---|---|
owner |
| The address that called the mint function to add liquidity. |
recipient |
| The address that will own the liquidity position and can collect fees on it. |
tickLower |
| The lower price tick of the price range for which the liquidity is being provided. |
tickUpper |
| The upper price tick of the price range for which the liquidity is being provided. |
amount0 |
| The amount of token0 that was added as liquidity. |
amount1 |
| The amount of token1 that was added as liquidity. |
Burn
Emitted by burn()
.
Parameters | Type | Description |
---|---|---|
owner |
| The address that owned the liquidity position and called the function to remove liquidity. |
tickLower |
| The lower price tick of the price range for which the liquidity was provided. |
tickUpper |
| The lower price tick of the price range for which the liquidity was provided. |
amount |
| The amount of liquidity that was removed. |
amount0 |
| The amount of token0 that was removed as liquidity. |
amount1 |
| The amount of token1 that was removed as liquidity. |
Swap
Emitted by swap()
.
Parameters | Type | Description |
---|---|---|
sender |
| The address that initiated the swap. |
recipient |
| The address that received the output tokens from the swap. |
amount0 |
| The change in the token0 balance as a result of the swap. A negative value indicates token0 was sold, and a positive value indicates token0 was bought. |
amount1 |
| The change in the token1 balance as a result of the swap. A negative value indicates token1 was sold, and a positive value indicates token1 was bought. |
sqrtPriceX96 |
| The price of the pool after the swap has occurred. Encoded as the square root of the ratio of token amounts, and stored as a fixed-point number with 96 bits to the right of the decimal point. |
liquidity |
| The active liquidity at the time the swap occurred. |
tick |
| Represents the tick index of the pool after the swap has occurred. |
Flash
Emitted by flash()
.
Parameters | Type | Description |
---|---|---|
sender |
| The address that initiated the flash swap. |
recipient |
| The address that received the tokens from the flash swap. |
amount0 |
| The amount of token0 that the recipient received in the flash swap. |
amount1 |
| The amount of token1 that the recipient received in the flash swap. |
paid0 |
| The amount of token0 that was paid back to the pool by the sender by the end of the transaction. |
paid1 |
| The amount of token1 that was paid back to the pool by the sender by the end of the transaction. |
IncreaseObservationCardinalityNext
Emitted by increaseObservationCardinalityNext()
.
Parameters | Type | Description |
---|---|---|
observationCardinalityNextOld |
| The previous value of the next maximum number of observations that the pool can store. |
observationCardinalityNextNew |
| The updated value of the next maximum number of observations that the pool will store. |
SetFeeProtocol
Emitted by setProtocolFee()
.
Parameters | Type | Description |
---|---|---|
feeProtocol0Old |
| The previous value of the fee protocol setting for token0. |
feeProtocol1Old |
| The previous value of the fee protocol setting for token1. |
feeProtocol0New |
| The updated value of the fee protocol setting for token0. |
feeProtocol1New |
| The updated value of the fee protocol setting for token1. |
CollectProtocol
Emitted by collectProtocol()
.
Parameters | Type | Description |
---|---|---|
sender |
| The address that initiated the collection of the protocol fees. |
recipient |
| The address that received the collected protocol fees. |
amount0 |
| The amount of token0 that was collected as a protocol fee. |
amount1 |
| The amount of token1 that was collected as a protocol fee. |
Write Functions
increaseObservationCardinalityNext
Used to schedule an increase in the maximum number of observations that the pool can store. Observations are utilized in the AMM pool for computing TWAPs. By invoking this function, the caller is expressing the intent to expand the observation capacity in the future. The actual expansion of capacity takes effect the next time the pool undergoes significant interaction, like when liquidity is added or when a swap occurs that moves the price.
Parameters | Type | Description |
---|---|---|
observationCardinalityNext |
| Specifies the desired future maximum number of observations the pool should be able to store. It should be set to a value greater than the current |
initialize
Used to set the initial price of the pool when it's first created. This function can only be called once for a pool, when it's transitioning from an uninitialized to an initialized state. Typically called by the AMM pool factory contract during the creation of a new pool.
Parameters | Type | Description |
---|---|---|
sqrtPriceX96 |
| The starting price for the pool. Represented as the square root of the ratio of token amounts, stored as a fixed-point number with 96 bits to the right of the decimal point. |
mint
Adds liquidity to a specific price range within the AMM pool.
Parameters | Type | Description |
---|---|---|
recipient |
| The address that will receive the liquidity position NFT. |
tickLower |
| The lower bound (in tick terms) of the price range for which liquidity is being provided. |
tickUpper |
| The upper bound (in tick terms) of the price range for which liquidity is being provided. |
amount |
| The amount of liquidity being added to the pool within the specified tick range. |
data |
| Arbitrary calldata that is forwarded to a callback function. |
Return values | Type | Description |
---|---|---|
amount0 |
| The amount of token0 that the liquidity provider added to the pool. |
amount1 |
| The amount of token1 that the liquidity provider added to the pool. |
collect
Allows liquidity providers to retrieve tokens owed from one or both of the two following scenarios:
To remove accumulated fees from their specified liquidity range.
To retrieve tokens from liquidity that has previously been removed from the position.
The function can be used to collect either or both of the pool tokens, up to amounts specified by the caller.
Parameters | Type | Description |
---|---|---|
recipient |
| The address that will receive the collected tokens (either fees or tokens from removed liquidity). |
tickLower |
| The lower tick bound of the price range. |
tickUpper |
| The upper tick bound of the price range. |
amount0Requested |
| The maximum amount of token0 that the caller wishes to collect. |
amount1Requested |
| The maximum amount of token1 that the caller wishes to collect. |
Return values | Type | Description |
---|---|---|
amount0 |
| The amount of token0 collected. |
amount1 |
| The amount of token1 collected. |
burn
Allows liquidity providers to remove, or "burn," a specified amount of liquidity from a particular price range in the pool. After burning liquidity, the underlying tokens aren't immediately returned to the caller. Instead, the tokens can be later collected using the collect
function
Parameters | Type | Description |
---|---|---|
tickLower |
| The lower tick bound of the price range of the specific liquidity position from which liquidity is being removed. |
tickUpper |
| The upper tick bound of the price range of the specific liquidity position from which liquidity is being removed. |
amount |
| The amount of liquidity that the caller wishes to remove from the specified position within the tick range. |
Return values | Type | Description |
---|---|---|
amount0 |
| The amount of token0 that was removed and is available for collection. |
amount1 |
| The amount of token1 that was removed and is available for collection. |
swap
Swaps one token for another, taking slippage, price limits, and other factors into account.
Parameters | Type | Description |
---|---|---|
recipient |
| The address that will receive the tokens resulting from the swap. |
zeroForOne |
| True to swap token0 for token1, false to swap token1 for token0. |
amountSpecified |
| The exact amount of the token being sold (if positive) or the maximum amount of token one is willing to buy (if negative). |
sqrtPriceLimitX96 |
| A price constraint for the trade. If |
data |
| Arbitrary calldata sent along with the function call, allowing for more complex logic or interactions if required by the recipient. It can be used for custom callbacks, which the pool will call after the core swap logic runs. |
Name | Type | Description |
---|---|---|
amount0 |
| The amount of |
amount1 |
| The amount of |
distributeRewards
Distributes the received vault fees among the current LPs, proportional to their liquidity contribution. Can only be called by feeDistributor, after it sends the reward tokens to this pool.
Parameters | Type | Description |
---|---|---|
rewardsAmount |
| The amount of reward tokens to distribute |
isToken0 |
| True if reward token is token0, false if it is token1. |
flash
Executes a flash swap within the AMM pool. Allows a smart contract to borrow tokens from the pool, execute specific logic, and then either repay the borrowed amount with fees or provide equivalent value in the other token—all within a single transaction.
Parameters | Type | Description |
---|---|---|
recipient |
| The address that receives the borrowed tokens and is responsible for ensuring their return within the same transaction. This should ideally be a smart contract that implements the necessary callback logic to repay the borrowed funds. |
amount0 |
| The amount of |
amount1 |
| The amount of |
data |
| Arbitrary calldata forwarded to the recipient's callback function. This allows the recipient smart contract to specify additional parameters or custom logic that should be executed after the tokens are borrowed and before they are returned. |
Owner Functions
setFeeProtocol
Allows the factory owner to set or update the fee protocol values for the pool. This function doesn't directly set the protocol fee rate (i.e., the fraction of LP fees that become protocol fees—which is set on the SwapRouter). Instead, it sets how these collected protocol fees are allocated. This function can be seen as a way to change the internal allocation mechanism or beneficiaries without affecting the global setting at the factory level.
Parameters | Type | Description |
---|---|---|
feeProtocol0 |
| The fee protocol value for |
feeProtocol1 |
| The fee protocol value for |
collectProtocol
Collects protocol fees that have accrued in the pool. Protocol fees are distinct from the fees accrued by individual liquidity providers; they're set at the protocol level and typically go to a treasury or another protocol-specific address.
Parameters | Type | Description |
---|---|---|
recipient |
| The address that will receive the collected protocol fees. This is typically a treasury or another designated address decided upon by the protocol's governance. |
amount0Requested |
| The maximum amount of |
amount1Requested |
| The maximum amount of |
Read Functions
snapshotCumulativesInside
Provides a snapshot of the cumulative tick and liquidity values inside a specific tick range. It's designed to give insights and facilitate calculations related to accrued fees and other operations for positions within that range.
Parameters | Type | Description |
---|---|---|
tickLower |
| The lower tick bound of the specific tick range for which the cumulative values are to be fetched. |
tickUpper |
| The upper tick bound of the specific tick range for which the cumulative values are to be fetched. |
Return values | Type | Description |
---|---|---|
tickCumulativeInside |
| The cumulative tick value within the specified range. This value represents the sum of all tick values each time liquidity was added or removed in the given range, up to the current moment. It's useful for calculating price changes over time. |
secondsPerLiquidityInsideX128 |
| A cumulative value representing the seconds per liquidity within the specified tick range, encoded in a 128-bit fixed-point format. It's useful for determining how long, on average, liquidity has been available in the tick range, helping in calculations related to fee accrual. |
secondsInside |
| The total time in seconds that the pool's current tick has been within the specified tick range. This is useful for determining how long the current price has been within a certain range. |
observe
Retrieves historical data on the pool's tick and liquidity based on a set of specified past timestamps. It's designed to facilitate off-chain computations, particularly for time-weighted averages and other time-dependent metrics.
Parameters | Type | Description |
---|---|---|
secondsAgos |
| An array of time durations in seconds. Each duration represents how far in the past the data should be fetched. For instance, if one of the durations is |
Return values | Type | Description |
---|---|---|
tickCumulatives |
| An array of cumulative tick values corresponding to each of the durations in the |
secondsPerLiquidityCumulativeX128s |
| An array of cumulative values representing the seconds per liquidity up to each of the durations in the |
Last updated