Skip to content
Merged
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
27 changes: 22 additions & 5 deletions bal_tools/drpc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
from requests import Session
from requests.adapters import HTTPAdapter, Retry
from web3 import Web3

DRPC_NAME_OVERRIDES = {
"mainnet": "ethereum",
"zkevm": "polygon-zkevm",
}
ADAPTER = HTTPAdapter(
pool_connections=20,
pool_maxsize=20,
max_retries=Retry(
total=10,
backoff_factor=0.5,
status_forcelist=[429, 500, 502, 503, 504, 520],
),
)
DRPC_SESSION = Session()
DRPC_SESSION.mount("https://", ADAPTER)


class Web3RpcByChain:
Expand Down Expand Up @@ -45,14 +58,18 @@ def items(self):
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}"
endpoint_uri = f"https://lb.drpc.org/ogrpc?network={drpc_chain}&dkey={DRPC_KEY}"
try:
self.w3 = Web3(
Web3.HTTPProvider(endpoint_uri=endpoint_uri, session=DRPC_SESSION)
)
except Exception as e:
raise ConnectionError(
f"Error connecting to {drpc_chain} on DRPC (url: {endpoint_uri}): {e}"
)
)
if not self.w3.is_connected():
raise ConnectionError(
f"Unable to connect to {drpc_chain} network with provided DRPC_KEY."
f"Not connected to {drpc_chain} on DRPC (url: {endpoint_uri})"
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't this expose the private DRPC key in the logs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

github ci recognises secrets in the logs and redacts it automatically as ***

)
try:
self.w3.eth.block_number
Expand Down