Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add classes to handle W3_RPC #27

Merged
merged 7 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/python_package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- name: Test with pytest
env:
ETHNODEURL: ${{ secrets.ETHNODEURL }}
DRPC_KEY: ${{ secrets.DRPC_KEY }}
run: |
pip install -r bal_tools/requirements-dev.txt
pytest
1 change: 1 addition & 0 deletions bal_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .subgraph import Subgraph
from .pools_gauges import BalPoolsGauges
from .ecosystem import Aura
from .drpc import Web3RpcByChain, Web3Rpc
56 changes: 56 additions & 0 deletions bal_tools/drpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from web3 import Web3
from bal_addresses import AddrBook

DRPC_NAME_OVERRIDES = {
"mainnet": "ethereum",
"zkevm": "polygon-zkevm",
}

class Web3RpcByChain:
def __init__(self, DRPC_KEY):
self.DRPC_KEY = DRPC_KEY
self._w3_by_chain = {}

def __getitem__(self, chain):
return self._get_or_create_w3(chain)

def __getattr__(self, chain):
Copy link
Collaborator

@jalbrekt85 jalbrekt85 Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i also added support for dot syntax, so a chain's web3 instance can be referenced like:

 Web3RpcByChain().mainnet
 # or
 Web3RpcByChain()["mainnet"]

since this class can basically be used across the board, i think making it more flexible is worth it

return self._get_or_create_w3(chain)

def _get_or_create_w3(self, chain):
if chain not in self._w3_by_chain:
w3 = Web3Rpc(chain, self.DRPC_KEY)
self._w3_by_chain[chain] = w3
return self._w3_by_chain[chain]

def __setitem__(self, chain, value):
self._w3_by_chain[chain] = value

def __delitem__(self, chain):
del self._w3_by_chain[chain]

def __iter__(self):
return iter(self._w3_by_chain)

def keys(self):
return self._w3_by_chain.keys()

def values(self):
return self._w3_by_chain.values()

def items(self):
return self._w3_by_chain.items()

class Web3Rpc:
def __init__(self, chain, DRPC_KEY):
drpc_chain = DRPC_NAME_OVERRIDES.get(chain, chain)
self.w3 = Web3(Web3.HTTPProvider(f"https://lb.drpc.org/ogrpc?network={drpc_chain}&dkey={DRPC_KEY}"))
if not self.w3.is_connected():
raise ConnectionError(f"Unable to connect to {drpc_chain} network with provided DRPC_KEY.")
try:
self.w3.eth.block_number
except Exception as e:
raise ConnectionError(f"Error fetching latest block number: {e}, chain: {chain}, url: {self.w3.provider.endpoint_uri}")

def __getattr__(self, name):
return getattr(self.w3, name)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = "0.1.1"
VERSION = "0.1.2"
DESCRIPTION = "Balancer Tools"
LONG_DESCRIPTION = "Balancer Maxi helper and ecosystem tools"

Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ def pool_snapshot_blocks():
"avalanche": 30000000,
"zkevm": 1200000,
}


@pytest.fixture(scope="module")
def DRPC_KEY():
return os.getenv("DRPC_KEY")
27 changes: 27 additions & 0 deletions tests/test_drpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from bal_tools.drpc import Web3RpcByChain, Web3Rpc
from tests.conftest import chains


def test_drpc_by_chain_dict_syntax(DRPC_KEY):
if not DRPC_KEY:
pytest.skip("Skipping DRPC_KEY not set")

w3_by_chain = Web3RpcByChain(DRPC_KEY)
assert not w3_by_chain.keys()

for chain in chains:
assert w3_by_chain[chain].eth

assert sorted(w3_by_chain.keys()) == sorted(chains)

def test_drpc_by_chain_dot_syntax(DRPC_KEY):
if not DRPC_KEY:
pytest.skip("Skipping DRPC_KEY not set")

w3_by_chain = Web3RpcByChain(DRPC_KEY)

assert not w3_by_chain.keys()
assert w3_by_chain.mainnet.eth
assert w3_by_chain.arbitrum.eth
assert list(w3_by_chain.keys()) == ["mainnet", "arbitrum"]