Skip to content

Commit 6c802cb

Browse files
author
Stuart Reed
committed
Fix problem with constructor _encode_data_in_transaction exception handling
1 parent 2828bd7 commit 6c802cb

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

web3/_utils/abi.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
ABI,
5050
ABIComponent,
5151
ABIComponentIndexed,
52+
ABIConstructor,
5253
ABIElement,
5354
ABIEvent,
5455
ABIFallback,
@@ -370,6 +371,24 @@ def _align_abi_input(
370371
)
371372

372373

374+
def find_constructor_abi_element_by_type(contract_abi: ABI) -> ABIConstructor:
375+
"""
376+
Find the constructor ABI element in the contract ABI.
377+
378+
This function is often used in place of `web3.utils.abi.get_abi_element` to find
379+
a constructor without considering it's argument types. This is used prior to
380+
encoding the abi, since the argument types are not known at that time.
381+
"""
382+
candidates = [abi for abi in contract_abi if abi["type"] == "constructor"]
383+
if len(candidates) == 1:
384+
return candidates[0]
385+
elif len(candidates) == 0:
386+
return None
387+
elif len(candidates) > 1:
388+
raise Web3ValueError("Found multiple constructors.")
389+
return None
390+
391+
373392
DYNAMIC_TYPES = ["bytes", "string"]
374393

375394
INT_SIZES = range(8, 257, 8)

web3/_utils/contracts.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ def encode_abi(
136136
abi,
137137
*arguments,
138138
abi_codec=w3.codec,
139-
**{},
140139
):
141140
raise Web3TypeError(
142141
"One or more arguments could not be encoded to the necessary "

web3/contract/base_contract.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
from web3._utils.abi import (
4949
fallback_func_abi_exists,
50+
find_constructor_abi_element_by_type,
5051
is_array_type,
5152
receive_func_abi_exists,
5253
)
@@ -86,7 +87,6 @@
8687
MutableAttributeDict,
8788
)
8889
from web3.exceptions import (
89-
ABIConstructorNotFound,
9090
ABIEventNotFound,
9191
ABIFallbackNotFound,
9292
ABIFunctionNotFound,
@@ -911,10 +911,7 @@ def _get_event_abi(
911911
def _encode_constructor_data(
912912
cls, *args: Sequence[Any], **kwargs: Dict[str, Any]
913913
) -> HexStr:
914-
try:
915-
constructor_abi = get_abi_element(cls.abi, "constructor")
916-
except ABIConstructorNotFound:
917-
constructor_abi = None
914+
constructor_abi = find_constructor_abi_element_by_type(cls.abi)
918915

919916
if constructor_abi:
920917
arguments = get_normalized_abi_inputs(constructor_abi, *args, **kwargs)
@@ -1095,12 +1092,7 @@ def __init__(
10951092

10961093
@combomethod
10971094
def _encode_data_in_transaction(self, *args: Any, **kwargs: Any) -> HexStr:
1098-
try:
1099-
constructor_abi = get_abi_element(self.abi, "constructor", *args, **kwargs)
1100-
except MismatchedABI as e:
1101-
raise TypeError(e)
1102-
except ABIConstructorNotFound:
1103-
constructor_abi = None
1095+
constructor_abi = find_constructor_abi_element_by_type(self.abi)
11041096

11051097
if constructor_abi:
11061098
if not args:
@@ -1109,6 +1101,7 @@ def _encode_data_in_transaction(self, *args: Any, **kwargs: Any) -> HexStr:
11091101
kwargs = {}
11101102

11111103
arguments = get_normalized_abi_inputs(constructor_abi, *args, **kwargs)
1104+
11121105
data = add_0x_prefix(
11131106
encode_abi(self.w3, constructor_abi, arguments, data=self.bytecode)
11141107
)

0 commit comments

Comments
 (0)