Skip to content

CQL primer ​

Everything an agent needs to write a CQL query, check it and run it through heto's tools, on one page.

Quick start ​

CQL is heto's query language for EVM chain data. A query names contracts, says which blocks to read, and shapes the rows with a pipeline of operators. You need no node, no ABI file and no script.

cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;

from stETH at latest-21600..latest every 7200 as s
| project { block: $block, ethPerShare: format(s.getPooledEthByShares(1e18), 18) }

Open in workbench →

Work in this loop:

  1. cql_primer lists this page's sections. Pass one as topic, such as calls or recipes, or pass a handbook path or a code like CQL2063.
  2. describe_contract({ chain, address }) lists a contract's functions and events as signature lines. Call only what it lists.
  3. check_query({ text }) plans the query without running it and spends no credits. Fix every error it returns, then check again.
  4. run_query({ text }) runs the query and spends the user's credits. It returns rows as CSV and a run_id, or status: "running" for a long run.
  5. get_run({ run_id, offset, wait_seconds }) returns the next page of rows, or waits on a run still going.

Check every query before you run it. Never run a query that check_query refuses. list_chains gives the chain names and heads, and list_abis the user's ABI library.

Four rules a first draft often breaks:

  • Every let ends with ;.
  • Every address carries its chain: ethereum:0x….
  • Name a column name: expr in project, and name = expr in extend and summarize. expr as name is a cast, not a name.
  • format(x, 18) turns a raw 18-decimal amount into tokens.

The shape of a query ​

let <name> = <value>;
from <contracts> [events <name>] [at <range>] [with { <options> }] [as <alias>]
| <operator> …
  • let binds a name to an address, an ABI or a constant. It cannot read a row (CQL2006).
  • from is the one source. from [a, b] reads each contract in the list. A second source goes inside join ( … ) or union ( … ).
  • Without events, the source reads state: one row per contract per block. You call functions through the alias, s.totalSupply(), and each call is answered at the row's block.
  • With events Transfer, it reads logs: one row per log, with the event's arguments as fields, t.value. events * reads every event the ABI declares, and $event names each one. An event the ABI lacks can be given as a signature: events 'TransferShares(address indexed from, address indexed to, uint256 sharesValue)'.
  • as s names the source, and a call needs that name. A let name is a call target too: stETH.totalSupply().

Blocks and time ​

atReads
none, on statethe newest block
none, on eventsa recent window, 10 000 blocks on Ethereum (CQL3901)
at latest, at finalizedthe newest block, or the newest the chain will not reorganise
at 26_000_000one block
at latest-7200..latesta block range, both ends included
at 25_800_000..26_000_000 every 7200each block divisible by 7 200 in the range, plus the end
at time '2026-09-20'..'2026-09-23' every 1dthe first block at or after each day boundary, plus the end
  • every takes a number on a block range and a duration (s, m, h, d, w) on a time range (CQL1019). It belongs to a state range. On one block or on events it is CQL3005.
  • A state range with no every is sampled for you, to stay under 10 000 rows per contract (CQL3900). Write every to choose the step. On Ethereum, 7 200 blocks is about a day.
  • latest is pinned when the run starts. Two runs a minute apart differ. Write block numbers when you need the same rows twice.
  • A block beyond the head is CQL3001. blockat(chain, t) is the last block at or before a time.

Operators ​

Each stage starts with a pipe and takes the rows of the stage above it.

OperatorDoesExample
wherekeeps rows whose condition is exactly truewhere t.to == wstETH
projectkeeps only the listed columnsproject { block: $block, supply }
extendadds columns and keeps the restextend pooled = s.getTotalPooledEther()
expandone row per element of an arrayexpand r.getStakingModuleIds() as id index i
summarizeaggregates, per group after bysummarize total = sum(t.value) by sender = t.from
joinpairs rows with a second pipeline on keysjoin kind=leftouter ( from … ) on block
unionappends a second pipeline's rowsunion ( from … )
order bysorts, stablyorder by total desc, sender asc
takekeeps the first n rows, n a literaltake 20
distinctkeeps the listed columns, the first row of each combinationdistinct treasury

The aggregates are count(), count_distinct, sum, avg, min, max, first, last, make_list, make_set, arg_max and arg_min. Each is a whole item of summarize. To format a sum, write summarize total = sum(t.value), then | extend total = format(total, 18).

Name every computed column (CQL2050). A column named by a keyword needs backticks or its alias: `from`, t.from. Write and, or and not, never && or ||. iff(c, a, b), x in (a, b) and x between (lo .. hi) work as they read.

Calls and ABIs ​

s.f(args) calls f at the row's block. Several named outputs are a record, r.field. A reverted call gives null, and revert_reason(x) says why. CQL never sends a transaction. A function that is not view is answered as a simulation (CQL3908).

Where an ABI comes from ​

CQL looks in four places, in this order:

  1. An ABI in the query. let v = abi ['function getDebt() view returns (uint256)'];. A JSON ABI array works too.
  2. A built-in. erc20, erc721, erc1155, erc4626 and multicall3.
  3. The user's library. A name in quotes: with { abi: 'lido' }. list_abis lists the names. A name found nowhere is CQL2008.
  4. The address itself. With no abi, CQL finds the ABI verified for the address. It follows a proxy one step to its implementation and adds that ABI. Most queries need no abi at all.

An address with no verified ABI is CQL3008. Use the library, an inline abi […], or a call by signature, which needs no ABI:

cql
let stETH  = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
let wstETH = ethereum:0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;

from stETH with { abi: none } as s
| project { shares: s.call('sharesOf(address)(uint256)', wstETH) }

Open in workbench →

The signature is name(inputs)(outputs). Without the outputs you get raw bytes. abi: none turns the lookup off. .call also settles an ambiguous overload (CQL2011).

Where to attach one ​

WhereWrittenApplies to
on the namelet x = ethereum:0x… with { abi: v };every use of x
on the sourcefrom x with { abi: v } as severy call through s
on one calls.f() with { abi: v }that call only
on an address the query findsexpand … as d with { abi: v }, extend y = d with { abi: v }, (s.f()) with { abi: v }every call through that address

The innermost wins, key by key. with takes three more options: caller, the address the call comes from, block, a block to answer at instead of the row's, and value, the value sent with the call. Options on a name or a source cannot use $block or a call (CQL2006). Put a per-row block on the call: s.f() with { block: $block - 7200 }.

Parentheses decide what with configures ​

An address that a call returns, or that expand takes from a list, has no ABI of its own, and CQL does not guess one. A with right after a call configures that call. A with after the call in parentheses attaches to the call's result, the address:

cql
let locatorAbi = abi ['function withdrawalQueue() view returns (address)',
                      'function treasury() view returns (address)'];
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;

from stETH as s
| project { locator: (s.getLidoLocator()) with { abi: locatorAbi } }
| project { locator, queue: locator.withdrawalQueue(), treasury: locator.treasury() }

Open in workbench →

  • (s.getLidoLocator()) with { abi: locatorAbi } calls getLidoLocator with stETH's own ABI. The column locator carries locatorAbi, and every later call through locator uses it.
  • s.getLidoLocator() with { abi: locatorAbi } uses locatorAbi for the call to getLidoLocator itself. The column carries no ABI, so locator.withdrawalQueue() is refused as CQL2063.

The ABI stays with the column through a project that copies or renames it, distinct, summarize … by, where, order by and join.

expand ​

cql
let router = ethereum:0xFdDf38947aFB03C621C71b06C9C70bce73f12999;

from router as r
| expand r.getStakingModuleIds() as moduleId index i
| project { i, moduleId, active: r.getStakingModuleIsActive(moduleId) }

Open in workbench →

  • as names the element and index i numbers it from zero. Every other column repeats, and the alias still works.
  • For a list of addresses, write the ABI after the alias or after the index: expand pool.getDestinations() as d index i with { abi: v }. A with straight after the operand, before as, configures the call getDestinations() and gives d no ABI.
  • expand [a, b] as holder unfolds a list you write. expand multiplies rows, so sample the source if you meet a cap after one.

Joins and unions ​

  • join ( … ) on key keeps rows with a match on both sides. kind= is inner (the default), leftouter, rightouter, fullouter, leftsemi or leftanti.
  • on block joins on a column both sides have, and it appears once. on t.to == w.from compares two columns and keeps both. Another column both sides have is kept twice, the right one renamed value1 or $address1.
  • A joined row is read at the left row's block. To read the right side at its own block, extend inside the parentheses.
  • Two state series join on $block when both have the same range and every. Across chains, on $block is CQL3007. Give each side extend t = bin($sampleAt, 1h) and join on t. A call on another chain's address needs with { block: blockat(chain(x), $timestamp) } (CQL3006).
  • union ( … ) appends rows and matches columns by name. After it the aliases name nothing, so call through a let name.
  • A join or a union holds both sides at once. Summarize or narrow each side inside its parentheses.

Types and numbers ​

TypeWrittenNote
bigint1_000_000, 1e18Whole, any size. Every uint256, int256 and $block.
decimal1.05exact, never floating point
addressethereum:0x…Always with its chain. Lowercase, or a correct checksum (CQL1006).
bytes, bytes1 … bytes320xffHex is bytes, not a number. tobigint(0xff) is 255.
datetime, durationdatetime('2026-09-01'), 1h, 7dUTC, to the second
  • format(x, 18) moves the point 18 places and never rounds: format(1e18, 18) is 1.0. parse(1.05, 18) goes back. For a token you do not know, read decimals() first.
  • bigint / bigint truncates: 7 / 2 is 3. Write todecimal(a) / b for a rate. A division with a decimal gives 38 significant digits, and a run that rounded says so (CQL4901).
  • null + 1 is null. coalesce(x, 0) and isnull(x) handle it, and where drops a null.
  • t.from == address(0) matches the zero address. A bare 0x… literal compared with an address ignores the chain.
  • datetime - datetime is a duration. Write $timestamp + 5s, not $timestamp + 5 (CQL2012).
  • x as address reads a 32-byte word, such as a storage slot: s.$storage[keccak256('aragonOS.appStorage.kernel')] as address.
  • In the CSV that run_query returns, a big integer is a decimal string.

System columns ​

ColumnHolds
$chainthe row's chain
$blockthe block that answered the row, a bigint
$blockHash, $timestampthat block's hash and time
$addressthe contract, or the event's emitter
$sampleAtthe boundary that selected a sampled row, otherwise null
$tx, $txIndex, $logIndexevents only: the transaction, its index, the log's index
$event, $topics, $dataevents only: the event's name, raw topics and undecoded data

Recipes ​

A balance at a block ​

cql
let stETH  = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
let wstETH = ethereum:0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;

from stETH at 26_000_000 with { abi: erc20 } as t
| project { block: $block, symbol: t.symbol(), balance: format(t.balanceOf(wstETH), 18) }

Open in workbench →

A value over time ​

cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;

from stETH at 25_800_000..26_000_000 every 7200 as s
| project { block: $block, time: $timestamp, ethPerShare: format(s.getPooledEthByShares(1e18), 18) }

Open in workbench →

Events in a range ​

cql
let stETH  = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
let wstETH = ethereum:0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;

from stETH events Transfer at 26_030_000..26_030_200 as t
| where t.to == wstETH
| project { block: $block, tx: $tx, sender: t.from, amount: format(t.value, 18) }

Open in workbench →

A where on an indexed argument, here t.to, goes to the node's log filter. One event source spans at most 10 000 000 blocks (CQL3015).

A contract set followed through a list ​

cql
let vault   = abi ['function getDestinations() view returns (address[])'];
let destAbi = abi ['function getDebt() view returns (uint256)'];
let autoETH = ethereum:0x0A2b94F6871c1D7A32Fe58E1ab5e6deA2f114E56;

from autoETH with { abi: vault } as pool
| expand pool.getDestinations() as dest with { abi: destAbi }
| project { dest, debt: dest.getDebt() }
| order by debt desc

Open in workbench →

A join across two sources ​

cql
let stETH  = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
let wstETH = ethereum:0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;

from stETH at 25_800_000..26_000_000 every 7200 as a
| extend ethPerShare = format(a.getPooledEthByShares(1e18), 18)
| join (
    from wstETH at 25_800_000..26_000_000 every 7200 as b
    | extend stEthPerToken = format(b.stEthPerToken(), 18)
  ) on $block
| project { block: $block, ethPerShare, stEthPerToken }

Open in workbench →

A summary ​

cql
let stETH  = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
let wstETH = ethereum:0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;

from stETH events Transfer at 26_030_000..26_040_000 as t
| where t.to == wstETH
| summarize total = sum(t.value), transfers = count() by sender = t.from
| order by total desc
| take 20
| project { sender, transfers, total: format(total, 18) }

Open in workbench →

For a summary over time, group by a bucket: by day = bin($timestamp, 1d).

Codes you will meet ​

check_query returns each diagnostic with its code, position, message and guidance. A code ending in 9xx is a warning or a note, never a refusal.

CodeMeansFix
CQL1016a syntax errorRead what it expected. Often a let without its ;, or a stage without its pipe.
CQL1012expr as namename = expr, or { name: expr } in project.
CQL1006an address fails its checksumUse the hint's spelling, or lowercase.
CQL2002an unknown nameCheck the alias, and whether an earlier project dropped the column.
CQL2004a column named by a keyword, such as fromt.from, `from`, or by sender = t.from.
CQL2006a let, or options on a name or a source, read a rowUse extend, or put the option on the call.
CQL2008an ABI name not built in and not in the libraryCheck list_abis, or write abi […] inline.
CQL2010a function the ABI does not haveCheck describe_contract. Attach the ABI that has it.
CQL2012no overload takes these typestobigint for bytes, $timestamp + 5s for time.
CQL2050an expression with no namename: expr.
CQL2062an alias whose address column a stage replaced or droppedextend bAddress = b.$address, or keep the column.
CQL2063a call through an address found at run time, with no ABIexpand … as d with { abi: v }, (s.f()) with { abi: v }, or .call().
CQL3001a block beyond the headCheck list_chains, or use latest.
CQL3005every on one block or on eventsRemove every.
CQL3006a call on another chain's address with no blockwith { block: blockat(chain(x), $timestamp) }.
CQL3007$block compared across chainsJoin on bin($sampleAt, 1h).
CQL3008no verified ABI for the addressInline abi […], the library, or .call().
CQL3013a function no ABI in range has, often on a proxy CQL did not followAttach the implementation's ABI with with { abi: … }.
CQL3014, CQL4004more than 100 000 rows out of one operatorNarrow the range, sample, or summarize.
CQL3016, CQL4008more than 50 000 requestsA larger every, a shorter range, or fewer calls.
CQL4006the run passed its timeoutNarrow the range or sample more coarsely.
CQL0402not enough credits for the runTell the user. Running again does not help.
CQL0429too many requestsWait the seconds it names, then try again.

cql_primer with a code as topic explains any other code.

Pass a path to cql_primer as topic: /cql/reference/with-options, /cql/reference/operators, /cql/reference/functions, /cql/concepts/finding-an-abi or /cql/concepts/long-ranges-and-limits.