Skip to content

algopy.arc4

NameDescription
ARC4ContractA contract that conforms to the ARC-4 ABI specification, functions decorated with
@abimethod or @baremethod will form the public interface of the contract
StringAn ARC-4 sequence of bytes containing a UTF8 string
UIntNAn ARC-4 UInt consisting of the number of bits specified.
BigUIntNAn ARC-4 UInt consisting of the number of bits specified.
UFixedNxMAn ARC-4 UFixed representing a decimal with the number of bits and precision specified.
BigUFixedNxMAn ARC-4 UFixed representing a decimal with the number of bits and precision specified.
ByteAn ARC-4 alias for a UInt8
BoolAn ARC-4 encoded bool
StaticArrayA fixed length ARC-4 Array of the specified type and length
DynamicArrayA dynamically sized ARC-4 Array of the specified type
AddressAn alias for an array containing 32 bytes representing an Algorand address
DynamicBytesA variable sized array of bytes
TupleAn ARC-4 ABI tuple, containing other ARC-4 ABI types
StructBase class for ARC-4 Struct types
ARC4ClientUsed to provide typed method signatures for ARC-4 contracts
NameDescription
abimethodDecorator that indicates a method is an ARC-4 ABI method.
baremethodDecorator that indicates a method is an ARC-4 bare method.
arc4_signatureReturns the ARC-4 encoded method selector for the specified signature or abi method
arc4_createProvides a typesafe and convenient way of creating an ARC4Contract via an inner transaction
arc4_updateProvides a typesafe and convenient way of updating an ARC4Contract via an inner transaction
emitEmit an ARC-28 event for the provided event signature or name, and provided args.
encodeEncode a value to ARC-4 bytes.
decodeDecode ARC-4 encoded bytes to a value of the specified type.
NameDescription
UInt8An ARC-4 UInt8
UInt16An ARC-4 UInt16
UInt32An ARC-4 UInt32
UInt64An ARC-4 UInt64
UInt128An ARC-4 UInt128
UInt256An ARC-4 UInt256
UInt512An ARC-4 UInt512
abi_callProvides a typesafe way of calling ARC-4 methods via an inner transaction

Bases: algopy.Contract

A contract that conforms to the ARC-4 ABI specification, functions decorated with @abimethod or @baremethod will form the public interface of the contract

The approval_program will be implemented by the compiler, and route application args according to the ARC-4 ABI specification

The clear_state_program will by default return True, but can be overridden

ARC4Contract.approval_program() → bool

ARC4Contract.clear_state_program() → UInt64 | bool

abimethod(*, name: str = …, create: Literal[allow, require, disallow] = ‘disallow’, allow_actions: Sequence[OnCompleteAction | Literal[NoOp, OptIn, CloseOut, UpdateApplication, DeleteApplication]] = (‘NoOp’,), resource_encoding: Literal[index, value] = …, readonly: bool = False, default_args: Mapping[str, str | _ReadOnlyNoArgsMethod | object] = …, validate_encoding: Literal[unsafe_disabled, args] = …) → Callable[[collections.abc.Callable[algopy.arc4._P, _R]], Callable[algopy.arc4._P, _R]]

Decorator that indicates a method is an ARC-4 ABI method.

  • Parameters:
    • name – Name component of the ABI method selector. Defaults to using the function name.
    • create – Controls the validation of the Application ID. “require” means it must be zero, “disallow” requires it must be non-zero, and “allow” disables the validation.
    • allow_actions – A sequence of allowed On-Completion Actions to validate against.
    • resource_encoding – If “index”, then resource types (Application, Asset, Account) should be passed as an index into their appropriate foreign array. The default option “value”, as of PuyaPy 5.0, is for these values to be passed directly. This can be overridden at the compiler level with the “resource_encoding” CLI flag. This option will be reflected in the method signature.
    • readonly – If True, then this method can be used via dry-run / simulate.
    • default_args – Default argument sources for clients to use. For dynamic defaults, this can be the name of, or reference to a method member, or the name of a storage member. For static defaults, this can be any expression which evaluates to a compile time constant of the exact same type as the parameter.
    • validate_encoding – Controls validation behaviour for this method. If “args”, then ABI arguments are validated automatically to ensure they are encoded correctly. If “unsafe_disabled”, then no automatic validation occurs. Arguments can instead be validated using the .validate() method. The default behaviour of this option can be controlled with the –validate-abi-args CLI flag.

baremethod(*, create: Literal[allow, require, disallow] = ‘disallow’, allow_actions: Sequence[OnCompleteAction | Literal[NoOp, OptIn, CloseOut, UpdateApplication, DeleteApplication]] = …) → Callable[[collections.abc.Callable[[_TARC4Contract], None]], Callable[[_TARC4Contract], None]]

Decorator that indicates a method is an ARC-4 bare method.

There can be only one bare method on a contract for each given On-Completion Action.

  • Parameters:
    • create – Controls the validation of the Application ID. “require” means it must be zero, “disallow” requires it must be non-zero, and “allow” disables the validation.
    • allow_actions – Which On-Completion Action(s) to handle.

arc4_signature(signature: str | Callable[algopy.arc4._P, _R], /) → Bytes

Returns the ARC-4 encoded method selector for the specified signature or abi method

String(value: String | str = ”, /)

Bases: algopy.arc4._ABIEncoded

An ARC-4 sequence of bytes containing a UTF8 string

A String can be initialized with a Python str literal, or a str variable declared at the module level

Return the String representation of the UTF8 string after ARC-4 decoding

String.__add__(other: String | String | str) → String

String.__iadd__(other: String | String | str) → String

String.__radd__(other: String | String | str) → String

String.__eq__(other: String | String | str) → bool

Return self==value.

String.__bool__() → bool

Returns True if length is not zero

String.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

String.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

UIntN(value: BigUInt | UInt64 | int = 0, /)

Bases: algopy.arc4._UIntN, typing.Generic[algopy.arc4._TBitSize]

An ARC-4 UInt consisting of the number of bits specified.

Max Size: 64 bits

Return the UInt64 representation of the value after ARC-4 decoding

UIntN.as_uint64() → UInt64

Return the UInt64 representation of the value after ARC-4 decoding

UIntN.as_biguint() → BigUInt

Return the BigUInt representation of the value after ARC-4 decoding

UIntN.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

UIntN.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

BigUIntN(value: BigUInt | UInt64 | int = 0, /)

Bases: algopy.arc4._UIntN, typing.Generic[algopy.arc4._TBitSize]

An ARC-4 UInt consisting of the number of bits specified.

Max size: 512 bits

Return the BigUInt representation of the value after ARC-4 decoding

BigUIntN.as_uint64() → UInt64

Return the UInt64 representation of the value after ARC-4 decoding

BigUIntN.as_biguint() → BigUInt

Return the BigUInt representation of the value after ARC-4 decoding

BigUIntN.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

BigUIntN.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

UFixedNxM(value: str = ‘0.0’, /)

Bases: algopy.arc4._ABIEncoded, typing.Generic[algopy.arc4._TBitSize, algopy.arc4._TDecimalPlaces]

An ARC-4 UFixed representing a decimal with the number of bits and precision specified.

Max size: 64 bits

Construct an instance of UFixedNxM where value (v) is determined from the original decimal value (d) by the formula v = round(d * (10^M))

UFixedNxM.__bool__() → bool

Returns True if not equal to zero

UFixedNxM.__eq__(other: Self) → bool

Compare for equality, note both operands must be the exact same type

UFixedNxM.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

UFixedNxM.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

BigUFixedNxM(value: str = ‘0.0’, /)

Bases: algopy.arc4._ABIEncoded, typing.Generic[algopy.arc4._TBitSize, algopy.arc4._TDecimalPlaces]

An ARC-4 UFixed representing a decimal with the number of bits and precision specified.

Max size: 512 bits

Construct an instance of UFixedNxM where value (v) is determined from the original decimal value (d) by the formula v = round(d * (10^M))

BigUFixedNxM.__bool__() → bool

Returns True if not equal to zero

BigUFixedNxM.__eq__(other: Self) → bool

Compare for equality, note both operands must be the exact same type

BigUFixedNxM.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

BigUFixedNxM.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

Byte(value: BigUInt | UInt64 | int = 0, /)

Bases: algopy.arc4.UIntN[typing.Literal[8]]

An ARC-4 alias for a UInt8

Return the UInt64 representation of the value after ARC-4 decoding

Byte.as_uint64() → UInt64

Return the UInt64 representation of the value after ARC-4 decoding

Byte.as_biguint() → BigUInt

Return the BigUInt representation of the value after ARC-4 decoding

Byte.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

Byte.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

An ARC-4 UInt8

An ARC-4 UInt16

An ARC-4 UInt32

An ARC-4 UInt64

An ARC-4 UInt128

An ARC-4 UInt256

An ARC-4 UInt512

Bool(value: bool = False, /)

Bases: algopy.arc4._ABIEncoded

An ARC-4 encoded bool

Bool.__eq__(other: Bool | bool) → bool

Return self==value.

Bool.__ne__(other: Bool | bool) → bool

Return self!=value.

Return the bool representation of the value after ARC-4 decoding

Bool.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

Bool.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

Bases: algopy.arc4._ABIEncoded, typing.Generic[algopy.arc4._TArrayItem, algopy.arc4._TArrayLength], collections.abc.Reversible[algopy.arc4._TArrayItem]

A fixed length ARC-4 Array of the specified type and length

StaticArray.__iter__() → Iterator[_TArrayItem]

Returns an iterator for the items in the array

StaticArray.__reversed__() → Iterator[_TArrayItem]

Returns an iterator for the items in the array, in reverse order

Returns the (compile-time) length of the array

StaticArray.__getitem__(index: UInt64 | int) → _TArrayItem

Gets the item of the array at provided index

StaticArray.__setitem__(index: UInt64 | int, value: _TArrayItem) → _TArrayItem

Sets the item of the array at specified index to provided value

StaticArray.copy() → Self

Create a copy of this array

StaticArray.to_native(element_type: type[_TNativeArrayItem], /) → FixedArray[algopy.arc4._TNativeArrayItem, _TArrayLength]

Convert to an algopy.FixedArray with the specified element type.

Only allowed if the element type is compatible with this arrays element type e.g. arc4.UInt64 -> UInt64

StaticArray.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

StaticArray.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

DynamicArray(*items: _TArrayItem)

Bases: algopy.arc4._ABIEncoded, typing.Generic[algopy.arc4._TArrayItem], collections.abc.Reversible[algopy.arc4._TArrayItem]

A dynamically sized ARC-4 Array of the specified type

Initializes a new array with items provided

DynamicArray.__iter__() → Iterator[_TArrayItem]

Returns an iterator for the items in the array

DynamicArray.__reversed__() → Iterator[_TArrayItem]

Returns an iterator for the items in the array, in reverse order

Returns the current length of the array

DynamicArray.__getitem__(index: UInt64 | int) → _TArrayItem

Gets the item of the array at provided index

DynamicArray.append(item: _TArrayItem, /) → None

Append an item to this array

DynamicArray.extend(other: Iterable[_TArrayItem], /) → None

Extend this array with the contents of another array

DynamicArray.__setitem__(index: UInt64 | int, value: _TArrayItem) → _TArrayItem

Sets the item of the array at specified index to provided value

DynamicArray.__add__(other: Iterable[_TArrayItem]) → DynamicArray[_TArrayItem]

Concat two arrays together, returning a new array

DynamicArray.pop() → _TArrayItem

Remove and return the last item of this array

DynamicArray.copy() → Self

Create a copy of this array

DynamicArray.__bool__() → bool

Returns True if not an empty array

DynamicArray.to_native(element_type: type[_TNativeArrayItem], /) → Array[_TNativeArrayItem]

Convert to an algopy.Array with the specified element type.

Only allowed if the element type is compatible with this arrays element type e.g. arc4.UInt64 -> UInt64

DynamicArray.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

DynamicArray.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

Address(value: Account | str | Bytes = …, /)

Bases: algopy.arc4.StaticArray[algopy.arc4.Byte, typing.Literal[32]]

An alias for an array containing 32 bytes representing an Algorand address

If value is a string, it should be a 58 character base32 string, ie a base32 string-encoded 32 bytes public key + 4 bytes checksum. If value is a Bytes, it’s length checked to be 32 bytes - to avoid this check, use Address.from_bytes(...) instead. Defaults to the zero-address.

Return the Account representation of the address after ARC-4 decoding

Address.__bool__() → bool

Returns True if not equal to the zero address

Address.__eq__(other: Address | Account | str) → bool

Address equality is determined by the address of another arc4.Address, Account or str

Address.__ne__(other: Address | Account | str) → bool

Address equality is determined by the address of another arc4.Address, Account or str

Returns the (compile-time) length of the array

Address.copy() → Self

Create a copy of this array

Address.to_native(element_type: type[_TNativeArrayItem], /) → FixedArray[algopy.arc4._TNativeArrayItem, _TArrayLength]

Convert to an algopy.FixedArray with the specified element type.

Only allowed if the element type is compatible with this arrays element type e.g. arc4.UInt64 -> UInt64

Address.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

Address.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

Bases: algopy.arc4.DynamicArray[algopy.arc4.Byte]

A variable sized array of bytes

Return the Bytes representation of the address after ARC-4 decoding

Returns the current length of the array

DynamicBytes.append(item: _TArrayItem, /) → None

Append an item to this array

DynamicBytes.extend(other: Iterable[_TArrayItem], /) → None

Extend this array with the contents of another array

DynamicBytes.pop() → _TArrayItem

Remove and return the last item of this array

DynamicBytes.copy() → Self

Create a copy of this array

DynamicBytes.to_native(element_type: type[_TNativeArrayItem], /) → Array[_TNativeArrayItem]

Convert to an algopy.Array with the specified element type.

Only allowed if the element type is compatible with this arrays element type e.g. arc4.UInt64 -> UInt64

DynamicBytes.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

DynamicBytes.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

Tuple(items: tuple[Unpack[_TTuple]], /)

Bases: algopy.arc4._ABIEncoded, tuple[typing.Unpack[algopy.arc4._TTuple]]

An ARC-4 ABI tuple, containing other ARC-4 ABI types

Construct an ARC-4 tuple from a native Python tuple

property Tuple.native : tuple[Unpack[_TTuple]]

Section titled “property Tuple.native : tuple[Unpack[_TTuple]]”

Convert to a native Python tuple - note that the elements of the tuple should be considered to be copies of the original elements

Tuple.copy() → Self

Create a copy of this tuple

Tuple.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

Tuple.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

Tuple.count()

Return number of occurrences of value.

Tuple.index()

Return first index of value.

Raises ValueError if the value is not present.

Bases: algopy.arc4._ABIEncoded

Base class for ARC-4 Struct types

Struct.__init_subclass__(*, frozen: bool = False, kw_only: bool = False)

Struct.copy() → Self

Create a copy of this struct

Struct.from_log(log: Bytes, /) → Self

Load an ABI type from application logs, checking for the ABI return prefix 0x151f7c75

Struct.validate() → None

Performs validation to ensure the value is well-formed, errors if it is not

Bases: typing.Protocol

Used to provide typed method signatures for ARC-4 contracts

Provides a typesafe way of calling ARC-4 methods via an inner transaction

def abi_call(
self,
method: Callable[..., _TABIResult_co] | str,
/,
*args: object,
app_id: algopy.Application | algopy.UInt64 | int = ...,
on_completion: algopy.OnCompleteAction = ...,
approval_program: algopy.Bytes | bytes | tuple[algopy.Bytes, ...] = ...,
clear_state_program: algopy.Bytes | bytes | tuple[algopy.Bytes, ...] = ...,
global_num_uint: UInt64 | int = ...,
global_num_bytes: UInt64 | int = ...,
local_num_uint: UInt64 | int = ...,
local_num_bytes: UInt64 | int = ...,
extra_program_pages: UInt64 | int = ...,
fee: algopy.UInt64 | int = 0,
sender: algopy.Account | str = ...,
note: algopy.Bytes | algopy.String | bytes | str = ...,
rekey_to: algopy.Account | str = ...,
) -> tuple[_TABIResult_co, algopy.itxn.ApplicationCallInnerTransaction]: ...

PARAMETERS:

  • method: The name, method selector or Algorand Python method to call
  • app_id: Application to call, if 0 or not specified will create a new application
  • on_completion: OnCompleteAction value for the transaction. If not specified will be inferred from Algorand Python method where possible
  • approval_program: When creating or updating an application, the approval program
  • clear_state_program: When creating or updating an application, the clear state program
  • global_num_uint: When creating an application the number of global uints
  • global_num_bytes: When creating an application the number of global bytes
  • local_num_uint: When creating an application the number of local uints
  • local_num_bytes: When creating an application the number of local bytes
  • extra_program_pages: When creating an application the The number of extra program pages
  • fee: The fee to pay for the transaction, defaults to 0
  • sender: The sender address for the transaction
  • note: Note to include with the transaction
  • rekey_to: Account to rekey to

RETURNS: If method references an Algorand Contract / Client or the function is indexed with a return type, then the result is a tuple containing the ABI result and the inner transaction of the call.

If no return type is specified, or the method does not have a return value then the result is the inner transaction of the call.

Examples:

# can reference another algopy contract method
result, txn = abi_call(HelloWorldContract.hello, arc4.String("World"), app=...)
assert result == "Hello, World"
# can reference a method selector
result, txn = abi_call[arc4.String]("hello(string)string", arc4.String("Algo"), app=...)
assert result == "Hello, Algo"
# can reference a method name, the method selector is inferred from arguments and return type
result, txn = abi_call[arc4.String]("hello", "There", app=...)
assert result == "Hello, There"
# calling a method without a return value
txn = abi_call(HelloWorldContract.no_return, arc4.String("World"), app=...)

arc4_create(method: Callable[algopy.arc4._P, _TABIResult_co], /, *args: object, compiled: CompiledContract = …, on_completion: OnCompleteAction = …, fee: UInt64 | int = 0, sender: Account | str = …, note: Bytes | bytes | str = …, rekey_to: Account | str = …) → tuple[algopy.arc4._TABIResult_co, ApplicationCallInnerTransaction]

Provides a typesafe and convenient way of creating an ARC4Contract via an inner transaction

  • Parameters:
    • method – An ARC-4 create method (ABI or bare), or an ARC4Contract with a single create method
    • args – ABI args for chosen method
    • compiled – If supplied will be used to specify transaction parameters required for creation, can be omitted if template variables are not used
    • on_completion – OnCompleteAction value for the transaction If not specified will be inferred from Algorand Python method where possible
    • fee – The fee to pay for the transaction, defaults to 0
    • sender – The sender address for the transaction
    • note – Note to include with the transaction
    • rekey_to – Account to rekey to

arc4_update(method: Callable[algopy.arc4._P, _TABIResult_co], /, *args: object, app_id: Application | UInt64 | int, compiled: CompiledContract = …, fee: UInt64 | int = 0, sender: Account | str = …, note: Bytes | bytes | str = …, rekey_to: Account | str = …, reject_version: UInt64 | int = …) → tuple[algopy.arc4._TABIResult_co, ApplicationCallInnerTransaction]

Provides a typesafe and convenient way of updating an ARC4Contract via an inner transaction

  • Parameters:
    • method – An ARC-4 update method (ABI or bare), or an ARC4Contract with a single update method
    • args – ABI args for chosen method
    • app_id – Application to update
    • compiled – If supplied will be used to specify transaction parameters required for updating, can be omitted if template variables are not used
    • fee – The fee to pay for the transaction, defaults to 0
    • sender – The sender address for the transaction
    • note – Note to include with the transaction
    • rekey_to – Account to rekey to

emit(event: str | Struct | Struct, /, *args: object) → None

Emit an ARC-28 event for the provided event signature or name, and provided args.

  • Parameters:
    • event – Either an ARC-4 Struct, a native Struct, an event name, or event signature.
      • If event is an ARC-4 Struct, the event signature will be determined from the Struct name and fields
      • If event is a native Struct, its fields will be automatically converted to their ARC-4 equivalents
      • If event is a signature, then the following args will be typed checked to ensure they match.
      • If event is just a name, the event signature will be inferred from the name and following arguments
    • args – When event is a signature or name, args will be used as the event data. They will all be encoded as single ARC-4 Tuple

Example:

from algopy import ARC4Contract, arc4
class Swapped(arc4.Struct):
a: arc4.UInt64
b: arc4.UInt64
class EventEmitter(ARC4Contract):
@arc4.abimethod
def emit_swapped(self, a: arc4.UInt64, b: arc4.UInt64) -> None:
arc4.emit(Swapped(b, a))
arc4.emit("Swapped(uint64,uint64)", b, a)
arc4.emit("Swapped", b, a)

encode(value: object, /) → Bytes

Encode a value to ARC-4 bytes.

When given a native value, performs ARC-4 encoding. When given an already ARC-4 encoded value, reinterprets the underlying bytes.

decode(typ: type[_TDecode], value: Bytes | bytes, /, *, validate: Literal[True, False] = True) → _TDecode

Decode ARC-4 encoded bytes to a value of the specified type.

  • Parameters:
    • typ – The target type to decode into.
    • value – The ARC-4 encoded bytes to decode.
    • validate – If True (default), validates the encoding of value bytes.