Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

In this article, I use Bunzz's DeCipher feature to examine the Uniswap V3 TickLens contract.

DeCipher is a service that uses AI to generate smart contract documentation.

For a guide to using the service, see the following article.

This article reflects my experience using DeCipher. Corrections are welcome. The original article was published in September 2023.

The DeCipher page used in this article is:

The corresponding Etherscan page is:

Overview

TickLens is a Uniswap V3 periphery contract that provides information about initialized ticks in liquidity pools.

It interacts with the pool through IUniswapV3Pool, which aggregates several interfaces: IUniswapV3PoolImmutables, IUniswapV3PoolState, IUniswapV3PoolDerivedState, IUniswapV3PoolActions, IUniswapV3PoolOwnerActions, and IUniswapV3PoolEvents.

These interfaces define pool configuration, state, derived state, actions, owner actions, and events. TickLens itself exposes a much narrower interface: it reads the pool's tick bitmap, tick spacing, and initialized tick records.

Applications can query token addresses, fee tiers, tick spacing, the current tick, active liquidity, and fee-related state directly from the pool. TickLens supplements these queries by retrieving initialized ticks in a specified bitmap word in a single call.

Combined with pool state and appropriate liquidity math, this data can help an application analyze liquidity distribution and estimate token amounts associated with positions. TickLens does not perform those calculations by itself.

Usage

Objective

The goal is to help developers and technical operators retrieve tick data from Uniswap V3 pools for monitoring, liquidity position analysis, and informed trading decisions.

1. Obtain a TickLens instance

Use an existing deployed TickLens contract on the relevant network or deploy an instance if necessary.

2. Select a pool

Identify the target Uniswap V3 pool and a signed tick bitmap word index. The displayed TickLens implementation does not have a constructor that binds it to a specific pool.

3. Retrieve pool information

Pool address

Obtain the pool address from the application's configuration or the appropriate Uniswap V3 factory.

Immutable pool configuration

Read pool methods such as token0(), token1(), fee(), and tickSpacing() directly.

Pool state

Use pool state methods such as slot0() and liquidity() to obtain current price-related state and active liquidity. Use TickLens to retrieve initialized tick data.

Derived pool state

Consult the pool's derived-state interface for observations and cumulative values. Applications may calculate additional price and liquidity metrics from available data.

Pool actions

The pool actions interface defines operations such as Mint, Burn, and Swap. These are pool methods, not TickLens query methods.

Owner actions

The pool owner-actions interface defines privileged operations such as configuring protocol fees and collecting protocol fees.

Pool events

Retrieve logs emitted by the pool to analyze historical liquidity changes, swaps, and fee collection.

The original article described conceptual functions such as getPoolImmutables and getPoolState, but those functions do not exist in the TickLens implementation reproduced below.

Applications of TickLens

1. Monitoring liquidity pool activity

  • Combine current active liquidity and tick information with TickLens data to monitor pool conditions.
  • Use current price and liquidity information to track changes over time.
  • Analyze historical pool events to understand trading and liquidity activity.

2. Analyzing liquidity positions

  • Read token addresses, fees, and tick spacing to understand pool configuration.
  • Analyze initialized ticks and active liquidity to understand liquidity distribution across price ranges.
  • Combine position-specific fee data and pool state to evaluate position performance. TickLens alone does not report a position's realized fees or profitability.

3. Supporting informed trading decisions

  • Understand the pool actions available through its interface.
  • Review owner actions to understand administrative controls.
  • Analyze historical events and liquidity distribution to inform trading and risk analysis.
  • EIP-1620: Money streaming standard.
    • A related financial application standard mentioned in the original article. TickLens does not directly implement it.
  • ERC-20: Fungible token standard.
    • Uniswap V3 pools typically trade ERC-20 tokens; their token addresses can be obtained directly from the pool.

For background on ERC-20, see the following article.

https://chaldene.net/erc20

  • ERC-721: Non-fungible token standard.
    • Uniswap V3's position manager can represent liquidity positions as NFTs. TickLens reads pool ticks rather than individual NFT positions.

For background on ERC-721, see the following article.

https://chaldene.net/erc721

  • ERC-1155: Multi-token standard.
    • Another related token standard. The displayed TickLens implementation does not directly query ERC-1155 tokens.

For background on ERC-1155, see the following article.

https://chaldene.net/erc1155

Parameters

The contract has no constructor parameters or persistent pool-specific configuration in the displayed implementation.

Contract

TickLens

PopulatedTick

PopulatedTick
solidity
struct PopulatedTick {
    int24 tick;
    int128 liquidityNet;
    uint128 liquidityGross;
}

Overview

A struct containing liquidity information for a particular initialized tick.

Fields

  • tick
    • An int24 value identifying the tick's price boundary.
  • liquidityNet
    • An int128 value describing the signed change in active liquidity when crossing the tick from lower to higher prices. It is not a difference between buy and sell orders.
  • liquidityGross
    • A uint128 value representing total position liquidity referencing that tick as a boundary.

tick

A tick is an integer representing a discrete price boundary in Uniswap V3. In raw token units, a tick corresponds to a token1/token0 price of 1.0001 raised to that tick.

For example, an illustrative interval can contain the following ticks:

  • Lower tick: -15000
  • Middle tick: 0
  • Upper tick: 15000

Higher tick values correspond to higher token1/token0 prices. The pool's tick spacing determines which ticks may be used as position boundaries.

A liquidity position specifies lower and upper tick boundaries. As the current price crosses initialized ticks, the pool updates active liquidity. Tick indices themselves remain fixed; it is the current tick that changes as price changes.

Ticks therefore provide the discrete price coordinate system used by concentrated liquidity.

liquidityNet

This is the signed change in active liquidity when the price crosses an initialized tick in the increasing-tick direction.

For example:

  • Tick: 5000
  • liquidityNet: 1500

When the pool crosses tick 5000 in the increasing direction, active liquidity increases by 1500 liquidity units. Crossing in the opposite direction reverses the sign of the adjustment.

A negative liquidityNet indicates a net reduction in active liquidity when crossing upward. A zero value can occur when liquidity starting and ending at a boundary offsets.

The value is a liquidity change, not the total liquidity within the price range.

liquidityGross

This is the total liquidity of positions using a particular tick as either their lower or upper boundary.

For example, if the combined position liquidity referencing a tick is 1000 liquidity units, liquidityGross is 1000.

This is not a dollar-denominated value or a sum of token0 and token1 balances. It is expressed in Uniswap's liquidity units.

Unlike liquidityNet, liquidityGross does not cancel positive and negative boundary contributions. Comparing the two values helps explain how much liquidity references a boundary and how active liquidity changes when it is crossed.

getPopulatedTicksInWord

getPopulatedTicksInWord
solidity
function getPopulatedTicksInWord(address pool, int16 tickBitmapIndex)
    public
    view
    override
    returns (PopulatedTick[] memory populatedTicks)
{
    // fetch bitmap
    uint256 bitmap = IUniswapV3Pool(pool).tickBitmap(tickBitmapIndex);

    // calculate the number of populated ticks
    uint256 numberOfPopulatedTicks;
    for (uint256 i = 0; i < 256; i++) {
        if (bitmap & (1 << i) > 0) numberOfPopulatedTicks++;
    }

    // fetch populated tick data
    int24 tickSpacing = IUniswapV3Pool(pool).tickSpacing();
    populatedTicks = new PopulatedTick[](numberOfPopulatedTicks);
    for (uint256 i = 0; i < 256; i++) {
        if (bitmap & (1 << i) > 0) {
            int24 populatedTick = ((int24(tickBitmapIndex) << 8) + int24(i)) * tickSpacing;
            (uint128 liquidityGross, int128 liquidityNet, , , , , , ) = IUniswapV3Pool(pool).ticks(populatedTick);
            populatedTicks[--numberOfPopulatedTicks] = PopulatedTick({
                tick: populatedTick,
                liquidityNet: liquidityNet,
                liquidityGross: liquidityGross
            });
        }
    }
}

Overview

Returns an array of PopulatedTick records for initialized ticks represented by a specific bitmap word of a Uniswap V3 pool.

Details

  • The function accepts a pool address and a tick bitmap word index.
  • It retrieves the bitmap and counts its set bits, then allocates an array of the required size.
  • It reads tick spacing and reconstructs each initialized tick index from the word index and bit position.
  • It queries the pool's tick record and stores the tick, net liquidity, and gross liquidity in the result. The displayed implementation fills the array backward.

Populated ticks

In this context, a populated or initialized tick is one whose bitmap bit is set, indicating that the pool has initialized a liquidity boundary there.

The function retrieves the initialized tick records in the requested word rather than returning data for every possible tick in the pool.

Bitmap

A bitmap represents a set or flags using bits, each of which is either 0 or 1. It allows applications to store membership information compactly and inspect whether individual elements are present.

For example, suppose a set contains A and C but not B:

  • A: 1 (present)
  • B: 0 (absent)
  • C: 1 (present)

With that illustrative display ordering, the bitmap is 101.

In TickLens, each bit of the 256-bit word represents a potential initialized tick at the pool's tick spacing. A set bit identifies an initialized tick; an unset bit indicates that the corresponding tick is not initialized.

This allows the function to identify which tick records need to be read without querying every tick individually.

Parameters

  • pool
    • The address of the Uniswap V3 pool to query.
  • tickBitmapIndex
    • The signed index of the bitmap word.

Return value

  • PopulatedTick[] memory populatedTicks
    • An array of records containing the initialized tick index, net liquidity, and gross liquidity.

Source code

ITickLens

ITickLens.sol
solidity
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.7.5;
pragma abicoder v2;

/// @title Tick Lens
/// @notice Provides functions for fetching chunks of tick data for a pool
/// @dev This avoids the waterfall of fetching the tick bitmap, parsing the bitmap to know which ticks to fetch, and
/// then sending additional multicalls to fetch the tick data
interface ITickLens {
    struct PopulatedTick {
        int24 tick;
        int128 liquidityNet;
        uint128 liquidityGross;
    }

    /// @notice Get all the tick data for the populated ticks from a word of the tick bitmap of a pool
    /// @param pool The address of the pool for which to fetch populated tick data
    /// @param tickBitmapIndex The index of the word in the tick bitmap for which to parse the bitmap and
    /// fetch all the populated ticks
    /// @return populatedTicks An array of tick data for the given word in the tick bitmap
    function getPopulatedTicksInWord(address pool, int16 tickBitmapIndex)
        external
        view
        returns (PopulatedTick[] memory populatedTicks);
}

TickLens

TickLens.sol
solidity
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
pragma abicoder v2;

import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol';

import '../interfaces/ITickLens.sol';

/// @title Tick Lens contract
contract TickLens is ITickLens {
    /// @inheritdoc ITickLens
    function getPopulatedTicksInWord(address pool, int16 tickBitmapIndex)
        public
        view
        override
        returns (PopulatedTick[] memory populatedTicks)
    {
        // fetch bitmap
        uint256 bitmap = IUniswapV3Pool(pool).tickBitmap(tickBitmapIndex);

        // calculate the number of populated ticks
        uint256 numberOfPopulatedTicks;
        for (uint256 i = 0; i < 256; i++) {
            if (bitmap & (1 << i) > 0) numberOfPopulatedTicks++;
        }

        // fetch populated tick data
        int24 tickSpacing = IUniswapV3Pool(pool).tickSpacing();
        populatedTicks = new PopulatedTick[](numberOfPopulatedTicks);
        for (uint256 i = 0; i < 256; i++) {
            if (bitmap & (1 << i) > 0) {
                int24 populatedTick = ((int24(tickBitmapIndex) << 8) + int24(i)) * tickSpacing;
                (uint128 liquidityGross, int128 liquidityNet, , , , , , ) = IUniswapV3Pool(pool).ticks(populatedTick);
                populatedTicks[--numberOfPopulatedTicks] = PopulatedTick({
                    tick: populatedTick,
                    liquidityNet: liquidityNet,
                    liquidityGross: liquidityGross
                });
            }
        }
    }
}

Conclusion

This article examined Uniswap V3's TickLens contract using Bunzz's DeCipher feature. Its main purpose is to retrieve initialized tick data in batches, reducing the need for separate bitmap and tick-data queries.

I plan to continue examining specific NFTs and smart contracts. I also publish articles about blockchain development and AI on my blog and Qiita.

https://chaldene.net/