Lesson: Introduction to SmartPy

Ask Question

Chapter 13

Tinkering with conditional code in SmartPy - sp.verify


In this chapter we are going to learn about verify and specifically sp.verify which lets us write conditional code.

sp.verify comes in handy to prevent an entry function from proceeding if certain conditions are not met.

Examples of usecases where sp.verify can help:

  1. To check whether your account has enough money to transfer before initiating transfer.
  2. To add barriers to your entry functions so that they are only callable by the owner.

In the later half of the chapter, we will use sp.verify to all of our entry point functions so that they are only callable by the bot_manager!

Study Time:

The syntax to use verify looks like this:

sp.verify(condition, message = "fail msg")

## example: 
sp.verify(2 + 2 == 4)
sp.verify(4 > 5, message = "5 > 4")


  1. sp.verify checks whether the condition evaluated to True and raises an error if it doesn’t. The condition can be anything as long as the final outcome results in a boolean.
  2. An optional parameter message is raised if condition is not met.

To specifically check for equality conditions, you can use sp.verify_equal.

sp.verify_equal(v1, v2, message = "fail msg")

## eg: 

sp.verify_equal(2 + 2, 4)

It serves the same purpose by checking equality between v1 and v2

#buidl-ing time:

New feature request!

Currently, entry functions of your Cryptobot contract i.e it’s abilities are callable by anybody in the world!

In theory that means anybody can:

  1. change your cryptobot’s name
  2. move the cryptobot anywhere in the world
  3. as well as empty all your bullets without your permission!

Let us first learn to solve the third problem by allowing only bot_manager’s to shoot aliens.

Step by step walkthrough:

Let’s start:

  1. In the shoot_alien entry point function add sp.verify.
  2. In the sp.verify’s condition field:
    • use self.data.bot_manager to access the address stored inside bot_manager.
    • check it’s equivalency with sp.sender:
      • SmartPy automatically populates sp.sender with the address that called the current entry point.
      • This is an important concept to keep in mind as sp.sender is very useful when working with entry point functions.
  3. In the sp.verify’s message field:
    • pass an error message on the lines of “Error: non manager call”.
    • This will be sent to users who are trying to be sneaky by calling our Cryptobot’s shoot_alien function!

That will show them who is the boss! 😎

Alt Text

Testing our code:

Now since we have added that only the address stored inside bot_manager is able to call shoot_alien entry point function, we would also need to modify shoot_alien calls in our testing function:

  1. As you remember, we had stored our user account address in my_address
  2. We will reference it when we call our shoot_alien function by using the run method.
    • Syntax: shoot_alien("<alien type>").run(sender = my_address)
    • The above code tells SmartPy that we are calling shoot_alien with sender value as my_address.
  3. SmartPy takes whatever address that was sent to sender and populates sp.sender in our shoot_alien’s function definition.
    • Didn’t we already mention that SmartPy is awesome!

The implementation for all entry functions will remain the same and we leave that as an exercise to the user!

When you’re finished:

  1. Click on “Check” to check your submission with the correct answer.
  2. Click on “Show Answer” in case you get stuck.

You can also tinker around with the final code on SmartPy’s online IDE. Try running the test by clicking on the ▶️ button!