-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
method
pseudo-op support for ABI methods (#153)
- Add support for `method` pseudo-opcode in PyTeal. - Add `name` field in `subroutine` to override __name__ from function implementation, for readability in generated code.
- Loading branch information
Showing
8 changed files
with
172 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import TYPE_CHECKING | ||
from pyteal.errors import TealInputError | ||
|
||
from pyteal.types import TealType | ||
|
||
from ..types import TealType | ||
from ..ir import TealOp, Op, TealBlock | ||
from .leafexpr import LeafExpr | ||
|
||
if TYPE_CHECKING: | ||
from ..compiler import CompileOptions | ||
|
||
|
||
class MethodSignature(LeafExpr): | ||
"""An expression that represents an ABI method selector""" | ||
|
||
def __init__(self, methodName: str) -> None: | ||
"""Create a new method selector for ABI method call. | ||
Args: | ||
methodName: A string containing a valid ABI method signature | ||
""" | ||
super().__init__() | ||
if type(methodName) is not str: | ||
raise TealInputError( | ||
"invalid input type {} to Method".format(type(methodName)) | ||
) | ||
elif len(methodName) == 0: | ||
raise TealInputError("invalid input empty string to Method") | ||
self.methodName = methodName | ||
|
||
def __teal__(self, options: "CompileOptions"): | ||
op = TealOp(self, Op.method_signature, '"{}"'.format(self.methodName)) | ||
return TealBlock.FromOp(options, op) | ||
|
||
def __str__(self) -> str: | ||
return "(method: {})".format(self.methodName) | ||
|
||
def type_of(self) -> TealType: | ||
return TealType.bytes | ||
|
||
|
||
MethodSignature.__module__ = "pyteal" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pytest | ||
|
||
from pyteal.ast.methodsig import MethodSignature | ||
|
||
from .. import * | ||
|
||
|
||
def test_method(): | ||
expr = MethodSignature("add(uint64,uint64)uint64") | ||
assert expr.type_of() == TealType.bytes | ||
|
||
expected = TealSimpleBlock( | ||
[TealOp(expr, Op.method_signature, '"add(uint64,uint64)uint64"')] | ||
) | ||
actual, _ = expr.__teal__(CompileOptions()) | ||
assert expected == actual | ||
|
||
|
||
def test_method_invalid(): | ||
with pytest.raises(TealInputError): | ||
MethodSignature(114514) | ||
|
||
with pytest.raises(TealInputError): | ||
MethodSignature(['"m0()void"', '"m1()uint64"']) | ||
|
||
with pytest.raises(TealInputError): | ||
MethodSignature("") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters