Skip to content

Commit 38934d2

Browse files
authored
allow app id to be none since it may be called during create (#592)
1 parent 3d061c2 commit 38934d2

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
# Fixed
4+
* Allowing the `MethodCall` and `ExecuteMethodCall` to be passed `None` as app_id argument in the case of an app create transaction ([#592](https://github.com/algorand/pyteal/pull/592))
5+
6+
37
# 0.20.1
48

59
## Added

pyteal/ast/itxn.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,10 @@ def SetFields(cls, fields: dict[TxnField, Expr | list[Expr]]) -> Expr:
253253
def ExecuteMethodCall(
254254
cls,
255255
*,
256-
app_id: Expr,
256+
app_id: Expr | None,
257257
method_signature: str,
258258
args: list[abi.BaseType | Expr | dict[TxnField, Expr | list[Expr]]],
259-
extra_fields: dict[TxnField, Expr | list[Expr]] = None,
259+
extra_fields: dict[TxnField, Expr | list[Expr]] | None = None,
260260
) -> Expr:
261261
"""Performs a single app call transaction formatted as an ABI method call.
262262
@@ -277,6 +277,7 @@ def ExecuteMethodCall(
277277
278278
Args:
279279
app_id: An expression that evaluates to a `TealType.uint64` corresponding to the application being called.
280+
If the call is meant to create an application, the value `None` should be passed
280281
method_signature: A string representing the method signature of the method we're calling. This is used to do
281282
type checking on the arguments passed and to create the method selector passed as the first argument.
282283
args: A list of arguments to pass to the application. The values in this list depend on the kind of argument you wish to pass:
@@ -310,10 +311,10 @@ def ExecuteMethodCall(
310311
def MethodCall(
311312
cls,
312313
*,
313-
app_id: Expr,
314+
app_id: Expr | None,
314315
method_signature: str,
315316
args: list[abi.BaseType | Expr | dict[TxnField, Expr | list[Expr]]],
316-
extra_fields: dict[TxnField, Expr | list[Expr]] = None,
317+
extra_fields: dict[TxnField, Expr | list[Expr]] | None = None,
317318
) -> Expr:
318319
"""Adds an ABI method call transaction to the current inner transaction group.
319320
@@ -323,6 +324,7 @@ def MethodCall(
323324
324325
Args:
325326
app_id: An expression that evaluates to a `TealType.uint64` corresponding to the application being called.
327+
If the call is meant to create an application, the value `None` should be passed
326328
method_signature: A string representing the method signature of the method we're calling. This is used to do
327329
type checking on the arguments passed and to create the method selector passed as the first argument.
328330
args: A list of arguments to pass to the application. The values in this list depend on the kind of argument you wish to pass:
@@ -344,14 +346,16 @@ def MethodCall(
344346

345347
from pyteal.ast.abi.util import type_spec_is_assignable_to
346348

347-
require_type(app_id, TealType.uint64)
348-
349-
# Default, always need these
349+
# Start collecting the fields we'd like to set
350350
fields_to_set = [
351351
cls.SetField(TxnField.type_enum, TxnType.ApplicationCall),
352-
cls.SetField(TxnField.application_id, app_id),
353352
]
354353

354+
# In the case of an app create, the `app_id` arg may be `None`
355+
if app_id is not None:
356+
require_type(app_id, TealType.uint64)
357+
fields_to_set.append(cls.SetField(TxnField.application_id, app_id))
358+
355359
# We only care about the args
356360
arg_type_specs: list[abi.TypeSpec]
357361
arg_type_specs, _ = abi.type_specs_from_signature(method_signature)

pyteal/ast/itxn_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,27 @@ def test_InnerTxnBuilder_Execute():
330330
),
331331
None,
332332
),
333+
# App create case
334+
(
335+
None,
336+
"create(byte[],uint64)void",
337+
[t6_1 := pt.abi.DynamicBytes(), t6_2 := pt.abi.Uint64()],
338+
{TxnField.fee: pt.Int(0)},
339+
pt.Seq(
340+
pt.InnerTxnBuilder.SetFields(
341+
{
342+
pt.TxnField.type_enum: TxnType.ApplicationCall,
343+
pt.TxnField.application_args: [
344+
pt.MethodSignature("create(byte[],uint64)void"),
345+
t6_1.encode(),
346+
t6_2.encode(),
347+
],
348+
pt.TxnField.fee: pt.Int(0),
349+
}
350+
),
351+
),
352+
None,
353+
),
333354
# Error cases
334355
(
335356
pt.Int(1),

0 commit comments

Comments
 (0)