Quick Start¶
The Basics¶
A Tealish program must begin with a Teal pragma
line specifying the Teal version.
Tealish programs are executed from top to bottom.
A programs execution must end with exit(1)
in order for the transaction to be accepted.
If the program fails with an error or ends with exit(0)
the transaction will be rejected.
Note
If you are familiar with Teal or PyTeal, exit
is a Tealish alias for the return
opcode.
The Smallest Tealish Programs¶
This is the smallest valid Tealish program, which will always approve a transaction. However, this is unsafe because it will approve any transaction, even an Application Update or Application Delete transaction!
#pragma version 8
exit(1)
This is a minimal immutable Tealish program that approves any Application Noop call, but rejects Update, Delete, etc.
#pragma version 8
assert(Txn.OnCompletion == NoOp)
exit(1)
A Simple Tealish Program¶
This is a more useful program that demonstrates the use of assertions, state, if statements and inner transactions:
#pragma version 8
# ApplicationID will be 0 when the app is being created
if Txn.ApplicationID == 0:
# Initialise the counter state to 0
app_global_put("counter", 0)
exit(1)
end
if Txn.OnCompletion == UpdateApplication:
# Only allow the Creator to update the app
assert(Txn.Sender == Global.CreatorAddress)
exit(1)
end
# Ensure that this is a NoOp application call. Don't allow optin, delete, etc
assert(Txn.OnCompletion == NoOp)
# Read the counter value from state and increment it
int counter = app_global_get("counter")
counter = counter + 1
app_global_put("counter", counter)
if counter == 10:
# If the counter is 10 make a Payment transaction
inner_txn:
TypeEnum: Pay
Receiver: Txn.Sender
Amount: 10000000
end
elif counter > 10:
# If the counter is past 10 fail the program
exit(0)
end
# Exit the program with approval
exit(1)
To understand what each line of this program does, please refer to the Language Guide and AVM Reference.
The program above demonstrates one main functionality, along with additional logic for creation and updates. While this program is relatively simple, most programs are more complex and require additional structure and the ability for the caller to specify the method or operation they wish to use. A common Tealish pattern is to ‘route’ the execution to different parts of the program using If/Elif/Else or Switch statements.
Tealish Boilerplate¶
The following code shows the common structure used in many larger Tealish programs. It is a good starting point for a new project.
#pragma version 8
if Txn.ApplicationID == 0:
# Handle Create App
exit(1)
end
switch Txn.OnCompletion:
NoOp: main
OptIn: opt_in
CloseOut: close_out
UpdateApplication: update_app
DeleteApplication: delete_app
end
block opt_in:
# Handle Opt In
# some statements here
# exit(1)
# OR
# Disallow Opt In
exit(0)
end
block close_out:
# Handle Close Out
# some statements here
# exit(1)
# OR
# Disallow Closing Out
exit(0)
end
block update_app:
# Handle Update App
# Example: Only allow the Creator to update the app (useful during development)
exit(Txn.Sender == Global.CreatorAddress)
# OR
# Disallow Update App
# exit(0)
end
block delete_app:
# Handle Delete App
# Example: Only allow the Creator to update the app (useful during development)
exit(Txn.Sender == Global.CreatorAddress)
# OR
# Disallow Delete App
# exit(0)
end
block main:
switch Txn.ApplicationArgs[0]:
"method_a": method_a
"method_b": method_b
"method_c": method_c
end
block method_a:
# Handle method_a
# some statements here
exit(1)
end
block method_b:
# Handle method_b
# some statements here
exit(1)
end
block method_c:
# Handle method_c
# some statements here
exit(1)
end
end
Every application requires both an Approval Program and a Clear State Program. So far we have only shown the Approval Program. In most cases the Clear State program is extremely simple and just accepts.
#pragma version 8
exit(1)
Build Your App¶
$ tealish compile approval.tl
$ tealish compile clear.tl
After compling the Tealish programs, the build/
directory will now contain approval.teal
and clear.teal
.
Deploying Your App¶
For instructions on deploying the application using the generated Teal files, please refer to the official Algorand Docs.