TEAL
Transaction Execution Approval Language - Algorand's native smart contract language
What is TEAL?
TEAL, or Transaction Execution Approval Language, is the smart contract language used in the Algorand blockchain. It is an assembly-like language processed by the Algorand Virtual Machine (AVM) and is Turing-complete, supporting both looping and subroutines. TEAL is primarily used for writing smart contracts and smart signatures, which can be authored directly in TEAL or via Python or TypeScript using AlgoKit.
TEAL scripts create conditions for transaction execution. Smart contracts written in TEAL can control Algorand's native assets, interact with users, or enforce custom business logic. These contracts either approve or reject transactions based on predefined conditions.
Use in Algorand Smart Contracts and Signatures
Smart Contracts
Smart contracts written in TEAL can control Algorand's native assets, interact with users, or enforce custom business logic. These contracts either approve or reject transactions based on predefined conditions.
Smart Signatures
Smart signatures enforce specific rules on transactions initiated by accounts, typically serving as a stateless contract. They provide a way to delegate transaction authorization based on programmatic conditions.
Relationship to the Algorand Virtual Machine
The AVM is responsible for processing TEAL programs. It interprets and executes the TEAL code, managing state changes and ensuring the contract's logic adheres to the set rules. The AVM also evaluates the computational cost of running TEAL code to enforce time limits on contract execution.
Processing
AVM interprets and executes TEAL programs
State Management
Manages state changes and rule enforcement
Cost Evaluation
Evaluates computational cost and enforces limits
TEAL Language Features
Assembly-like Structure
TEAL resembles assembly language, where operations are performed in a sequential manner. Each line in a TEAL program represents a single operation.
Stack-based Operations
TEAL is a stack-based language, meaning it relies heavily on a stack to manage data. Operations in TEAL typically involve pushing data onto the stack, manipulating it, and then popping the result off the stack.
Data Types
TEAL supports two primary data types:
- Unsigned 64-bit Integers
- Byte Strings
These data types are used in various operations, including comparisons, arithmetic, and logical operations.
Operators and Flow Control
TEAL includes a set of operators for performing arithmetic (+, -), comparisons (==, <, >), and logical operations (&&, ||). Flow control in TEAL is managed through branching (bnz, bz) and subroutine calls (callsub, retsub).
Access to Transaction Properties and Global Values
TEAL programs can access properties of transactions (e.g., sender, receiver, amount) and global values (e.g., current round, group size) using specific opcodes like txn, gtxn, and global.
Program Versions and Compatibility
Currently, Algorand supports versions 1 through 10 of TEAL. When writing contracts with program version 2 or higher, make sure to add #pragma version # where # should be replaced by the specific number as the first line of the program. If this line does not exist, the protocol will treat the contract as a version 1 contract.
Important: If upgrading a contract to version 2 or higher, it is important to verify you are checking the RekeyTo property of all transactions that are attached to the contract.
Looping and Subroutines
TEAL contracts written in version 4 or higher can use loops and subroutines. Loops can be performed using any branching opcodes b, bz, and bnz. For example, the TEAL below loops ten times.
#pragma version 10 // loop 1 - 10 // init loop var int 0 loop: int 1 + dup // implement loop code // ... // check upper bound int 10 <= bnz loop // once the loop exits, the last counter value will be left on stack
Subroutines can be implemented using labels and the callsub and retsub opcodes. The sample below illustrates a sample subroutine call.
#pragma version 10 // jump to main loop b main // subroutine my_subroutine: // implement subroutine code // with the two args retsub main: int 1 int 5 callsub my_subroutine return
Dynamic Operational Cost
Size Limitations
Smart signatures are limited to 1000 bytes in size. Size encompasses the compiled program plus arguments. Smart contracts are limited to 2KB total for the compiled approval and clear programs. This size can be increased in 2KB increments, up to an 8KB limit for both programs.
Computational Cost
For optimal performance, smart contracts and smart signatures are also limited in opcode cost. This cost is evaluated when a smart contract runs and is representative of its computational expense. Every opcode executed by the AVM has a numeric value that represents its computational cost.
- Most opcodes have a computational cost of 1
- SHA256 has a cost of 35
- ed25519verify has a cost of 1900
Budget Limits
- • Smart signatures: 20,000 total computational cost
- • Smart contracts (single transaction): 700 for either program
- • Smart contracts (group transactions): 700 × number of application transactions
- • Maximum group budget: 700 × (16 + 256) = 190,400
If the smart contract is invoked via a group of application transactions, the computational budget for approval programs is considered pooled. The total opcode budget will be 700 multiplied by the number of application transactions within the group (including inner transactions).
Tools and Development
For developers who prefer Python or TypeScript, you can also write smart contracts in Python or TypeScript using AlgoKit. AlgoKit abstracts many low-level details of TEAL while providing the same functionality.
AlgoKit
The official development toolkit with TEAL compilation, testing, and deployment capabilities
goal
Command-line tool for compiling TEAL programs and interacting with the Algorand network
TEAL Debugger
Step-by-step debugging tools to trace TEAL execution and identify issues
Key Features
Low-Level Control
Direct access to AVM operations and maximum efficiency
Stack-Based
Simple execution model with predictable gas costs
Readable
Human-readable assembly language with clear syntax
