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
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 checkSuccessful output resembles:
✔ 3 contracts checkedWhen 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)Run basic checks
Use clarinet check to validate your contracts and catch type/syntax errors before deployment.
Check a specific contract
Focus validation during development on a single contract file:
Integrate into CI
Automate validation in continuous integration pipelines. Example GitHub Actions workflow:
Validation scope
Clarinet validates multiple aspects of your contracts:
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
::toggle_costsEnable automatic cost display after every expression:
Execution tracing with ::trace
::traceTrace function calls to understand execution flow:
Interactive debugging with ::debug
::debugSet breakpoints and step through execution:
Common navigation commands:
Using ::get_costs for targeted analysis
::get_costs for targeted analysisSpotting costly operations with ::trace
::traceReview 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
::traceUsing ::encode and ::decode for inspection
::encode and ::decode for inspectionTesting time-dependent logic
Last updated
Was this helpful?
