From 04383c11c395a8694e3c645c8479d216b99d0c62 Mon Sep 17 00:00:00 2001 From: Neeraj Kashyap Date: Mon, 2 Aug 2021 06:21:37 -0700 Subject: [PATCH] Added support for HTTPProviders --- crawlers/moonstreamcrawlers/ethereum.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/crawlers/moonstreamcrawlers/ethereum.py b/crawlers/moonstreamcrawlers/ethereum.py index 53486818..df75506d 100644 --- a/crawlers/moonstreamcrawlers/ethereum.py +++ b/crawlers/moonstreamcrawlers/ethereum.py @@ -1,8 +1,8 @@ from concurrent.futures import Future, ProcessPoolExecutor, wait -from typing import List, Optional, Tuple +from typing import List, Optional, Tuple, Union from sqlalchemy import desc -from web3 import Web3 +from web3 import Web3, IPCProvider, HTTPProvider from web3.types import BlockData from .settings import MOONSTREAM_IPC_PATH, MOONSTREAM_CRAWL_WORKERS @@ -14,9 +14,14 @@ from moonstreamdb.models import ( ) -# TODO(kompotkot): Write logic to chose between http and ipc -def connect(ipc_path: str = MOONSTREAM_IPC_PATH): - web3_client = Web3(Web3.IPCProvider(ipc_path)) +def connect(web3_uri: Optional[str] = MOONSTREAM_IPC_PATH): + web3_provider: Union[IPCProvider, HTTPProvider] = Web3.IPCProvider() + if web3_uri is not None: + if web3_uri.startswith("http://") or web3_uri.startswith("https://"): + web3_provider = Web3.HTTPProvider(web3_uri) + else: + web3_provider = Web3.IPCProvider(web3_uri) + web3_client = Web3(web3_provider) return web3_client