A PyTeal expression has one of the following two data types:
- :any:`TealType.uint64`, 64 bit unsigned integer
- :any:`TealType.bytes`, a slice of bytes
For example, all the transaction arguments (e.g. Arg(0)
) are of type :any:`TealType.bytes`.
The first valid round of current transaction (:any:`Txn.first_valid() <TxnObject.first_valid()>`) is typed :any:`TealType.uint64`.
Int(n)
creates a TealType.uint64
constant, where n >= 0 and n < 2 ** 64
.
A byte slice is a binary string. There are several ways to encode a byte slice in PyTeal:
Byte slices can be created from UTF-8 encoded strings. For example:
Bytes("hello world")
Byte slices can be created from a RFC 4648 base16 encoded
binary string, e.g. "0xA21212EF"
or "A21212EF"
. For example:
Bytes("base16", "0xA21212EF")
Bytes("base16", "A21212EF") # "0x" is optional
Byte slices can be created from a RFC 4648 base32 encoded
binary string with or without padding, e.g. "7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M"
.
Bytes("base32", "7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M")
Byte slices can be created from a RFC 4648 base64 encoded
binary string, e.g. "Zm9vYmE="
.
Bytes("base64", "Zm9vYmE=")
All PyTeal expressions are type checked at construction time, for example, running
the following code triggers a TealTypeError
:
Int(0) < Arg(0)
Since <
(overloaded Python operator, see :ref:`arithmetic_expressions` for more details)
requires both operands of type TealType.uint64
,
while Arg(0)
is of type TealType.bytes
.
Converting a value to its corresponding value in the other data type is supported by the following two operators:
- :any:`Itob(n) <Itob>`: generate a
TealType.bytes
value from aTealType.uint64
valuen
- :any:`Btoi(b) <Btoi>`: generate a
TealType.uint64
value from aTealType.bytes
valueb
Note: These operations are not meant to convert between human-readable strings and numbers.
Itob
produces a big-endian 8-byte encoding of an unsigned integer, not a human readable
string. For example, Itob(Int(1))
will produce the string "\x00\x00\x00\x00\x00\x00\x00\x01"
not the string "1"
.