Smart Contract Overview

Algorand Smart Contracts (ASC1) are small programs that serve various functions on the blockchain and operate on layer-1. Smart contracts are separated into two main categories, stateful and stateless. The type of contract that is written will determine when and how the logic of the program is evaluated. See the following sections to understand how each type of contract is used on the Algorand blockchain. Both types of contracts are written in the Transaction Execution Approval Language (TEAL), which is an assembly-like language that is interpreted by each Algorand node. TEAL programs can be written by hand or by using the Python language with the PyTEAL compiler. See the TEAL section below for more details.

Warning

When writing smart contracts, make sure to follow TEAL guidelines. This is very important in order to prevent smart contracts from being compromised.

Stateless Smart Contracts

Stateless smart contracts are primarily used to replace signature authority for transactions. All transactions in Algorand must be signed, by either an account or a multi-signature account. With stateless smart contracts, transactions can also be signed with logic, which Algorand designates as a LogicSignature. Stateless smart contracts can further be broken into two primary modes of use: contract accounts, and signature delegation.

Contract accounts act in a similar fashion to escrow accounts, where when the smart contract is compiled it produces an Algorand address. This address can accept Algos or Algorand ASAs with standard transactions, where the contract’s address is the receiver of the transaction. The program’s logic then determines when funds are allowed to be spent from the account. A simple example of this would be a program that would allow a specific account to empty the contents of a contract account at some point in the future, determined by the logic within the contract.

Stateless smart contracts can also be used to delegate signature authority. When used in this mode, the logic of the smart contract is signed by a specific account or multi-signature account. This signed logic can then be shared with another party that can use it to withdrawal Algos or Algorand ASAs from the signing account, based on the logic of the contract. The signing account is delegating some portion of authority for their account to another account. A simple example of a delegation stateless smart contract may be something like a bill pay application. In this case, an account may allow a mortgage company to withdrawal a limited amount of funds from the account once a month. See the Stateless smart contract section of the developer portal for more information on writing stateless smart contracts.

Stateful Smart Contracts

Stateful smart contracts are contracts that live on the chain and are used to keep track of some form of global and/or local state for the contract. For example, a voting application may be implemented as a stateful smart contract, where the list of candidates and their current vote tallies would be considered global state values. When an account casts a vote, their local account may be marked by the stateful smart contract with a boolean indicating that the account has already voted.

Stateful smart contracts form the backbone of applications that intend to run on the Algorand blockchain. Stateful smart contracts act similar to Algorand ASAs in that they have specific global values and per-user values. They differ from ASAs in that the smart contract controls the logic of how these values are modified. Stateful smart contracts are called through special Algorand transactions that are processed by the contract’s code. Contracts support several types of calls including creation, update, option in, deletion, closeout, clear, and standard call transactions. The contracts' code can then determine what to do with these calls and how it will affect the global state of the contract or the local state of the specific account that is making the call to the stateless smart contract.

In addition to functioning in a standalone fashion, stateful smart contracts can be combined with all the other Algorand layer-1 features to produce even more complex application types. To continue with the voting application, the smart contract can be combined with a voting token implemented as an Algorand ASA to create a permissioned voting application. When used in this fashion, a voter would spend a voting token at the same time they submit their vote to the stateful smart contract. These two transactions would be grouped using Algorand’s Atomic Transfers to guarantee both happen at the same time. A centralized source would be responsible for giving out the voting tokens.

This is just one example of combining several technologies with stateful smart contracts. Using similar combinations allows feature-rich blockchain applications to be created. To learn more about building stateful smart contracts, see the stateful smart contract documentation.

Transaction Execution Approval Language (TEAL)

Stateful and Stateless smart contracts are written in Transaction Execution Approval Language (TEAL). They can additionally be written in Python and compiled to TEAL using the PyTeal library.

TEAL is an assembly-like language and is processed with a stack machine. The primary purpose of this program is to return true or false. The program will return true if and only if one positive value is left on the stack at the completion of the program execution. For all other conditions, the program will return false. For stateless smart contracts, this generally will approve a transaction. For stateful smart contracts, the TEAL program may also modify the global state of the contract or a specific account’s local storage. If the stateful smart contract returns false, any changes to the global or local state will be canceled.

TEAL provides access to many opcodes which operate on the stack values. Smart contracts have access to arguments, global variables, balances, Algorand ASAs, and grouped transaction properties. Some opcodes are only available to a specific contract type (stateful or stateless). These opcodes are marked in the opcode documentation with a mode field that is set to either Application (stateful contract) or Signature (stateless contract).

The TEAL language is fully described in the TEAL Specification, and the TEAL Overview guide walks through the general operation of TEAL. Algorand provides a debugger to debug smart contracts, as well. To learn more about debugging a smart contract, see the debugging smart contracts documentation.