A step-by-step guide to getting started
Every computer language written to date share a common set of logic constructs. However different they may be one from another, all of them will allow programmers to create conditional expressions and loops to perform an action based on some evaluation.
This third part of our series of articles about blockchain Smart Contract programming in Python will show how to use conditional expressions and loops in SmartPy language.
Lets begin by opening the SmartPy online editor by pointing our browser to https://smartpy.io/demo
Then copy&paste the code below (from our last article) to the editor screen:
# Imports the SmartPy library. import smartpy as sp# Defines the class MyClass and its constructor. class MyClass(sp.Contract): def __init__(self): self.init(result = 0) # Defines the Smart Contract entry point. @sp.entryPoint def myEntryPoint(self, params): self.data.result = params.op1 + params.op2 # Creates the "test" to simulate the Smart Contract call. @addTest(name = "myFirstSmartContractTest") def mySmartContractTest(): # Creates a string variable to build the output. html = "" # Instantiates an object of class "MyClass". mySmartContract = MyClass() # Calls the "myEntryPoint" method passing parameters. html += mySmartContract.myEntryPoint(op1 = 1, op2 = 2).html() # Outputs the result to screen. setOutput(html)
Before we begin, click on the button “Evaluate script & run tests” to check if everything is running fine.
Ok. Now, lets suppose (for didatics purpose) we have to add a rule to our sum script. Consider we cannot allow the user to pass zero as a parameter value to our Smart Contract. So we will have to put a condition in our code. In the majority of computer languages this would be done by adding an “IF” statement. In SmartPy it’s no different:
sp.if (params.op1 == 0): self.data.result = -1 sp.else: self.data.result = params.op1 + params.op2
The above piece of code just tests if the first passed parameter (op1) is equal to zero. If this is true, the script puts -1 in the storage result variable of the contract and will not execute the sum operation (as it is inside the “else” statement). Copy&paste the code right after the definition of “myEntryPoint”. Pay attention to indentation (must be 4 chars or TAB). Change the first passed parameter in the call for “myEntryPoint” to 0. Run the contract by clicking in “Evaluate script & run tests”. You should get a result like this:
Now, change back the passed parameter to 1. Run again. You will see that now the sum was made, because the code inside the “else” statement got executed.
Another way of checking conditions is using the “verify” statement:
sp.verify(params.op1 != 0) self.data.result = params.op1 + params.op2
Two differences here. First, note that you now check the condition you want to match (op1 being different from zero). Second (and most important), is that this “verify” statement will halt the script execution an leave with an “exception” (error). Copy&Paste the above code into the editor (replacing the sp.if-sp.else structure). Then, change the op1 parameter to 0. Run the script. See what you get:
In computer programming, loops are code execution repetition over a certain number of times or known condition. Lets suppose we want to execute 5 times a sum operation. We have to use a conditional loop usually known as “for-next” or “do-while”. In SmartPy that’s the way we do it:
sp.for i in sp.range(0, 5): self.data.result += params.op1 + params.op2
Put the above code on your editor and run your script. Using op1=1 and op2=2, you should get “15” as a result (it will sum 1+2 = 3, then accumulate the result 5 times = 15):
Another way to loop over a condition is to use the “whileBlock” construct:
@sp.entryPoint def myEntryPoint(self, params): self.data.i = 0 with sp.whileBlock(self.data.i <= 30): self.data.i += params.op1 + params.op2
This will loop the sum operation until our variable “i” has a value higher than 30:
And that’s it for today! Do your own experimentation with the different types of conditional expressions shown here. Remember that practice makes perfect. The purpose of these tiny lessons is to make you more familiar with the SmartPy IDE and command syntax, so we can do more advanced things in the future. I also recommend going through the templates that come with the online editor, as it is a nice way to discover new things in advance.