Decoding Uniswap V4’s bit-packed swap data at scale

Our tech series gives you a behind-the-scenes look at what it takes to run a reliable and scalable blockchain data pipeline. This is the backbone of everything we do and what powers our customer-facing products. We currently run in-house node infrastructure for over 80 chains and manage a 400TB+ data warehouse.
Stay tuned for more!
Earlier this year, Uniswap V4 introduced one of its most powerful features yet: hooks.
Hooks unlock new flexibility for developers, but introduce new complexity for those working w/ the raw onchain data.
In short:
- Uniswap V4 compresses swap data by packing two signed 128-bit integers into a single 256-bit word.
- We built a scalable decoding layer using BigQuery UDFs to extract those values efficiently across millions of rows.
- This enables us to support financial metrics not just for Uniswap V4, but for any protocol building on top of it, like Euler Finance's EulerSwap.
1. Bit-packing implementation
Uniswap V4 uses bitwise operations to pack swap amounts into a single 256-bit word. The toBalanceDelta() function stores amount0 in the upper 128 bits and amount1 in the lower 128, compressed for gas efficiency.
Here’s a visual breakdown of how it works 👇
2. Efficiency tradeoff
This kind of bit-level packing is great for optimizing gas, but far less convenient for data consumers. To extract real values like amount0 and amount1, we need to reverse the bitwise logic exactly.
It’s not just a decoding challenge. It’s a scale and reliability challenge when you’re parsing millions of rows – every day.
3. Existing solution with inline SQL
The Uniswap team published an example on Dune showing how to decode the packed swap delta using raw SQL.
It works well for exploratory analysis and dashboards (see query below). But for production-scale pipelines, where we decode millions of rows daily, inline SQL becomes harder to maintain, optimize, and audit over time.
4. Introducing the solution (BigQuery UDFs)
We solved this with BigQuery UDFs, user-defined JavaScript functions that run inside SQL queries. They let us encapsulate the decoding logic once, and apply it across millions of rows without repeating the same logic across our codebase.
It made our queries faster to write, easier to audit, and cheaper to run:
5. Result of decoding with UDFs
With UDFs in place, we could decode every packed swapDelta field into its underlying amount0 and amount1 values, directly in BigQuery.
That gave us clean, structured outputs we could use for protocol-level metrics, without any manual post-processing:
6. Production impact and scale
With this decoding in place, we can now fully support Uniswap V4 in our internal DEX trades abstraction, used across dozens of downstream metric transformations.
The decoded amount0 and amount1 values map directly to trade fields like token_bought_amount_raw and token_sold_amount_raw, as shown below.
BigQuery handles the scale, and our UDFs keep the system fast, cost-efficient, and easy to maintain.
7. What the decoded data powers
We use these decoded values directly in production to power Uniswap V4 metrics, like trading volume, fees, and revenue.
But that’s just the starting point. Because many new protocols are being built on top of V4 using hooks, this also enables us to support projects like EulerSwap now and in the future.
We're building a full-stack onchain data platform at Token Terminal.
We currently index ~100 chains, and manage over 1 PB of onchain data in our in-house data warehouse.
We'll be sharing more about our data-related work over the coming weeks, stay tuned!
The authors of this content, or members, affiliates, or stakeholders of Token Terminal may be participating or are invested in protocols or tokens mentioned herein. The foregoing statement acts as a disclosure of potential conflicts of interest and is not a recommendation to purchase or invest in any token or participate in any protocol. Token Terminal does not recommend any particular course of action in relation to any token or protocol. The content herein is meant purely for educational and informational purposes only, and should not be relied upon as financial, investment, legal, tax or any other professional or other advice. None of the content and information herein is presented to induce or to attempt to induce any reader or other person to buy, sell or hold any token or participate in any protocol or enter into, or offer to enter into, any agreement for or with a view to buying or selling any token or participating in any protocol. Statements made herein (including statements of opinion, if any) are wholly generic and not tailored to take into account the personal needs and unique circumstances of any reader or any other person. Readers are strongly urged to exercise caution and have regard to their own personal needs and circumstances before making any decision to buy or sell any token or participate in any protocol. Observations and views expressed herein may be changed by Token Terminal at any time without notice. Token Terminal accepts no liability whatsoever for any losses or liabilities arising from the use of or reliance on any of this content.
Stay in the loop
Join our mailing list to get the latest insights!
Continue reading
- Token Terminal x CoinGecko: Token Terminal’s onchain data is now available on CoinGecko
Token Terminal x CoinGecko: Token Terminal’s onchain data is now available on CoinGecko
Token Terminal’s standardized onchain metrics are now live on CoinGecko! You can find them on every asset page under the Financials section. The first data sets include historical data for fees and revenues, with more metrics to come.
- Binance Leverages Token Terminal’s Data Infrastructure To Deliver On-chain Analytics for Millions of Users
Binance Leverages Token Terminal’s Data Infrastructure To Deliver On-chain Analytics for Millions of Users
Over the coming months, Binance users will get access to key financial and usage metrics for crypto assets — all from a single, trusted source.
- Sonic is live on Token Terminal
Sonic is live on Token Terminal
Token Terminal has completed its integration of Sonic to enable institutional-grade onchain analytics for the Sonic ecosystem. This integration improves the accessibility of Sonic's onchain data to enable more informed decision-making for all stakeholders in the ecosystem.