Exemple #1
0
def test_or_invalid():
    with pytest.raises(pt.TealInputError):
        pt.Or()

    with pytest.raises(pt.TealTypeError):
        pt.Or(pt.Int(1), pt.Txn.receiver())

    with pytest.raises(pt.TealTypeError):
        pt.Or(pt.Txn.receiver(), pt.Int(1))

    with pytest.raises(pt.TealTypeError):
        pt.Or(pt.Txn.receiver(), pt.Txn.receiver())
Exemple #2
0
def test_or_one():
    arg = pt.Int(1)
    expr = pt.Or(arg)
    assert expr.type_of() == pt.TealType.uint64

    expected = pt.TealSimpleBlock([pt.TealOp(arg, pt.Op.int, 1)])

    actual, _ = expr.__teal__(options)

    assert actual == expected
Exemple #3
0
def tallygo():
    result = pt.ScratchVar(pt.TealType.bytes)
    # pt.If-Then is a hook for creating + opting in without providing any args
    return (pt.If(
        pt.Or(pt.App.id() == pt.Int(0),
              pt.Txn.application_args.length() == pt.Int(0))).Then(
                  pt.Int(1)).Else(
                      pt.Seq(
                          result.store(pt.Bytes("dummy")),
                          tally(pt.Int(4), result),
                          pt.Btoi(result.load()),
                      )))
Exemple #4
0
def fac_by_ref_args():
    n = pt.ScratchVar(pt.TealType.uint64)
    return pt.Seq(
        pt.If(
            pt.Or(
                pt.App.id() == pt.Int(0),
                pt.Txn.application_args.length() == pt.Int(0),
            )).Then(pt.Int(1)).Else(
                pt.Seq(
                    n.store(pt.Btoi(pt.Txn.application_args[0])),
                    factorial(n),
                    n.load(),
                )))
Exemple #5
0
def test_or_two():
    args = [pt.Int(1), pt.Int(0)]
    expr = pt.Or(args[0], args[1])
    assert expr.type_of() == pt.TealType.uint64

    expected = pt.TealSimpleBlock([
        pt.TealOp(args[0], pt.Op.int, 1),
        pt.TealOp(args[1], pt.Op.int, 0),
        pt.TealOp(expr, pt.Op.logic_or),
    ])

    actual, _ = expr.__teal__(options)
    actual.addIncoming()
    actual = pt.TealBlock.NormalizeBlocks(actual)

    assert actual == expected
Exemple #6
0
def test_method_config():
    never_mc = pt.MethodConfig(no_op=pt.CallConfig.NEVER)
    assert never_mc.is_never()
    assert never_mc.approval_cond() == 0
    assert never_mc.clear_state_cond() == 0

    on_complete_pow_set = power_set(ON_COMPLETE_CASES)
    approval_check_names_n_ocs = [
        (camel_to_snake(oc.name), oc)
        for oc in ON_COMPLETE_CASES
        if str(oc) != str(pt.OnComplete.ClearState)
    ]
    for on_complete_set in on_complete_pow_set:
        oc_names = [camel_to_snake(oc.name) for oc in on_complete_set]
        ordered_call_configs = full_ordered_combination_gen(
            list(pt.CallConfig), len(on_complete_set)
        )
        for call_configs in ordered_call_configs:
            mc = pt.MethodConfig(**dict(zip(oc_names, call_configs)))
            match mc.clear_state:
                case pt.CallConfig.NEVER:
                    assert mc.clear_state_cond() == 0
                case pt.CallConfig.CALL:
                    assert mc.clear_state_cond() == 1
                case pt.CallConfig.CREATE | pt.CallConfig.ALL:
                    with pytest.raises(
                        pt.TealInputError,
                        match=r"Only CallConfig.CALL or CallConfig.NEVER are valid for a clear state CallConfig, since clear state can never be invoked during creation$",
                    ):
                        mc.clear_state_cond()
            if mc.is_never() or all(
                getattr(mc, i) == pt.CallConfig.NEVER
                for i, _ in approval_check_names_n_ocs
            ):
                assert mc.approval_cond() == 0
                continue
            elif all(
                getattr(mc, i) == pt.CallConfig.ALL
                for i, _ in approval_check_names_n_ocs
            ):
                assert mc.approval_cond() == 1
                continue
            list_of_cc = [
                (
                    typing.cast(
                        pt.CallConfig, getattr(mc, i)
                    ).approval_condition_under_config(),
                    oc,
                )
                for i, oc in approval_check_names_n_ocs
            ]
            list_of_expressions = []
            for expr_or_int, oc in list_of_cc:
                match expr_or_int:
                    case pt.Expr():
                        list_of_expressions.append(
                            pt.And(pt.Txn.on_completion() == oc, expr_or_int)
                        )
                    case 0:
                        continue
                    case 1:
                        list_of_expressions.append(pt.Txn.on_completion() == oc)
            with pt.TealComponent.Context.ignoreExprEquality():
                assert assemble_helper(mc.approval_cond()) == assemble_helper(
                    pt.Or(*list_of_expressions)
                )
Exemple #7
0
def lots_o_vars():
    z = pt.Int(0)
    one = pt.ScratchVar()
    two = pt.ScratchVar()
    three = pt.ScratchVar()
    four = pt.ScratchVar()
    five = pt.Bytes("five")
    six = pt.Bytes("six")
    seven = pt.Bytes("seven")
    eight = pt.Bytes("eight")
    nine = pt.Bytes("nine")
    ten = pt.Bytes("ten")
    eleven = pt.Bytes("eleven")
    twelve = pt.Bytes("twelve")
    int_cursor = pt.DynamicScratchVar(pt.TealType.uint64)
    bytes_cursor = pt.DynamicScratchVar(pt.TealType.bytes)
    thirteen = pt.ScratchVar(pt.TealType.uint64, 13)
    fourteen = pt.ScratchVar(pt.TealType.bytes, 14)
    fifteen = pt.ScratchVar(pt.TealType.uint64)
    sixteen = pt.ScratchVar(pt.TealType.bytes)
    leet = pt.Int(1337)
    ngl = pt.Bytes("NGL: ")
    return (pt.If(
        pt.Or(pt.App.id() == pt.Int(0),
              pt.Txn.application_args.length() == pt.Int(0))).Then(
                  pt.Int(1)).Else(
                      pt.Seq(
                          one.store(pt.Int(1)),
                          two.store(pt.Bytes("two")),
                          three.store(pt.Int(3)),
                          four.store(pt.Bytes("four")),
                          pt.App.localPut(z, five, pt.Int(5)),
                          pt.App.localPut(z, six, six),
                          pt.App.localPut(z, seven, pt.Int(7)),
                          pt.App.localPut(z, eight, eight),
                          pt.App.globalPut(nine, pt.Int(9)),
                          pt.App.globalPut(ten, ten),
                          pt.App.globalPut(eleven, pt.Int(11)),
                          pt.App.globalPut(twelve, twelve),
                          one.store(one.load() + leet),
                          two.store(pt.Concat(ngl, two.load())),
                          three.store(three.load() + leet),
                          four.store(pt.Concat(ngl, four.load())),
                          pt.App.localPut(z, five,
                                          leet + pt.App.localGet(z, five)),
                          pt.App.localPut(
                              z, six, pt.Concat(ngl, pt.App.localGet(z, six))),
                          pt.App.localPut(z, seven, pt.App.localGet(z, seven)),
                          pt.App.localPut(
                              z, eight,
                              pt.Concat(ngl, pt.App.localGet(z, eight))),
                          pt.App.globalPut(nine,
                                           leet + pt.App.globalGet(nine)),
                          pt.App.globalPut(
                              ten, pt.Concat(ngl, pt.App.globalGet(ten))),
                          pt.App.globalPut(eleven,
                                           leet + pt.App.globalGet(eleven)),
                          pt.App.globalPut(
                              twelve, pt.Concat(ngl,
                                                pt.App.globalGet(twelve))),
                          thirteen.store(pt.Btoi(pt.Txn.application_args[0])),
                          fourteen.store(pt.Txn.application_args[1]),
                          fifteen.store(pt.Btoi(pt.Txn.application_args[2])),
                          sixteen.store(pt.Txn.application_args[3]),
                          pt.Pop(one.load()),
                          pt.Pop(two.load()),
                          pt.Pop(three.load()),
                          pt.Pop(four.load()),
                          pt.Pop(pt.App.localGet(z, five)),
                          pt.Pop(pt.App.localGet(z, six)),
                          pt.Pop(pt.App.localGet(z, seven)),
                          pt.Pop(pt.App.localGet(z, eight)),
                          pt.Pop(pt.App.globalGet(nine)),
                          pt.Pop(pt.App.globalGet(ten)),
                          pt.Pop(pt.App.globalGet(eleven)),
                          pt.Pop(pt.App.globalGet(twelve)),
                          int_cursor.set_index(thirteen),
                          pt.Log(pt.Itob(int_cursor.load())),
                          bytes_cursor.set_index(fourteen),
                          pt.Log(bytes_cursor.load()),
                          int_cursor.set_index(fifteen),
                          pt.Log(pt.Itob(int_cursor.load())),
                          bytes_cursor.set_index(sixteen),
                          pt.Log(bytes_cursor.load()),
                          leet,
                      )))