Debugging
You can debug SmartPy programs just like any other Python program; just make sure that the Python version that you are using has SmartPy installed.
Logging
SmartPy includes ways to write debugging information to the console and the compiler log. Within entrypoints, you can print to the console with the sp.trace
statement.
- sp.trace(x)
Prints
x
to standard error. Does not have any effect in the compiled contract or in the online IDE.
As an example, consider the fibonacci_view.py template. In order to gain a better understanding of which recursive calls are made in which order, we can insert an sp.trace
statement at the beginning of each call to the main view, resulting in the following code:
import smartpy as sp
@sp.module
def main():
class FibonacciView(sp.Contract):
@sp.onchain_view()
def fibonacci(self, n):
sp.trace(n)
sp.cast(n, int)
if n < 2:
return n
else:
n1 = sp.view("fibonacci", sp.self_address(), n - 1, int).unwrap_some()
n2 = sp.view("fibonacci", sp.self_address(), n - 2, int).unwrap_some()
return n1 + n2
@sp.add_test()
def test():
s = sp.test_scenario("FibonacciView", main)
c = main.FibonacciView()
s += c
s.verify(c.fibonacci(5) == 5)
Now when we run this, a line is printed on standard error for each call to the fibonacci
view:
$ python fibonacci_view.py
5
4
3
2
1
0
1
2
1
0
3
2
1
0
1
To print values outside a contract, such as in a test scenario, you can use the ordinary Python print()
function. In test scenarios you can also use the scenario.show()
function to print to the compiler log, as described in Test scenarios.