What counts
Every statement and expression owns the tokens it was written with, apart from those that belong to the statements and expressions inside it. A node’s tokens are counted just before it runs, so a run counts exactly the code it executed:- Declarations are free.
request Name, a contract’s name, version and parameter list, and the body’s outer braces aren’t counted. ifcounts what it evaluates. Each condition is counted when it is evaluated, together with itsiforelse if, its parentheses and its braces. Then only the body of the branch taken is counted.elseand its braces count only when the else body runs. Untaken branches cost nothing.- Short circuits skip. The operand that
&&or||doesn’t evaluate isn’t counted. - Strings are one token, plus the tokens inside each
${}interpolation as it is evaluated. - Parentheses count with the expression that encloses them, and an assignment’s target counts with the assignment.
The
else branch would have cost 5 (else { } and return "B"), so the most any run of this script can execute is 4 + 8 + 5 = 17.
The most a script can execute
RequestScript has no loops and no user-defined functions, so every script has a finite maximum, known before it runs: every statement counted once, with eachif taking its costliest path. A host can price the worst case up front, then charge only what a run used.
In the library
requestscript 1.2.0 and later exports the meter:
result.usage.tokensis the count for a run that finished.meter.tokensholds it too, including for a run that failed partway.- With a limit, a node that would take the count past it isn’t run or counted, and the run fails with
TokenLimitError, aRuntimeError. signalis an optionalAbortSignal, checked before each statement, expression and resource call.
tokens. An if statement’s tokens are on its branches (tokens) and its elseTokens.