TEAL
Transaction Execution Approval Language - Algorand's low-level assembly language.
Overview
TEAL is the low-level language that runs on the Algorand Virtual Machine (AVM). All PuyaTS and Puya contracts compile to TEAL before deployment.
Warning
Writing TEAL directly is not recommended for most projects. Use PuyaTS or Puya for better productivity and safety.
TEAL Basics
TEAL is a stack-based language. Operations push values onto the stack and pop values to use them.
simple.teal TEAL
1#pragma version 10
2
3// Push two numbers onto the stack
4int 5
5int 3
6
7// Add them (pops 2 values, pushes result)
8+
9
10// Result (8) is now on the stack
11// Program succeeds if top of stack is non-zero
12return
Contract Structure
A typical TEAL program structure:
TEAL
1#pragma version 10
2
3// Route based on application call type
4txn OnCompletion
5int NoOp
6==
7bnz handle_noop
8
9txn OnCompletion
10int OptIn
11==
12bnz handle_optin
13
14// Reject unknown calls
15err
16
17handle_noop:
18 // Handle regular calls
19 // Check method selector and route
20 txna ApplicationArgs 0
21 method "increment()void"
22 ==
23 bnz increment
24 err
25
26handle_optin:
27 // Handle opt-in
28 int 1
29 return
30
31increment:
32 // Increment counter
33 byte "count"
34 app_global_get
35 int 1
36 +
37 byte "count"
38 swap
39 app_global_put
40 int 1
41 return
Common Opcodes
Stack Operations
int N- Push integer Nbyte "..."- Push bytesdup- Duplicate top of stackpop- Remove top of stackswap- Swap top two values
Arithmetic
+- Add-- Subtract*- Multiply/- Divide%- Modulo
Comparison
==- Equal!=- Not equal<- Less than>- Greater than<=- Less than or equal>=- Greater than or equal
Control Flow
bnz label- Branch if non-zerobz label- Branch if zerob label- Unconditional branchreturn- End programerr- Fail program
State Access
app_global_get- Read global stateapp_global_put- Write global stateapp_local_get- Read local stateapp_local_put- Write local state
Transaction Fields
txn Sender- Transaction sendertxn Amount- Payment amounttxn ApplicationID- Current app IDtxna ApplicationArgs N- Argument N
Complete Example
A simple counter contract in TEAL:
counter.teal TEAL
1#pragma version 10
2
3// Check if this is an application call
4txn TypeEnum
5int appl
6==
7assert
8
9// Route based on OnCompletion
10txn OnCompletion
11int NoOp
12==
13bnz route_method
14
15txn OnCompletion
16int UpdateApplication
17==
18bnz reject
19
20txn OnCompletion
21int DeleteApplication
22==
23bnz reject
24
25// Allow OptIn, CloseOut, ClearState
26int 1
27return
28
29reject:
30err
31
32route_method:
33// Check first argument for method selector
34txna ApplicationArgs 0
35
36// increment()void
37method "increment()void"
38==
39bnz do_increment
40
41// decrement()void
42method "decrement()void"
43==
44bnz do_decrement
45
46// getCount()uint64
47method "getCount()uint64"
48==
49bnz do_get_count
50
51err
52
53do_increment:
54byte "count"
55dup
56app_global_get
57int 1
58+
59app_global_put
60int 1
61return
62
63do_decrement:
64byte "count"
65app_global_get
66int 0
67>
68assert
69byte "count"
70dup
71app_global_get
72int 1
73-
74app_global_put
75int 1
76return
77
78do_get_count:
79byte 0x151f7c75 // Return prefix
80byte "count"
81app_global_get
82itob
83concat
84log
85int 1
86return
Debugging Tips
- Use the TEAL Preview to see compiled output from PuyaTS/Puya
- Check stack contents at each step mentally or with simulation
- Use
logto output debug information - Keep track of the opcode budget