-
Notifications
You must be signed in to change notification settings - Fork 8
/
inner.py
144 lines (125 loc) · 4.83 KB
/
inner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from __future__ import annotations
import typing
from typing import Sequence
from puya.awst import wtypes
from puya.awst.nodes import (
Expression,
InnerTransactionField,
Literal,
SubmitInnerTransaction,
TxnField,
)
from puya.awst_build.eb.base import (
ExpressionBuilder,
IntermediateExpressionBuilder,
TypeClassExpressionBuilder,
)
from puya.awst_build.eb.transaction.base import (
BaseTransactionExpressionBuilder,
expect_wtype,
)
from puya.awst_build.eb.var_factory import var_expression
from puya.awst_build.utils import expect_operand_wtype
from puya.errors import CodeError
if typing.TYPE_CHECKING:
import mypy.nodes
from puya.awst_build.constants import TransactionType
from puya.parse import SourceLocation
class InnerTransactionArrayExpressionBuilder(IntermediateExpressionBuilder):
def __init__(
self,
transaction: Expression,
field: TxnField,
location: SourceLocation,
):
super().__init__(location)
self.transaction = transaction
self.field = field
def call(
self,
args: Sequence[ExpressionBuilder | Literal],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
original_expr: mypy.nodes.CallExpr,
) -> ExpressionBuilder:
match args:
case [(ExpressionBuilder() | Literal(value=int())) as eb]:
index_expr = expect_operand_wtype(eb, wtypes.uint64_wtype)
expr = InnerTransactionField(
source_location=location,
wtype=self.field.wtype,
itxn=self.transaction,
field=self.field,
array_index=index_expr,
)
return var_expression(expr)
case _:
raise CodeError("Invalid/unhandled arguments", location)
class InnerTransactionExpressionBuilder(BaseTransactionExpressionBuilder):
def __init__(self, expr: Expression):
self.wtype = expect_wtype(expr, wtypes.WInnerTransaction)
super().__init__(expr)
def get_field_value(self, field: TxnField, location: SourceLocation) -> Expression:
return InnerTransactionField(
itxn=self.expr,
field=field,
source_location=location,
wtype=field.wtype,
)
def get_array_member(self, field: TxnField, location: SourceLocation) -> ExpressionBuilder:
return InnerTransactionArrayExpressionBuilder(self.expr, field, location)
class InnerTransactionClassExpressionBuilder(TypeClassExpressionBuilder):
def __init__(self, location: SourceLocation, wtype: wtypes.WInnerTransaction):
super().__init__(location)
self.wtype = wtype
def produces(self) -> wtypes.WType:
return self.wtype
def call(
self,
args: Sequence[ExpressionBuilder | Literal],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
original_expr: mypy.nodes.CallExpr,
) -> typing.Never:
params_wtype = wtypes.WInnerTransactionFields.from_type(self.wtype.transaction_type)
raise CodeError(
f"{self.wtype} cannot be instantiated directly, "
f"create a {params_wtype} and submit instead",
location,
)
def _get_transaction_type_from_arg(
literal_or_expr: ExpressionBuilder | Literal,
) -> TransactionType | None:
if isinstance(literal_or_expr, ExpressionBuilder):
wtype = literal_or_expr.rvalue().wtype
if isinstance(wtype, wtypes.WInnerTransactionFields):
return wtype.transaction_type
raise CodeError("Expected an InnerTxnParams argument", literal_or_expr.source_location)
class SubmitInnerTransactionExpressionBuilder(IntermediateExpressionBuilder):
def call(
self,
args: Sequence[ExpressionBuilder | Literal],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
original_expr: mypy.nodes.CallExpr,
) -> ExpressionBuilder:
if len(args) > 1:
transaction_types = {a: _get_transaction_type_from_arg(a) for a in args}
return var_expression(
SubmitInnerTransaction(
wtype=wtypes.WTuple.from_types(
wtypes.WInnerTransaction.from_type(transaction_types[a]) for a in args
),
itxns=tuple(
expect_operand_wtype(
a, wtypes.WInnerTransactionFields.from_type(transaction_types[a])
)
for a in args
),
source_location=location,
)
)
raise CodeError("submit_txns must be called with 2 or more parameters")