Appearance
Expressions
Everything you can write inside an operator: arithmetic, comparison, conditions, member access, calls on contracts, raw calls, storage reads and casts.
Arithmetic
+, -, *, / and % work on numbers, with the usual precedence. -x negates a number or a duration. Whole-number division truncates. A division involving a decimal is computed to 38 digits. Numbers has the detail. + is not concatenation — concat(a, b) joins strings and bytes.
Comparison
== and != work between any two values of one type. <, <=, >, >= work between numbers, times, durations and same-width bytes. CQL refuses a chain of comparisons, a < b < c. Write a < b and b < c.
cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
from stETH at latest-21600..latest every 7200 as s
| extend pooled = s.getTotalPooledEther(), shares = s.getTotalShares()
| where pooled >= shares and shares != 0
| project { block: $block, pooled, shares }and, or, not
Three-valued: null and false is false, null or true is true, and any other mix with null is null. not binds more loosely than a comparison, so not a == b means not (a == b). CQL refuses a || b and a && b. Write or and and.
The conditional
iff(c, a, b) gives a when c is true and b otherwise, null included. c ? a : b is the same thing in another spelling. Only the chosen branch is evaluated, so a call in the other branch costs nothing.
cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
from stETH at latest-21600..latest every 7200 as s
| project { block: $block, staking: s.isStakingPaused() ? 'paused' : 'open' }Put a space after the : when the branch after it starts with 0x. Without the space, b:0x… reads as an address.
in and between
x in (a, b, c) asks whether x equals one of the listed values. x in xs — no parentheses — asks whether x is an element of the array xs. x between (lo .. hi) includes both bounds.
cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
let wstETH = ethereum:0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
let treasury = ethereum:0x3e40D73EB977Dc6a537aF587D48316feE66E9C8c;
from stETH at latest-21600..latest every 7200 as s
| extend paidTo = s.getTreasury(), ethPerShare = format(s.getPooledEthByShares(1e18), 18)
| where paidTo in (treasury, wstETH) and ethPerShare between (1 .. 2)
| project { block: $block, ethPerShare }Members and calls
. reads a field or calls a function:
e.reportTimestampis an event argument.s.getTotalShares()calls the contract at the row's block.s.$addressis a system column of that source.r.fieldreads a field of a record.xs[0]indexes an array, counted from zero.
The ABI gives a call's result its type:
- A single output is the value.
- Several named outputs are a record.
- Several unnamed outputs are
_0,_1, ….
A call the contract reverts gives null. revert_reason says why.
revert_reason — why a call gave null
revert_reason(x) gives the reason a call gave null:
cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
let wstETH = ethereum:0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
let nobody = ethereum:0x000000000000000000000000000000000000dEaD;
from stETH as s
| extend moved = s.transferFrom(wstETH, nobody, 1)
| project {
moved,
why: revert_reason(moved),
ok: revert_reason(s.getTotalShares()),
noCode: revert_reason(nobody.call('balanceOf(address)(uint256)', wstETH))
}| moved | why | ok | noCode |
|---|---|---|---|
| null | ALLOWANCE_EXCEEDED | null | no return data |
wstETH gave no allowance to the zero address, so stETH refuses the transferFrom. moved is null, and why is the message stETH reverted with. getTotalShares() succeeded, so ok is null. nobody has no code, so its call returns nothing.
x is a call or a .call, or a column that holds one, as moved does here. Anything else is CQL2021. CQL reads x and revert_reason(x) from the same call, so the reason costs no extra request.
| The call | revert_reason gives |
|---|---|
| succeeded | null |
reverted with a message, as require(ok, "message") does | the message |
| panicked, for example on an overflow | Panic(0x11), with the panic code |
| reverted with a custom error its ABI declares | Name(arg1, arg2) |
| reverted with an error CQL cannot name | the revert data as hex |
| reverted with no data | the empty string |
| returned nothing, because the address has no code | no return data |
| returned data its outputs do not decode | decode error |
| had an argument outside its parameter's range | encode error: … |
had a with { value } outside 0 to 2^256 − 1 | encode error: value … is outside uint256. CQL sends no request. |
had a with { block } below zero | block out of range |
had a null target or argument | null. CQL sends no request. |
.call() — a call without an ABI
x.call(sig, args…) calls any function by its signature and needs no ABI at all:
cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
let wstETH = ethereum:0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
from stETH with { abi: none } as s
| project {
totalShares: s.call('getTotalShares()(uint256)'),
shares: s.call('sharesOf(address)(uint256)', wstETH),
raw: s.call('getTotalShares()'),
bySel: s.call('0xd5002f2e', '(uint256)')
}| totalShares | shares | raw | bySel |
|---|---|---|---|
| 7778304188384733948163046 | 3719230805612981875032590 | 0x000000000000000000000000000000000000000000066f1eb707f2179c1633e6 | 7778304188384733948163046 |
The signature is name(inputs)(outputs). If you omit the outputs, you get raw bytes. If you give a 4-byte selector instead of a name, the outputs become a second argument, and CQL infers the input types from the types of the arguments. A bytes argument is always bytes, also when it is 32 bytes long. Named outputs make a record. with { abi: none } keeps CQL from looking up an ABI at all. .call is also the way past an ambiguous overload (CQL2011).
Three of the four columns send the same bytes to the contract, so CQL makes that call once.
$storage — raw storage
x.$storage[slot] reads one 32-byte word of a contract's storage at the row's block. The slot is a number or a bytes32. The result is bytes32, which you usually cast:
cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
from stETH as s
| project {
kernel: s.$storage[keccak256('aragonOS.appStorage.kernel')] as address,
elRewards: s.$storage[keccak256('lido.Lido.totalELRewardsCollected')] as uint256
}stETH keeps its state in named slots, so keccak256 of the name is the slot. For a mapping entry, an array element or a struct member, slot() computes the slot from Solidity's layout — Functions.
Casts
x as T reinterprets a 32-byte word: as address, as uint256 (or as bigint), as int128, as bytes4, as bool, as string. It is a reinterpretation, not a conversion. To convert a decimal to a bigint, use tobigint. This as is not the one that names a source.
Options on one call
s.transfer(nobody, 1e18) with { caller: wstETH } sets the options of that call alone — who is asking, at which block, with what value, which ABI. with options.
Backticks
If a column's name is a keyword, write the name in backticks: `from`. A column named to needs no backticks, but from does. The backticks change nothing else. `value` and value are the same column.
cql
let stETH = ethereum:0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
from stETH at latest-21600..latest every 7200 as s
| project { `from`: $block, ethPerShare: format(s.getPooledEthByShares(1e18), 18) }
| where `from` > 0