1- from typing import TYPE_CHECKING , cast
1+ from typing import TYPE_CHECKING , cast , Union
22
33from pyteal .config import MAX_GROUP_SIZE
44
55from ..errors import TealInputError , verifyFieldVersion , verifyTealVersion
66from ..ir import TealOp , Op , TealBlock
7+ from .expr import Expr
78from .txn import TxnExpr , TxnField , TxnObject , TxnaExpr
9+ from ..types import require_type , TealType
810
911if TYPE_CHECKING :
1012 from ..compiler import CompileOptions
@@ -15,6 +17,15 @@ class GitxnExpr(TxnExpr):
1517
1618 def __init__ (self , txnIndex : int , field : TxnField ) -> None :
1719 super ().__init__ (Op .gitxn , "Gitxn" , field )
20+
21+ # Currently we do not have gitxns. Only gitxn with immediate transaction index supported.
22+ if type (txnIndex ) is not int :
23+ raise TealInputError (
24+ "Invalid gitxn syntax with immediate transaction field {} and transaction index {}" .format (
25+ field , txnIndex
26+ )
27+ )
28+
1829 self .txnIndex = txnIndex
1930
2031 def __str__ (self ):
@@ -23,14 +34,6 @@ def __str__(self):
2334 def __teal__ (self , options : "CompileOptions" ):
2435 verifyFieldVersion (self .field .arg_name , self .field .min_version , options .version )
2536
26- # currently we do not have gitxns, only gitxn with immediate transaction index supported
27- if type (self .txnIndex ) is not int :
28- raise TealInputError (
29- "Invalid gitxn syntax with immediate transaction field {} and transaction index {}" .format (
30- self .field , self .txnIndex
31- )
32- )
33-
3437 verifyTealVersion (
3538 Op .gitxn .min_version ,
3639 options .version ,
@@ -46,8 +49,14 @@ def __teal__(self, options: "CompileOptions"):
4649class GitxnaExpr (TxnaExpr ):
4750 """An expression that accesses an inner transaction array field from an inner transaction in the last inner group."""
4851
49- def __init__ (self , txnIndex : int , field : TxnField , index : int ) -> None :
50- super ().__init__ (Op .gitxna , None , "Gitxna" , field , index )
52+ def __init__ (self , txnIndex : int , field : TxnField , index : Union [int , Expr ]) -> None :
53+ super ().__init__ (Op .gitxna , Op .gitxnas , "Gitxna" , field , index )
54+
55+ if type (txnIndex ) is not int :
56+ raise TealInputError (
57+ f"Invalid txnIndex type: Expected int, but received { txnIndex } ."
58+ )
59+
5160 self .txnIndex = txnIndex
5261
5362 def __str__ (self ):
@@ -57,18 +66,23 @@ def __str__(self):
5766
5867 def __teal__ (self , options : "CompileOptions" ):
5968 verifyFieldVersion (self .field .arg_name , self .field .min_version , options .version )
60- if type (self .txnIndex ) is not int or type (self .index ) is not int :
61- raise TealInputError (
62- "Invalid gitxna syntax with immediate transaction index {}, transaction field {}, array index {}" .format (
63- self .txnIndex , self .field , self .index
64- )
65- )
69+
70+ if type (self .index ) is int :
71+ opToUse = Op .gitxna
72+ else :
73+ opToUse = Op .gitxnas
6674
6775 verifyTealVersion (
68- Op .gitxna .min_version , options .version , "TEAL version too low to use gitxna"
76+ opToUse .min_version ,
77+ options .version ,
78+ "TEAL version too low to use op {}" .format (opToUse ),
6979 )
70- op = TealOp (self , Op .gitxna , self .txnIndex , self .field .arg_name , self .index )
71- return TealBlock .FromOp (options , op )
80+
81+ if type (self .index ) is int :
82+ op = TealOp (self , opToUse , self .txnIndex , self .field .arg_name , self .index )
83+ return TealBlock .FromOp (options , op )
84+ op = TealOp (self , opToUse , self .txnIndex , self .field .arg_name )
85+ return TealBlock .FromOp (options , op , cast (Expr , self .index ))
7286
7387
7488GitxnaExpr .__module__ = "pyteal"
@@ -85,14 +99,14 @@ def __getitem__(self, txnIndex: int) -> TxnObject:
8599
86100 if txnIndex < 0 or txnIndex >= MAX_GROUP_SIZE :
87101 raise TealInputError (
88- "Invalid Gtxn index {}, shoud be in [0, {})" .format (
102+ "Invalid Gtxn index {}, should be in [0, {})" .format (
89103 txnIndex , MAX_GROUP_SIZE
90104 )
91105 )
92106
93107 return TxnObject (
94108 lambda field : GitxnExpr (txnIndex , field ),
95- lambda field , index : GitxnaExpr (txnIndex , field , cast ( int , index ) ),
109+ lambda field , index : GitxnaExpr (txnIndex , field , index ),
96110 )
97111
98112
0 commit comments