Skip to content

Latest commit

 

History

History
160 lines (132 loc) · 12.4 KB

accessing_transaction_field.rst

File metadata and controls

160 lines (132 loc) · 12.4 KB

Transaction Fields and Global Parameters

PyTeal smart contracts can access properties of the current transaction and the state of the blockchain when they are running.

Transaction Fields

Information about the current transaction being evaluated can be obtained using the :any:`Txn` object. Below are the PyTeal expressions that refer to transaction fields:

Operator Type Notes    
:any:`Txn.sender() <TxnObject.sender>` TealType.bytes 32 byte address
:any:`Txn.fee() <TxnObject.fee>` TealType.uint64 in microAlgos
:any:`Txn.first_valid() <TxnObject.first_valid>` TealType.uint64 round number
:any:`Txn.last_valid() <TxnObject.last_valid>` TealType.uint64 round number
:any:`Txn.note() <TxnObject.note>` TealType.bytes transaction note in bytes
:any:`Txn.lease() <TxnObject.lease>` TealType.bytes transaction lease in bytes
:any:`Txn.receiver() <TxnObject.receiver>` TealType.bytes 32 byte address
:any:`Txn.amount() <TxnObject.amount>` TealType.uint64 in microAlgos
:any:`Txn.close_remainder_to() <TxnObject.close_remainder_to>` TealType.bytes 32 byte address
:any:`Txn.vote_pk() <TxnObject.vote_pk>` TealType.bytes 32 byte address
:any:`Txn.selection_pk() <TxnObject.selection_pk>` TealType.bytes 32 byte address
:any:`Txn.vote_first() <TxnObject.vote_first>` TealType.uint64  
:any:`Txn.vote_last() <TxnObject.vote_last>` TealType.uint64  
:any:`Txn.vote_key_dilution() <TxnObject.vote_key_dilution>` TealType.uint64  
:any:`Txn.type() <TxnObject.type>` TealType.bytes  
:any:`Txn.type_enum() <TxnObject.type_enum>` TealType.uint64 see table below
:any:`Txn.xfer_asset() <TxnObject.xfer_asset>` TealType.uint64 ID of asset being transferred
:any:`Txn.asset_amount() <TxnObject.asset_amount>` TealType.uint64 value in Asset's units
:any:`Txn.asset_sender() <TxnObject.asset_sender>` TealType.bytes 32 byte address, causes clawback of all value if sender is the clawback
:any:`Txn.asset_receiver() <TxnObject.asset_receiver>` TealType.bytes 32 byte address
:any:`Txn.asset_close_to() <TxnObject.asset_close_to>` TealType.bytes 32 byte address
:any:`Txn.group_index() <TxnObject.group_index>` TealType.uint64 position of this transaction within a transaction group, starting at 0
:any:`Txn.tx_id() <TxnObject.tx_id>` TealType.bytes the computed ID for this transaction, 32 bytes
:any:`Txn.application_id() <TxnObject.application_id>` TealType.uint64  
:any:`Txn.on_completion() <TxnObject.on_completion>` TealType.uint64  
:any:`Txn.approval_program() <TxnObject.approval_program>` TealType.bytes  
:any:`Txn.clear_state_program() <TxnObject.clear_state_program>` TealType.bytes  
:any:`Txn.rekey_to() <TxnObject.rekey_to>` TealType.bytes 32 byte address
:any:`Txn.config_asset() <TxnObject.config_asset>` TealType.uint64 ID of asset being configured
:any:`Txn.config_asset_total() <TxnObject.config_asset_total>` TealType.uint64  
:any:`Txn.config_asset_decimals() <TxnObject.config_asset_decimals>` TealType.uint64  
:any:`Txn.config_asset_default_frozen() <TxnObject.config_asset_default_frozen>` TealType.uint64  
:any:`Txn.config_asset_unit_name() <TxnObject.config_asset_unit_name>` TealType.bytes  
:any:`Txn.config_asset_name() <TxnObject.config_asset_name>` TealType.bytes  
:any:`Txn.config_asset_url() <TxnObject.config_asset_url>` TealType.bytes  
:any:`Txn.config_asset_metadata_hash() <TxnObject.config_asset_metadata_hash>` TealType.bytes  
:any:`Txn.config_asset_manager() <TxnObject.config_asset_manager>` TealType.bytes 32 byte address
:any:`Txn.config_asset_reserve() <TxnObject.config_asset_reserve>` TealType.bytes 32 byte address
:any:`Txn.config_asset_freeze() <TxnObject.config_asset_freeze>` TealType.bytes 32 byte address
:any:`Txn.config_asset_clawback() <TxnObject.config_asset_clawback>` TealType.bytes 32 byte address
:any:`Txn.freeze_asset() <TxnObject.freeze_asset>` TealType.uint64  
:any:`Txn.freeze_asset_account() <TxnObject.freeze_asset_account>` TealType.bytes 32 byte address
:any:`Txn.freeze_asset_frozen() <TxnObject.freeze_asset_frozen>` TealType.uint64  
:any:`Txn.global_num_uints() <TxnObject.global_num_uints>` TealType.uint64 Maximum global integers in app schema
:any:`Txn.global_num_byte_slices() <TxnObject.global_num_byte_slices>` TealType.uint64 Maximum global byte strings in app schema
:any:`Txn.local_num_uints() <TxnObject.local_num_uints>` TealType.uint64 Maximum local integers in app schema
:any:`Txn.local_num_byte_slices() <TxnObject.local_num_byte_slices>` TealType.uint64 Maximum local byte strings in app schema
:any:`Txn.application_args <TxnObject.application_args>` TealType.bytes[] Array of application arguments
:any:`Txn.accounts <TxnObject.accounts>` TealType.bytes[] Array of application accounts
:any:`Txn.assets <TxnObject.assets>` TealType.uint64[] Array of application assets
:any:`Txn.applications <TxnObject.applications>` TealType.uint64[] Array of applications

Transaction Type

The :any:`Txn.type_enum() <TxnObject.type_enum>` values can be checked using the :any:`TxnType` enum:

Value Numerical Value Type String Description
:any:`TxnType.Unknown` 0 unkown unknown type, invalid
:any:`TxnType.Payment` 1 pay payment
:any:`TxnType.KeyRegistration` 2 keyreg key registration
:any:`TxnType.AssetConfig` 3 acfg asset config
:any:`TxnType.AssetTransfer` 4 axfer asset transfer
:any:`TxnType.AssetFreeze` 5 afrz asset freeze
:any:`TxnType.ApplicationCall` 6 appl application call

Transaction Array Fields

Some of the exposed transaction fields are arrays with the type TealType.uint64[] or TealType.bytes[]. These fields are Txn.application_args, Txn.assets, Txn.accounts, and Txn.applications.

The length of these array fields can be found using the .length() method, and individual items can be accesses using bracket notation. For example:

Txn.application_args.length() # get the number of application arguments in the transaction
Txn.application_args[0] # get the first application argument
Txn.application_args[1] # get the second application argument

Special case: Txn.accounts and Txn.applications

The Txn.accounts and Txn.applications arrays are special cases. Normal arrays in PyTeal are 0-indexed, but these are 1-indexed with special values at index 0.

For the accounts array, Txn.accounts[0] is always equivalent to Txn.sender().

For the applications array, Txn.applications[0] is always equivalent to Txn.application_id().

IMPORTANT: Since these arrays are 1-indexed, their lengths are handled differently. For example, if Txn.accounts.length() or Txn.applications.length() is 2, then indexes 0, 1, and 2 will be present. In fact, the index 0 will always evaluate to the special values above, even when length() is 0.

Atomic Transfer Groups

Atomic Transfers are irreducible batch transactions that allow groups of transactions to be submitted at one time. If any of the transactions fail, then all the transactions will fail. PyTeal allows programs to access information about the transactions in an atomic transfer group using the :any:`Gtxn` object. This object acts like a list of :any:`TxnObject`, meaning all of the above transaction fields on Txn are available on the elements of Gtxn. For example:

Gtxn[0].sender() # get the sender of the first transaction in the atomic transfer group
Gtxn[1].receiver() # get the receiver of the second transaction in the atomic transfer group

Gtxn is zero-indexed and the maximum size of an atomic transfer group is 16. The size of the current transaction group is available as :any:`Global.group_size()`. A standalone transaction will have a group size of 1.

To find the current transaction's index in the transfer group, use :any:`Txn.group_index() <TxnObject.group_index>`. If the current transaction is standalone, it's group index will be 0.

Global Parameters

Information about the current state of the blockchain can be obtained using the following :any:`Global` expressions:

Operator Type Notes
:any:`Global.min_txn_fee()` TealType.uint64 in microAlgos
:any:`Global.min_balance()` TealType.uint64 in mircoAlgos
:any:`Global.max_txn_life()` TealType.uint64 number of rounds
:any:`Global.zero_address()` TealType.bytes 32 byte address of all zero bytes
:any:`Global.group_size()` TealType.uint64 number of txns in this atomic transaction group, at least 1
:any:`Global.logic_sig_version()` TealType.uint64 the maximum supported TEAL version
:any:`Global.round()` TealType.uint64 the current round number
:any:`Global.latest_timestamp()` TealType.uint64 the latest confirmed block UNIX timestamp
:any:`Global.current_application_id()` TealType.uint64 the ID of the current application executing