With PyTeal, developers can easily write Algorand Smart Contracts (ASC1s) in Python. PyTeal supports both stateless and statefull smart contracts.
Below is an example of writing a basic stateless smart contract that allows a specific receiver to withdraw funds from an account.
# This example is provided for informational purposes only and has not been audited for security. from pyteal import * """Basic Bank""" def bank_for_account(receiver): """Only allow receiver to withdraw funds from this contract account. Args: receiver (str): Base 32 Algorand address of the receiver. """ is_payment = Txn.type_enum() == TxnType.Payment is_single_tx = Global.group_size() == Int(1) is_correct_receiver = Txn.receiver() == Addr(receiver) no_close_out_addr = Txn.close_remainder_to() == Global.zero_address() no_rekey_addr = Txn.rekey_to() == Global.zero_address() acceptable_fee = Txn.fee() <= Int(1000) return And( is_payment, is_single_tx, is_correct_receiver, no_close_out_addr, no_rekey_addr, acceptable_fee, ) if __name__ == "__main__": program = bank_for_account( "ZZAF5ARA4MEC5PVDOP64JM5O5MQST63Q2KOY2FLYFLXXD3PFSNJJBYAFZM" ) print(compileTeal(program, mode=Mode.Signature, version=3))
As shown in this exmaple, the logic of smart contract is expressed using PyTeal expressions constructed in Python. PyTeal overloads Python’s arithmetic operators
== (more overloaded operators can be found in Arithmetic Operations), allowing Python developers express smart contract logic more naturally.
compileTeal is called to convert an PyTeal expression
to a TEAL program, consisting of a sequence of TEAL opcodes.
The output of the above example is:
#pragma version 3 txn TypeEnum int pay == global GroupSize int 1 == && txn Receiver addr ZZAF5ARA4MEC5PVDOP64JM5O5MQST63Q2KOY2FLYFLXXD3PFSNJJBYAFZM == && txn CloseRemainderTo global ZeroAddress == && txn RekeyTo global ZeroAddress == && txn Fee int 1000 <= && return