Example #1
0
def test_build_program_clear_state_valid_config():
    action = pt.If(pt.Txn.fee() == pt.Int(4)).Then(pt.Approve()).Else(pt.Reject())
    config = pt.CallConfig.CALL

    router_with_bare_call = pt.Router(
        "test",
        pt.BareCallActions(
            clear_state=pt.OnCompleteAction(action=action, call_config=config)
        ),
    )
    _, actual_clear_state_with_bare_call, _ = router_with_bare_call.build_program()

    expected_clear_state_with_bare_call = assemble_helper(
        pt.Cond([pt.Txn.application_args.length() == pt.Int(0), action])
    )

    with pt.TealComponent.Context.ignoreExprEquality():
        assert (
            assemble_helper(actual_clear_state_with_bare_call)
            == expected_clear_state_with_bare_call
        )

    router_with_method = pt.Router("test")

    @pt.ABIReturnSubroutine
    def clear_state_method():
        return action

    router_with_method.add_method_handler(
        clear_state_method, method_config=pt.MethodConfig(clear_state=config)
    )

    _, actual_clear_state_with_method, _ = router_with_method.build_program()

    expected_clear_state_with_method = assemble_helper(
        pt.Cond(
            [
                pt.Txn.application_args[0]
                == pt.MethodSignature("clear_state_method()void"),
                pt.Seq(clear_state_method(), pt.Approve()),
            ]
        )
    )

    with pt.TealComponent.Context.ignoreExprEquality():
        assert (
            assemble_helper(actual_clear_state_with_method)
            == expected_clear_state_with_method
        )
Example #2
0
def test_cond_has_return():
    exprWithReturn = pt.Cond([pt.Int(1), pt.Return(pt.Int(1))],
                             [pt.Int(0), pt.Return(pt.Int(0))])
    assert exprWithReturn.has_return()

    exprWithoutReturn = pt.Cond([pt.Int(1), pt.Bytes("one")],
                                [pt.Int(0), pt.Bytes("zero")])
    assert not exprWithoutReturn.has_return()

    exprSemiReturn = pt.Cond(
        [pt.Int(1), pt.Return(pt.Int(1))],
        [pt.Int(0),
         pt.App.globalPut(pt.Bytes("key"), pt.Bytes("value"))],
    )
    assert not exprSemiReturn.has_return()
Example #3
0
def test_cond_two_pred():
    expr = pt.Cond([pt.Int(1), pt.Bytes("one")], [pt.Int(0), pt.Bytes("zero")])
    assert expr.type_of() == pt.TealType.bytes

    cond1, _ = pt.Int(1).__teal__(options)
    pred1, _ = pt.Bytes("one").__teal__(options)
    cond1Branch = pt.TealConditionalBlock([])
    cond2, _ = pt.Int(0).__teal__(options)
    pred2, _ = pt.Bytes("zero").__teal__(options)
    cond2Branch = pt.TealConditionalBlock([])
    end = pt.TealSimpleBlock([])

    cond1.setNextBlock(cond1Branch)
    cond1Branch.setTrueBlock(pred1)
    cond1Branch.setFalseBlock(cond2)
    pred1.setNextBlock(end)

    cond2.setNextBlock(cond2Branch)
    cond2Branch.setTrueBlock(pred2)
    cond2Branch.setFalseBlock(pt.Err().__teal__(options)[0])
    pred2.setNextBlock(end)

    expected = cond1

    actual, _ = expr.__teal__(options)

    with pt.TealComponent.Context.ignoreExprEquality():
        assert actual == expected
Example #4
0
def test_cond():
    cond1 = pt.Txn.fee() < pt.Int(2000)
    cond2 = pt.Txn.amount() > pt.Int(5000)
    cond3 = pt.Txn.receiver() == pt.Txn.sender()
    core = pt.Cond(
        [pt.Global.group_size() == pt.Int(2), cond1],
        [pt.Global.group_size() == pt.Int(3), cond2],
        [pt.Global.group_size() == pt.Int(4), cond3],
    )
    pt.compileTeal(core, mode=pt.Mode.Signature, version=2)
Example #5
0
def test_cond_one_pred():
    expr = pt.Cond([pt.Int(1), pt.Int(2)])
    assert expr.type_of() == pt.TealType.uint64

    cond1, _ = pt.Int(1).__teal__(options)
    pred1, _ = pt.Int(2).__teal__(options)
    cond1Branch = pt.TealConditionalBlock([])
    cond1.setNextBlock(cond1Branch)
    cond1Branch.setTrueBlock(pred1)
    cond1Branch.setFalseBlock(pt.Err().__teal__(options)[0])
    pred1.setNextBlock(pt.TealSimpleBlock([]))
    expected = cond1

    actual, _ = expr.__teal__(options)

    with pt.TealComponent.Context.ignoreExprEquality():
        assert actual == expected
Example #6
0
def test_cond_invalid():
    with pytest.raises(pt.TealInputError):
        pt.Cond()

    with pytest.raises(pt.TealInputError):
        pt.Cond([])

    with pytest.raises(pt.TealInputError):
        pt.Cond([pt.Int(1)], [pt.Int(2), pt.Pop(pt.Txn.receiver())])

    with pytest.raises(pt.TealTypeError):
        pt.Cond([pt.Int(1), pt.Int(2)], [pt.Int(2), pt.Txn.receiver()])

    with pytest.raises(pt.TealTypeError):
        pt.Cond([pt.Arg(0), pt.Int(2)])

    with pytest.raises(pt.TealTypeError):
        pt.Cond([pt.Int(1), pt.Int(2)], [pt.Int(2), pt.Pop(pt.Int(2))])

    with pytest.raises(pt.TealTypeError):
        pt.Cond([pt.Int(1), pt.Pop(pt.Int(1))], [pt.Int(2), pt.Int(2)])
Example #7
0
def test_cond_two_pred_multi():
    args = [
        pt.Int(1),
        [pt.Pop(pt.Int(1)), pt.Bytes("one")],
        pt.Int(0),
        [pt.Pop(pt.Int(2)), pt.Bytes("zero")],
    ]
    expr = pt.Cond(
        [args[0]] + args[1],
        [args[2]] + args[3],
    )
    assert expr.type_of() == pt.TealType.bytes

    cond1, _ = args[0].__teal__(options)
    pred1, pred1End = pt.Seq(args[1]).__teal__(options)
    cond1Branch = pt.TealConditionalBlock([])
    cond2, _ = args[2].__teal__(options)
    pred2, pred2End = pt.Seq(args[3]).__teal__(options)
    cond2Branch = pt.TealConditionalBlock([])
    end = pt.TealSimpleBlock([])

    cond1.setNextBlock(cond1Branch)
    cond1Branch.setTrueBlock(pred1)
    cond1Branch.setFalseBlock(cond2)
    pred1End.setNextBlock(end)

    cond2.setNextBlock(cond2Branch)
    cond2Branch.setTrueBlock(pred2)
    cond2Branch.setFalseBlock(pt.Err().__teal__(options)[0])
    pred2End.setNextBlock(end)

    expected = cond1

    actual, _ = expr.__teal__(options)

    with pt.TealComponent.Context.ignoreExprEquality():
        assert actual == expected