What is PyTeal?

Transcript

Toggle full transcript
00:00

Hello, everyone. My name is Chris. And in this video we will kick off the PyTEAL video series by talking about what PyTEAL is and why it's one of the best ways to

00:10

build smart contracts on Algorand. Also, make sure to check out the Beaker Framework series overlaying on the top right of

00:20

this video right now to learn how Beaker wraps PyTEAL and makes writing smart contract with PyTEAL even easier. So what is PyTEAL? PyTEAL is a Python library that lets you write Algorand

00:30

smart contracts with python like syntax. I say python like syntax because PyTEAL programs are not really written with python

00:40

but are collections of PyTEAL expressions composed of other PyTEAL expressions. Now don't worry if you don't really understand what I mean here. There will be a separate video just on PyTEAL expressions.

00:50

So this is how it works under the hood. When you write your Algorand smart contract with PyTEAL you use the PyTEAL's compile program method to produce TEAL code automatically.

01:00

TEAL. This is shorthand for Transaction Execution Approval Language, and it's a low level assembly language that can be easily compiled to byte code for the Algorand Virtual

01:10

Machine to execute. Even if you write your smart contract with PyTEAL, it will greatly help you to understand how your contract works by learning about how TEAL works and what capabilities

01:20

DBM has. So be sure to check out the TEAL series that should be popping up on the top right now. Let's look at a simple counter example written with PyTEAL to get a feel of what a Python

01:30

smart contract looks like. Before we look at the code for you to follow along this video, you need to have

01:40

Docker Sandbox and PyTEAL installed and have sandbox running a local Algorand blockchain in dev mode. I will leave a link to the development environment setup video on the top right, so make

01:50

sure you have your dev environment set up before you continue. Here I have a new VS Code editor opened up with the project folder. What is PyTEAL? Before you start coding an Algorand smart contract, it's recommended

02:00

to create a python virtual environment (venv) to keep your projects separate from your operating machine. To do that, you want to open up your terminal.

02:10

Going to expand it a little. And then you want to do python -m env [space] env and that is going to create the venv folder.

02:20

Then to activate your virtual environment you want to do source venv/bin/activate.

02:30

Now if you do pip list, you can see that you're starting from a clean slate. So first thing you want to do is install PyTEAL. To do that you want to do PIP install PyTEAL.

02:40

And we did hit list again.

02:50

You can see that PyTEAL is installed. Now you're all ready to start coding. Now, for the sake of time. I'm going to use a completed example, but if you really want

03:00

to learn how to write PyTEAL smart contracts, you should follow along by coding for yourself. Now I am inside the what is PyTEAL folder and inside of it I created a counter.py

03:10

file where I'm going to write the code for my counter smart contract. And then I created a artifacts folder where all of the approval program,

03:20

clear program and my API is going to go when I compile my program. Now first thing I do is I import PyTEAL in.

03:30

And let's first look at this thing called a router. So whenever you write a PyTEAL smart contract, you want to set up the router first because this is like a command center that tells a smart contract, Hey,

03:40

you run this method when you get this kind of application call. You do this when you get a create application call and basically it handles

03:50

routing for all of the incoming application calls. Now, here inside of the router class, the first argument is going to be the name of the router. I called it Simple Counter.

04:00

And as a second argument I put in the BareCallActions. Inside of the BareCallActions, you want to specify what happens during creation, during opt in and all the other on complete application calls.

04:10

Since this is a simple example, I've only defined what happens during creation of the application. And if you look at the definition here, you can see that the rest of

04:20

the on complete actions are default to never, which means to never approve these incoming on complete application calls.

04:30

Now inside the create only, as an argument, I'm putting in this handle creation variable and you can see that I have defined that above over here and handle creation is basically

04:40

a variable that holds a PyTEAL expression. Now this PyTEAL expression is a sequence, so inside of the sequence are going to be multiple PyTEAL expressions and all of them have to go through for

04:50

this sequence to go through. And the line for what we're doing here is you're creating a global state by doing

05:00

app.globalPut. The key of the global state is called count (in bytes) and the default value of that global state will be an int value of zero.

05:10

So in other words, when we are creating this application, this is going to initialize a global state called count to zero. Great. Now, coming down here in line 16, we have an increment method and you can see that

05:20

this increment method is decorated by this router method. This decorator is registering this increment method to the router class that we have

05:30

defined up here so that the router knows when to execute this increment method. Now going inside we create a scratch bar of TEAL type that you in 64

05:40

that it's creating a temporary storage on the virtual machine that will go away afterwards. And we're assigning that to the scratchCount variable.

05:50

And this increment method is returning a PyTEAL expression in line 19. We're getting the global state count by doing app.globalGet

06:00

in there, we're storing that value in the scratch variable and then we're going to write a new value to count global state. The new value will be the value that is in the scratch variable right now

06:10

by doing scratchCount.load() and we're going to add Int(1) to that. So this is essentially going to increment the count by one.

06:20

And down here we have a read_count method. And whatever comes after the star in the parentheses is an output.

06:30

And we can see here that the read count is going to output a uint64 type. And this is returning a PyTEAL expression where output is set to

06:40

the global stage count that we get by doing at the globalGet. So that's a simple counter example written in PyTEAL.

06:50

Now down here, when I run this file, what I'm going to do is I'm going to compile the program by doing router.compile_program and I specifying the version here as version six.

07:00

This is going to spit out three values an approval program in TEAL, clear program in TEAL, and the contract ABI JSON file.

07:10

And down here I'm taking these three values and I'm going to dump those values into the file system. So if you open up a terminal and run python counter.py

07:20

that is going to write the three values into the filesystem.

07:30

So if you come to the what is Python folder and inside of the artifacts folder, we have the three files written here, the approval.teal, which is the TEAL program of the smart contract that we have written, a clear program

07:40

written in TEAL and the contract.json file, which is an API specification for the frontend.

07:50

Now let's try deploying this simple counter smart contract and calling the increment method to see if it works. To do that, I'm going to use a development tool called Dappflow.

08:00

This is a web based development tool for Algorand, and you can come to this Web site by going to dappflow.org. When I open the application and with Dappflow, you can connect to your sandbox

08:10

by going over here, clicking sandbox and save. Now, whatever you see on the Dappflow website is going to be the current state

08:20

of your sandbox blockchain. So before doing anything else, you want to create a dev wallet and come over here and click create wallet to create a developer wallet that you can use to play around with.

08:30

You can click the dispense button right here to automatically dispense ten algos to your account.

08:40

Come down here. Click the Connect wallet button and then connect to your dev wallet that you created. Now come to the ABI studio and this is where you can use to contract.json ABI file that we got before to

08:50

deploy and call your counter smart contract. To do that you want to click on import ABI, go to file, upload,

09:00

file and find the folder that you wrote your smart contract in, go to the artifacts folder and then import in the contract.json file.

09:10

And as you can see, you can see the name of the router over here and the two external methods that we have defined over here, increment and read count.

09:20

Before we call these methods. We want to create this application. To do that, come to create app, go to bare. And then here first you want to specify how much storage the smart contract uses.

09:30

Our counter example only has one global state that is a uint64 type. So we're going to have this as zero, one for global int, zero for

09:40

both local bytes and local. Then upload your approval program, upload your clear program. These are all in your artifacts folder and then click create.

09:50

Now you'll see the message application created successfully. Now let's try reading the default count value.

10:00

So I'm going to go to read count. If I click Execute, you can see that the value is set to zero now. If you quickly go back to our code, this is because during creation we have created a global state called count

10:10

and initialize it to zero. Going back to Dappflow now I'm going to click increment.

10:20

Click, execute. You can see the method executed successfully. And if you go back to read_count and execute that again, you can see that the value

10:30

updated to one. Dappflow is really great. If you want to do quick checking while you're developing your smart contract and you can use the ABI Studio to do what I just did right here, I'm going to be using Dappflow a lot in this PyTEAL video series,

10:40

so don't worry if you don't fully understand how to use it right now. Today we took our first glance at our PyTEAL is how it works under the hood

10:50

and saw a simple counter example to get a feel of what a PyTEAL program looks like, that we're going to go deeper into core PyTEAL concepts and make you a Python master by

11:00

the end of the series! So let's get started.