Skip to content

Commit 809cce0

Browse files
daniel-makerxboblat
authored andcommitted
refactor: simplify _WTypeToABIName.visit_basic_type
1 parent e035da3 commit 809cce0

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/puya/ir/types_.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -469,29 +469,24 @@ def wtype_to_abi_name(
469469
return wtype.accept(visitor)
470470

471471

472-
_BASIC_TYPES_MAP: dict[wtypes.WType, str] = {
472+
_RESOURCE_INDEX_TO_VALUE = {
473+
wtypes.account_wtype: wtypes.arc4_address_alias,
474+
wtypes.application_wtype: wtypes.uint64_wtype,
475+
wtypes.asset_wtype: wtypes.uint64_wtype,
476+
}
477+
_BASIC_TYPES_TO_ABI_NAME = {
478+
wtypes.account_wtype: "account",
479+
wtypes.application_wtype: "application",
480+
wtypes.asset_wtype: "asset",
473481
wtypes.biguint_wtype: "uint512",
474482
wtypes.bool_wtype: "bool",
475483
wtypes.uint64_wtype: "uint64",
476484
wtypes.string_wtype: "string",
485+
# address is not a basic type, but appears if account is mapped to a value type
486+
wtypes.arc4_address_alias: "address",
477487
wtypes.void_wtype: "void",
478488
}
479489

480-
_BASIC_TYPES_TO_ABI_NAME_MAP = {
481-
"index": {
482-
wtypes.account_wtype: "account",
483-
wtypes.application_wtype: "application",
484-
wtypes.asset_wtype: "asset",
485-
}
486-
| _BASIC_TYPES_MAP,
487-
"value": {
488-
wtypes.account_wtype: "address",
489-
wtypes.application_wtype: "uint64",
490-
wtypes.asset_wtype: "uint64",
491-
}
492-
| _BASIC_TYPES_MAP,
493-
}
494-
495490

496491
class _WTypeToABIName(WTypeVisitor[str]):
497492
def __init__(
@@ -503,12 +498,13 @@ def __init__(
503498
self.resource_encoding = resource_encoding
504499

505500
def visit_basic_type(self, wtype: wtypes.WType) -> str:
506-
type_map = _BASIC_TYPES_TO_ABI_NAME_MAP[self.resource_encoding]
501+
if self.resource_encoding == "value":
502+
wtype = _RESOURCE_INDEX_TO_VALUE.get(wtype, wtype)
507503

508-
if wtype in type_map:
509-
return type_map[wtype]
510-
511-
self._unencodable(wtype)
504+
try:
505+
return _BASIC_TYPES_TO_ABI_NAME[wtype]
506+
except KeyError:
507+
self._unencodable(wtype)
512508

513509
def visit_basic_arc4_type(self, wtype: wtypes.ARC4Type) -> str:
514510
if wtype == wtypes.arc4_bool_wtype:

0 commit comments

Comments
 (0)