-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinner.py
97 lines (87 loc) · 3.42 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
import typing
from collections.abc import Sequence
import mypy.nodes
from puya.awst.nodes import Expression, InnerTransactionField, SubmitInnerTransaction
from puya.awst.txn_fields import TxnField
from puya.errors import CodeError
from puya.parse import SourceLocation
from puyapy.awst_build import pytypes
from puyapy.awst_build.eb import _expect as expect
from puyapy.awst_build.eb._base import FunctionBuilder
from puyapy.awst_build.eb.factories import builder_for_instance
from puyapy.awst_build.eb.interface import InstanceBuilder, NodeBuilder, TypeBuilder
from puyapy.awst_build.eb.transaction.base import BaseTransactionExpressionBuilder
from puyapy.awst_build.eb.tuple import TupleExpressionBuilder
class InnerTransactionTypeBuilder(TypeBuilder[pytypes.InnerTransactionResultType]):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> typing.Never:
typ = self.produces()
params_typ = pytypes.InnerTransactionFieldsetTypes[typ.transaction_type]
raise CodeError(
f"{typ} cannot be instantiated directly, create a {params_typ} and submit instead",
location,
)
class InnerTransactionExpressionBuilder(BaseTransactionExpressionBuilder):
def __init__(self, expr: Expression, typ: pytypes.PyType):
assert isinstance(typ, pytypes.InnerTransactionResultType)
super().__init__(typ, expr)
@typing.override
def get_field_value(
self, field: TxnField, typ: pytypes.RuntimeType, location: SourceLocation
) -> InstanceBuilder:
expr = InnerTransactionField(
itxn=self.resolve(),
field=field,
wtype=typ.wtype,
source_location=location,
)
return builder_for_instance(typ, expr)
@typing.override
def get_array_field_value(
self,
field: TxnField,
typ: pytypes.RuntimeType,
index: InstanceBuilder,
location: SourceLocation,
) -> InstanceBuilder:
assert pytypes.UInt64Type <= index.pytype
expr = InnerTransactionField(
itxn=self.resolve(),
field=field,
array_index=index.resolve(),
wtype=typ.wtype,
source_location=location,
)
return builder_for_instance(typ, expr)
class SubmitInnerTransactionExpressionBuilder(FunctionBuilder):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
arg_exprs = []
result_types = []
for arg in args:
match arg:
case InstanceBuilder(
pytype=pytypes.InnerTransactionFieldsetType(transaction_type=txn_type)
):
arg_exprs.append(arg.resolve())
arg_result_type = pytypes.InnerTransactionResultTypes[txn_type]
result_types.append(arg_result_type)
case other:
expect.not_this_type(other, default=expect.default_raise)
result_typ = pytypes.GenericTupleType.parameterise(result_types, location)
return TupleExpressionBuilder(
SubmitInnerTransaction(itxns=arg_exprs, source_location=location),
result_typ,
)