1) Algorand smart contracts can be written in TEAL or PyTeal and deployed to the Algorand blockchain.
2) PyTeal is a Python library that compiles Python code into TEAL bytecode that can be run as smart contracts on Algorand.
3) Smart contracts have access to transaction properties, can call other contracts, and read from and write to global state stored on the blockchain.
2. Agenda
• Overview of Smart Contracts
• Transaction Execution Approval Language (TEAL)
• Developing Smart Contracts with Python
3. Mobile/Web Off Chain
App
Algorand Dapp Architecture
Algorand Blockchain
Algorand SDKs
Smart
Contract
Smart
Contract
Smart
Contract
T
T
T
Transactions
Mobile App
WalletConnect
AlgoSigner SDK
MyAlgo SDK
Sign
Chrome Plugin
JavaScript Lib
Others
Wallet
4. What is a Smart Contract
Smart
Contract
In Development
Smart
Contract
Smart
Contract
Smart
Contract
Ledger
Algorand Blockchain
Deploy
● Small Piece of Logic that lives on the chain
● Initiated by a transaction
● Returns True or False-Approve/Fail TX
● Can be grouped with other Transactions
● Written in TEAL or PyTeal or Generated By
Reach Framework
5. Smart Contract High Level
Smart
Contract
Smart
Contract
Smart
Contract
Ledger
Algorand Blockchain
Application Transaction
Contract Logic
and data read
from Ledger
determine pass
or fail
Contract can
write global/local
values to the
ledger
Contract can
also initiate new
transactions
6. Smart Contracts aka Apps
Application Transaction
TEAL
Global State
Local State User 1
Local State User 1
Local State User 1
Local State User 1
Approval Program
Clear Program
TEAL
AVM
Communicate with Application
Transactions
7. Transaction Sub-Types for Application
NoOp TEAL Logic
TEAL Logic
Approval Program
Clear Program
Optin
DeleteApplication
UpdateApplication
CloseOut
Clear
Used to Communicate With Smart Contract
Graceful
Non-Graceful
ie Will clear
regardless
10. Smart Contract Escrow
Application Transaction Smart
Contract
Global State
Local State User 1
Local State User 1
Local State User 1
Local State User 1
AVM
Inner Transactions (Up To 256):
● Payment Transactions
● Create and/or Transfer ASAs
● Freeze or Clawback ASAs
● Opt into another ASA
● Application Call
T
T
T
11. Smart Contract to Smart Contract Calling
Contract A
Process B Return
using itxn logs
Contract B
Process C Return
using itxn logs
Contract C
Application
Transaction
Inner
Transaction
Inner
Transaction
Returns Returns
Level 1 Level 2 Level 3 Level 8
15. Application Binary Interface (ABI)
• Standard for Describing Methods, Interfaces and Contracts
• Described in JSON
• SDKs read and convert to proper smart contract calls, parameters and return types
https://developer.algorand.org/articles/contract-to-contract-calls-and-an-abi-come-to-algorand/
https://github.com/algorandfoundation/ARCs/blob/main/ARCs/arc-0004.md
"add(uint64)uint64"
Method Signature
https://www.youtube.com/watch?v=ajAAy9d3B_0
17. TEAL - Transaction Execution Approval
Language
● Bytecode based stack language
● Turing Complete
● Looping and Subroutines
● Only Uint64 and Byte[] allowed on stack
● True or False - Positive value on stack
● > 140 Opcodes
● PyTeal library to write in python
&&
arg 0
len
…
Push
Pop
18. Simple Stack Example
txn Amount
int 1000
>=
Transaction Amount
txn Amount
int 1000
>=
1
txn Amount
int 1000
>=
2
txn Amount
int 1000
>=
T
r
a
n
s
a
c
t
i
o
n
A
m
o
u
n
t
3
1
0
0
0
Transaction Amount >= 1000
1000
Transaction Amount
0/1
Push
Push
Pop
Push
22. PyTeal
• Python Library that produces TEAL
• TEAL compiled to bytecode and
runs on Algorand AVM
• Contracts are created as PyTeal
Expression Trees
PyTeal Code
TEAL Source
Compile To TEAL
Create with Python
Compile with PyTeal
https://pyteal.readthedocs.io/en/stable/index.html
23. PyTeal to Teal
TEAL
Approval Program
Clear Program
TEAL
Smart Contract
PyTeal
Expression
Tree
PyTeal
Expression
Tree
Python Source File
24. PyTeal Expressions
● PyTeal Object that compiles to TEAL
● Expressions are created using PyTeal Class methods
● Expression Trees are just a collection of connected Expressions
Python Variable PyTEAL Expression
PyTeal Method to
Produce Teal from
Expression Tree
Leaf
Expression
Root
Expression
Leaf
Expression
Expressions that take parameters Must Be Passed other Expressions as
parameters
31. Create and Initialize the Global counter to 0
https://pyteal.readthedocs.io/en/stable/control_structures.html#chaining-expressions-seq
https://pyteal.readthedocs.io/en/stable/state.html#writing-global-state
All Expressions But The Last Must
Resolve to Teal Type of None
Not Allowed
Seq( Int(1), Int(1) )
Only byte[] or uint64 Allowed
32. Application Call Arrays
Smart Contract
Application
Application
Transaction
Accounts
Applications
Assets
Arguments
Accounts[0] is Always Sender
Applications[0] is Always Current App
Arguments[0] Contains Meth Sig for
ABI Calls
33. Switch on Specific ABI Method - call add or
sub
https://pyteal.readthedocs.io/en/stable/accessing_transaction_field.html?#transaction-array-fields
https://pyteal.readthedocs.io/en/stable/arithmetic_expression.html
PyTeal Overloads Python Operators
34. Implement Add and Deduct
https://pyteal.readthedocs.io/en/stable/control_structures.html#simple-branching-if
Do not let the Counter go below 0
https://pyteal.readthedocs.io/en/stable/state.html#reading-global-state
If( test-expr, then-expr, else-expr )