Skip to content

Commit 20c0111

Browse files
committed
Merge remote-tracking branch 'origin/gmx_bindings' into gmx_bindings
2 parents 862cfb3 + 5f62e5a commit 20c0111

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

packages/eth_rpc/src/eth_rpc/codegen.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ def object_to_type(obj):
3434
return _convert_type(obj["type"])
3535

3636

37-
def convert_types(types_):
37+
def convert_types(types_, full_struct_names: bool = True):
3838
lst = []
3939
models = []
4040
for type_ in types_:
4141
if "components" in type_:
42-
py_model_name = type_["internalType"].split(".")[-1].removeprefix("struct ")
42+
if full_struct_names:
43+
py_model_name = ''.join(type_["internalType"].split(".")).removeprefix("struct ")
44+
else:
45+
py_model_name = type_["internalType"].split(".")[-1].removeprefix("struct ")
4346
field_name = py_model_name
4447
while field_name.endswith("[]"):
4548
field_name = f"list[{py_model_name[:-2]}]"
@@ -55,7 +58,7 @@ def convert_types(types_):
5558
return (tuple[*lst], models)
5659

5760

58-
def codegen(abi: list[dict[str, Any]], contract_name: str) -> str: # noqa: C901
61+
def codegen(abi: list[dict[str, Any]], contract_name: str, full_struct_names: bool = True) -> str: # noqa: C901
5962
"""
6063
Convert an ABI to the string implementation of a ProtocolBase.
6164
@@ -93,15 +96,18 @@ class WETH(ProtocolBase):
9396
inputs = func.get("inputs", [])
9497
outputs = func.get("outputs", [])
9598

96-
input_type, _models = convert_types(inputs)
99+
input_type, _models = convert_types(inputs, full_struct_names=full_struct_names)
97100
for model_name, model in _models:
98101
if model_name not in model_dict:
99102
model_dict[model_name] = model
100103
elif model_dict[model_name] == model:
101104
continue
102105
else:
103-
print("Warning: Duplicate model name with different fields")
104-
model_dict[model_name + "_extra"] = model
106+
print(f"Warning: Duplicate model name {model_name} with different fields")
107+
count = 1
108+
while model_name + f"_{count}" in model_dict:
109+
count += 1
110+
model_dict[model_name + f"_{count}"] = model
105111

106112
output_type, __models = convert_types(outputs)
107113

@@ -112,8 +118,11 @@ class WETH(ProtocolBase):
112118
elif model_dict[model_name] == model[1]:
113119
continue
114120
else:
115-
print("Warning: Duplicate model name with different fields")
116-
model_dict[model_name + "_extra"] = model[1]
121+
print(f"Warning: Duplicate model name {model_name} with different fields")
122+
count = 1
123+
while model_name + f"_{count}" in model_dict:
124+
count += 1
125+
model_dict[model_name + f"_{count}"] = model[1]
117126

118127
has_name_annotation: bool = False
119128
alias: str
@@ -149,7 +158,10 @@ class WETH(ProtocolBase):
149158
for _, fields in list(model_dict.items()):
150159
for field in fields:
151160
if field["internalType"].startswith("struct"):
152-
model_name = field["internalType"].split(".")[-1].replace("[]", "")
161+
if full_struct_names:
162+
model_name = ''.join(field["internalType"].split(".")).replace("[]", "").removeprefix("struct ")
163+
else:
164+
model_name = field["internalType"].split(".")[-1].replace("[]", "")
153165
if model_name not in model_dict:
154166
model_dict[model_name] = field["components"]
155167

@@ -161,7 +173,10 @@ class {name}(Struct):
161173
embedded_types = []
162174
for field in fields:
163175
if (internalType := field["internalType"]).startswith("struct"):
164-
type_ = internalType.split(".")[-1].replace("[]", "")
176+
if full_struct_names:
177+
type_ = ''.join(internalType.split(".")).replace("[]", "").removeprefix("struct ")
178+
else:
179+
type_ = internalType.split(".")[-1].replace("[]", "")
165180
while internalType.endswith("[]"):
166181
type_ = list[type_] # type: ignore[valid-type]
167182
internalType = internalType[:-2]

0 commit comments

Comments
 (0)