V3 Pool
DiamondSwap V3 Pool Smart Contract
The DiamondSwap V3 Pool contract is the core component that facilitates the swapping of tokens and the provision of liquidity. Each pool represents a unique trading pair with a specific fee tier and allows users to swap between tokens while liquidity providers earn fees based on their contributions. Below is a comprehensive breakdown of the functions, events, and variables associated with the DiamondSwap V3 Pool contract.
Functions
initialize(uint160 sqrtPriceX96)
initialize(uint160 sqrtPriceX96)
This function initializes the pool with a specified initial price. It can only be called once per pool and sets the initial state for the price of the two tokens in the pool.
- sqrtPriceX96: The square root of the initial price, represented as a Q64.96 number (a fixed-point number with 96 fractional bits). This value sets the initial relative price between the two tokens in the pool. The choice of initial price is critical, as it determines the starting point for trades and liquidity provision in the pool.
mint(address recipient, int24 tickLower, int24 tickUpper, uint128 amount, bytes data) → uint256 amount0, uint256 amount1
mint(address recipient, int24 tickLower, int24 tickUpper, uint128 amount, bytes data) → uint256 amount0, uint256 amount1
This function allows liquidity providers to add liquidity to the pool within a specified price range. The liquidity is represented by a position defined by the lower and upper tick boundaries.
- recipient: The address receiving the liquidity tokens, which represent the ownership of the liquidity in the pool.
- tickLower: The lower tick of the liquidity range.
- tickUpper: The upper tick of the liquidity range.
- amount: The amount of liquidity to be added.
- data: Additional data that can be used for callbacks.
This function returns the amounts of the two tokens (amount0
and amount1
) that the liquidity provider needs to contribute based on the specified price range.
collect(address recipient, int24 tickLower, int24 tickUpper, uint128 amount0Requested, uint128 amount1Requested) → uint128 amount0, uint128 amount1
collect(address recipient, int24 tickLower, int24 tickUpper, uint128 amount0Requested, uint128 amount1Requested) → uint128 amount0, uint128 amount1
This function allows liquidity providers to collect the fees earned on their liquidity positions. Fees are accumulated over time as trades occur within the specified price range.
- recipient: The address receiving the collected fees.
- tickLower: The lower tick of the liquidity position.
- tickUpper: The upper tick of the liquidity position.
- amount0Requested: The maximum amount of token0 to collect.
- amount1Requested: The maximum amount of token1 to collect.
The function returns the actual amounts of tokens (amount0
and amount1
) collected, which may be less than the requested amounts if fewer fees are available.
burn(int24 tickLower, int24 tickUpper, uint128 amount) → uint256 amount0, uint256 amount1
burn(int24 tickLower, int24 tickUpper, uint128 amount) → uint256 amount0, uint256 amount1
This function allows liquidity providers to remove liquidity from the pool. By burning liquidity tokens, the provider withdraws their proportional share of the tokens from the pool.
- tickLower: The lower tick of the liquidity range.
- tickUpper: The upper tick of the liquidity range.
- amount: The amount of liquidity to be burned.
This function returns the amounts of the two tokens (amount0
and amount1
) that the liquidity provider will receive when removing liquidity.
swap(address recipient, bool zeroForOne, int256 amountSpecified, uint160 sqrtPriceLimitX96, bytes data) → int256 amount0, int256 amount1
swap(address recipient, bool zeroForOne, int256 amountSpecified, uint160 sqrtPriceLimitX96, bytes data) → int256 amount0, int256 amount1
This function facilitates the swapping of tokens within the pool. Users can swap one token for another, with the pool's price adjusting based on the trade.
- recipient: The address receiving the output of the swap.
- zeroForOne: A boolean indicating the direction of the swap. If
true
, token0 is swapped for token1; iffalse
, token1 is swapped for token0. - amountSpecified: The amount of the input token to swap. This can be positive (exact input) or negative (exact output).
- sqrtPriceLimitX96: The price limit for the swap, represented as a Q64.96 number. This limit ensures that the swap doesn't move the price beyond a certain point.
- data: Additional data that can be used for callbacks.
The function returns the amounts of the two tokens (amount0
and amount1
) involved in the swap.
flash(address recipient, uint256 amount0, uint256 amount1, bytes data)
flash(address recipient, uint256 amount0, uint256 amount1, bytes data)
This function enables flash swaps, where tokens can be borrowed from the pool and must be returned (along with a fee) within the same transaction.
- recipient: The address receiving the borrowed tokens.
- amount0: The amount of token0 to borrow.
- amount1: The amount of token1 to borrow.
- data: Additional data that can be used for callbacks.
Flash swaps are useful for arbitrage opportunities, liquidations, or other strategies that require temporary access to liquidity.
increaseObservationCardinalityNext(uint16 observationCardinalityNext)
increaseObservationCardinalityNext(uint16 observationCardinalityNext)
This function increases the pool's observation capacity, allowing for more historical data points to be stored. This is important for accurate price oracles and TWAP (Time-Weighted Average Price) calculations.
- observationCardinalityNext: The desired minimum number of observations to be stored.
observe(uint32[] secondsAgos) → int56[] tickCumulatives, uint160[] secondsPerLiquidityCumulativesX128
observe(uint32[] secondsAgos) → int56[] tickCumulatives, uint160[] secondsPerLiquidityCumulativesX128
This function retrieves historical data from the pool, specifically the cumulative tick and seconds per liquidity data. This data is essential for calculating TWAPs and understanding the historical price movements of the pool.
- secondsAgos: An array of timestamps to retrieve data for, relative to the current block timestamp.
The function returns the cumulative tick and seconds per liquidity data corresponding to the provided timestamps.
snapshotCumulativesInside(int24 tickLower, int24 tickUpper) → int56 tickCumulativeInside, uint160 secondsPerLiquidityInsideX128, uint32 secondsInside
snapshotCumulativesInside(int24 tickLower, int24 tickUpper) → int56 tickCumulativeInside, uint160 secondsPerLiquidityInsideX128, uint32 secondsInside
This function takes a snapshot of the cumulative tick and seconds per liquidity inside a specified price range. This is useful for tracking the performance of specific liquidity positions over time.
- tickLower: The lower tick of the price range.
- tickUpper: The upper tick of the price range.
The function returns the cumulative tick, seconds per liquidity, and total time spent within the specified price range.
Events
Initialize(uint160 sqrtPriceX96, int24 tick)
Initialize(uint160 sqrtPriceX96, int24 tick)
This event is emitted when the pool is initialized with its initial price.
- sqrtPriceX96: The square root of the initial price.
- tick: The initial tick corresponding to the price.
Mint(address sender, address indexed owner, int24 indexed tickLower, int24 indexed tickUpper, uint128 amount, uint256 amount0, uint256 amount1)
Mint(address sender, address indexed owner, int24 indexed tickLower, int24 indexed tickUpper, uint128 amount, uint256 amount0, uint256 amount1)
This event is emitted whenever liquidity is added to the pool.
- sender: The address that initiated the mint.
- owner: The address that owns the liquidity position.
- tickLower: The lower tick of the liquidity range.
- tickUpper: The upper tick of the liquidity range.
- amount: The amount of liquidity added.
- amount0: The amount of token0 added.
- amount1: The amount of token1 added.
Burn(address indexed owner, int24 indexed tickLower, int24 indexed tickUpper, uint128 amount, uint256 amount0, uint256 amount1)
Burn(address indexed owner, int24 indexed tickLower, int24 indexed tickUpper, uint128 amount, uint256 amount0, uint256 amount1)
This event is emitted whenever liquidity is removed from the pool.
- owner: The address that owns the liquidity position.
- tickLower: The lower tick of the liquidity range.
- tickUpper: The upper tick of the liquidity range.
- amount: The amount of liquidity burned.
- amount0: The amount of token0 withdrawn.
- amount1: The amount of token1 withdrawn.
Swap(address indexed sender, address indexed recipient, int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick)
Swap(address indexed sender, address indexed recipient, int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick)
This event is emitted whenever a swap occurs within the pool.
- sender: The address that initiated the swap.
- recipient: The address receiving the output of the swap.
- amount0: The change in token0 balance due to the swap.
- amount1: The change in token1 balance due to the swap.
- sqrtPriceX96: The price of the pool after the swap.
- liquidity: The liquidity of the pool after the swap.
- tick: The tick after the swap.
Flash(address indexed sender, address indexed recipient, uint256 amount0, uint256 amount1, uint256 paid0, uint256 paid1)
Flash(address indexed sender, address indexed recipient, uint256 amount0, uint256 amount1, uint256 paid0, uint256 paid1)
This event is emitted whenever a flash swap occurs.
- sender: The address that initiated the flash swap.
- recipient: The address receiving the borrowed tokens.
- amount0: The amount of token0 borrowed.
- amount1: The amount of token1 borrowed.
- paid0: The amount of token0 repaid.
- paid1: The amount of token1 repaid.
IncreaseObservationCardinalityNext(uint16 observationCardinalityNextOld, uint16 observationCardinalityNextNew)
IncreaseObservationCardinalityNext(uint16 observationCardinalityNextOld, uint16 observationCardinalityNextNew)
This event is emitted whenever the observation capacity of the pool is increased.
- observationCardinalityNextOld: The previous observation capacity.
- observationCardinalityNextNew: The new observation capacity.
Variables
slot0() → (uint160 sqrtPriceX96, int24 tick, uint16 observationIndex, uint16 observationCardinality, uint16 observationCardinalityNext, uint8 feeProtocol, bool unlocked)
slot0() → (uint160 sqrtPriceX96, int24 tick, uint16 observationIndex, uint16 observationCardinality, uint16 observationCardinalityNext, uint8 feeProtocol, bool unlocked)
This function returns multiple variables that describe the current state of the pool:
- sqrtPriceX96: The current price of the pool, represented as a Q64.96 number.
- tick: The current tick of the pool.
- observationIndex: The index of the last observation in the pool's historical data.
- observationCardinality: The current maximum number of observations stored.
- observationCardinalityNext: The next maximum number of observations to be stored.
- feeProtocol: The protocol fee as a percentage of the swap fee.
- unlocked: A boolean indicating whether the pool is currently locked (preventing re-entrancy).
feeGrowthGlobal0X128() → uint256
feeGrowthGlobal0X128() → uint256
This variable tracks the total fee growth in token0 across all liquidity positions in the pool.
feeGrowthGlobal1X128() → uint256
feeGrowthGlobal1X128() → uint256
This variable tracks the total fee growth in token1 across all liquidity positions in the pool.
liquidity() → uint128
liquidity() → uint128
This variable represents the total liquidity currently active in the pool.
tickSpacing() → int24
tickSpacing() → int24
This variable indicates the minimum tick spacing between initialized ticks in the pool. Tick spacing determines the granularity of liquidity positions.
maxLiquidityPerTick() → uint128
maxLiquidityPerTick() → uint128
This variable indicates the maximum liquidity that can be placed within a single tick. It is a safeguard to prevent excessive concentration of liquidity in one tick.
Conclusion
The DiamondSwap V3 Pool contract is the backbone of the DiamondSwap protocol, enabling efficient token swaps, liquidity provision, and fee collection. By understanding the functions, events, and variables within this contract, users and developers can fully leverage the power of DiamondSwap V3. Whether you are a trader seeking low slippage, a liquidity provider aiming to earn fees, or a developer building on top of the protocol, this detailed breakdown provides the necessary insights into how the DiamondSwap V3 Pool operates.
Updated 3 months ago