Giter Club home page Giter Club logo

web3-flashbots's Introduction

web3-flashbots

This library works by injecting flashbots as 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 the flashbot method which injects the correct header on POST.

Quickstart

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_SIGNER_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.

Goerli

To use goerli, add the goerli relay RPC to the flashbot function arguments.

flashbot(w3, ETH_ACCOUNT_SIGNATURE, "https://relay-goerli.flashbots.net")

Development and testing

Install poetry

Poetry will automatically fix your venv and all packages needed.

poetry install

Tips: PyCharm has a poetry plugin

Simple Goerli Example

See examples/simple.py for environment variable definitions.

poetry shell
ETH_SENDER_KEY=<sender_private_key> \
PROVIDER_URL=https://eth-goerli.alchemyapi.io/v2/<alchemy_key> \
ETH_SIGNER_KEY=<signer_private_key> \
python examples/simple.py

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 .

web3-flashbots's People

Contributors

0xverif avatar antsiferovbogdan avatar azavalla avatar barabazs avatar blinkystitt avatar bowtieddevil avatar come-maiz avatar epheph avatar gakonst avatar guanqun avatar itsdeka avatar lekhovitsky avatar milancermak avatar n0k0 avatar thomasj02 avatar zeroxbrock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

web3-flashbots's Issues

No longer able to run the example simple.py

A couple of days ago I was able to run the simple.py on GOERLI and on Mainnet, I did up to 4 transactions in a single bundle.
Not sure what happened but I'm now getting the following error during simulation:
Simulation error {'code': -32000, 'message': 'err: %!w(<nil>); txhash 0x2a87221dda99210c637533057ebea8269bc33a87ebf52170bae7ad9b033f4be0'}
My account has Goerli ETH, the RPC seems to run fine, I'm lost about what could've happened.

Continuous Send Bundle Transactions, the simulate goes to failed by "nonce too high" error

Hi,
When I write some test case, for example, 1st step, swap ETH to UNI, 2nd step approve uniswap router, then the 3rd step swap UNI back to ETH. I always meet "nonce too high" error when I do simulation of the approve transaction, because the 1st step block number is same as the simulate block, and the nonce to the block has not increased yet, but new transaction's nonce has to increase.
So, do I have to do a delay to stagger this block? Or this is just a bug?

Forbidden for url error

I was testing on goerli and set https://relay-goerli.flashbots.net/ as my httpprovider.
w3 = Web3(HTTPProvider('https://relay-goerli.flashbots.net/')) w3.middleware_onion.add(construct_sign_and_send_raw_middleware(ETH_ACCOUNT_FROM)) flashbot(w3, ETH_ACCOUNT_SIGNATURE) ...... result = w3.flashbots.send_bundle(bundle, target_block_number=w3.eth.blockNumber + 3)
I have nearly the same codes as the sample code but it gives me this error.

File "/usr/local/lib/python3.9/dist-packages/requests/models.py", line 943, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://relay-goerli.flashbots.net/

Any suggestions? I though this web3 lib will automatically inject the head when we call w3.flashbots

Support web3.py v6

web3.py v6 has breaking changes. Currently flashbots only supports web3 (>=5.22.0,<6)

raw_bundle_formatter() missing 1 required positional argument: 'resp'

I am trying to send_bundle, but i am getting type error, anyone have clue?
Thank you in advance.

Python 3.7.6

Name: web3
Version: 5.13.0

Traceback (most recent call last):
File "/Users/mike/Desktop/github/SideProjects/flashbot_example/src/flash_test.py", line 49, in
result = w3.flashbots.send_bundle(bundle, target_block_number=w3.eth.blockNumber + 3)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web3/module.py", line 42, in caller
(method_str, params), response_formatters = method.process_params(module, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/web3/method.py", line 180, in process_params
response_formatters = (self.result_formatters(method), self.error_formatters(method))
TypeError: raw_bundle_formatter() missing 1 required positional argument: 'resp'

Web3py is detecting incorrect tx and causing simulations to fail

Issue:

Simulator is failing due to incorrect nonce vs transaction.

Description

Tested web3py script using Flashbots code. Simulator consistently detects incorrect tx vs nonce across:

  • Non-Flashbots RPC/node
  • Flashbots RPC/node
  • Different accounts, some with exposure to the flashbots relay and others not. In one case, the tx was listed as tx 20 despite the fact that the account had a nonce of 169+

Over previous months (about 11 or 12 based on my read of the Discord), others have also seen this issue/problem, but no support/remediation has been provided.

Error Message

Simulating on block 16020691
Simulation error {'code': -32000, 'message': 'err: nonce too high: address XXXX, tx: 21 state: 20 ... 

Suggestion

  • Investigate and solve issue
  • Provide more verbose error messages in Webpy3 so that users can debug and remediate

fix CI (black)

always getting this error when CI runs black now:

+ black --version
Traceback (most recent call last):
  File "/usr/local/bin/black", line 8, in <module>
    sys.exit(patched_main())
  File "/usr/local/lib/python3.8/site-packages/black/__init__.py", line [6](https://github.com/flashbots/web3-flashbots/runs/5762475813?check_suite_focus=true#step:4:6)606, in patched_main
    patch_click()
  File "/usr/local/lib/python3.[8](https://github.com/flashbots/web3-flashbots/runs/5762475813?check_suite_focus=true#step:4:8)/site-packages/black/__init__.py", line 65[9](https://github.com/flashbots/web3-flashbots/runs/5762475813?check_suite_focus=true#step:4:9)5, in patch_click
    from click import _unicodefun  # type: ignore
ImportError: cannot import name '_unicodefun' from 'click' (/usr/local/lib/python3.8/site-packages/click/__init__.py)

This may be relevant: psf/black#2964

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。

install issues

when I try to execute poetry install I have got error:
Note: This error originates from the build backend, and is likely not a problem with poetry but with cytoolz (0.11.2) not supporting PEP 517 builds. You can verify this by running 'pip wheel --use-pep517 "cytoolz (==0.11.2)"'.
how can i fix it ?

Suggestion: automatic dependency checking

A recent change in dependencies has broken the package's interaction with a key implicit component, Brownie. One resolution is to add automatic dependency checking to catch these breaking changes before they effect users. One such tool is Dependabot.

Im not sure how this would catch interactions with Brownie as it's not an explicit dependency but majority of Python solidity dev/deployment is managed with it.

Error: invalid remainder

Hello friends,
I am trying to add support for type 2 transactions (#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?

Run simulate in python

Several times I tried to use the lib to run transaction simulations, but it seems to have been difficult and unsuccessful work. This is how I am importing and using the lib:

from flashbots import flashbot

signer: LocalAccount = Account.from_key(private_key)
flashbot_con = flashbot(w3, account.address)

print(flashbot_con)

The output of this print returns None, and that prevents me from moving forward with the implementation. Does anyone have an idea what it could be?

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)

flashbots 1.1.1 requires web3<6,>=5.22.0, but you have web3 6.13.0 which is incompatible

I'm using the last stable version of web3 (v6.13.0) but flashbots is incompatible with it and I receive the error:
(AttributeError("'Flashbots' object has no attribute 'web3'"))

While installing flashbots it with pip I receive the error:
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
flashbots 1.1.1 requires web3<6,>=5.22.0, but you have web3 6.13.0 which is incompatible.

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/

Support for greater web.py versions

Hello Flashbots!

Problem

The most recently released version of flashbots has a web.py version requirement that is too low for the environment I need to run this package in (<5.18.0,>=5.17.0). My environment requires web.py >= 5.28.0.

Solution

It looks like this might already be fixed in the master branch of this repository via the changes I see in the pyproject.toml file.
Maybe a release is all that is needed?

Extra

My situation requires I work with a released version of the package

Does this lib support relayer

Hi developer,is this lib designed connected to the defalut flashbots relayer?
It seems that I need to run mev-geth and connect to miners?

Sending bundle with FlashbotsBundleDictTx

I am trying to send a bundle using the FlashbotsBundleDictTx type (based on this line of code from the library), but I am getting the following error: 400 Client Error: Bad Request for url: https://relay-goerli.flashbots.net/

send_bundle.py:

from eth_account.signers.local import LocalAccount
from eth_account.account import Account
from web3 import Web3, HTTPProvider
from web3.types import TxParams
from web3.exceptions import TransactionNotFound
from flashbots import flashbot
from flashbots.flashbots import FlashbotsBundleResponse
from uuid import uuid4
from hexbytes import HexBytes
import json

w3 = Web3(Web3.HTTPProvider(NODE_URL))

    tx = {
        'from': PUBLIC_KEY,
        'nonce': w3.eth.get_transaction_count(PUBLIC_KEY),
        'to': '0xDB74803F14e9499F6064D8A68565FA02Bf09f8F6',
        'value': Web3.toWei(0.01, 'ether'),
        'chainId': 5,
        'gas': 50000,
        'maxFeePerGas': Web3.toWei(500, 'gwei'),
        'maxPriorityFeePerGas': Web3.toWei(200, 'gwei'),
        'data': '0x0'
    }

    signed_tx = w3.eth.account.sign_transaction(tx, PRIVATE_KEY)

bundle = [
        {
            'v': signed_victim_tx['v'],
            'r': HexBytes(signed_victim_tx['r']),
            's': HexBytes(signed_victim_tx['s']),
            'nonce': victim_tx['nonce'],
            'input': victim_tx['data'],
            'value': victim_tx['value'],
            'gas': victim_tx['gas'],
            'maxFeePerGas': victim_tx['maxFeePerGas'],
            'maxPriorityFeePerGas': victim_tx['maxPriorityFeePerGas'],
            'accessList': [],
            'chainId': victim_tx['chainId'],
            'to': victim_tx['to'],
            'hash': signed_victim_tx['hash']
        }
    ]

while True:
        block_number = w3.eth.block_number
        print(f'Simulating on block {block_number}')

        try:
            w3.flashbots.simulate(bundle, block_number)
            print(f'Simulation successful')
        except Exception as e:
            print(f'Simulation error {e}')
            raise e

        replacement_uuid = str(uuid4())
        
        res: FlashbotsBundleResponse = w3.flashbots.send_bundle(
            bundle,
            target_block_number=block_number + 1,
            opts={'replacementUuid': replacement_uuid}
        )

        print(f'Bundle hash: {w3.toHex(res.bundle_hash())}')

        res.wait()

        try:
            receipts = res.receipts()
            print(f'Bundle was mined in block {receipts[0].blockNumber}')
            break
        except TransactionNotFound:
            print(f'Bundle not found in block {block_number + 1}')
            cancel_res = w3.flashbots.cancel_bundles(replacement_uuid)
            print(f'Canceled {cancel_res}')

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.


Setting custom headers for 3rd party relays

Hello I am trying to set a custom header via the flashbots provider but can't seem to figure out where to set it. I see in the flashbots provider it takes a header in put here

    headers = self.get_request_headers() | {
        "X-Flashbots-Signature": f"{self.signature_account.address}:{signed_message.signature.hex()}"

but I cannot find where get_request_headers() is looking for the header data. If someone has an example using the flashbots wrapper with custom headers, please drop an example if you can!

Thanks

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?

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?

Support more endpoints

I tried using this library with various other endpoints e.g. rsync, beaverbuild etc because Flashbot doesn't get a lot of blocks anymore... but none of them seem to be supported.

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

Munger throws TypeError if optional `opts` dict not provided

The default argument opts=None within send_raw_bundle_munger leads to exception TypeError: argument of type 'NoneType' is not iterable inside this code block:

def send_raw_bundle_munger(
    self,
    signed_bundled_transactions: List[HexBytes],
    target_block_number: int,
    opts: Optional[FlashbotsOpts] = None,
) -> List[Any]:
    """Given a raw signed bundle, it packages it up with the block number and the timestamps"""
    # convert to hex
    return [
        {
            "txs": list(map(lambda x: self.to_hex(x), signed_bundled_transactions)),
            "blockNumber": hex(target_block_number),
            "minTimestamp": opts["minTimestamp"] if "minTimestamp" in opts else 0,
            "maxTimestamp": opts["maxTimestamp"] if "maxTimestamp" in opts else 0,
            "revertingTxHashes": opts["revertingTxHashes"]
            if "revertingTxHashes" in opts
            else [],
            "replacementUuid": opts["replacementUuid"]
            if "replacementUuid" in opts
            else None,
        }
    ]

None is not iterable, so the ternary assignment "minTimestamp": opts["minTimestamp"] if "minTimestamp" in opts else 0, throws the title exception.

The code works fine if opts={} is provided to send_bundle, so the fix is to set opts={} inside the munger if a value was not included.

Will submit a PR after doing a few tests.

Stuck reading the documentation example

In this example, I'm stuck here:

ETH_ACCOUNT_TO: LocalAccount = Account.from_key(os.environ.get("ETH_PRIVATE_TO"))

How could I possibly have the private key of the receiving address?

And also here, why should the ETH_ACCOUNT_TO sign the transaction and not, instead, the ETH_ACCOUNT_FROM?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.