aboutsummaryrefslogtreecommitdiffstats
path: root/python-packages/order_utils/src
diff options
context:
space:
mode:
authorF. Eugene Aumson <feuGeneA@users.noreply.github.com>2018-11-15 06:00:41 +0800
committerGitHub <noreply@github.com>2018-11-15 06:00:41 +0800
commitdaf5719f082f62e46073b1badd141da445b33b09 (patch)
treec658ea51eea0af5f5bdfbd99a876f534267008bd /python-packages/order_utils/src
parente9754b4c08fc7a18460595f7279d06775786f357 (diff)
downloaddexon-sol-tools-daf5719f082f62e46073b1badd141da445b33b09.tar
dexon-sol-tools-daf5719f082f62e46073b1badd141da445b33b09.tar.gz
dexon-sol-tools-daf5719f082f62e46073b1badd141da445b33b09.tar.bz2
dexon-sol-tools-daf5719f082f62e46073b1badd141da445b33b09.tar.lz
dexon-sol-tools-daf5719f082f62e46073b1badd141da445b33b09.tar.xz
dexon-sol-tools-daf5719f082f62e46073b1badd141da445b33b09.tar.zst
dexon-sol-tools-daf5719f082f62e46073b1badd141da445b33b09.zip
fix(order_utils.py): lazy load contract artifacts (#1262)
Diffstat (limited to 'python-packages/order_utils/src')
-rw-r--r--python-packages/order_utils/src/zero_ex/order_utils/__init__.py29
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 (