-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
baad55a
add_w3_rpc_class
Tritium-VLK 957d8c0
Merge branch 'main' into DRPC_class
Tritium-VLK 053bcb5
Don't offer chains that don't exist in the by chain class and fix nam…
Tritium-VLK d346f22
Merge remote-tracking branch 'origin/DRPC_class' into DRPC_class
Tritium-VLK 7f702ef
only init w3 on reference, drpc test
jalbrekt85 31e0df1
support dot syntax
jalbrekt85 0f40f08
add drpc key to workflow
jalbrekt85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
since this class can basically be used across the board, i think making it more flexible is worth it