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
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
Ape is a framework for Web3 Python applications and smart contracts, with advanced functionality for testing, deployment, and on-chain interactions.

Ape Framework Ape is a framework for Web3 Python applications and smart contracts, with advanced functionality for testing, deployment, and on-chain i

ApeWorX Ltd. 552 Dec 30, 2022
Web APIs for Django. 🎸

Django REST framework Awesome web-browsable Web APIs. Full documentation for the project is available at https://www.django-rest-framework.org/. Fundi

Encode 24.7k Jan 3, 2023
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

null 1k Jan 4, 2023
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

null 1k Jan 15, 2021
Flask Sugar is a web framework for building APIs with Flask, Pydantic and Python 3.6+ type hints.

Flask Sugar is a web framework for building APIs with Flask, Pydantic and Python 3.6+ type hints. check parameters and generate API documents automatically. Flask Sugar是一个基于flask,pyddantic,类型注解的API框架, 可以检查参数并自动生成API文档

null 162 Dec 26, 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 4, 2022
Goblet is an easy-to-use framework that enables developers to quickly spin up fully featured REST APIs with python on GCP

GOBLET Goblet is a framework for writing serverless rest apis in python in google cloud. It allows you to quickly create and deploy python apis backed

Austen 78 Dec 27, 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 2, 2021
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
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 8, 2021
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

null 687 Nov 27, 2022
You can use the mvc pattern in your flask application using this extension.

You can use the mvc pattern in your flask application using this extension. Installation Run the follow command to install mvc_flask: $ pip install mv

Marcus Pereira 37 Dec 17, 2022
Otter is framework for creating microservices in Flask like fassion using RPC communication via message queue.

Otter Framework for microservices. Overview Otter is framework for creating microservices in Flask like fassion using RPC communication via message qu

Volodymyr Biloshytskyi 4 Mar 23, 2022
Pyrin is an application framework built on top of Flask micro-framework to make life easier for developers who want to develop an enterprise application using Flask

Pyrin A rich, fast, performant and easy to use application framework to build apps using Flask on top of it. Pyrin is an application framework built o

Mohamad Nobakht 10 Jan 25, 2022
Bromelia-hss implements an HSS by using the Python micro framework Bromélia.

Bromélia HSS bromelia-hss is the second official implementation of a Diameter-based protocol application by using the Python micro framework Bromélia.

henriquemr 7 Nov 2, 2022
A simple todo app using flask and sqlachemy

TODO app This is a simple TODO app made using Flask. Packages used: DoodleCSS Special thanks to Chris McCormick (@mccrmx) :) Flask Flask-SQLAlchemy Fl

Lenin 1 Dec 26, 2021
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
Poetry plugin to bundle projects into various formats

Poetry bundle plugin This package is a plugin that allows the bundling of Poetry projects into various formats. Installation The easiest way to instal

Poetry 54 Jan 2, 2023
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