diff options
-rw-r--r-- | python-packages/order_utils/src/zero_ex/order_utils/__init__.py | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/python-packages/order_utils/src/zero_ex/order_utils/__init__.py b/python-packages/order_utils/src/zero_ex/order_utils/__init__.py index a86128e0e..24c6bfd4e 100644 --- a/python-packages/order_utils/src/zero_ex/order_utils/__init__.py +++ b/python-packages/order_utils/src/zero_ex/order_utils/__init__.py @@ -34,13 +34,26 @@ from zero_ex.json_schemas import assert_valid class _Constants: """Static data used by order utilities.""" - contract_name_to_abi = { - "Exchange": json.loads( - resource_string( - "zero_ex.contract_artifacts", "artifacts/Exchange.json" - ) - )["compilerOutput"]["abi"] - } + _contract_name_to_abi: Dict[str, Dict] = {} # class data, not instance + + @classmethod + def contract_name_to_abi(cls, contract_name: str) -> Dict: + """Return the ABI for the given contract name. + + First tries to get data from the class level storage + `_contract_name_to_abi`. If it's not there, loads it from disk, stores + it in the class data (for the next caller), and then returns it. + """ + try: + return cls._contract_name_to_abi[contract_name] + except KeyError: + cls._contract_name_to_abi[contract_name] = json.loads( + resource_string( + "zero_ex.contract_artifacts", + f"artifacts/{contract_name}.json", + ) + )["compilerOutput"]["abi"] + return cls._contract_name_to_abi[contract_name] network_to_exchange_addr: Dict[str, str] = { "1": "0x4f833a24e1f95d70f028921e27040ca56e09ab0b", @@ -233,7 +246,7 @@ def is_valid_signature( # false positive from pylint: disable=no-member contract: datatypes.Contract = web3_instance.eth.contract( address=to_checksum_address(contract_address), - abi=_Constants.contract_name_to_abi["Exchange"], + abi=_Constants.contract_name_to_abi("Exchange"), ) try: return ( |