fix(order_utils.py): lazy load contract artifacts (#1262)

This commit is contained in:
F. Eugene Aumson 2018-11-14 17:00:41 -05:00 committed by GitHub
parent e9754b4c08
commit daf5719f08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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(
_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", "artifacts/Exchange.json"
"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 (