Web3.py plugin for using Flashbots' bundle APIs

Overview

This library works by injecting a new module in the Web3.py instance, which allows submitting "bundles" of transactions directly to miners. This is done by also creating a middleware which captures calls to eth_sendBundle and eth_callBundle, and sends them to an RPC endpoint which you have specified, which corresponds to mev-geth. To apply correct headers we use FlashbotProvider which injects the correct header on post

Example

from eth_account.signers.local import LocalAccount
from web3 import Web3, HTTPProvider
from flashbots import flashbot
from eth_account.account import Account
import os

ETH_ACCOUNT_SIGNATURE: LocalAccount = Account.from_key(os.environ.get("ETH_SIGNATURE_KEY"))


w3 = Web3(HTTPProvider("http://localhost:8545"))
flashbot(w3, ETH_ACCOUNT_SIGNATURE)

Now the w3.flashbots.sendBundle method should be available to you. Look in examples/simple.py for usage examples

Development and testing

Setup and run (mev-)geth with Websocket support:

geth --http --http.api eth,net,web3,txpool --syncmode full

Install poetry

Poetry will automatically fix your venv and all packages needed

poetry install

Tips: PyCharm has a poetry plugin

Linting

It's advisable to run black with default rules for linting

sudo pip install black # Black should be installed with a global entrypoint
black .
Comments
  • The example as-is doesn't work and doesn't make sense

    The example as-is doesn't work and doesn't make sense

    Firstly, greatly appreciate the effort in making flashbots available to the Python community. Ty!

    Running the simply.py example as is as I see it, has 3 issues for me. One is logical - as mentioned elsewhere - in which rarely does one have the receiver's private key to sign. The second may be code related and the compiler throws an error in the bundle declaration (line 72) with some dictionary merge issue. Is this common? Lastly, the library doesn't seem to be updated to work with EIP1559 - requiring gasFee as opposed to their new counterparts, which is not a work-stopper, but it's sub-optimal.

    Is it expected for this library to be maintained further?

    (If I had the knowledge to contribute and further this library, I would)

    Thanks again

    opened by dcapeluto 7
  • goerli testnet 400 Client Error: Bad Request for url

    goerli testnet 400 Client Error: Bad Request for url

    I have an error when using the send_private_transaction method send_bundel is ok can someone tell me what is the problem。 image

    code: bundle = {"signed_transaction": tx1_signed.rawTransaction} w3.flashbots.send_private_transaction(bundle)

    opened by hongwei520 6
  • Add EIP-1559 support

    Add EIP-1559 support

    In this PR:

    • Modify Flashbots.sign_bundle to work with type 1 and 2 transactions (as described in EIP-2178)
    • Implement parsing of signed transactions to correctly handle nonces and more-or-less match the js library behavior
    • Add minimal working example of post-London flashbots usage
    • Bump web3py dependency to 5.22 and project version to 1.0.1
    • Some, generally unnecessary, code style refinements
    opened by lekhovitsky 5
  • sim error on eip1559 example when using Goerli network

    sim error on eip1559 example when using Goerli network

    I used the example as is but made some modification, including

    1. Changing chainID to 5 for Goerli test network
    2. Adding "w3.middleware_onion.inject(geth_poa_middleware, layer=0)" to resolve the POA chain error

    However I still received the following error: sim error {'code': -32000, 'message': 'missing trie node 4216c08cb01c7e93ab89e22710bff15671947a723d5bd3986bbf7d26262096c7 (path )'}

    Also I keep getting "Bundle was not executed" message even when setting a higher gas price...anyone has the same issue?

    opened by tztlzspond 4
  • Added support for transactions expressed as dictionaries

    Added support for transactions expressed as dictionaries

    Added support for transactions expressed as dictionaries, as provided by w3.eth.get_block('pending', full_transactions=True)

    Example

    AttributeDict({'blockHash': HexBytes('0xe26e77c7426bb4b370dede3e6de140e67c2411e90813c3017ff2761a0e9420b8'), 'blockNumber': 10713060, 'from': '0x81F87187235223c2eb194EA63B2B292332EF8f82', 'gas': 134238, 'gasPrice': 10000000000, 'hash': HexBytes('0xbbd78a51741b3c972e646ac09b36441ed0dd3a023bbfb62d0d6de9da6f6319a9'), 'input': '0x7ff36ab500000000000000000000000000000000000000000000000000000000001b4139000000000000000000000000000000000000000000000000000000000000008000000000000000000000000081f87187235223c2eb194ea63b2b292332ef8f820000000000000000000000000000000000000000000000000000000061979f0d0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c778417e063141139fce010982780140aa0cd5ab000000000000000000000000dbc941fec34e8965ebc4a25452ae7519d6bdfc4e', 'nonce': 177917, 'r': HexBytes('0xa76ae800d6eaa9ba72589f0f94ac8cfeef182bb677f2f38768ba44bbf1bd45ca'), 's': HexBytes('0x535e932ef0d42fde8b40b2bee0672b07af03327b569d05a64fbef622cd74d811'), 'to': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'transactionIndex': 0, 'type': '0x0', 'v': 42, 'value': 729641236761779})

    opened by itsdeka 4
  • Use web3>=5.19.0

    Use web3>=5.19.0

    In April, web3 first removed Module in favor of ModuleV2 and then renamed ModuleV2 to Module. The changes have been live since web3 5.19.0.

    This PR bumps the web3 dependency to use at least 5.19.0. It allows us to use the flashbots package with latest eth-brownie version, which depend on web3 5.21.0.

    Note I haven't thoroughly tested this yet. Please let me know if I missed something.

    opened by milancermak 4
  • Change simulate to return signed_bundled_transactions

    Change simulate to return signed_bundled_transactions

    I think this could be helpful. This way you can build an unsigned bundle, call simulate, check results, then send_bundle_munger(signed_bundled_transactions, ...).

    Without this, you can pass the unsigned bundle to send_bundle_munger, but it has to sign things twice then.

    opened by WyseNynja 4
  • Reformatting and cleanup of the code base

    Reformatting and cleanup of the code base

    Hi! I've started on a cleanup of the code base located over here: https://github.com/N0K0/web3-flashbots Its mainly based on good old PEP-8 and Black https://github.com/psf/black

    Do the Flashbots organization have any opinions on codestyle++ :) Other than that my goal is adding the missing signature header and clean up the demo files.

    opened by N0K0 3
  • Error: invalid remainder

    Error: invalid remainder

    Hello friends, I am trying to add support for type 2 transactions (https://github.com/flashbots/web3-flashbots/pull/31) but I am having a few issues.

    typed_transaction = TypedTransaction.from_dict({
                    'chainId': mempool_tx['chainId'],
                    'nonce': mempool_tx["nonce"],
                    'maxPriorityFeePerGas': mempool_tx["maxPriorityFeePerGas"],
                    'maxFeePerGas': mempool_tx["maxFeePerGas"],
                    'gas': mempool_tx["gas"],
                    'value': mempool_tx["value"],
                    'data': HexBytes(mempool_tx["input"]),
                    'accessList': mempool_tx['accessList'],
                    'to': HexBytes(mempool_tx['to']),
                    'type': mempool_tx['type'],
                    'v': v,
                    'r': r,
                    's': s
    })
    
    print(typed_transaction.as_dict())
    
    raw = encode_transaction(typed_transaction, (v, r, s))
    
    w3.eth.send_raw_transaction(raw)
    

    {'chainId': 1, 'to': HexBytes('0xe592427a0aece92de3edee1f18e0157c05861564'), 'value': 0, 'data': HexBytes('0xc04b8d59000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000009bdcfcd6b4b4403a457fb68fd6130da07b422669000000000000000000000000000000000000000000000000000000006131e2ab00000000000000000000000000000000000000000000000000000055ab5e1928000000000000000000000000000000000000000000000005270c0c4c40384a6a0000000000000000000000000000000000000000000000000000000000000042dac17f958d2ee523a2206206994597c13d831ec70001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f4c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000000000000000000000'), 'accessList': [], 'nonce': 717, 'maxPriorityFeePerGas': 1533500047, 'maxFeePerGas': 132166886538, 'gas': 342634, 'v': 0, 'r': 67327043002212475186977304167754595225730875999171967519505434404847271226951, 's': 36595555180238550166126321802692921865110803882641978438054671199525379451154, 'type': 2}

    ValueError: {'message': 'invalid remainder', 'code': -32000, 'data': {'stack': 'Error: invalid remainder\n at Object.t.decode (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:4:9150)\n at S.queueRawTransaction (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:210203)\n at p.eth_sendRawTransaction (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:200119)\n at p.handleRequest (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:196970)\n at t (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:65024)\n at a.handleRequest (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:195252)\n at t (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:65024)\n at f.s.handleRequest (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:178304)\n at f.handleRequest (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:176115)\n at t (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:65024)\n at o.handleRequest (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:184369)\n at t (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:65024)\n at a.handleRequest (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:183814)\n at t (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:65024)\n at u._handleAsync (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:65060)\n at Timeout._onTimeout (/usr/local/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:55:64488)', 'name': 'Error'}}

    What is wrong?

    opened by itsdeka 2
  • Simulating / sending bundle results in HTTP error 400.

    Simulating / sending bundle results in HTTP error 400.

    Flashbots Relay is complaining if field 'gasPrice' is set. Removing line 91 in flashbots.py resolves this issue.

    https://github.com/flashbots/web3-flashbots/blob/46c1cb08b250b964b51974384109e07cdf42eb98/flashbots/flashbots.py#L91

    opened by avocadochicken 2
  • Bug diagnosed

    Bug diagnosed

    As per https://discord.com/channels/755466764501909692/795777653197635596/844259896999804949 we would like to fix types.py code to work with Python 3.8.x.

    This was the necessary change in the beginning of the file:

    from eth_account.account import Account
    from web3.types import TxParams
    from typing import TypedDict
    import typing
    
    FlashbotsBundleTx = TypedDict(
        "FlashbotsBundleTx",
        {
            "transaction": TxParams,
            "signer": Account,
        },
    )
    
    FlashbotsBundleRawTx = TypedDict(
        "FlashbotsBundleRawTx",
        {
            "signed_transaction": str,
        },
    )
    
    FlashbotsOpts = TypedDict(
        "FlashbotsOpts",
        {"minTimestamp": int, "maxTimestamp": int, "revertingTxHashes": typing.List[str]},
    )```
    
    Also would be nice to have `poetry install` mentioned in the Readme for semi-noobs like me who never got in touch with it before.
    
    
    
    opened by Huge 2
  • Fix callBundle: 400 unable to decode txs issue

    Fix callBundle: 400 unable to decode txs issue

    Why?

    I tried to simulate my bundle against historical blocks to backtest, and I keep receiving 400 Bad Request - decode txs issue. Example like:

    flashbot.simulate(
            [
                web3.eth.get_transaction(historical_tx.hash),
                ...
            ],
            # hisotrical transaction's block information
            historical_tx.block - 1 ,
            historical_tx.block - 1,
            historical_tx.block_timestamp
    )
    

    What?

    When I submit the callBundle request to some public builders I have a more detailed error message: cannot unmarshal hex string without 0x prefix into Go struct field CallBundleArgs.txs

    It should be call_bundle_munger using some expired hexing handling.

    How?

    Updated to self.to_hex() to handle the transactions bundle bytes hexing.

    opened by kavimaluskam 0
  • cancel private tx failed

    cancel private tx failed

    when i want to cancel a transaction i made, using method

    self.w3.flashbots.cancel_private_transaction(tx_hash)

    i got response from relay:

    502 Server Error: Bad Gateway for url: https://relay.flashbots.net/

    opened by tcong-aa 0
  • adding extra delay to give web3 providers more time to synch and load…

    adding extra delay to give web3 providers more time to synch and load…

    Added an extra delay to the code that checks for bundle inclusion. I have encountered bugs where the TransactionNotFound error is raised even when my bundles land on chain.

    This suggests that web3 providers sometimes update the value returned by 'w3.eth.block.number' before the 'w3.eth.get_transaction()' endpoint is aware of all transaction hashes from a recent block.

    This may cause an issue where end-users send the same bundle twice because they do not receive proper confirmation.

    opened by mdigi14 0
  • Whether it is now supported on polygons

    Whether it is now supported on polygons

    I am a novice, does the flash robot support polygon? I replace the provider and variable and then run the sample.py file, but get a exception on w3.flashbots.send_bundle(bundle, target_block_number=block + 1) function, the error details is Bundle not found in block,I don't know if it's my problem。

    opened by langdangren 0
  • Deploying a Contract with Flashbots

    Deploying a Contract with Flashbots

    I took a look at the example, and I'm finding it difficult to translate it to a contract deployment

    https://web3py.readthedocs.io/en/stable/contracts.html#contract-deployment-example

    As we can see here, it is possible to deploy a contract from web3py. Any ideas anyone?

    opened by BDANG 0
  • ImportError: cannot import name 'ModuleV2' from 'web3.module'

    ImportError: cannot import name 'ModuleV2' from 'web3.module'

    I use web3 5.23.1, Python 3.9.6 and GCC 9.3.0 on linux.

    When trying to import the flashbots module:

    from flashbots import flashbot

    This is the output:

    Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.9/dist-packages/flashbots/init.py", line 9, in from .flashbots import Flashbots File "/usr/local/lib/python3.9/dist-packages/flashbots/flashbots.py", line 5, in from web3.module import ModuleV2 ImportError: cannot import name 'ModuleV2' from 'web3.module' (/usr/local/lib/python3.9/dist-packages/web3/module.py)

    Maybe flashbots isn't compatible with the latest version of web3? Or what am I missing?

    opened by cattieinthere 2
Releases(v1.0.0)
  • v1.0.0(Apr 20, 2022)

    • adds new API methods to web3.flashbots:
      • get_bundle_stats
      • get_user_stats
      • send_private_transaction
      • cancel_private_transaction
    • fixes goerli middleware bug
    • FlashbotsTransactionResponse becomes FlashbotsBundleResponse, and FlashbotsPrivateTransactionResponse is added
      • sign_bundle and send_bundle are affected
    • adds EIP-1559 compatibility
    • corrects typos & formatting errors
    • upgrades dependencies
    • adds missing type hints
    Source code(tar.gz)
    Source code(zip)
Owner
Flashbots
Flashbots
Try to create a python mircoservice framework.

Micro current_status: prototype. ... Python microservice framework. More in Document. You should clone this project and run inv docs. Install Not now.

修昊 1 Dec 07, 2021
Asita is a web application framework for python.

What is Asita ? Asita is a web application framework for python. It is designed to be easy to use and be more easy for javascript users to use python

Mattéo 4 Nov 16, 2021
PipeLayer is a lightweight Python pipeline framework

PipeLayer is a lightweight Python pipeline framework. Define a series of steps, and chain them together to create modular applications

greaterthan 64 Jul 21, 2022
Quiz Web App with Flask and MongoDB as the Databases

quiz-app Quiz Web Application made with flask and mongodb as the Databases Before you run this application, change the inside MONGODB_URI ( in config.

gibran abdillah 7 Dec 14, 2022
Light, Flexible and Extensible ASGI API framework

Starlite Starlite is a light, opinionated and flexible ASGI API framework built on top of pydantic and Starlette. Check out the Starlite documentation

Na'aman Hirschfeld 1.6k Jan 09, 2023
Web3.py plugin for using Flashbots' bundle APIs

This library works by injecting a new module in the Web3.py instance, which allows submitting "bundles" of transactions directly to miners. This is do

Flashbots 293 Dec 31, 2022
The Web framework for perfectionists with deadlines.

Django Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Thanks for checking it out. All docu

Django 67.9k Dec 29, 2022
Low code web framework for real world applications, in Python and Javascript

Full-stack web application framework that uses Python and MariaDB on the server side and a tightly integrated client side library.

Frappe 4.3k Dec 30, 2022
A shopping list and kitchen inventory management app.

Flask React Project This is the backend for the Flask React project. Getting started Clone this repository (only this branch) git clone https://github

11 Jun 03, 2022
Python AsyncIO data API to manage billions of resources

Introduction Please read the detailed docs This is the working project of the next generation Guillotina server based on asyncio. Dependencies Python

Plone Foundation 183 Nov 15, 2022
A proof-of-concept CherryPy inspired Python micro framework

Varmkorv Varmkorv is a CherryPy inspired micro framework using Werkzeug. This is just a proof of concept. You are free to use it if you like, or find

Magnus Karlsson 1 Nov 22, 2021
The no-nonsense, minimalist REST and app backend framework for Python developers, with a focus on reliability, correctness, and performance at scale.

The Falcon Web Framework Falcon is a reliable, high-performance Python web framework for building large-scale app backends and microservices. It encou

Falconry 9k Jan 01, 2023
Dockerized web application on Starlite, SQLAlchemy1.4, PostgreSQL

Production-ready dockerized async REST API on Starlite with SQLAlchemy and PostgreSQL

Artur Shiriev 10 Jan 03, 2023
Flask-Potion is a RESTful API framework for Flask and SQLAlchemy, Peewee or MongoEngine

Flask-Potion Description Flask-Potion is a powerful Flask extension for building RESTful JSON APIs. Potion features include validation, model resource

DTU Biosustain 491 Dec 08, 2022
Daniel Vaz Gaspar 4k Jan 08, 2023
Pyramid - A Python web framework

Pyramid Pyramid is a small, fast, down-to-earth, open source Python web framework. It makes real-world web application development and deployment more

Pylons Project 3.7k Dec 30, 2022
Flask + Docker + Nginx + Gunicorn + MySQL + Factory Method Pattern

This Flask project is reusable and also an example of how to merge Flask, Docker, Nginx, Gunicorn, MySQL, new: Flask-RESTX, Factory Method design pattern, and other optional dependencies such as Dyna

Facundo Padilla 19 Jul 23, 2022
Library for building WebSocket servers and clients in Python

What is websockets? websockets is a library for building WebSocket servers and clients in Python with a focus on correctness and simplicity. Built on

Aymeric Augustin 4.3k Dec 31, 2022
Swagger/OpenAPI First framework for Python on top of Flask with automatic endpoint validation & OAuth2 support

Connexion Connexion is a framework that automagically handles HTTP requests based on OpenAPI Specification (formerly known as Swagger Spec) of your AP

Zalando SE 4.2k Jan 07, 2023
Phoenix LiveView but for Django

Reactor, a LiveView library for Django Reactor enables you to do something similar to Phoenix framework LiveView using Django Channels. What's in the

Eddy Ernesto del Valle Pino 526 Jan 02, 2023