Skip to content

Commit

Permalink
refactor: improve network handling and type safety
Browse files Browse the repository at this point in the history
- Convert Network type to Enum for better type safety
- Update FLASHBOTS_NETWORKS to use Network enum as keys
- Remove get_networks() function, use Network enum values directly
- Add EnumAction class for argparse to handle Network enum
- Update parse_arguments() and related functions to use Network enum
- Adjust type hints throughout the code to reflect these changes
  • Loading branch information
odysseus0 committed Jul 30, 2024
1 parent bd4a0b6 commit 78ee934
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 235 deletions.
30 changes: 23 additions & 7 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import logging
import os
import secrets
from enum import Enum
from uuid import uuid4

from eth_account.account import Account
Expand All @@ -33,7 +34,7 @@
from web3.types import TxParams

from flashbots import FlashbotsWeb3, flashbot
from flashbots.constants import FLASHBOTS_NETWORKS, get_networks
from flashbots.constants import FLASHBOTS_NETWORKS
from flashbots.types import Network

# Configure logging
Expand All @@ -45,14 +46,29 @@
logger = logging.getLogger(__name__)


class EnumAction(argparse.Action):
def __init__(self, **kwargs):
enum_type = kwargs.pop("type", None)
if enum_type is None:
raise ValueError("type must be assigned an Enum when using EnumAction")
if not issubclass(enum_type, Enum):
raise TypeError("type must be an Enum when using EnumAction")
kwargs.setdefault("choices", tuple(e.value for e in enum_type))
super(EnumAction, self).__init__(**kwargs)
self._enum = enum_type

def __call__(self, parser, namespace, values, option_string=None):
value = self._enum(values)
setattr(namespace, self.dest, value)


def parse_arguments() -> Network:
networks = get_networks()
parser = argparse.ArgumentParser(description="Flashbots simple example")
parser.add_argument(
"network",
type=str,
choices=networks,
help=f"The network to use ({', '.join(networks)})",
type=Network,
action=EnumAction,
help=f"The network to use ({', '.join(e.value for e in Network)})",
)
parser.add_argument(
"--log-level",
Expand Down Expand Up @@ -81,7 +97,7 @@ def get_account_from_env(key: str) -> LocalAccount:
return Account.from_key(env(key))


def setup_web3(network: str) -> FlashbotsWeb3:
def setup_web3(network: Network) -> FlashbotsWeb3:
provider_url = os.environ.get(
"PROVIDER_URL", FLASHBOTS_NETWORKS[network]["provider_url"]
)
Expand All @@ -105,7 +121,7 @@ def log_account_balances(w3: Web3, sender: str, receiver: str) -> None:


def create_transaction(
w3: Web3, sender: str, receiver: str, nonce: int, network: str
w3: Web3, sender: str, receiver: str, nonce: int, network: Network
) -> TxParams:
# Get the latest gas price information
latest = w3.eth.get_block("latest")
Expand Down
36 changes: 16 additions & 20 deletions flashbots/constants.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
from typing import Dict, Tuple
from typing import Dict

from eth_typing import URI

from .types import Network, NetworkConfig

FLASHBOTS_NETWORKS: Dict[Network, NetworkConfig] = {
"sepolia": {
"chain_id": 11155111,
"provider_url": URI("https://rpc-sepolia.flashbots.net"),
"relay_url": URI("https://relay-sepolia.flashbots.net"),
},
"holesky": {
"chain_id": 17000,
"provider_url": URI("https://rpc-holesky.flashbots.net"),
"relay_url": URI("https://relay-holesky.flashbots.net"),
},
"mainnet": {
"chain_id": 1,
"provider_url": URI("https://rpc.flashbots.net"),
"relay_url": URI("https://relay.flashbots.net"),
},
Network.SEPOLIA: NetworkConfig(
chain_id=11155111,
provider_url=URI("https://rpc-sepolia.flashbots.net"),
relay_url=URI("https://relay-sepolia.flashbots.net"),
),
Network.HOLESKY: NetworkConfig(
chain_id=17000,
provider_url=URI("https://rpc-holesky.flashbots.net"),
relay_url=URI("https://relay-holesky.flashbots.net"),
),
Network.MAINNET: NetworkConfig(
chain_id=1,
provider_url=URI("https://rpc.flashbots.net"),
relay_url=URI("https://relay.flashbots.net"),
),
}


def get_networks() -> Tuple[Network, ...]:
return tuple(FLASHBOTS_NETWORKS.keys())
10 changes: 7 additions & 3 deletions flashbots/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Literal, Optional, TypedDict, Union
from enum import Enum
from typing import List, Optional, TypedDict, Union

from eth_account.signers.local import LocalAccount
from eth_typing import URI, HexStr
Expand Down Expand Up @@ -87,8 +88,11 @@
},
)

# Add the following new types
Network = Literal["sepolia", "holesky", "mainnet"]

class Network(Enum):
SEPOLIA = "sepolia"
HOLESKY = "holesky"
MAINNET = "mainnet"


class NetworkConfig(TypedDict):
Expand Down
Loading

0 comments on commit 78ee934

Please sign in to comment.