Skip to main content

PyTEAL 的额外操作

项目描述

Pyteal 扩展

Python 的其他有用操作

可用操作

  • MulDiv64: 计​​算m1*m2/d乘法时不会溢出 (TEAL 3+)
  • Mul128, FastExp: 优化采用 uint 和输出字节(大整数)的操作
  • Min, Max: 计算 2 个表达式的最小值/最大值,而不使用插槽或多次评估参数 (TEAL 4+)
  • LazyAnd, LazyOr: 懒惰地评估 And/Or 操作中的参数
  • SaturatingAdd, SaturatingSub: 饱和加法/减法
  • 内部交易:简化内部交易的制作
    • MakeInnerPaymentTxnMakeInnerAssetTransferTxn还有更多!
    • MakeInnerGroupTxn执行内部原子组事务
  • SerializeIntegers, DeserializeIntegers, DeserializeIntegersToSlots: 序列化/反序列化整数到/从字节
  • GlobalState, LocalState, GlobalStateArray, LocalStateArray, GlobalStateArray2D, LocalStateArray2D: 轻松访问全局/本地状态

状态操纵

GlobalStateLocalState允许分别操纵全局和局部状态。它们都具有相同的界面。

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))
)

想要在全局状态下模拟数组?使用GlobalStateArrayLocalStateArray。从技术上讲,索引可以是任何 uint64 值,并且不必是连续的。每个数组元素在本地/全局状态下占用一个插槽。

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)),
    )

示例用法

示例用法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"),
)

安装

pip install pytealext

测试

pytest


由 Łukasz Ptak 和 Paweł Rejkowicz 创建

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

pytealext-2.9.0.tar.gz (38.2 kB 查看哈希)

已上传 source

内置分布

pytealext-2.9.0-py3-none-any.whl (45.4 kB 查看哈希

已上传 py3