Skip to content

Commit

Permalink
feat: support a wider range of types for inner transaction applicatio…
Browse files Browse the repository at this point in the history
…n args
  • Loading branch information
daniel-makerx authored and achidlow committed Jun 25, 2024
1 parent 070d224 commit 28b5197
Show file tree
Hide file tree
Showing 23 changed files with 1,855 additions and 921 deletions.
2 changes: 1 addition & 1 deletion examples/sizes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
inner_transactions 1259 1193 66 1193 0
inner_transactions/ArrayAccess 214 195 19 195 0
inner_transactions/CreateAndTransfer 139 123 16 123 0
inner_transactions/FieldTuple 505 462 43 462 0
inner_transactions/FieldTuple 653 537 116 537 0
inner_transactions/Greeter 329 305 24 305 0
inner_transactions/itxn_loop 192 184 8 184 0
intrinsics/ImmediateVariants 166 162 4 162 0
Expand Down
4 changes: 3 additions & 1 deletion src/puya/awst_build/eb/_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Lvalue,
Statement,
)
from puya.awst_build import pytypes
from puya.awst_build import intrinsic_factory, pytypes
from puya.awst_build.eb.interface import (
BuilderBinaryOp,
BuilderComparisonOp,
Expand Down Expand Up @@ -79,6 +79,8 @@ def to_bytes(self, location: SourceLocation) -> Expression:
return BytesConstant(
value=bytes_value, encoding=BytesEncoding.unknown, source_location=location
)
case bool():
return intrinsic_factory.itob(self.resolve(), location)
raise CodeError(f"cannot serialize literal of type {self.pytype}", location)

@typing.override
Expand Down
26 changes: 25 additions & 1 deletion src/puya/awst_build/eb/transaction/inner_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import mypy.nodes

from puya import log
from puya.awst import wtypes
from puya.awst.nodes import (
INNER_PARAM_TXN_FIELDS,
Copy,
CreateInnerTransaction,
Expression,
SubmitInnerTransaction,
TupleExpression,
TxnField,
TxnFields,
UInt64Constant,
Expand All @@ -21,6 +23,7 @@
from puya.awst_build.eb._utils import bool_eval_to_constant, expect_no_args
from puya.awst_build.eb.interface import InstanceBuilder, NodeBuilder, TypeBuilder
from puya.awst_build.eb.transaction import get_field_python_name
from puya.awst_build.eb.tuple import TupleLiteralBuilder
from puya.awst_build.eb.void import VoidExpressionBuilder
from puya.awst_build.utils import (
expect_operand_type,
Expand All @@ -31,6 +34,7 @@
from puya.errors import CodeError, InternalError
from puya.parse import SourceLocation

logger = log.get_logger(__name__)
_parameter_mapping: typing.Final = {
get_field_python_name(f): (f, pytypes.from_basic_wtype(f.wtype))
for f in INNER_PARAM_TXN_FIELDS
Expand All @@ -46,13 +50,33 @@ def get_field_expr(arg_name: str, arg: InstanceBuilder) -> tuple[TxnField, Expre
return remapped_field
elif field.is_array:
match arg:
case TupleLiteralBuilder(
items=items,
source_location=tup_loc,
) if field == TxnFields.app_args:
for item in items:
if item.pytype == pytypes.AccountType:
logger.warning(
f"{item.pytype} will not be added to foreign array,"
f" use .bytes to suppress this warning",
location=item.source_location,
)
elif item.pytype in (pytypes.AssetType, pytypes.ApplicationType):
logger.warning(
f"{item.pytype} will not be added to foreign array,"
f" use .id to suppress this warning",
location=item.source_location,
)
expr: Expression = TupleExpression.from_items(
[item.to_bytes(item.source_location) for item in items], tup_loc
)
return field, expr
case InstanceBuilder(pytype=pytypes.TupleType(items=tuple_item_types)) if all(
field.valid_type(t.wtype) for t in tuple_item_types
):
expr = arg.resolve()
return field, expr
raise CodeError(f"{arg_name} should be of type tuple[{field.type_desc}, ...]")
# TODO(first): pull the below out, and reuse above inside tuples, allowing literals in tuples
elif field_pytype == pytypes.BytesType:
# this handles the overlapping case of allowing Bytes && String to a single field
field_expr = arg.to_bytes(arg.source_location)
Expand Down
8 changes: 4 additions & 4 deletions stubs/algopy-stubs/itxn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class InnerTransaction(_InnerTransaction[InnerTransactionResult]):
local_num_uint: UInt64 | int = ...,
local_num_bytes: UInt64 | int = ...,
extra_program_pages: UInt64 | int = ...,
app_args: tuple[Bytes | BytesBacked, ...] = ...,
app_args: tuple[object, ...] = ...,
accounts: tuple[Account, ...] = ...,
assets: tuple[Asset, ...] = ...,
apps: tuple[Application, ...] = ...,
Expand Down Expand Up @@ -193,7 +193,7 @@ class InnerTransaction(_InnerTransaction[InnerTransactionResult]):
local_num_uint: UInt64 | int = ...,
local_num_bytes: UInt64 | int = ...,
extra_program_pages: UInt64 | int = ...,
app_args: tuple[Bytes | BytesBacked, ...] = ...,
app_args: tuple[object, ...] = ...,
accounts: tuple[Account, ...] = ...,
assets: tuple[Asset, ...] = ...,
apps: tuple[Application, ...] = ...,
Expand Down Expand Up @@ -385,7 +385,7 @@ class ApplicationCall(_InnerTransaction[ApplicationCallInnerTransaction]):
local_num_uint: UInt64 | int = ...,
local_num_bytes: UInt64 | int = ...,
extra_program_pages: UInt64 | int = ...,
app_args: tuple[Bytes | BytesBacked, ...] = ...,
app_args: tuple[object, ...] = ...,
accounts: tuple[Account, ...] = ...,
assets: tuple[Asset, ...] = ...,
apps: tuple[Application, ...] = ...,
Expand All @@ -406,7 +406,7 @@ class ApplicationCall(_InnerTransaction[ApplicationCallInnerTransaction]):
local_num_uint: UInt64 | int = ...,
local_num_bytes: UInt64 | int = ...,
extra_program_pages: UInt64 | int = ...,
app_args: tuple[Bytes | BytesBacked, ...] = ...,
app_args: tuple[object, ...] = ...,
accounts: tuple[Account, ...] = ...,
assets: tuple[Asset, ...] = ...,
apps: tuple[Application, ...] = ...,
Expand Down
18 changes: 17 additions & 1 deletion test_cases/inner_transactions/field_tuple_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
ARC4Contract,
Bytes,
OnCompleteAction,
String,
UInt64,
arc4,
itxn,
op,
)

LOG_1ST_ARG_AND_APPROVE = (
Expand All @@ -26,7 +29,15 @@ def test_assign_tuple(self) -> None:
approval_program=ALWAYS_APPROVE,
clear_state_program=ALWAYS_APPROVE,
on_completion=OnCompleteAction.DeleteApplication,
app_args=(Bytes(b"1a"), Bytes(b"2a")),
app_args=(
Bytes(b"1a"),
Bytes(b"2a"),
b"hello",
"world",
String("!"),
UInt64(42),
True,
),
),
itxn.ApplicationCall(
approval_program=ALWAYS_APPROVE,
Expand All @@ -40,6 +51,11 @@ def test_assign_tuple(self) -> None:

assert txn_1.app_args(0) == b"1a"
assert txn_1.app_args(1) == b"2a"
assert txn_1.app_args(2) == b"hello"
assert txn_1.app_args(3) == b"world"
assert txn_1.app_args(4) == b"!"
assert txn_1.app_args(5) == op.itob(42)
assert txn_1.app_args(6) == op.itob(1)
assert txn_2.app_args(0) == b"3a"
assert txn_2.app_args(1) == b"4a"
assert txn_2.app_args(2) == b"5a"
Expand Down
Loading

0 comments on commit 28b5197

Please sign in to comment.