Skip to content

Commit

Permalink
Enhance simple.py example and improve Flashbots module
Browse files Browse the repository at this point in the history
- Add command-line arguments for network selection and log level
- Implement dynamic logging configuration
- Update docstrings with usage instructions and examples
- Refactor network type to 'Network' for consistency
- Add get_networks() function to retrieve available networks
- Improve flashbot() function documentation
- Minor code cleanup and formatting improvements
  • Loading branch information
odysseus0 committed Jul 23, 2024
1 parent 11962e7 commit bd4a0b6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 23 deletions.
54 changes: 37 additions & 17 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
Minimal viable example of flashbots usage with dynamic fee transactions.
Sends a bundle of two transactions which transfer some ETH into a random account.
"eth_sendBundle" is a generic method that can be used to send a bundle to any relay.
For instance, you can use the following relay URLs:
titan: 'https://rpc.titanbuilder.xyz/'
beaver: 'https://rpc.beaverbuild.org/'
builder69: 'https://builder0x69.io/'
rsync: 'https://rsync-builder.xyz/'
flashbots: 'https://relay.flashbots.net'
You can simply replace the URL in the flashbot method to use a different relay like:
flashbot(w3, signer, YOUR_CHOSEN_RELAY_URL)
Environment Variables:
- ETH_SENDER_KEY: Private key of account which will send the ETH.
- ETH_SIGNER_KEY: Private key of account which will sign the bundle.
- This account is only used for reputation on flashbots and should be empty.
- PROVIDER_URL: (Optional) HTTP JSON-RPC Ethereum provider URL. If not set, Flashbots Protect RPC will be used.
- LOG_LEVEL: (Optional) Set the logging level. Default is 'INFO'. Options: DEBUG, INFO, WARNING, ERROR, CRITICAL.
Usage:
python examples/simple.py <network> [--log-level LEVEL]
Arguments:
- network: The network to use (e.g., mainnet, goerli)
- --log-level: (Optional) Set the logging level. Default is 'INFO'.
Example:
LOG_LEVEL=DEBUG python examples/simple.py mainnet --log-level DEBUG
"""

import argparse
import logging
import os
import secrets
Expand All @@ -32,17 +33,36 @@
from web3.types import TxParams

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

# Configure logging
log_level = os.environ.get("LOG_LEVEL", "INFO").upper()
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
level=getattr(logging, log_level),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)

# Define the network to use
NETWORK: NetworkType = "holesky" # Options: "sepolia", "holesky", "mainnet"

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)})",
)
parser.add_argument(
"--log-level",
type=str,
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO",
help="Set the logging level",
)
args = parser.parse_args()
return args.network


def env(key: str) -> str:
Expand Down Expand Up @@ -110,7 +130,7 @@ def create_transaction(


def main() -> None:
network = NETWORK
network = parse_arguments()
sender = get_account_from_env("ETH_SENDER_KEY")
receiver = Account.create().address
w3 = setup_web3(network)
Expand Down
26 changes: 24 additions & 2 deletions flashbots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,30 @@ def flashbot(
signature_account: LocalAccount,
endpoint_uri: Optional[Union[URI, str]] = None,
) -> FlashbotsWeb3:
"""
Injects the flashbots module and middleware to w3.
"""Inject the Flashbots module and middleware into a Web3 instance.
This method enables sending bundles to various relays using "eth_sendBundle".
Args:
w3: The Web3 instance to modify.
signature_account: The account used for signing transactions.
endpoint_uri: The relay endpoint URI. Defaults to Flashbots relay.
Returns:
The modified Web3 instance with Flashbots functionality.
Examples:
Using default Flashbots relay:
>>> flashbot(w3, signer)
Using custom relay:
>>> flashbot(w3, signer, CUSTOM_RELAY_URL)
Available relay URLs:
- Titan: 'https://rpc.titanbuilder.xyz/'
- Beaver: 'https://rpc.beaverbuild.org/'
- Rsync: 'https://rsync-builder.xyz/'
- Flashbots: 'https://relay.flashbots.net' (default)
"""

flashbots_provider = FlashbotProvider(signature_account, endpoint_uri)
Expand Down
10 changes: 7 additions & 3 deletions flashbots/constants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Dict
from typing import Dict, Tuple

from eth_typing import URI

from .types import NetworkConfig, NetworkType
from .types import Network, NetworkConfig

FLASHBOTS_NETWORKS: Dict[NetworkType, NetworkConfig] = {
FLASHBOTS_NETWORKS: Dict[Network, NetworkConfig] = {
"sepolia": {
"chain_id": 11155111,
"provider_url": URI("https://rpc-sepolia.flashbots.net"),
Expand All @@ -21,3 +21,7 @@
"relay_url": URI("https://relay.flashbots.net"),
},
}


def get_networks() -> Tuple[Network, ...]:
return tuple(FLASHBOTS_NETWORKS.keys())
1 change: 1 addition & 0 deletions flashbots/flashbots.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def raw_bundle_formatter(self, resp) -> Any:
mungers=[send_bundle_munger],
result_formatters=raw_bundle_formatter,
)

send_bundle = sendBundle

def cancel_bundles_munger(
Expand Down
2 changes: 1 addition & 1 deletion flashbots/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
)

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


class NetworkConfig(TypedDict):
Expand Down

0 comments on commit bd4a0b6

Please sign in to comment.