From 6ace422f48f2f1cd12e8c659e2b1bd8811ec6ead Mon Sep 17 00:00:00 2001 From: ackness Date: Tue, 1 Aug 2023 11:56:18 +0800 Subject: [PATCH] support ide autocomplete, fix some path error on windows platform --- README.md | 2 +- many_abis/__init__.py | 6 +-- many_abis/abis.py | 16 +++++--- many_abis/chains.py | 24 ++++++----- many_abis/constants.py | 1 + many_abis/meta.py | 91 ++++++++++++++++++++++++++++++++++++++++++ many_abis/utils.py | 17 ++++---- setup.py | 11 ++--- 8 files changed, 135 insertions(+), 33 deletions(-) create mode 100644 many_abis/meta.py diff --git a/README.md b/README.md index 7be5b28..405927a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Many Abis -![Version](https://img.shields.io/badge/many--abis-v0.1.7-green) +![Version](https://img.shields.io/badge/many--abis-v0.1.8-green) ![Pypi](https://img.shields.io/pypi/dm/many-abis) ![GitHub Org's stars](https://img.shields.io/github/stars/ackness/many_abis?style=social) diff --git a/many_abis/__init__.py b/many_abis/__init__.py index efdc5e0..3eee46c 100644 --- a/many_abis/__init__.py +++ b/many_abis/__init__.py @@ -1,5 +1,5 @@ -from .utils import * -from .chains import * -from .abis import * from ._base import * +from .abis import * +from .chains import * from .constants import * +from .utils import * diff --git a/many_abis/abis.py b/many_abis/abis.py index 1e5a303..18f0ac1 100644 --- a/many_abis/abis.py +++ b/many_abis/abis.py @@ -1,14 +1,21 @@ -from .utils import load_all_abis -from addict import Dict +from pathlib import PurePath + import requests +from addict import Dict + +from .meta import ABIMetaData +from .utils import load_all_abis + +# __all__ = ["ABIS"] -def all_abis(): +def all_abis() -> tuple[list[str], ABIMetaData]: all_abis = load_all_abis() names = [] all_abis_rename = {} for k, v in all_abis.items(): - ns = k.split('/')[2:] + ns = list(PurePath(k).parts)[2:] + # ns = k.split('/')[2:] # only for linux or mac name = '_'.join(ns).upper() names.append(name) all_abis_rename[name] = v @@ -16,7 +23,6 @@ def all_abis(): ALL_ABIS_NAME, ABIS = all_abis() -globals().update(ABIS) def get_abi_from_address(address: str, api_key: str, chain_api: str): diff --git a/many_abis/chains.py b/many_abis/chains.py index d0bfb56..96aec66 100644 --- a/many_abis/chains.py +++ b/many_abis/chains.py @@ -1,21 +1,25 @@ -from .utils import load_chains -from addict import Dict from functools import reduce +from typing import Union + +from addict import Dict + +from .meta import WethMetaData, ChainMetaData, SingleDexMetaData, ChainsMetaData +from .utils import load_chains -CHAINS = Dict(load_chains()) -SUPPORTED_CHAINS = list(CHAINS.keys()) +CHAINS: ChainsMetaData = Dict(load_chains()) +SUPPORTED_CHAINS: list[str] = list(CHAINS.keys()) -def all_chains(): +def all_chains() -> list[str]: return SUPPORTED_CHAINS -def get_chain_by_name(name: str): +def get_chain_by_name(name: str) -> ChainMetaData: assert name in SUPPORTED_CHAINS, f"Chain {name} not supported" return CHAINS[name] -def get_chain_by_id(chain_id: int): +def get_chain_by_id(chain_id: int) -> ChainMetaData: for name, info in CHAINS.items(): if "chain_id" not in info: raise ValueError(f"Chain {name} does not have chain_id") @@ -24,7 +28,7 @@ def get_chain_by_id(chain_id: int): raise ValueError(f"Chain id {chain_id} not supported") -def get_chain(name=None, chain_id=None): +def get_chain(name=None, chain_id=None) -> ChainMetaData: assert name or chain_id, "Must provide either name or chain_id" if name: name = name.lower() @@ -34,11 +38,11 @@ def get_chain(name=None, chain_id=None): return get_chain_by_id(chain_id) -def chain(*args, **kwargs): +def chain(*args, **kwargs) -> ChainMetaData: return get_chain(*args, **kwargs) -def get(*keys): +def get(*keys) -> Union[ChainMetaData, SingleDexMetaData, WethMetaData, ChainsMetaData]: try: return reduce(lambda d, key: d[key], keys, CHAINS) except KeyError as e: diff --git a/many_abis/constants.py b/many_abis/constants.py index 6829a7e..f39cae8 100644 --- a/many_abis/constants.py +++ b/many_abis/constants.py @@ -16,6 +16,7 @@ class CHAIN_SCAN_API: CRONOS: str = 'https://api.cronoscan.com/api' OPT: str = 'https://api-optimistic.etherscan.io/api' + @dataclass(frozen=True) class CHAIN_CONTRACT_API: ETH: str = 'https://api.etherscan.io/api?module=contract&action=getabi&address={contract_address}&apikey={api_key}' diff --git a/many_abis/meta.py b/many_abis/meta.py new file mode 100644 index 0000000..4613294 --- /dev/null +++ b/many_abis/meta.py @@ -0,0 +1,91 @@ +from typing import Iterable, Mapping, Sequence + + +class WethMetaData: + address: str + name: str + symbol: str + + +class SingleDexMetaData: + factory_address: str + router_address: str + name: str + website: str + + +class ChainMetaData: + chain_id: int + charts: Mapping[str, str] + dex: Mapping[str, SingleDexMetaData] + explorer: str + name: str + rpc: Sequence[str] + stable_coins: Mapping[str, str] + weth: WethMetaData + + +class ChainsMetaData: + eth: ChainMetaData + bsc: ChainMetaData + polygon: ChainMetaData + heco: ChainMetaData + fantom: ChainMetaData + arbitrum: ChainMetaData + cronos: ChainMetaData + avalanche: ChainMetaData + kcc: ChainMetaData + moonriver: ChainMetaData + okx: ChainMetaData + bsc_test: ChainMetaData + optimism: ChainMetaData + + +class ABIMetaData(Iterable): + AAVE_V1_ATOKEN: Sequence[Mapping] + AAVE_V1_LENDING_POOL: Sequence[Mapping] + AAVE_V1_LENDING_POOL_ADDRESSES_PROVIDER: Sequence[Mapping] + AAVE_V1_LENDING_POOL_CORE: Sequence[Mapping] + AAVE_V2_COLLECTOR: Sequence[Mapping] + AAVE_V2_INCENTIVES_CONTROLLER: Sequence[Mapping] + AAVE_V2_LENDING_POOL: Sequence[Mapping] + AAVE_V2_LENDING_POOL_ADDRESSES_PROVIDER: Sequence[Mapping] + AAVE_V2_LENDING_POOL_ADDRESSES_PROVIDER_REGISTRY: Sequence[Mapping] + AAVE_V2_LENDING_POOL_COLLATERAL_MANAGER: Sequence[Mapping] + AAVE_V2_LENDING_POOL_CONFIGURATOR: Sequence[Mapping] + AAVE_V2_LENDING_RATE_ORACLE: Sequence[Mapping] + AAVE_V2_POOL_ADMIN: Sequence[Mapping] + AAVE_V2_PRICE_ORACLE: Sequence[Mapping] + AAVE_V2_PROTOCAL_DATA_PROVIDER: Sequence[Mapping] + AAVE_V2_UI_INCENTIVE_DATA_PROVIDER: Sequence[Mapping] + AAVE_V2_UI_POOL_DATA_PROVIDER: Sequence[Mapping] + AAVE_V2_WETH_GATEWAY: Sequence[Mapping] + JOE_V2_FACTORY: Sequence[Mapping] + JOE_V2_PAIR: Sequence[Mapping] + JOE_V2_ROUTER: Sequence[Mapping] + PANCAKE_V3_IPERIPHERY_PAYMENTS_WITH_FEE: Sequence[Mapping] + PANCAKE_V3_MASTER_CHEF_V3: Sequence[Mapping] + PANCAKE_V3_NON_FUNGIBLE_POSITION_MANAGER: Sequence[Mapping] + PANCAKE_V3_POOL_V3: Sequence[Mapping] + PANCAKE_V3_QUOTER: Sequence[Mapping] + PANCAKE_V3_QUOTER_V2: Sequence[Mapping] + PANCAKE_V3_ROUTER_V3: Sequence[Mapping] + PANCAKE_V3_SELF_PERMIT: Sequence[Mapping] + PANCAKE_V3_STAKER: Sequence[Mapping] + UNISWAP_BSC_ROUTER: Sequence[Mapping] + UNISWAP_V1_EXCHANGE: Sequence[Mapping] + UNISWAP_V1_FACTORY: Sequence[Mapping] + UNISWAP_V2_FACTORY: Sequence[Mapping] + UNISWAP_V2_PAIR: Sequence[Mapping] + UNISWAP_V2_ROUTER: Sequence[Mapping] + UNISWAP_V3_FACTORY: Sequence[Mapping] + UNISWAP_V3_MULTICALL: Sequence[Mapping] + UNISWAP_V3_NON_FUNGIBLE_POSITION_MANAGE: Sequence[Mapping] + UNISWAP_V3_POOL: Sequence[Mapping] + UNISWAP_V3_QUOTER: Sequence[Mapping] + UNISWAP_V3_ROUTER: Sequence[Mapping] + ERC1155: Sequence[Mapping] + ERC20: Sequence[Mapping] + ERC721: Sequence[Mapping] + ERC777: Sequence[Mapping] + WETH9: Sequence[Mapping] diff --git a/many_abis/utils.py b/many_abis/utils.py index 1bc3d71..0d31dfc 100644 --- a/many_abis/utils.py +++ b/many_abis/utils.py @@ -1,29 +1,29 @@ +import glob import json import os -import glob -from types import SimpleNamespace +from typing import Any -root_path = f"{os.path.dirname(os.path.abspath(__file__))}/assets" +root_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets") -def _load_json_file(fp): +def _load_json_file(fp) -> Any: with open(fp) as f: abi = json.load(f) return abi -def load_abi(name: str): +def load_abi(name: str) -> dict[str, str]: with open(os.path.abspath(os.path.join(root_path, f'{name}.abi'))) as f: abi = json.load(f) return abi -def load_all_abis(): +def load_all_abis() -> dict[str, dict[str, str]]: all_abis_fp = list(glob.iglob(f"{root_path}/**/*.abi", recursive=True)) return {f'{p[len(root_path):][:-4]}': _load_json_file(p) for p in all_abis_fp} -def load_chains(): +def load_chains() -> dict[str, Any]: fp = os.path.join(root_path, "utils", "chains.json") return _load_json_file(fp) @@ -36,7 +36,6 @@ def print_all_dex(): print(f" - [{j + 1}] [{dex.name}]({dex.website})") -def supported_abis(): +def supported_abis() -> list[str]: from .abis import ALL_ABIS_NAME - # print(ALL_ABIS_NAME) return ALL_ABIS_NAME diff --git a/setup.py b/setup.py index cacd997..9cf1079 100644 --- a/setup.py +++ b/setup.py @@ -2,14 +2,14 @@ with open('README.md', 'r', encoding='utf-8') as fh: long_description = fh.read() -# print(find_packages()) + setup( name='many_abis', - packages=['many_abis'], + # packages=['many_abis'], + packages=find_packages(where="many_abis"), package_dir={'many_abis': 'many_abis'}, - package_data={'': [ - '**/*.abi', '**/*.json']}, - version='0.1.7', + package_data={'': ["assets/*"]}, + version='0.1.8', license='MIT', description='A simple way to get different DEXs abis for block chains.', long_description=long_description, @@ -24,5 +24,6 @@ "eth_utils", "addict" ], + python_requires='>=3.8', )