Skip to content

Match RPC reads to Subgraph data

Read contract state at the block a Subgraph answered from, so indexed entities and live state describe the same chain.

A Subgraph answers from the block it has indexed, which usually trails the chain head by a few blocks. If your application combines a Subgraph's entities with contract state read over RPC at latest, the two can describe different blocks: a position from the Subgraph and a balance from a newer block, for example. Read both at one block and they agree.

One API key with both RPC and Subgraphs access covers the two requests, and both draw on the same credits.

Prerequisites

  • An API key that includes RPC and Subgraphs.
  • A Subgraph that indexes the same network as your RPC endpoint.
  • Archive access on that network if the block may be older than recent state. See Archive reads.

1. Read entities and contract state at one block

Add _meta { block { number } } to the query that reads your entities. Its block is the one those entities describe. Pass that number to eth_call, eth_getBalance or any other state method; with viem, set blockNumber.

This program reads the Uniswap V3 USDC/WETH pool and its token0 from a Subgraph, then calls that token's balanceOf for the pool over RPC at the block the Subgraph answered from. The same GRAPHOPS_API_KEY authorizes both requests:

Prerequisites

  1. Install Node.js.

  2. Install tsx and viem.

    npm install tsx viem
  3. Set GRAPHOPS_API_KEY to your GraphOps API key.

  4. Replace <ALIAS> with your Endpoint Alias.

TypeScript · viem

import { createPublicClient, erc20Abi, http } from "viem";
import { mainnet } from "viem/chains";

// One API key authorizes the Subgraph query and the RPC read.
const headers = { Authorization: `Bearer ${process.env.GRAPHOPS_API_KEY}` };

const response = await fetch("https://subgraphs.graphops.xyz/v1/alias/<ALIAS>", {
  method: "POST",
  headers: { ...headers, "Content-Type": "application/json" },
  body: JSON.stringify({
    query: `{
      pool(id: "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640") { id token0 { id symbol } }
      _meta { block { number } }
    }`,
  }),
});
if (!response.ok) throw new Error(await response.text());
const { data, errors } = await response.json();
if (errors) throw new Error(JSON.stringify(errors));

const client = createPublicClient({
  chain: mainnet,
  transport: http("https://rpc.graphops.xyz/v1/evm/1", { fetchOptions: { headers } }),
});

// Read the pool's token0 balance at the block the Subgraph answered from.
const block = BigInt(data._meta.block.number);
const balance = await client.readContract({
  address: data.pool.token0.id,
  abi: erc20Abi,
  functionName: "balanceOf",
  args: [data.pool.id],
  blockNumber: block,
});

console.log(
  JSON.stringify({
    block: block.toString(),
    pool: data.pool.id,
    token: data.pool.token0.symbol,
    balance: balance.toString(),
  }),
);

Output

{  "block": "26524222",Block the Subgraph answered from; the RPC read used it too: 26,524,222  "pool": "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",Entity read from the Subgraph  "token": "USDC",  "balance": "11699920811676"Pool's USDC balance over RPC at that block: 11,699,920,811,676 base units, 11,699,920.811676 USDC}

A recent block number can name a different block after a reorganization. Add hash to the _meta block and compare it with the block your RPC read used, or read at a finalized block as described below, when that matters.

2. Keep later Subgraph reads on the same block

A Subgraph keeps indexing between your requests. To read more entities at the same block, pass block: { number: N } or block: { hash: "0x…" } as an argument on each entity field, as Paginate results does for pages.

Choose the block

  • For what a user sees now: take the block from _meta on the Subgraph read, then read RPC at it. The Subgraph is the slower of the two, so its block is the newest both can answer.
  • For a result that must not change: use the finalized block from RPC, and query the Subgraph at that block once _meta shows it has indexed past it.

Next steps

Last updated