Lesson: Introduction to SmartPy

Ask Question

Chapter 7

Creating a basic entry point function


Now that you know how to define and test your Cryptobot contract, time to start giving it small abilities!

Previously you had given the name of “terminator” to our Cryptobot contract. But then, if you come up with a cooler name in the future, how do you go about changing your cryptobot’s name?

In this chapter, you will learn to add a functionality that enables you to change your cryptobot’s name using entry point functions!

Study Time:

What are entry point functions?

Entry functions are basically class methods with the added superpowers to modify a contract’s local storage!

As we learnt about the concept of “state” and “state variables” in
Chapter 5, these functions are mainly used to access and modify a contract’s state by altering state variables values after the contract is deployed.

Show me an example:

Remember the example of our Bank contract from previous chapters:

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

Let us add an entry point function to the above contract so that we can modify name’s value:

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

Explaining the code above:

  1. We add a @sp.entry_point decorator to instruct the compiler that the following function is an entry point function.

  2. We then define the modify_name function and pass self and new_name as function arguments.

    • new_name will contain the new name that we would pass to this function
  3. Then we access name, which is a variable stored inside our contract storage, by using self.data.

    • This is an important concept to remember, for anytime you need to access anything from the contract storage, you need to use self.data.
  4. We assign new_name to self.data.name.

    • This line of code will change the name contents to whatever you send in the new_name variable!

Simple, right?

Let us use our testing skills that we gained from Chapter 6 to test our newly created modify_name function!

class Bank(sp.Contract):
   def __init__(self):
       self.init(name = "World Bank")
   def modify_name(self, new_name):
       self.data.name = new_name
@sp.add_test(name = "Test Bank Contract")
def test():
    scenario = sp.test_scenario()
    bank_test_contract =  Bank()
    scenario += bank_test_contract
    scenario += bank_test_contract.modify_name("Local Bank")

Explaining the code above:

  1. We had previously given our Bank contract the name of “World Bank”.
  2. In our test function:
    • we call the modify_name entry point function on bank_test_contract, which is an instance of Bank
    • and in calling the function we pass the new name i.e “Local bank”.

As we know the above testing code simulates how smart contract code will behave once deployed.

The above code when simulated, will change the name variable’s content from “World Bank” to “Local Bank”

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

#buidl-ing time:

Let’s change our Cryptobot’s name from “terminator” to “punky terminator” by doing the following:

  1. Add a change_name entry point function just after the scope of __init__ initialization function finishes.

    • Pass self and new_name as arguments to change_name function.
    • Use self.data.name to access Cryptobot contract’s internal storage. And assign it the value of new_name.
  2. Test your newly created change_name function by calling change_name on test_bot.

    • Pass in “punky terminator” in change_name’s function call.

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.