Debug a contract

Step through and debug your contracts inside the command line.


Inside the console, you can use these debugging commands:

Terminal
$
::trace (contract-call? .my-contract my-function)
$
::debug (contract-call? .my-contract my-function)
$
break my-function

Debug navigation commands:

  • step or s - Step into
  • finish or f - Step out
  • next or n - Step over
  • continue or c - Continue execution

Let's take the clarity_counter_ contract as an example:

(define-map Counters principal uint)
(define-read-only (get-count (who principal))
(default-to u0 (map-get? Counters who))
)
(define-public (count-up)
(ok (map-set Counters tx-sender (+ (get-count tx-sender) u1)))
)

Trace

The ::trace command expects an expression, so make a contract-call? with our count-up function and see what happens.

Terminal
$
::trace (contract-call? .counter count-up)

Breakpoints

You can also set a breakpoint at a specific line to better understand what's happening in your contract.

With ::debug, you can add breakpoints at a specific line of a contract or function to better understand what's happening in your contracts.

(define-map Counters principal uint)
(define-read-only (get-count (who principal))
(default-to u0 (map-get? Counters who))
)
(define-public (count-up)
(ok (map-set Counters tx-sender (+ (get-count tx-sender) u1)))
)
(define-public (count-twice)
(double)
)
(define-private (double)
(begin
(unwrap-panic (count-up))
(count-up)
)
)
Terminal
$
::debug (contract-call? .counter count-twice)
$
break count-up
Breakpoint Commands

To step through these breakpoints, you can use one of the following commands:

  • Step-in (step or s): Step into the sub-expressions.
  • Step-out (finish or f): Complete execution of the current expression and return the result to the parent.
  • Step-over (next or n): Continue to completion of the current expression, stepping over sub-expressions.
  • Continue (continue or c): Continue execution until hitting a breakpoint or completing execution.

Using the continue command, the breakpoint you set in the double function will trigger twice due to two count-up calls, which enables you to do variable and map analysis.

Next steps