Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overriding name to method spec so it comes through in contract json #550

Merged
merged 9 commits into from
Oct 7, 2022
2 changes: 1 addition & 1 deletion pyteal/ast/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def add_method_handler(
f"with {self.method_selector_to_sig[method_selector]}"
)

meth = method_call.method_spec()
meth = method_call.method_spec(overriding_name)
if description is not None:
meth.desc = description
self.methods.append(meth)
Expand Down
21 changes: 21 additions & 0 deletions pyteal/ast/router_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,24 @@ def clear_state_method():
assemble_helper(actual_clear_state_with_method)
== expected_clear_state_with_method
)


def test_override_names():
michaeldiamant marked this conversation as resolved.
Show resolved Hide resolved

router = pt.Router("test")

@router.method(name="handle")
def handle_asa(deposit: pt.abi.AssetTransferTransaction):
"""handles the deposit where the input is an asset transfer"""
return pt.Assert(deposit.get().asset_amount() > pt.Int(0))

@router.method(name="handle")
def handle_algo(deposit: pt.abi.PaymentTransaction):
"""handles the deposit where the input is a payment"""
return pt.Assert(deposit.get().amount() > pt.Int(0))

_, _, contract = router.compile_program(version=7)
assert len(contract.methods) > 0
for meth in contract.methods:
dmeth = meth.dictify()
assert dmeth["name"] == "handle"
5 changes: 3 additions & 2 deletions pyteal/ast/subroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ def method_signature(self, overriding_name: str = None) -> str:
overriding_name = self.name()
return f"{overriding_name}({','.join(args)}){self.type_of()}"

def method_spec(self) -> sdk_abi.Method:
def method_spec(self, overriding_name: str = None) -> sdk_abi.Method:
desc: str = ""
arg_descs: dict[str, str] = {}
return_desc: str = ""
Expand Down Expand Up @@ -674,7 +674,8 @@ def method_spec(self) -> sdk_abi.Method:
return_obj["desc"] = return_desc

# Create the method spec, adding description if set
spec = {"name": self.name(), "args": args, "returns": return_obj}
name = overriding_name if overriding_name is not None else self.name()
spec = {"name": name, "args": args, "returns": return_obj}
if desc:
spec["desc"] = desc

Expand Down
11 changes: 11 additions & 0 deletions pyteal/ast/subroutine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,3 +1464,14 @@ def withdraw(amount: pt.abi.Uint64, recipient: pt.abi.Account):
== "An account who will receive the withdrawn Algos. This may or may not be the same as the method call sender."
)
assert "desc" not in mspec_dict["returns"]


def test_override_abi_method_name():
def abi_meth(a: pt.abi.Uint64, b: pt.abi.Uint64, *, output: pt.abi.Uint64):
return output.set(a.get() + b.get())

mspec = ABIReturnSubroutine(abi_meth).method_spec().dictify()
assert mspec["name"] == "abi_meth"

mspec = ABIReturnSubroutine(abi_meth).method_spec("add").dictify()
assert mspec["name"] == "add"