Debugging smart contracts
It’s a common situation when you encounter an error in your smart contract. In some cases, you will see that an unexpected exit code is returned. This can be a sign of a bug in your contract. There are several techniques to debug your contract.Log to the console
A straightforward and effective technique. The most common values to print are transactions and get‑method results. There are helper functions that let you inspect transactions in a developer‑friendly way.TypeScript
Dump values from a contract
Three TVM debug instructions exist: Availability depends on the language you use.- In Tolk, use functions from the globally available
debugobject. - In FunC, these methods are available globally in
stdlib.fc. - In Tact, use
dumpStackforDUMPSTKand dump function for the other two. Tact also prints the exact line wheredumpis called, so you can quickly find it in your code.
Explore TVM logs
TypeScript
vm_logs— outputs VM logs for each transaction; includes executed instructions and occurred exceptions.vm_logs_full— outputs full VM logs for each transaction; includes executed instructions with binary offsets, the current stack for each instruction, and gas used by each instruction.
vm_logs looks like this:
LDU 64) from the slice, but there is not enough data, so exit code 9 occurs.
Inspect the same code with the vm_logs_full verbosity level. (Output is heavily truncated from the top)
[bottom, ..., top], where top is the top of the stack.
Here, the stack contains two values:
- Slice being read
- Integer (
500000000)
load_uint(64) call that causes the issue and ensure enough bits are available or adjust the read width.
TVM log limits
The size of TVM debug output depends on the verbosity level:
| Level | Setting | Max size |
|---|---|---|
| 0 | none | 256 bytes (default) |
| 1–4 | vm_logs vm_logs_location vm_logs_gas vm_logs_full | 1 MB |
| 5 + | vm_logs_verbose and above | 32 MB |
Explore the transaction tree
For simple transaction trees, print all transactions and inspect them.TypeScript
- TonDevWallet trace view — requires the TonDevWallet application; does not require a custom
@ton/sandbox; requires the@tondevwallet/tracespackage. - TxTracer Sandbox — requires a custom
@ton/sandboxpackage; runs in your browser.
Debugging with TVM Retracer
Even when a contract executes successfully (exit code =0) with no errors, its actions may not produce the expected on-chain effect. TVM Retracer lets you replay the transaction and inspect VM-level execution.
Scenarios for retracing
- All transaction phases complete without errors, yet the expected outcome is missing.
- An action is skipped, or a transfer does not reach its destination.
- You need a step-by-step view of how the TVM executed your contract logic.
How to analyze a transaction
- Obtain the transaction hash from a blockchain explorer.
- Open TVM Retracer and enter the transaction hash.
- Review the execution:
- Inspect Logs section for executed instructions and exceptions.
- Examine Actions cell (C5) to review data passed between contracts.
- Check message modes — some modes can suppress errors, causing actions to be skipped.