Skip to content

Commit 47089aa

Browse files
committed
Refactor
1 parent 5ab3ed3 commit 47089aa

File tree

5 files changed

+81
-89
lines changed

5 files changed

+81
-89
lines changed

tests/core/utilities/test_abi_is_encodable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
)
7676
def test_is_encodable(value, _type, expected):
7777
w3 = Web3(EthereumTesterProvider())
78-
actual = w3.is_encodable(_type, value)
78+
actual = w3.codec.is_encodable(_type, value)
7979
assert actual is expected
8080

8181

@@ -107,5 +107,5 @@ def test_is_encodable_strict(value, _type, expected):
107107
w3 = Web3(EthereumTesterProvider())
108108
w3.enable_strict_bytes_type_checking()
109109

110-
actual = w3.is_encodable(_type, value)
110+
actual = w3.codec.is_encodable(_type, value)
111111
assert actual is expected

web3/_utils/abi.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import warnings
1616

1717
from eth_abi import (
18+
decoding,
1819
encoding,
1920
)
2021
from eth_abi.base import (
@@ -28,6 +29,10 @@
2829
TupleType,
2930
parse,
3031
)
32+
from eth_abi.registry import (
33+
BaseEquals,
34+
registry as default_registry
35+
)
3136
from eth_typing import (
3237
TypeStr,
3338
)
@@ -782,3 +787,67 @@ def strip_abi_type(elements):
782787
return elements.data
783788
else:
784789
return elements
790+
791+
792+
def build_default_registry():
793+
# We make a copy here just to make sure that eth-abi's default registry is not
794+
# affected by our custom encoder subclasses
795+
registry = default_registry.copy()
796+
797+
registry.unregister('address')
798+
registry.unregister('bytes<M>')
799+
registry.unregister('bytes')
800+
registry.unregister('string')
801+
802+
registry.register(
803+
BaseEquals('address'),
804+
AddressEncoder, decoding.AddressDecoder,
805+
label='address',
806+
)
807+
registry.register(
808+
BaseEquals('bytes', with_sub=True),
809+
BytesEncoder, decoding.BytesDecoder,
810+
label='bytes<M>',
811+
)
812+
registry.register(
813+
BaseEquals('bytes', with_sub=False),
814+
ByteStringEncoder, decoding.ByteStringDecoder,
815+
label='bytes',
816+
)
817+
registry.register(
818+
BaseEquals('string'),
819+
TextStringEncoder, decoding.StringDecoder,
820+
label='string',
821+
)
822+
return registry
823+
824+
825+
def build_strict_registry():
826+
registry = default_registry.copy()
827+
828+
registry.unregister('address')
829+
registry.unregister('bytes<M>')
830+
registry.unregister('bytes')
831+
registry.unregister('string')
832+
833+
registry.register(
834+
BaseEquals('address'),
835+
AddressEncoder, decoding.AddressDecoder,
836+
label='address',
837+
)
838+
registry.register(
839+
BaseEquals('bytes', with_sub=True),
840+
ExactLengthBytesEncoder, decoding.BytesDecoder,
841+
label='bytes<M>',
842+
)
843+
registry.register(
844+
BaseEquals('bytes', with_sub=False),
845+
StrictByteStringEncoder, decoding.ByteStringDecoder,
846+
label='bytes',
847+
)
848+
registry.register(
849+
BaseEquals('string'),
850+
TextStringEncoder, decoding.StringDecoder,
851+
label='string',
852+
)
853+
return registry

web3/_utils/contracts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def prepare_transaction(
172172
TODO: add new prepare_deploy_transaction API
173173
"""
174174
if fn_abi is None:
175-
fn_abi = find_matching_fn_abi(contract_abi, web3, fn_identifier, fn_args, fn_kwargs)
175+
fn_abi = find_matching_fn_abi(contract_abi, web3.codec, fn_identifier, fn_args, fn_kwargs)
176176

177177
validate_payable(transaction, fn_abi)
178178

web3/contract.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def decode_function_input(self, data):
418418
@combomethod
419419
def find_functions_by_args(self, *args):
420420
def callable_check(fn_abi):
421-
return check_if_arguments_can_be_encoded(fn_abi, self.web3, args=args, kwargs={})
421+
return check_if_arguments_can_be_encoded(fn_abi, self.web3.codec, args=args, kwargs={})
422422

423423
return find_functions_by_identifier(
424424
self.abi, self.web3, self.address, callable_check
@@ -454,7 +454,7 @@ def _prepare_transaction(cls,
454454
@classmethod
455455
def _find_matching_fn_abi(cls, fn_identifier=None, args=None, kwargs=None):
456456
return find_matching_fn_abi(cls.abi,
457-
cls.web3,
457+
cls.web3.codec,
458458
fn_identifier=fn_identifier,
459459
args=args,
460460
kwargs=kwargs)
@@ -686,7 +686,7 @@ def _none_addr(datatype, data):
686686
class ImplicitMethod(ConciseMethod):
687687
def __call_by_default(self, args):
688688
function_abi = find_matching_fn_abi(self._function.contract_abi,
689-
self._function.web3,
689+
self._function.web3.codec,
690690
fn_identifier=self._function.function_identifier,
691691
args=args)
692692

@@ -766,7 +766,7 @@ def _set_function_info(self):
766766
if not self.abi:
767767
self.abi = find_matching_fn_abi(
768768
self.contract_abi,
769-
self.web3,
769+
self.web3.codec,
770770
self.function_identifier,
771771
self.args,
772772
self.kwargs
@@ -1350,7 +1350,7 @@ def call_contract_function(
13501350
return_data = web3.eth.call(call_transaction, block_identifier=block_id)
13511351

13521352
if fn_abi is None:
1353-
fn_abi = find_matching_fn_abi(contract_abi, web3, function_identifier, args, kwargs)
1353+
fn_abi = find_matching_fn_abi(contract_abi, web3.codec, function_identifier, args, kwargs)
13541354

13551355
output_types = get_abi_output_types(fn_abi)
13561356

web3/main.py

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
from eth_abi import (
2-
decoding,
3-
)
41
from eth_abi.codec import (
52
ABICodec,
63
)
7-
from eth_abi.registry import (
8-
BaseEquals,
9-
registry as default_registry,
10-
)
114
from eth_utils import (
125
add_0x_prefix,
136
apply_to_return_value,
@@ -25,13 +18,9 @@
2518

2619
from ens import ENS
2720
from web3._utils.abi import (
21+
build_default_registry,
22+
build_strict_registry,
2823
map_abi_data,
29-
AddressEncoder,
30-
BytesEncoder,
31-
ByteStringEncoder,
32-
ExactLengthBytesEncoder,
33-
StrictByteStringEncoder,
34-
TextStringEncoder,
3524
)
3625
from web3._utils.decorators import (
3726
combomethod,
@@ -156,46 +145,10 @@ def __init__(self, provider=None, middlewares=None, modules=None, ens=empty):
156145

157146
attach_modules(self, modules)
158147

159-
self.codec = ABICodec(self.build_default_registry())
148+
self.codec = ABICodec(build_default_registry())
160149

161150
self.ens = ens
162151

163-
def is_encodable(self, _type, value):
164-
# TODO - there is probably a better place to put this
165-
return self.codec.is_encodable(_type, value)
166-
167-
def build_default_registry(self):
168-
# We make a copy here just to make sure that eth-abi's default registry is not
169-
# affected by our custom encoder subclasses
170-
registry = default_registry.copy()
171-
172-
registry.unregister('address')
173-
registry.unregister('bytes<M>')
174-
registry.unregister('bytes')
175-
registry.unregister('string')
176-
177-
registry.register(
178-
BaseEquals('address'),
179-
AddressEncoder, decoding.AddressDecoder,
180-
label='address',
181-
)
182-
registry.register(
183-
BaseEquals('bytes', with_sub=True),
184-
BytesEncoder, decoding.BytesDecoder,
185-
label='bytes<M>',
186-
)
187-
registry.register(
188-
BaseEquals('bytes', with_sub=False),
189-
ByteStringEncoder, decoding.ByteStringDecoder,
190-
label='bytes',
191-
)
192-
registry.register(
193-
BaseEquals('string'),
194-
TextStringEncoder, decoding.StringDecoder,
195-
label='string',
196-
)
197-
return registry
198-
199152
@property
200153
def middleware_onion(self):
201154
return self.manager.middleware_onion
@@ -300,35 +253,5 @@ def enable_unstable_package_management_api(self):
300253
if not hasattr(self, '_pm'):
301254
PM.attach(self, '_pm')
302255

303-
def build_strict_registry(self):
304-
registry = default_registry.copy()
305-
306-
registry.unregister('address')
307-
registry.unregister('bytes<M>')
308-
registry.unregister('bytes')
309-
registry.unregister('string')
310-
311-
registry.register(
312-
BaseEquals('address'),
313-
AddressEncoder, decoding.AddressDecoder,
314-
label='address',
315-
)
316-
registry.register(
317-
BaseEquals('bytes', with_sub=True),
318-
ExactLengthBytesEncoder, decoding.BytesDecoder,
319-
label='bytes<M>',
320-
)
321-
registry.register(
322-
BaseEquals('bytes', with_sub=False),
323-
StrictByteStringEncoder, decoding.ByteStringDecoder,
324-
label='bytes',
325-
)
326-
registry.register(
327-
BaseEquals('string'),
328-
TextStringEncoder, decoding.StringDecoder,
329-
label='string',
330-
)
331-
return registry
332-
333256
def enable_strict_bytes_type_checking(self):
334-
return setattr(self, 'codec', ABICodec(self.build_strict_registry()))
257+
return setattr(self, 'codec', ABICodec(build_strict_registry()))

0 commit comments

Comments
 (0)