Tinkering with strings & boolean types in SmartPy
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!
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")
- You would rarely use the
sp.string(“...”)syntax in practice.
- You can read more in depth about how SmartPy handles strings here.
The type of boolean values i.e
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")
- You would rarely use the
sp.bool(“...”)syntax in practice.
- You can read more in depth about how SmartPy handles boolean here.
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?
- The Tezos blockchain runs 24*7 round the year.
- Since your
Cryptobotcontract is going to be deployed on the Tezos blockchain, using the rules you have defined in your
Cryptobotcontract, 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:
Let us add
is_alivevariable to our
Cryptobotcontract storage which takes in the current state of life from the user at the time of contract initialization.
Since we want to let user initialize
is_alivevalue at class invocation time, add
life_stateas an argument to the
Testing our code:
In our test function, pass
life_state = Trueas an argument when invoking
We will use
scenario.verify, to check the current whether Cryptobot is alive or not by checking the current value of
## 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:
- Click on “Check” to check your submission with the correct answer.
- Click on “Show Answer” in case you get stuck.