Skip to main content

Pyteal Extensions

Additional useful operations for Python

Available Operations

  • MulDiv64: calculate m1*m2/d with no overflow on multiplication (TEAL 3+)
  • Uint64Array: for when you need convenient integer arrays.
  • Mul128, FastExp: Optimize operations that take uints and output bytes (big ints)
  • Min, Max: calculate minimum/maximum of 2 expressions, without using slots or evaluating arguments more than once (TEAL 4+)
  • LazyAnd, LazyOr: lazily evaluate arguments in And/Or operation
  • SaturatingAdd, SaturatingSub: saturating addition/subtraction
  • AutoLoadScratchVar: more convenient way to use scratch variables. Removes the need to use .load() everytime, adds .increment() method.
  • Inner Transactions: simplified making of inner transactions
    • MakeInnerPaymentTxn, MakeInnerAssetTransferTxn and more!
    • MakeInnerGroupTxn to execute inner atomic group transactions
  • SerializeIntegers, DeserializeIntegers, DeserializeIntegersToSlots: serialize/deserialize integers to/from bytes
  • GlobalState, LocalState, GlobalStateArray, LocalStateArray, GlobalStateArray2D, LocalStateArray2D: easily access global/local state

State manipulation

GlobalState and LocalState allow for manipulating global and local state respectively. They both have the same interface.

from pyteal import App, Bytes, Int, Seq, TealType
from pytealext import LocalState

user_counter = LocalState("UC", TealType.uint64)
program = Seq(
    # increment using pyteal local state
    App.localPut(Int(0), Bytes("UC"), App.localGet(Int(0), Bytes("UC")) + Int(1)),
    # increment using put/get
    user_counter.put(user_counter.get() + Int(1)),
    # increment using add_assign
    user_counter.add_assign(Int(1))
    # decrement
    user_counter.sub_assign(Int(1))
)

Want to simulate arrays in the global state? Use GlobalStateArray and LocalStateArray. Technically, the indexes can be any uint64 value and don't have to be sequential. Each array element takes one slot in the local/global state.

from pyteal import Assert, For, Int, Return, ScratchVar, Seq
from pytealext import LocalStateArray

user_counters = LocalStateArray("Counters")
    i = ScratchVar()
    accumulator = ScratchVar()
    program = Seq(
        user_counters[0].put(Int(10)),
        user_counters[0].add_assign(Int(1)),
        user_counters[0].sub_assign(Int(2)),
        Assert(user_counters[0].get() == Int(9)),
        # set some other indexes
        user_counters[1].put(Int(9)),
        user_counters[2].put(Int(9)),
        # Indexes can also be accessed with Exprs
        accumulator.store(Int(0)),
        For(i.store(Int(0)), i.load() < Int(3), i.store(i.load() + Int(1))).Do(
            accumulator.store(accumulator.load() + user_counters[i.load()].get())
        ),
        Return(accumulator.load() == Int(27)),
    )

Example usage

Example usage for LazyAnd:

from pyteal import Gtxn, TxnType, Bytes, Int
from pytealext import LazyAnd

# Evaluate fields of some transaction but don't panic if an argument down the line would panic
validation = LazyAnd(
    Gtxn[0].type_enum() == TxnType.ApplicationCall,
    Gtxn[0].application_args.length() == Int(1),
    Gtxn[0].application_args[0] == Bytes("AX"),
)

Installation

pip install pytealext

Testing

pytest


Created by Łukasz Ptak and Paweł Rejkowicz

Metadata

Release files for pytealext 3.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytealext 3.2.0
File Size Uploaded
pytealext-3.2.0.tar.gz 46.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytealext 3.2.0
File Interpreter ABI Platform
pytealext-3.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 102.0 kB

Release files / pytealext-3.2.0.tar.gz

Download URL pytealext-3.2.0.tar.gz
Size 46.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6f72748ecf6cb6fff883dad39b2e262367d09098fee1f2a486fdb101d782eff8
BLAKE2b-256 checksum
How to use checksums
d6c2327b617e82669fa274459500690635544125036f7cd1f2598b6a72a874a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release files / pytealext-3.2.0-py3-none-any.whl

Download URL pytealext-3.2.0-py3-none-any.whl
Size 56.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a5858b33cd720655b187db9d90d0b9af405a9fee3bfb0b171954bbe377aa1e3
BLAKE2b-256 checksum
How to use checksums
52b9100ee17c1442b535bc860a51eb55b80fb1dbc53a4a95273db5859b6e74f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release history Release notifications | RSS feed

This release

3.2.0 This release

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.9.0

2 release files

2.8.0

2 release files

2.7.0

2 release files

2.6.1

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.4

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page