File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """Configuration package for backend services."""
Original file line number Diff line number Diff line change 1+ """Centralized configuration for Hardhat settings."""
2+
3+ import os
4+ from web3 import Web3
5+
6+
7+ class HardhatConfig :
8+ """Configuration class for Hardhat network settings."""
9+
10+ @staticmethod
11+ def get_port () -> str :
12+ """Get the Hardhat port from environment variable."""
13+ return os .environ .get ("HARDHAT_PORT" , "8545" )
14+
15+ @staticmethod
16+ def get_base_url () -> str :
17+ """Get the Hardhat base URL from environment variable."""
18+ return os .environ .get ("HARDHAT_URL" , "http://localhost" )
19+
20+ @staticmethod
21+ def get_full_url () -> str :
22+ """Get the complete Hardhat URL with port."""
23+ port = HardhatConfig .get_port ()
24+ url = HardhatConfig .get_base_url ()
25+ return f"{ url } :{ port } "
26+
27+ @staticmethod
28+ def get_web3_instance () -> Web3 :
29+ """Get a Web3 instance connected to Hardhat network."""
30+ hardhat_url = HardhatConfig .get_full_url ()
31+ return Web3 (Web3 .HTTPProvider (hardhat_url ))
Original file line number Diff line number Diff line change 1515import time
1616from backend .domain .types import TransactionType
1717from web3 import Web3
18- import os
18+ from backend . config . hardhat_config import HardhatConfig
1919
2020
2121class TransactionAddressFilter (Enum ):
@@ -105,10 +105,7 @@ def __init__(
105105 self .session = session
106106
107107 # Connect to Hardhat Network
108- port = os .environ .get ("HARDHAT_PORT" )
109- url = os .environ .get ("HARDHAT_URL" )
110- hardhat_url = f"{ url } :{ port } "
111- self .web3 = Web3 (Web3 .HTTPProvider (hardhat_url ))
108+ self .web3 = HardhatConfig .get_web3_instance ()
112109
113110 @staticmethod
114111 def _parse_transaction_data (transaction_data : Transactions ) -> dict :
Original file line number Diff line number Diff line change 1111from flask_jsonrpc .exceptions import JSONRPCError
1212from functools import partial , wraps
1313import requests
14- import os
1514import traceback
1615from backend .protocol_rpc .aio import run_in_main_server_loop
1716from backend .protocol_rpc .message_handler .base import MessageHandler
17+ from backend .config .hardhat_config import HardhatConfig
1818
1919
2020def get_json_rpc_method_name (function : Callable , method_name : str | None = None ):
@@ -67,9 +67,7 @@ def unfold(x: typing.Any):
6767def setup_eth_method_handler (jsonrpc : JSONRPC ):
6868 """Forwards eth_ methods to Hardhat if no own implementation is available"""
6969 app = jsonrpc .app
70- port = os .environ .get ("HARDHAT_PORT" )
71- url = os .environ .get ("HARDHAT_URL" )
72- HARDHAT_URL = f"{ url } :{ port } "
70+ hardhat_url = HardhatConfig .get_full_url ()
7371
7472 @app .before_request
7573 def handle_eth_methods ():
@@ -89,7 +87,7 @@ def handle_eth_methods():
8987 try :
9088 with requests .Session () as http :
9189 result = http .post (
92- HARDHAT_URL ,
90+ hardhat_url ,
9391 json = request_json ,
9492 headers = {"Content-Type" : "application/json" },
9593 ).json ()
Original file line number Diff line number Diff line change 11import json
2- import os
3- from web3 import Web3
42from typing import Optional , Dict , Any
53from pathlib import Path
64from hexbytes import HexBytes
97from backend .rollup .default_contracts .consensus_main import (
108 get_default_consensus_main_contract ,
119)
10+ from backend .config .hardhat_config import HardhatConfig
1211
1312
1413class ConsensusService :
@@ -17,10 +16,7 @@ def __init__(self):
1716 Initialize the ConsensusService class
1817 """
1918 # Connect to Hardhat Network
20- port = os .environ .get ("HARDHAT_PORT" )
21- url = os .environ .get ("HARDHAT_URL" )
22- hardhat_url = f"{ url } :{ port } "
23- self .web3 = Web3 (Web3 .HTTPProvider (hardhat_url ))
19+ self .web3 = HardhatConfig .get_web3_instance ()
2420
2521 self .web3_connected = self .web3 .is_connected ()
2622
Original file line number Diff line number Diff line change @@ -30,4 +30,5 @@ services:
3030 hardhat :
3131 condition : service_healthy
3232 environment :
33- - HARDHAT_URL=http://hardhat:8545
33+ - HARDHAT_URL=http://hardhat
34+ - HARDHAT_PORT=8545
Original file line number Diff line number Diff line change 1- from web3 import Web3
21import json
32from eth_account import Account
4- import os
3+ from backend . config . hardhat_config import HardhatConfig
54
65
76def test_eth_account ():
@@ -41,8 +40,7 @@ def connect_to_hardhat():
4140 Raises:
4241 Exception: If the connection to the Hardhat network fails.
4342 """
44- hardhat_url = os .environ .get ("HARDHAT_URL" )
45- web3 = Web3 (Web3 .HTTPProvider (hardhat_url ))
43+ web3 = HardhatConfig .get_web3_instance ()
4644
4745 # Check connection
4846 if not web3 .is_connected ():
You can’t perform that action at this time.
0 commit comments