Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pyteal/ast/global_.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class GlobalField(Enum):
creator_address = (9, "CreatorAddress", TealType.bytes, 3)
current_app_address = (10, "CurrentApplicationAddress", TealType.bytes, 5)
group_id = (11, "GroupID", TealType.bytes, 5)
opcode_budget = (12, "OpcodeBudget", TealType.uint64, 6)
caller_app_id = (13, "CallerApplicationID", TealType.uint64, 6)
caller_app_address = (14, "CallerApplicationAddress", TealType.bytes, 6)

def __init__(self, id: int, name: str, type: TealType, min_version: int) -> None:
self.id = id
Expand Down Expand Up @@ -131,5 +134,30 @@ def group_id(cls) -> "Global":
Requires TEAL version 5 or higher."""
return cls(GlobalField.group_id)

@classmethod
def opcode_budget(cls) -> "Global":
"""Get the remaining opcode execution budget

Requires TEAL version 6 or higher."""
return cls(GlobalField.opcode_budget)

@classmethod
def caller_app_id(cls) -> "Global":
"""Get the id of the app that submitted the InnerTransaction that triggered this app to execute.

If not called from another app, this will return 0

Requires TEAL version 6 or higher."""
return cls(GlobalField.caller_app_id)

@classmethod
def caller_app_address(cls) -> "Global":
"""Get the address of the app that submitted the InnerTransaction that triggered this app to execute.

If not called from another app, this will return the ZeroAddress

Requires TEAL version 6 or higher."""
return cls(GlobalField.caller_app_address)


Global.__module__ = "pyteal"
43 changes: 43 additions & 0 deletions pyteal/ast/global_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
teal2Options = CompileOptions(version=2)
teal3Options = CompileOptions(version=3)
teal5Options = CompileOptions(version=5)
teal6Options = CompileOptions(version=6)


def test_global_min_txn_fee():
Expand Down Expand Up @@ -149,3 +150,45 @@ def test_global_group_id():

with pytest.raises(TealInputError):
expr.__teal__(teal3Options)


def test_global_opcode_budget():
expr = Global.opcode_budget()
assert expr.type_of() == TealType.uint64

expected = TealSimpleBlock([TealOp(expr, Op.global_, "OpcodeBudget")])

actual, _ = expr.__teal__(teal6Options)

assert actual == expected

with pytest.raises(TealInputError):
expr.__teal__(teal5Options)


def test_global_caller_application_id():
expr = Global.caller_app_id()
assert expr.type_of() == TealType.uint64

expected = TealSimpleBlock([TealOp(expr, Op.global_, "CallerApplicationID")])

actual, _ = expr.__teal__(teal6Options)

assert actual == expected

with pytest.raises(TealInputError):
expr.__teal__(teal5Options)


def test_global_caller_app_address():
expr = Global.caller_app_address()
assert expr.type_of() == TealType.bytes

expected = TealSimpleBlock([TealOp(expr, Op.global_, "CallerApplicationAddress")])

actual, _ = expr.__teal__(teal6Options)

assert actual == expected

with pytest.raises(TealInputError):
expr.__teal__(teal5Options)