Web3.py plugin for using Flashbots' bundle APIs

Overview

web3-flashbots

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.

Example

from web3 import Web3, HTTPProvider
from web3_flashbots import flashbot
w3 = Web3(HTTPProvider("http://localhost:8545"))
flashbot(w3, flashbots_key_id="MY_API_ID", flashbots_secret="MY_API_SECRET", flashbots_url="http://localhost:8545")

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

Test

  1. Clone and run mev-geth as instructed in the link
  2. PYTHONPATH=pwd && python examples/demo.py
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
Georgios Konstantopoulos
blockchain scalability, information security and mechanism design. That Layer 2 guy. Aspiring Rustacean.
Georgios Konstantopoulos
web.py is a web framework for python that is as simple as it is powerful.

web.py is a web framework for Python that is as simple as it is powerful. Visit http://webpy.org/ for more information. The latest stable release 0.62

5.8k Dec 30, 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
A Flask API REST to access words' definition

A Flask API to access words' definitions

Pablo Emídio S.S 9 Jul 22, 2022
A micro web-framework using asyncio coroutines and chained middleware.

Growler master ' dev Growler is a web framework built atop asyncio, the asynchronous library described in PEP 3156 and added to the standard library i

687 Nov 27, 2022
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
Fast, asynchronous and elegant Python web framework.

Warning: This project is being completely re-written. If you're curious about the progress, reach me on Slack. Vibora is a fast, asynchronous and eleg

vibora.io 5.7k Jan 08, 2023
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
Chisel is a light-weight Python WSGI application framework built for creating well-documented, schema-validated JSON web APIs

chisel Chisel is a light-weight Python WSGI application framework built for creating well-documented, schema-validated JSON web APIs. Here are its fea

Craig Hobbs 2 Dec 02, 2021
🦍 The Cloud-Native API Gateway

Kong or Kong API Gateway is a cloud-native, platform-agnostic, scalable API Gateway distinguished for its high performance and extensibility via plugi

Kong 33.8k Jan 09, 2023
A boilerplate Flask API for a Fullstack Project with some additional packages and configuration prebuilt. ⚙

Flask Boilerplate to quickly get started with production grade flask application with some additional packages and configuration prebuilt.

Yasser Tahiri 32 Dec 24, 2022
Fast⚡, simple and light💡weight ASGI micro🔬 web🌏-framework for Python🐍.

NanoASGI Asynchronous Python Web Framework NanoASGI is a fast ⚡ , simple and light 💡 weight ASGI micro 🔬 web 🌏 -framework for Python 🐍 . It is dis

Kavindu Santhusa 8 Jun 16, 2022
Endpoints is a lightweight REST api framework written in python and used in multiple production systems that handle millions of requests daily.

Endpoints Quickest API builder in the West! Endpoints is a lightweight REST api framework written in python and used in multiple production systems th

Jay Marcyes 30 Mar 05, 2022
A framework that let's you compose websites in Python with ease!

Perry Perry = A framework that let's you compose websites in Python with ease! Perry works similar to Qt and Flutter, allowing you to create componen

Linkus 13 Oct 09, 2022
Djask is a web framework for python which stands on the top of Flask and will be as powerful as Django.

Djask is a web framework for python which stands on the top of Flask and will be as powerful as Django.

Andy Zhou 27 Sep 08, 2022
The source code to the Midnight project

MidnightSniper Started: 24/08/2021 Ended: 24/10/2021 What? This is the source code to a project developed to snipe minecraft names Why release? The ad

Kami 2 Dec 03, 2021
TinyAPI - 🔹 A fast & easy and lightweight WSGI Framework for Python

TinyAPI - 🔹 A fast & easy and lightweight WSGI Framework for Python

xArty 3 Apr 08, 2022
WebSocket and WAMP in Python for Twisted and asyncio

Autobahn|Python WebSocket & WAMP for Python on Twisted and asyncio. Quick Links: Source Code - Documentation - WebSocket Examples - WAMP Examples Comm

Crossbar.io 2.4k Jan 06, 2023
Pulumi-checkly - Checkly Pulumi Provider With Python

🚨 This project is still in very early stages and is not stable, use at your own

Checkly 16 Dec 15, 2022
bottle.py is a fast and simple micro-framework for python web-applications.

Bottle: Python Web Framework Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module a

Bottle Micro Web Framework 7.8k Dec 31, 2022
Async Python 3.6+ web server/framework | Build fast. Run fast.

Sanic | Build fast. Run fast. Build Docs Package Support Stats Sanic is a Python 3.6+ web server and web framework that's written to go fast. It allow

Sanic Community Organization 16.7k Dec 28, 2022