Validation and Analysis

Clarinet provides powerful tools for validating, analyzing, and debugging your smart contracts. From static type checking to real-time cost analysis, you can ensure your contracts are correct and efficient before deployment.

Contract validation spans static analysis, runtime debugging, and cost optimization. Each discipline helps you gain confidence in contract behavior.

Understanding contract validation

Static analysis vs. runtime debugging

Static analysis
Runtime debugging

Catches issues before deployment

Reveals behavior during execution

Flags type mismatches and syntax errors

Shows actual execution costs

Ensures trait compliance

Exposes state changes and side effects

Detects undefined variables

Highlights transaction flow

Validates function signatures

Surfaces performance bottlenecks

Static analysis

Run comprehensive validation with clarinet check:

clarinet check

Successful output resembles:

✔ 3 contracts checked

When validation fails, Clarinet provides detailed diagnostics:

✖ 1 error detected

Error in contracts/token.clar:15:10
  |
15|  (ok (+ balance amount))
  |         ^^^^^^^
  |
  = Type error: expected uint, found (response uint uint)
1

Run basic checks

Use clarinet check to validate your contracts and catch type/syntax errors before deployment.

2

Check a specific contract

Focus validation during development on a single contract file:

3

Integrate into CI

Automate validation in continuous integration pipelines. Example GitHub Actions workflow:

Validation scope

Clarinet validates multiple aspects of your contracts:

Validation type
What it checks

Type safety

Function parameters, return values, variable types

Trait compliance

Implementation matches trait definitions

Response consistency

ok/err branches return the same types

Variable scope

Variables defined before use

Function visibility

Proper use of public, private, and read-only

Runtime analysis

The Clarinet console offers runtime tools that help you inspect behavior during execution.

Cost analysis with ::toggle_costs

Enable automatic cost display after every expression:

Execution tracing with ::trace

Trace function calls to understand execution flow:

Interactive debugging with ::debug

Set breakpoints and step through execution:

Common navigation commands:

  • step or s – step into subexpressions

  • finish or f – complete the current expression

  • next or n – step over subexpressions

  • continue or c – resume execution

Using ::get_costs for targeted analysis

Spotting costly operations with ::trace

Review the trace for loops with high iteration counts, nested map/filter operations, repeated contract calls, and large data structure manipulations.

Debugging workflows

Master interactive debugging to identify issues quickly:

Analyzing failed transactions with ::trace

Using ::encode and ::decode for inspection

Testing time-dependent logic

Last updated

Was this helpful?