Lesson: Introduction to SmartPy

Ask Question

Chapter 12

Tinkering with addresses in SmartPy

Intro:

In Chapter 2, we read that a blockchain is made up of accounts and each account has an address serving as a unique identifier.

The unique identifier points to an account on the blockchain and it looks like this:

tz1Syu3KacZ8cy4286a4vaCeoMtwqVKHkaoj

In this chapter we are going to learn how SmartPy handles addresses under the hood. In the later half of the chapter, we will add the ability to add ownership rights to our cryptobot!

Study time:

Addresses can be of two types:

  1. An address of a user account eg: address of your Tezos wallet which you use to transfer XTZ to your friends.
  2. An address of a smart contract eg: address of your Cryptobot contract.

Addresses are represented by type sp.TAddress in smartpy.

## bot_manager is a variable 
## that stores the address of the user 
## who manages the cryptobot. 

my_address = sp.address("tz1Syu3KacZ8cy4286a4vaCeoMtwqVKHkaoj")

self.init(bot_manager = my_address)

## Update bot_manager value 
## in case you decide to sell your cryptobot
self.data.bot_manager = sp.address("tz1dFyuNkiSabQrU1ie9yTKbiTje3YbuXjX1")

Notes:

  1. Since addresses are fundamentally string values, we call sp.address on an address string to explicitly tell the compiler that this is an address value, and not a string.

buidl-ing time:

New feature request!

In our current Cryptobot contract implementation, there is no way to tell that you are the maker and manager of this Cryptobot.

You are spending so much time evolving the cryptobot’s abilities, that you had forgotten about this tiny but crucial detail!

Step by step walkthrough:

  1. Add a bot_manager state variable to the contract’s storage that will hold the address of the owner.

  2. Pass manager_address as parameter to the initialization function and assign it to bot_manager

Testing our code:

  1. Define my_address variable and give it the address value of tz1Syu3KacZ8cy4286a4vaCeoMtwqVKHkaoj

  2. Pass my_address as value to manager_address function argument and then use it to invoke the Cryptobot contract.


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!

Contract.py

Loading...