Lesson: Introduction to SmartPy

Ask Question

Chapter 9

Tinkering with strings & boolean types in SmartPy

Intro:

Let us quickly understand how strings and boolean types are handled in SmartPy!

In the later half of the chapter, you also will learn to add the ability to know whether your Cryptobot is dead or alive when you are away!

drawing

Study Time:

String:

A string is traditionally a sequence of characters eg: “terminator”.

String type is represented by type sp.TString in SmartPy.

## Here name has type of "sp.TString" 
## which SmartPy automatically infers by it’s use
self.init(name = "World Bank")

## You can also explicitly give 
## the string type by doing the following:
self.data.name = sp.string("terminator")

Notes:

  1. You would rarely use the sp.string(“...”) syntax in practice.
  2. You can read more in depth about how SmartPy handles strings here.

Boolean:

The type of boolean values i.e True, False.

Boolean type is represented by type sp.TBool in SmartPy.

## crypto_bot_is_alive is a variable that stores 
## whether the Cryptobot is alive or got eaten by a Alien!. 

## Here crypto_bot_is_alive has type of "sp.TBool" 
## which SmartPy automatically infers by its use. 
self.init(crypto_bot_is_alive = True)

## You can also explicitly give the 
## bool type by doing the following:
self.data.crypto_bot_is_alive = sp.bool("True")

Notes:

  1. You would rarely use the sp.bool(“...”) syntax in practice.
  2. You can read more in depth about how SmartPy handles boolean here.

#buidl-ing time:

New feature Request!

Do you know that your Cryptobot can keep fighting aliens 24*7 while you can netflix and chill?

How is that possible?

  1. The Tezos blockchain runs 24*7 round the year.
  2. Since your Cryptobot contract is going to be deployed on the Tezos blockchain, using the rules you have defined in your Cryptobot contract, the Cryptobot can keep fighting without requiring your attention. ⚔️

That’s the power of smart contracts!

But at the moment, there is no way to know whether your Cryptobot is dead or alive while you are away. You should get alerted if your Cryptobot dies! 💀

Step by step walkthrough:

  1. Let us add is_alive variable to our Cryptobot contract storage which takes in the current state of life from the user at the time of contract initialization.

  2. Since we want to let user initialize is_alive value at class invocation time, add life_state as an argument to the __init__ function.

  3. Assign life_state argument to is_alive state variable.

Testing our code:

  1. In our test function, pass life_state = True as an argument when invoking Cryptobot class

  2. We will use scenario.verify, to check the current whether Cryptobot is alive or not by checking the current value of is_alive state variable:

## Syntax for using `scenario.verify`: 

scenario.verify(condition)

## Example usecase: 
## To check whether Bank's current name is World Bank or not. 
scenario.verify(bank_test_contract.data.name == "World Bank")

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.

Contract.py

Loading...