Appearance
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) }Work in this loop:
cql_primerlists this page's sections. Pass one astopic, such ascallsorrecipes, or pass a handbook path or a code likeCQL2063.describe_contract({ chain, address })lists a contract's functions and events as signature lines. Call only what it lists.check_query({ text })plans the query without running it and spends no credits. Fix every error it returns, then check again.run_query({ text })runs the query and spends the user's credits. It returns rows as CSV and arun_id, orstatus: "running"for a long run.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
letends with;. - Every address carries its chain:
ethereum:0x…. - Name a column
name: exprinproject, andname = exprinextendandsummarize.expr as nameis 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> …letbinds a name to an address, an ABI or a constant. It cannot read a row (CQL2006).fromis the one source.from [a, b]reads each contract in the list. A second source goes insidejoin ( … )orunion ( … ).- 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$eventnames each one. An event the ABI lacks can be given as a signature:events 'TransferShares(address indexed from, address indexed to, uint256 sharesValue)'. as snames the source, and a call needs that name. Aletname is a call target too:stETH.totalSupply().
Blocks and time
at | Reads |
|---|---|
| none, on state | the newest block |
| none, on events | a recent window, 10 000 blocks on Ethereum (CQL3901) |
at latest, at finalized | the newest block, or the newest the chain will not reorganise |
at 26_000_000 | one block |
at latest-7200..latest | a block range, both ends included |
at 25_800_000..26_000_000 every 7200 | each block divisible by 7 200 in the range, plus the end |
at time '2026-09-20'..'2026-09-23' every 1d | the first block at or after each day boundary, plus the end |
everytakes a number on a block range and a duration (s,m,h,d,w) on atimerange (CQL1019). It belongs to a state range. On one block or on events it isCQL3005.- A state range with no
everyis sampled for you, to stay under 10 000 rows per contract (CQL3900). Writeeveryto choose the step. On Ethereum, 7 200 blocks is about a day. latestis 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.
| Operator | Does | Example |
|---|---|---|
where | keeps rows whose condition is exactly true | where t.to == wstETH |
project | keeps only the listed columns | project { block: $block, supply } |
extend | adds columns and keeps the rest | extend pooled = s.getTotalPooledEther() |
expand | one row per element of an array | expand r.getStakingModuleIds() as id index i |
summarize | aggregates, per group after by | summarize total = sum(t.value) by sender = t.from |
join | pairs rows with a second pipeline on keys | join kind=leftouter ( from … ) on block |
union | appends a second pipeline's rows | union ( from … ) |
order by | sorts, stably | order by total desc, sender asc |
take | keeps the first n rows, n a literal | take 20 |
distinct | keeps the listed columns, the first row of each combination | distinct 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:
- An ABI in the query.
let v = abi ['function getDebt() view returns (uint256)'];. A JSON ABI array works too. - A built-in.
erc20,erc721,erc1155,erc4626andmulticall3. - The user's library. A name in quotes:
with { abi: 'lido' }.list_abislists the names. A name found nowhere isCQL2008. - 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 noabiat 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) }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
| Where | Written | Applies to |
|---|---|---|
| on the name | let x = ethereum:0x… with { abi: v }; | every use of x |
| on the source | from x with { abi: v } as s | every call through s |
| on one call | s.f() with { abi: v } | that call only |
| on an address the query finds | expand … 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() }(s.getLidoLocator()) with { abi: locatorAbi }callsgetLidoLocatorwith stETH's own ABI. The columnlocatorcarrieslocatorAbi, and every later call throughlocatoruses it.s.getLidoLocator() with { abi: locatorAbi }useslocatorAbifor the call togetLidoLocatoritself. The column carries no ABI, solocator.withdrawalQueue()is refused asCQL2063.
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) }asnames the element andindex inumbers 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 }. Awithstraight after the operand, beforeas, configures the callgetDestinations()and givesdno ABI. expand [a, b] as holderunfolds a list you write.expandmultiplies rows, so sample the source if you meet a cap after one.
Joins and unions
join ( … ) on keykeeps rows with a match on both sides.kind=isinner(the default),leftouter,rightouter,fullouter,leftsemiorleftanti.on blockjoins on a column both sides have, and it appears once.on t.to == w.fromcompares two columns and keeps both. Another column both sides have is kept twice, the right one renamedvalue1or$address1.- A joined row is read at the left row's block. To read the right side at its own block,
extendinside the parentheses. - Two state series join on
$blockwhen both have the same range andevery. Across chains,on $blockisCQL3007. Give each sideextend t = bin($sampleAt, 1h)and joinon t. A call on another chain's address needswith { block: blockat(chain(x), $timestamp) }(CQL3006). union ( … )appends rows and matches columns by name. After it the aliases name nothing, so call through aletname.- A join or a union holds both sides at once. Summarize or narrow each side inside its parentheses.
Types and numbers
| Type | Written | Note |
|---|---|---|
bigint | 1_000_000, 1e18 | Whole, any size. Every uint256, int256 and $block. |
decimal | 1.05 | exact, never floating point |
address | ethereum:0x… | Always with its chain. Lowercase, or a correct checksum (CQL1006). |
bytes, bytes1 … bytes32 | 0xff | Hex is bytes, not a number. tobigint(0xff) is 255. |
datetime, duration | datetime('2026-09-01'), 1h, 7d | UTC, to the second |
format(x, 18)moves the point 18 places and never rounds:format(1e18, 18)is1.0.parse(1.05, 18)goes back. For a token you do not know, readdecimals()first.bigint / biginttruncates:7 / 2is3. Writetodecimal(a) / bfor a rate. A division with adecimalgives 38 significant digits, and a run that rounded says so (CQL4901).null + 1isnull.coalesce(x, 0)andisnull(x)handle it, andwheredrops anull.t.from == address(0)matches the zero address. A bare0x…literal compared with an address ignores the chain.datetime - datetimeis aduration. Write$timestamp + 5s, not$timestamp + 5(CQL2012).x as addressreads a 32-byte word, such as a storage slot:s.$storage[keccak256('aragonOS.appStorage.kernel')] as address.- In the CSV that
run_queryreturns, a big integer is a decimal string.
System columns
| Column | Holds |
|---|---|
$chain | the row's chain |
$block | the block that answered the row, a bigint |
$blockHash, $timestamp | that block's hash and time |
$address | the contract, or the event's emitter |
$sampleAt | the boundary that selected a sampled row, otherwise null |
$tx, $txIndex, $logIndex | events only: the transaction, its index, the log's index |
$event, $topics, $data | events 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) }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) }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) }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 descA 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 }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) }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.
| Code | Means | Fix |
|---|---|---|
CQL1016 | a syntax error | Read what it expected. Often a let without its ;, or a stage without its pipe. |
CQL1012 | expr as name | name = expr, or { name: expr } in project. |
CQL1006 | an address fails its checksum | Use the hint's spelling, or lowercase. |
CQL2002 | an unknown name | Check the alias, and whether an earlier project dropped the column. |
CQL2004 | a column named by a keyword, such as from | t.from, `from`, or by sender = t.from. |
CQL2006 | a let, or options on a name or a source, read a row | Use extend, or put the option on the call. |
CQL2008 | an ABI name not built in and not in the library | Check list_abis, or write abi […] inline. |
CQL2010 | a function the ABI does not have | Check describe_contract. Attach the ABI that has it. |
CQL2012 | no overload takes these types | tobigint for bytes, $timestamp + 5s for time. |
CQL2050 | an expression with no name | name: expr. |
CQL2062 | an alias whose address column a stage replaced or dropped | extend bAddress = b.$address, or keep the column. |
CQL2063 | a call through an address found at run time, with no ABI | expand … as d with { abi: v }, (s.f()) with { abi: v }, or .call(). |
CQL3001 | a block beyond the head | Check list_chains, or use latest. |
CQL3005 | every on one block or on events | Remove every. |
CQL3006 | a call on another chain's address with no block | with { block: blockat(chain(x), $timestamp) }. |
CQL3007 | $block compared across chains | Join on bin($sampleAt, 1h). |
CQL3008 | no verified ABI for the address | Inline abi […], the library, or .call(). |
CQL3013 | a function no ABI in range has, often on a proxy CQL did not follow | Attach the implementation's ABI with with { abi: … }. |
CQL3014, CQL4004 | more than 100 000 rows out of one operator | Narrow the range, sample, or summarize. |
CQL3016, CQL4008 | more than 50 000 requests | A larger every, a shorter range, or fewer calls. |
CQL4006 | the run passed its timeout | Narrow the range or sample more coarsely. |
CQL0402 | not enough credits for the run | Tell the user. Running again does not help. |
CQL0429 | too many requests | Wait the seconds it names, then try again. |
cql_primer with a code as topic explains any other code.
Read next
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.