Example #1
0
    def contract_construct(self) -> sdk_abi.Contract:
        """A helper function in constructing a `Contract` object.

        It takes out the method spec from approval program methods,
        and constructs an `Contract` object.

        Returns:
            A Python SDK `Contract` object constructed from the registered methods on this router.
        """

        return sdk_abi.Contract(self.name, self.methods, self.descr)
Example #2
0
def test_build_program_all_empty():
    router = pt.Router("test")

    approval, clear_state, contract = router.build_program()

    expected_empty_program = pt.TealSimpleBlock(
        [
            pt.TealOp(None, pt.Op.int, 0),
            pt.TealOp(None, pt.Op.return_),
        ]
    )

    with pt.TealComponent.Context.ignoreExprEquality():
        assert assemble_helper(approval) == expected_empty_program
        assert assemble_helper(clear_state) == expected_empty_program

    expected_contract = sdk_abi.Contract("test", [])
    assert contract == expected_contract
Example #3
0
def test_build_program_clear_state_empty():
    router = pt.Router(
        "test", pt.BareCallActions(no_op=pt.OnCompleteAction.always(pt.Approve()))
    )

    approval, clear_state, contract = router.build_program()

    expected_empty_program = pt.TealSimpleBlock(
        [
            pt.TealOp(None, pt.Op.int, 0),
            pt.TealOp(None, pt.Op.return_),
        ]
    )

    with pt.TealComponent.Context.ignoreExprEquality():
        assert assemble_helper(approval) != expected_empty_program
        assert assemble_helper(clear_state) == expected_empty_program

    expected_contract = sdk_abi.Contract("test", [])
    assert contract == expected_contract
Example #4
0
def test_contract_json_obj():
    abi_subroutines = list(
        filter(lambda x: isinstance(x, pt.ABIReturnSubroutine), GOOD_SUBROUTINE_CASES)
    )
    contract_name = "contract_name"
    on_complete_actions = pt.BareCallActions(
        clear_state=pt.OnCompleteAction.call_only(safe_clear_state_delete)
    )
    router = pt.Router(contract_name, on_complete_actions)
    method_list: list[sdk_abi.Method] = []
    for subroutine in abi_subroutines:

        doc = subroutine.subroutine.implementation.__doc__
        desc = None
        if doc is not None and doc.strip() == "replace me":
            desc = "dope description"

        router.add_method_handler(subroutine, description=desc)

        ms = subroutine.method_spec()

        # Manually replace it since the override is applied in the method handler
        # not attached to the ABIReturnSubroutine itself
        ms.desc = desc if desc is not None else ms.desc

        sig_method = sdk_abi.Method.from_signature(subroutine.method_signature())

        assert ms.name == sig_method.name

        for idx, arg in enumerate(ms.args):
            assert arg.type == sig_method.args[idx].type

        method_list.append(ms)

    sdk_contract = sdk_abi.Contract(contract_name, method_list)
    contract = router.contract_construct()
    assert contract == sdk_contract