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
Django Ninja - Fast Django REST Framework

Django Ninja is a web framework for building APIs with Django and Python 3.6+ type hints.

Vitaliy Kucheryaviy 3.8k Jan 02, 2023
The core of a service layer that integrates with the Pyramid Web Framework.

pyramid_services The core of a service layer that integrates with the Pyramid Web Framework. pyramid_services defines a pattern and helper methods for

Michael Merickel 78 Apr 15, 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
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
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
Mini Web Framework on MicroPython (Esp8266)

dupgee Dupgee is a mini web framework developed for micro-python(Tested on esp8266). Installation pip install dupgee Create Project dupgee create newp

ahmet kotan 38 Jul 25, 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
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
Online Boutique is a cloud-native microservices demo application

Online Boutique is a cloud-native microservices demo application. Online Boutique consists of a 10-tier microservices application. The application is

Matt Reider 1 Oct 22, 2021
Restful API framework wrapped around MongoEngine

Flask-MongoRest A Restful API framework wrapped around MongoEngine. Setup from flask import Flask from flask_mongoengine import MongoEngine from flask

Close 525 Jan 01, 2023
A very simple asynchronous wrapper that allows you to get access to the Oracle database in asyncio programs.

cx_Oracle_async A very simple asynchronous wrapper that allows you to get access to the Oracle database in asyncio programs. Easy to use , buy may not

36 Dec 21, 2022
Containers And REST APIs Workshop

Containers & REST APIs Workshop Containers vs Virtual Machines Ferramentas Podman: https://podman.io/ Docker: https://www.docker.com/ IBM CLI: https:/

Vanderlei Munhoz 8 Dec 16, 2021
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
News search API developed for the purposes of the ColdCase Project.

Saxion - Cold Case - News Search API Setup Local – Linux/MacOS Make sure you have python 3.9 and pip 21 installed. This project uses a MySQL database,

Dimitar Rangelov 3 Jul 01, 2021
Flask like web framework for AWS Lambda

lambdarest Python routing mini-framework for AWS Lambda with optional JSON-schema validation. ⚠️ A user study is currently happening here, and your op

sloev / Johannes Valbjørn 91 Nov 10, 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 Python package to easily create APIs in Python.

API_Easy An Python Package for easily create APIs in Python pip install easy-api-builder Requiremnets: = python 3.6 Required modules -- Flask Docume

Envyre-Coding 2 Jan 04, 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
easyopt is a super simple yet super powerful optuna-based Hyperparameters Optimization Framework that requires no coding.

easyopt is a super simple yet super powerful optuna-based Hyperparameters Optimization Framework that requires no coding.

Federico Galatolo 9 Feb 04, 2022
A comprehensive reference for all topics related to building and maintaining microservices

This pandect (πανδέκτης is Ancient Greek for encyclopedia) was created to help you find and understand almost anything related to Microservices that i

Ivan Bilan 64 Dec 09, 2022