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
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
A high-level framework for building GitHub applications in Python.

A high-level framework for building GitHub applications in Python. Core Features Async Proper ratelimit handling Handles interactions for you (

Vish M 3 Apr 12, 2022
Embrace the APIs of the future. Hug aims to make developing APIs as simple as possible, but no simpler.

Read Latest Documentation - Browse GitHub Code Repository hug aims to make developing Python driven APIs as simple as possible, but no simpler. As a r

Hug API Framework 6.7k Dec 27, 2022
Distribution Analyser is a Web App that allows you to interactively explore continuous distributions from SciPy and fit distribution(s) to your data.

Distribution Analyser Distribution Analyser is a Web App that allows you to interactively explore continuous distributions from SciPy and fit distribu

Robert Dzudzar 46 Nov 08, 2022
🔥 Fire up your API with this flamethrower

🔥 Fire up your API. Documentation: https://flama.perdy.io Flama Flama aims to bring a layer on top of Starlette to provide an easy to learn and fast

José Antonio Perdiguero 216 Dec 26, 2022
The Modern And Developer Centric Python Web Framework. Be sure to read the documentation and join the Slack channel questions: http://slack.masoniteproject.com

NOTE: Masonite 2.3 is no longer compatible with the masonite-cli tool. Please uninstall that by running pip uninstall masonite-cli. If you do not unin

Masonite 1.9k Jan 04, 2023
Asynchronous HTTP client/server framework for asyncio and Python

Async http client/server framework Key Features Supports both client and server side of HTTP protocol. Supports both client and server Web-Sockets out

aio-libs 13.2k Jan 05, 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
Screaming-fast Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser.

Japronto! There is no new project development happening at the moment, but it's not abandoned either. Pull requests and new maintainers are welcome. I

Paweł Piotr Przeradowski 8.6k Dec 29, 2022
Klein - A micro-framework for developing production-ready web services with Python

Klein, a Web Micro-Framework Klein is a micro-framework for developing production-ready web services with Python. It is 'micro' in that it has an incr

Twisted Matrix Labs 814 Jan 08, 2023
A familiar HTTP Service Framework for Python.

Responder: a familiar HTTP Service Framework for Python Powered by Starlette. That async declaration is optional. View documentation. This gets you a

Taoufik 3.6k Dec 27, 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
NO LONGER MAINTAINED - A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.

NO LONGER MAINTAINED This repository is no longer maintained due to lack of time. You might check out the fork https://github.com/mrevutskyi/flask-res

1k Jan 04, 2023
aiohttp-ratelimiter is a rate limiter for the aiohttp.web framework.

aiohttp-ratelimiter aiohttp-ratelimiter is a rate limiter for the aiohttp.web fr

JGL Technologies 4 Dec 11, 2022
Microservice example with Python, Faust-Streaming and Kafka (Redpanda)

Microservices Orchestration with Python, Faust-Streaming and Kafka (Redpanda) Example project for PythonBenin meetup. It demonstrates how to use Faust

Lé 3 Jun 13, 2022
Full duplex RESTful API for your asyncio web apps

TBone TBone makes it easy to develop full-duplex RESTful APIs on top of your asyncio web application or webservice. It uses a nonblocking asynchronous

TBone Framework 37 Aug 07, 2022
An alternative serializer implementation for REST framework written in cython built for speed.

drf-turbo An alternative serializer implementation for REST framework written in cython built for speed. Free software: MIT license Documentation: htt

Mng 74 Dec 30, 2022
Bablyon 🐍 A small ASGI web framework

A small ASGI web framework that you can make asynchronous web applications using uvicorn with using few lines of code

xArty 8 Dec 07, 2021
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
A public API written in Python using the Flask web framework to determine the direction of a road sign using AI

python-public-API This repository is a public API for solving the problem of the final of the AIIJC competition. The task is to create an AI for the c

Lev 1 Nov 08, 2021