IDiamondSwapV3Pool
DiamondSwap V3 Pool Interface (IDiamondSwapV3Pool)
The DiamondSwap V3 Pool Interface (IDiamondSwapV3Pool) is the cornerstone of the DiamondSwap protocol. It provides the essential functions and events that allow users to interact with liquidity pools, enabling efficient token swaps, liquidity provision, and fee management. Below is a detailed explanation of the functions, events, and variables associated with the DiamondSwap V3 Pool Interface.
Functions
initialize(uint160 sqrtPriceX96)
initialize(uint160 sqrtPriceX96)
This function initializes the pool with a specific initial price. It sets the starting price for the token pair in the pool and can only be called once during the pool's lifetime.
- sqrtPriceX96: The square root of the initial price of the pool, represented as a Q64.96 number (a fixed-point number with 96 fractional bits). This initial price determines the starting point for swaps 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 a user to add liquidity to the pool within a specified price range. The liquidity is represented by a position between the lower and upper ticks.
- recipient: The address receiving the liquidity tokens, which represent ownership of the liquidity in the pool.
- tickLower: The lower bound of the price range for the liquidity position.
- tickUpper: The upper bound of the price range for the liquidity position.
- amount: The amount of liquidity being added.
- data: Optional callback data for executing additional logic during the mint.
The function returns the amounts of the two tokens (amount0
and amount1
) that the user needs to deposit to create the liquidity position.
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 a liquidity provider to collect fees earned on their liquidity position. The fees accumulate as trades occur within the specified price range.
- recipient: The address receiving the collected fees.
- tickLower: The lower bound of the liquidity position.
- tickUpper: The upper bound 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 a liquidity provider to remove liquidity from the pool. By burning liquidity tokens, the provider withdraws their share of tokens from the pool.
- tickLower: The lower bound of the price range.
- tickUpper: The upper bound of the price range.
- amount: The amount of liquidity to burn.
The function returns the amounts of the two tokens (amount0
and amount1
) that the user 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 token swaps 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 prevents the swap from moving the price beyond a certain point.
- data: Optional callback data for executing additional logic during the swap.
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 allows for 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: Optional callback data for executing additional logic during the flash swap.
Flash swaps are often used for arbitrage, liquidations, or other strategies requiring temporary access to liquidity.
increaseObservationCardinalityNext(uint16 observationCardinalityNext)
increaseObservationCardinalityNext(uint16 observationCardinalityNext)
This function increases the number of price observations stored by the pool. More observations provide more historical data for calculating time-weighted average prices (TWAP).
- observationCardinalityNext: The new minimum number of observations to store.
observe(uint32[] secondsAgos) → (int56[] tickCumulatives, uint160[] secondsPerLiquidityCumulativesX128)
observe(uint32[] secondsAgos) → (int56[] tickCumulatives, uint160[] secondsPerLiquidityCumulativesX128)
This function retrieves historical price data from the pool. It returns the cumulative tick and seconds per liquidity data for the specified time intervals.
- secondsAgos: An array of time intervals (in seconds) relative to the current block timestamp.
The function returns two arrays: one for cumulative ticks and one for cumulative seconds per liquidity.
snapshotCumulativesInside(int24 tickLower, int24 tickUpper) → (int56 tickCumulativeInside, uint160 secondsPerLiquidityInsideX128, uint32 secondsInside)
snapshotCumulativesInside(int24 tickLower, int24 tickUpper) → (int56 tickCumulativeInside, uint160 secondsPerLiquidityInsideX128, uint32 secondsInside)
This function captures a snapshot of cumulative tick and liquidity data within a specific price range. This is useful for tracking the performance of specific liquidity positions over time.
- tickLower: The lower bound of the price range.
- tickUpper: The upper bound 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 bound of the liquidity range.
- tickUpper: The upper bound 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 bound of the liquidity range.
- tickUpper: The upper bound 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 Interface (IDiamondSwapV3Pool) is the backbone of the DiamondSwap protocol, providing the essential functions for interacting with liquidity pools. Whether you're swapping tokens, providing liquidity, or collecting fees, understanding this interface is crucial for efficient and effective participation in the DiamondSwap ecosystem. With its robust set of functions and events, this interface empowers users and developers to build and interact with decentralized finance (DeFi) applications on top of the DiamondSwap protocol.
Updated 3 months ago