Skip to main content

Smart Contract Debugging

Introduction

Debugging smart contracts is possible with the integrated debugger in Visual Studio Code. You will be able to debug your contract just like you would debug a regular program.

Prerequisites

For this tutorial, you will need:

If you want to follow along, you can clone the mx-sdk-rs repository and use the crowdfunding-esdt example.

Step by step debugging

In VSCode, you can put breakpoints anywhere in your code, by simply clicking to the left of the line number. A red dot should appear to mark the breakpoint as registered by the environment:

img

Once you've done that, you can debug your test function by pressing the Debug button above the test function name:

img

If it doesn't appear, you might have to wait a bit for rust-analyser to load, or you might've forgotten the #[test] annotation.

Once you've started the test, it should stop at the breakpoint and highlight the current line for you:

img

Then, you can use VSCode's step by step debugging (usually F10 to step over, F11 to step into, or shift + F11 to step out).

Inspecting variables

For base Rust types, like u64 and such, you will be able to simply hover over them and see the value.

You might however, try to hover over the target variable for instance, and will be immediately disappointed, since all you'll see is something like this:

handle:0
_phantom:{...}

This is not very helpful. Unfortunately, for managed types you don't have the actual data in the type itself, you only have a handle (i.e. an index) in a stack somewhere.

For that reason, we have the sc_print! macro:

sc_print!("{}", target);

Adding this line to the beginning of the #[init] function will print 2000 in the console.

Printing formatted messages

If you want to print other data types, maybe even with a message, you can use the sc_print! macro all the same.

For example, if you were to add this to the start of the #[init] function:

sc_print!(
"I accept {}, a number of {}, and only until {}",
token_identifier,
target,
deadline
);

This macro would print the following:

"I accept CROWD-123456, a number of 2000, and only until 604800"

Note: For ASCII or decimal representation, use {}, and for hex, use {:x}.