title: “Hands-on debugging”
Debugging smart contracts
It’s a common situation when you encounter an error in your smart contract. In some cases you will see that 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.
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.