1
- from typing import Any , Callable , Generic , Optional , Sequence , TypeVar , cast
2
- from dataclasses import dataclass
1
+ from typing import Any , Callable , Sequence , cast
3
2
4
3
import algosdk .abi
5
4
from algosdk .v2client import algod
6
5
7
6
from graviton import blackbox
8
- from graviton .blackbox import DryRunInspector , DryRunExecutor
7
+ from graviton .blackbox import (
8
+ DryRunInspector ,
9
+ DryRunExecutor ,
10
+ DryRunTransactionParams as TxParams ,
11
+ )
9
12
from graviton .models import PyTypes
10
13
11
14
from pyteal .ast .subroutine import OutputKwArgInfo
@@ -138,37 +141,13 @@ def decorator_blackbox(func: SubroutineFnWrapper | ABIReturnSubroutine):
138
141
# ---- API ---- #
139
142
140
143
141
- Output = TypeVar ("Output" )
142
- Lazy = Callable [[], Output ]
143
-
144
-
145
- @dataclass (frozen = True )
146
- class _MatchMode (Generic [Output ]):
147
- runner : Optional ["PyTealDryRunExecutor" ]
148
- app_case : Lazy
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 )
155
-
156
- def __call__ (self , mode : Mode , * args , ** kwargs ) -> Output :
157
- match mode :
158
- case Mode .Application :
159
- return self .app_case ()
160
- case Mode .Signature :
161
- return self .signature_case ()
162
- case _:
163
- raise Exception (f"Unknown mode { mode } of type { type (mode )} " )
164
-
165
-
166
144
def mode_to_execution_mode (mode : Mode ) -> blackbox .ExecutionMode :
167
- return _MatchMode (
168
- runner = None ,
169
- app_case = lambda : blackbox .ExecutionMode .Application ,
170
- signature_case = lambda : blackbox .ExecutionMode .Signature ,
171
- )(mode )
145
+ if mode == Mode .Application :
146
+ return blackbox .ExecutionMode .Application
147
+ if mode == Mode .Signature :
148
+ return blackbox .ExecutionMode .Signature
149
+
150
+ raise ValueError (f"Can't handle { mode = } " )
172
151
173
152
174
153
class PyTealDryRunExecutor :
@@ -182,9 +161,11 @@ def __init__(self, subr: BlackboxWrapper, mode: Mode):
182
161
mode: type of program to produce: logic sig (Mode.Signature) or app (Mode.Application)
183
162
"""
184
163
input_types = subr .input_types
185
- assert (
186
- input_types is not None
187
- ), "please provide input_types in your @Subroutine or @ABIReturnSubroutine annotation (this is crucial for generating proper end-to-end testable PyTeal)"
164
+ assert input_types is not None , (
165
+ "please provide input_types in your @Subroutine or @ABIReturnSubroutine "
166
+ "annotation. "
167
+ "(this is crucial for generating proper end-to-end testable PyTeal)"
168
+ )
188
169
189
170
self .subr , self .mode , self .input_types = subr , mode , input_types
190
171
match subr .subroutine :
@@ -417,68 +398,45 @@ def approval():
417
398
return approval
418
399
419
400
def compile (self , version : int , assemble_constants : bool = False ) -> str :
420
- return _MatchMode (
421
- runner = self ,
422
- app_case = lambda : compileTeal (
423
- self . program () ,
424
- self . mode ,
425
- version = version ,
426
- assembleConstants = assemble_constants ,
427
- ),
428
- signature_case = lambda : compileTeal (
429
- self . program (),
430
- self .mode ,
431
- version = version ,
432
- assembleConstants = assemble_constants ,
433
- ) ,
434
- )( self . mode )
435
-
436
- def dryrun_on_sequence (
401
+ return compileTeal (
402
+ self . program () ,
403
+ self . mode ,
404
+ version = version ,
405
+ assembleConstants = assemble_constants ,
406
+ )
407
+
408
+ def executor ( self , compiler_version : int = 6 ) -> DryRunExecutor :
409
+ return DryRunExecutor (
410
+ algod = algod_with_assertion (),
411
+ mode = mode_to_execution_mode ( self .mode ) ,
412
+ teal = self . compile ( compiler_version ) ,
413
+ abi_method_signature = self . abi_method_signature () ,
414
+ omit_method_selector = True ,
415
+ )
416
+
417
+ def dryrun_sequence (
437
418
self ,
438
419
inputs : list [Sequence [PyTypes ]],
420
+ * ,
439
421
compiler_version = 6 ,
422
+ txn_params : TxParams | None = None ,
423
+ verbose : bool = False ,
440
424
) -> list [DryRunInspector ]:
441
- teal = self .compile (compiler_version )
442
- return _MatchMode (
443
- self ,
444
- app_case = lambda : DryRunExecutor .dryrun_app_on_sequence (
445
- algod = algod_with_assertion (),
446
- teal = teal ,
447
- inputs = inputs ,
448
- abi_method_signature = self .abi_method_signature (),
449
- omit_method_selector = True ,
425
+ return cast (
426
+ list ,
427
+ self .executor (compiler_version ).run_sequence (
428
+ inputs , txn_params = txn_params , verbose = verbose
450
429
),
451
- signature_case = lambda : DryRunExecutor .dryrun_logicsig_on_sequence (
452
- algod = algod_with_assertion (),
453
- teal = teal ,
454
- inputs = inputs ,
455
- abi_method_signature = self .abi_method_signature (),
456
- omit_method_selector = True ,
457
- ),
458
- trace = teal ,
459
- )(self .mode )
430
+ )
460
431
461
- def dryrun (
432
+ def dryrun_one (
462
433
self ,
463
434
args : Sequence [bytes | str | int ],
435
+ * ,
464
436
compiler_version = 6 ,
437
+ txn_params : TxParams | None = None ,
438
+ verbose : bool = False ,
465
439
) -> DryRunInspector :
466
- teal = self .compile (compiler_version )
467
- return _MatchMode (
468
- self ,
469
- app_case = lambda : DryRunExecutor .dryrun_app (
470
- algod_with_assertion (),
471
- teal ,
472
- args ,
473
- abi_method_signature = self .abi_method_signature (),
474
- omit_method_selector = True ,
475
- ),
476
- signature_case = lambda : DryRunExecutor .dryrun_logicsig (
477
- algod_with_assertion (),
478
- teal ,
479
- args ,
480
- abi_method_signature = self .abi_method_signature (),
481
- omit_method_selector = True ,
482
- ),
483
- trace = teal ,
484
- )(self .mode )
440
+ return self .executor (compiler_version ).run_one (
441
+ args , txn_params = txn_params , verbose = verbose
442
+ )
0 commit comments