1
- from typing import Callable , Generic , Sequence , TypeVar , cast
1
+ from typing import Any , Callable , Generic , Optional , Sequence , TypeVar , cast
2
2
from dataclasses import dataclass
3
3
4
4
import algosdk .abi
@@ -144,8 +144,14 @@ def decorator_blackbox(func: SubroutineFnWrapper | ABIReturnSubroutine):
144
144
145
145
@dataclass (frozen = True )
146
146
class _MatchMode (Generic [Output ]):
147
+ runner : Optional ["PyTealDryRunExecutor" ]
147
148
app_case : Lazy
148
149
signature_case : Lazy
150
+ trace : Any = None
151
+
152
+ def __post_init__ (self ):
153
+ if self .runner and self .trace :
154
+ self .runner .add_trace (self .trace )
149
155
150
156
def __call__ (self , mode : Mode , * args , ** kwargs ) -> Output :
151
157
match mode :
@@ -159,6 +165,7 @@ def __call__(self, mode: Mode, *args, **kwargs) -> Output:
159
165
160
166
def mode_to_execution_mode (mode : Mode ) -> blackbox .ExecutionMode :
161
167
return _MatchMode (
168
+ runner = None ,
162
169
app_case = lambda : blackbox .ExecutionMode .Application ,
163
170
signature_case = lambda : blackbox .ExecutionMode .Signature ,
164
171
)(mode )
@@ -192,9 +199,31 @@ def __init__(self, subr: BlackboxWrapper, mode: Mode):
192
199
193
200
self ._pyteal_lambda : Callable [..., Expr ] = approval
194
201
202
+ self .traces : list = []
203
+
204
+ def add_trace (self , trace : Any ) -> None :
205
+ self .traces .append (trace )
206
+
195
207
def is_abi (self ) -> bool :
196
208
return isinstance (self .subr .subroutine , ABIReturnSubroutine )
197
209
210
+ def abi_method_signature (self ) -> None | str :
211
+ if self .is_abi ():
212
+ abi_subr = cast (ABIReturnSubroutine , self .subr .subroutine )
213
+ return abi_subr .method_signature ()
214
+
215
+ # create an artificial method signature
216
+ # based on the `abi_argument_types()` and `abi_return_type()`
217
+ if arg_types := self .abi_argument_types ():
218
+ if all (t is None for t in arg_types ):
219
+ return None
220
+
221
+ ret_type = self .abi_return_type ()
222
+ ret = str (ret_type ) if ret_type else "void"
223
+ return f"ptdre_foo({ ',' .join (map (str , arg_types ))} ){ ret } "
224
+
225
+ return None
226
+
198
227
def abi_argument_types (self ) -> None | list [algosdk .abi .ABIType ]:
199
228
if not (self .input_types or self .is_abi ()):
200
229
return None
@@ -389,6 +418,7 @@ def approval():
389
418
390
419
def compile (self , version : int , assemble_constants : bool = False ) -> str :
391
420
return _MatchMode (
421
+ runner = self ,
392
422
app_case = lambda : compileTeal (
393
423
self .program (),
394
424
self .mode ,
@@ -408,41 +438,47 @@ def dryrun_on_sequence(
408
438
inputs : list [Sequence [PyTypes ]],
409
439
compiler_version = 6 ,
410
440
) -> list [DryRunInspector ]:
441
+ teal = self .compile (compiler_version )
411
442
return _MatchMode (
443
+ self ,
412
444
app_case = lambda : DryRunExecutor .dryrun_app_on_sequence (
413
445
algod = algod_with_assertion (),
414
- teal = self . compile ( compiler_version ) ,
446
+ teal = teal ,
415
447
inputs = inputs ,
416
- abi_argument_types = self .abi_argument_types (),
417
- abi_return_type = self . abi_return_type () ,
448
+ abi_method_signature = self .abi_method_signature (),
449
+ omit_method_selector = True ,
418
450
),
419
451
signature_case = lambda : DryRunExecutor .dryrun_logicsig_on_sequence (
420
452
algod = algod_with_assertion (),
421
- teal = self . compile ( compiler_version ) ,
453
+ teal = teal ,
422
454
inputs = inputs ,
423
- abi_argument_types = self .abi_argument_types (),
424
- abi_return_type = self . abi_return_type () ,
455
+ abi_method_signature = self .abi_method_signature (),
456
+ omit_method_selector = True ,
425
457
),
458
+ trace = teal ,
426
459
)(self .mode )
427
460
428
461
def dryrun (
429
462
self ,
430
463
args : Sequence [bytes | str | int ],
431
464
compiler_version = 6 ,
432
465
) -> DryRunInspector :
466
+ teal = self .compile (compiler_version )
433
467
return _MatchMode (
468
+ self ,
434
469
app_case = lambda : DryRunExecutor .dryrun_app (
435
470
algod_with_assertion (),
436
- self . compile ( compiler_version ) ,
471
+ teal ,
437
472
args ,
438
- self .abi_argument_types (),
439
- self . abi_return_type () ,
473
+ abi_method_signature = self .abi_method_signature (),
474
+ omit_method_selector = True ,
440
475
),
441
476
signature_case = lambda : DryRunExecutor .dryrun_logicsig (
442
477
algod_with_assertion (),
443
- self . compile ( compiler_version ) ,
478
+ teal ,
444
479
args ,
445
- self .abi_argument_types (),
446
- self . abi_return_type () ,
480
+ abi_method_signature = self .abi_method_signature (),
481
+ omit_method_selector = True ,
447
482
),
483
+ trace = teal ,
448
484
)(self .mode )
0 commit comments