sifting/io
DEX & DeFi
4 min readSiftingIO Team

How to Get the Latest Price and Volume From a DEX Without the On-Chain Math

A DEX has no last-price field. Getting a usable price and volume means pool math, decimals, token ordering, and swap-log parsing behind an RPC node. Here is what that takes and the one-call alternative.

How to Get the Latest Price and Volume From a DEX Without the On-Chain Math

The short answer#

A decentralized exchange does not expose a last price. It holds token balances in a pool, and the price is derived from those balances at read time. Producing a usable last price and volume means locating the relevant pools, reading their on-chain state, normalizing for token decimals and ordering, converting to USD, and parsing swap events to total volume, all on top of an RPC node. The same result is available from SiftingIO as one normalized feed. This post covers what the manual approach involves and what the API call replaces.

Deriving the price from pool state#

A constant-product pool (Uniswap v2 and its forks) stores two reserves. The spot price of token0 in terms of token1 is the reserve ratio, scaled by the decimal difference between the two tokens:

price = (reserve1 / reserve0) * 10 ** (decimals0 - decimals1)

Concentrated-liquidity pools (Uniswap v3 and similar) do not store reserves you can divide. They expose sqrtPriceX96, a fixed-point square root of the price. You square it, remove the 2^96 scaling, then apply the same decimal adjustment:

price = (sqrtPriceX96 ** 2 / 2 ** 192) * 10 ** (decimals0 - decimals1)

Each pool type needs its own code path, so supporting several protocols means maintaining several formulas.

The details that produce wrong numbers#

Token decimals are declared per token. A common stablecoin uses 6 and a wrapped native token uses 18. Skip the decimal adjustment and the price is off by a factor of 10^12.

Token order is assigned by sorting the two contract addresses, not by which asset is treated as the base. Assume the wrong order and the price is inverted. The ordering has to be read per pool.

A pool prices one token only in terms of the other. Expressing an arbitrary token in USD usually requires routing through a second pool, for example token to wrapped native to stablecoin, which adds another read and another multiplication.

Volume is not stored. To total it over a window, query the pool's Swap event logs for that block range and decode the amounts from each event.

A single pool is not a reliable price#

Beyond the work, one pool is a weak source. A low-liquidity pool can sit at a stale value, and a single large swap can move it well away from the broader market. Code that reads one pool inherits those prints directly.

Aggregating across pools reduces that exposure. SiftingIO normalizes swap prices from multiple independent venues and publishes one value as a volume-weighted and reputation-weighted median rather than an average. A median has a 50% breakdown point, so a majority of venues must move the same wrong way before the output shifts, which bounds the effect of one thin or manipulated pool. Each tick carries a quality flag for normal or degraded state.

The API call#

The latest price and trade size for a pair, as a live stream:

const ws = new WebSocket(`wss://stream.sifting.io/ws/v1?key=${SIFTING_KEY}`);

ws.onopen = () => {
  ws.send(JSON.stringify({
    op: "subscribe",
    product: "dex",
    symbols: ["eth:WETH-USDC"]
  }));
};

ws.onmessage = (event) => {
  const frame = JSON.parse(event.data);
  if (frame.f === "tick") {
    console.log(frame.chain, frame.s, frame.p, frame.P);
  }
};

The symbol is chain prefixed. eth:WETH-USDC selects the pair on Ethereum; base: and arbitrum: switch chains with no other change. Each tick carries the price in p, the latest swap size in P, the chain in chain, and a millisecond timestamp in t. On subscribe, the server sends the last cached value before live updates begin.

For a single current value rather than a stream, the REST snapshot endpoint returns the last trade for a pair in the same shape. For volume over a fixed window, the historical bars endpoint returns OHLCV candles at intervals from one minute up. The documentation lists the DEX symbols, supported chains, and bar parameters.

Summary#

A DEX last price has to be derived from pool reserves or sqrtPriceX96, corrected for decimals and token order, converted to USD, and combined with swap-log parsing for volume, per protocol and per chain, behind an RPC node. The result still reflects whatever a single pool reports. A normalized feed returns the same price and volume in one shape and aggregates across venues to limit single-pool error. The free plan needs no credit card, so you can create a free account and read a live on-chain price in a few minutes.

Tagsdexdefion-chain datatoken priceuniswapethereumwebsocketdex price apitrading volume
All postsLast updated June 5, 2026
Keep reading

Related posts