fix(order_utils.py): lazy load contract artifacts (#1262)
This commit is contained in:
parent
e9754b4c08
commit
daf5719f08
@ -34,13 +34,26 @@ from zero_ex.json_schemas import assert_valid
|
|||||||
class _Constants:
|
class _Constants:
|
||||||
"""Static data used by order utilities."""
|
"""Static data used by order utilities."""
|
||||||
|
|
||||||
contract_name_to_abi = {
|
_contract_name_to_abi: Dict[str, Dict] = {} # class data, not instance
|
||||||
"Exchange": json.loads(
|
|
||||||
resource_string(
|
@classmethod
|
||||||
"zero_ex.contract_artifacts", "artifacts/Exchange.json"
|
def contract_name_to_abi(cls, contract_name: str) -> Dict:
|
||||||
)
|
"""Return the ABI for the given contract name.
|
||||||
)["compilerOutput"]["abi"]
|
|
||||||
}
|
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] = {
|
network_to_exchange_addr: Dict[str, str] = {
|
||||||
"1": "0x4f833a24e1f95d70f028921e27040ca56e09ab0b",
|
"1": "0x4f833a24e1f95d70f028921e27040ca56e09ab0b",
|
||||||
@ -233,7 +246,7 @@ def is_valid_signature(
|
|||||||
# false positive from pylint: disable=no-member
|
# false positive from pylint: disable=no-member
|
||||||
contract: datatypes.Contract = web3_instance.eth.contract(
|
contract: datatypes.Contract = web3_instance.eth.contract(
|
||||||
address=to_checksum_address(contract_address),
|
address=to_checksum_address(contract_address),
|
||||||
abi=_Constants.contract_name_to_abi["Exchange"],
|
abi=_Constants.contract_name_to_abi("Exchange"),
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
return (
|
return (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user