Skip to content

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 }

Open in workbench →

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' }

Open in workbench →

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 }

Open in workbench →

Members and calls ​

. reads a field or calls a function:

  • e.reportTimestamp is an event argument.
  • s.getTotalShares() calls the contract at the row's block.
  • s.$address is a system column of that source.
  • r.field reads 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))
  }

Open in workbench →

movedwhyoknoCode
nullALLOWANCE_EXCEEDEDnullno 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 callrevert_reason gives
succeedednull
reverted with a message, as require(ok, "message") doesthe message
panicked, for example on an overflowPanic(0x11), with the panic code
reverted with a custom error its ABI declaresName(arg1, arg2)
reverted with an error CQL cannot namethe revert data as hex
reverted with no datathe empty string
returned nothing, because the address has no codeno return data
returned data its outputs do not decodedecode error
had an argument outside its parameter's rangeencode error: …
had a with { value } outside 0 to 2^256 − 1encode error: value … is outside uint256. CQL sends no request.
had a with { block } below zeroblock out of range
had a null target or argumentnull. 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)')
  }

Open in workbench →

totalSharessharesrawbySel
777830418838473394816304637192308056129818750325900x000000000000000000000000000000000000000000066f1eb707f2179c1633e67778304188384733948163046

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
  }

Open in workbench →

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

Open in workbench →

See also ​