Lesson: Introduction to SmartPy

Ask Question

Chapter 5

Initializing contract's storage


In this chapter, we will give our cryptobot a name and store it inside the contract!

Let us break the above down to arrive at what concepts you will be learning:

  1. “Give our cryptobot a name” - we will need to learn about variables. In the world of smart contracts, variables are more commonly referred to as state variables.
  2. “And store it inside the contract” - we will need to learn about the concept of contract storage.

Let’s learn them in the reverse order since that is how the concepts relate with each other.

Study Time:

What is contract storage?

Think of this as your contract’s personal database. It is used to record the current “state” of the of your smart contract by using variables.

When you deploy the contract on the blockchain, this storage becomes part of the global storage of all the contracts in the world!

What is state in smart contract?

A “state” represents the current status of the smart contract like a visual screenshot. If you’d like your smart contract to do anything - like building shooting ability in your Cryptobot contract - it will involve changing the contract’s current state.

What are state variables?

State variables act as a single slot in the “contract storage” and are the fundamental unit to record the current state of your smart contract.

For example:

bank_contract_storage = {
  'name': "World Bank"
  'money_in_all_accounts': 10000000

Explaining the code above:

  1. Here name and money_in_all_accounts act as state variables for bank contract.
  2. There values i.e “World Bank” and 10000000 act as the current values of the state.
  3. To change the name or money_in_all_accounts you will need to change their values i.e altering the contract’s current state into a new state.

### State A: Current State

bank_contract_storage = {
  'name': "World Bank"
  'money_in_all_accounts': 10000000

### State B: Reduce money_in_all_accounts by $1

bank_contract_storage = {
  'name': "World Bank"
  'money_in_all_accounts': 9999999

### Here we changed values by going from State A to State B. 

By looking at current values of contract’s storage, you are able to get vantage of the current state of the smart contract.

These are primarily used to :

  1. query the current state of your contract.
    • Eg: “What is the current amount of money stored in all accounts in World Bank?”
  2. change current state:
    • Eg: “Reduce amount of money stored in World Bank by $50,000”

by functions inside the contract, which will be learning in Chapter 7.

Example of initializing contract storage:

import smartpy as sp

class Bank(sp.Contract):
    def __init__(self):
        self.init(name = "World Bank")

Explaining the code above:

  1. We scaffold the Bank contract as we had learnt in the previous chapter.
  2. Next, using __init__ we define an initialization method for Bank that determines the initial storage during contract origination.
  3. _init_ calls self.init() which assigns name, a state variable, with “World Bank” and stores it inside the “contract storage”.

Python concept refresher:

Helpful links that may help in refreshing your Python concepts:

  1. Initializing a Python class.

#buidl-ing time:

Now let us practice what we learned so far and give a name to Cryptobot contract`!

  1. Using __init__, define the initialization function of your Cryptobot contract.
  2. Define name variable and initialize it’s value with “terminator” since that is the name of the species of the Cryptobot that you are building.

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.