flashbots

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2022 License: MIT Imports: 16 Imported by: 0

README

flashbots ⚡🤖

Go Reference

Package flashbots implements RPC API bindings for the Flashbots relay and mev-geth for use with the w3 package.

Install

go get github.com/lmittmann/flashbots

Getting Started

Connect to the Flashbots relay and send a bundle.

// Private key for request authentication
var privKey *ecdsa.PrivateKey

// Connect to relay
rpcClient, err := rpc.DialHTTPWithClient(
	"https://relay.flashbots.net",
	&http.Client{
		Transport: flashbots.AuthTransport(privKey),
	},
)
if err != nil {
	fmt.Printf("Failed to connect to Flashbots relay: %v\n", err)
	return
}

client := w3.NewClient(rpcClient)
defer client.Close()

// Send bundle
var (
    // list of signed transactions
	bundle types.Transactions

	bundleHash common.Hash
)
err = client.Call(
	flashbots.SendBundle(&flashbots.SendBundleParam{
		Transactions: bundle,
		BlockNumber:  big.NewInt(999_999_999),
	}).Returns(&bundleHash),
)
if err != nil {
	fmt.Printf("Failed to send bundle: %v\n", err)
	return
}
fmt.Printf("Sent bundle successfully: %s\n", bundleHash)

Note that the Flashbots relay does not support batch requests. Thus, sending more than one request in Client.Call will result in a server error.

RPC Methods

List of supported RPC methods:

Method Go Code
eth_sendBundle flashbots.SendBundle(param *flashbots.SendBundleParam).Returns(bundleHash *common.Hash)
eth_callBundle TODO
flashbots_getUserStats flashbots.UserStats(blockNumber *big.Int).Returns(resp *flashbots.UserStatsResponse)
flashbots_getBundleStats flashbots.BundleStats(bundleHash common.Hash, blockNumber *big.Int).Returns(resp *flashbots.BundleStatsResponse)

Documentation

Overview

Package flashbots implements RPC API bindings for the Flashbots relay and mev-geth for use with the w3 package (github.com/lmittmann/w3).

Example
package main

import (
	"crypto/ecdsa"
	"fmt"
	"math/big"
	"net/http"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/rpc"
	"github.com/lmittmann/flashbots"
	"github.com/lmittmann/w3"
)

func main() {
	// Private key for request authentication
	var privKey *ecdsa.PrivateKey

	// Connect to relay
	rpcClient, err := rpc.DialHTTPWithClient(
		"https://relay.flashbots.net",
		&http.Client{
			Transport: flashbots.AuthTransport(privKey),
		},
	)
	if err != nil {
		fmt.Printf("Failed to connect to Flashbots relay: %v\n", err)
		return
	}

	client := w3.NewClient(rpcClient)
	defer client.Close()

	// Send bundle
	var (
		bundle types.Transactions // list of signed transactions

		bundleHash common.Hash
	)
	err = client.Call(
		flashbots.SendBundle(&flashbots.SendBundleParam{
			Transactions: bundle,
			BlockNumber:  big.NewInt(999_999_999),
		}).Returns(&bundleHash),
	)
	if err != nil {
		fmt.Printf("Failed to send bundle to Flashbots relay: %v\n", err)
		return
	}
	fmt.Printf("Sent bundle successfully: %s\n", bundleHash)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthTransport

func AuthTransport(privKey *ecdsa.PrivateKey) http.RoundTripper

AuthTransport returns a http.RoundTripper that adds the 'X-Flashbots-Signature' header to every request.

Types

type BundleStatsFactory

type BundleStatsFactory struct {
	// contains filtered or unexported fields
}

func BundleStats

func BundleStats(bundleHash common.Hash, blockNumber *big.Int) *BundleStatsFactory

BundleStats requests the bundles Flashbots relay stats. The given block number must be within 20 blocks of the current chain tip.

func (*BundleStatsFactory) CreateRequest

func (f *BundleStatsFactory) CreateRequest() (rpc.BatchElem, error)

CreateRequest implements the w3/core.RequestCreator interface.

func (*BundleStatsFactory) HandleResponse

func (f *BundleStatsFactory) HandleResponse(elem rpc.BatchElem) error

HandleResponse implements the w3/core.ResponseHandler interface.

func (*BundleStatsFactory) Returns

func (f *BundleStatsFactory) Returns(bundleStats *BundleStatsResponse) *BundleStatsFactory

type BundleStatsResponse

type BundleStatsResponse struct {
	IsSimulated    bool      `json:"isSimulated"`
	IsSentToMiners bool      `json:"isSentToMiners"`
	IsHighPriority bool      `json:"isHighPriority"`
	SimulatedAt    time.Time `json:"simulatedAt"`
	SubmittedAt    time.Time `json:"submittedAt"`
	SentToMinersAt time.Time `json:"sentToMinersAt"`
}

type SendBundleFactory

type SendBundleFactory struct {
	// contains filtered or unexported fields
}

func SendBundle

func SendBundle(param *SendBundleParam) *SendBundleFactory

SendBundle sends a bundle to the network.

func (*SendBundleFactory) CreateRequest

func (f *SendBundleFactory) CreateRequest() (rpc.BatchElem, error)

CreateRequest implements the w3/core.RequestCreator interface.

func (*SendBundleFactory) HandleResponse

func (f *SendBundleFactory) HandleResponse(elem rpc.BatchElem) error

HandleResponse implements the w3/core.ResponseHandler interface.

func (*SendBundleFactory) Returns

func (f *SendBundleFactory) Returns(hash *common.Hash) *SendBundleFactory

type SendBundleParam

type SendBundleParam struct {
	Transactions      types.Transactions `json:"-"`                               // List of signed transactions to execute in a bundle.
	RawTransactions   [][]byte           `json:"txs"         gencodec:"required"` // List of signed raw transactions to execute in a bundle.
	BlockNumber       *big.Int           `json:"blockNumber" gencodec:"required"` // Block number for which the bundle is valid
	MinTimestamp      *big.Int           `json:"minTimestamp,omitempty"`          // Minimum Unix Timestamp for which the bundle is valid
	MaxTimestamp      *big.Int           `json:"maxTimestamp,omitempty"`          // Maximum Unix Timestamp for which the bundle is valid
	RevertingTxHashes []common.Hash      `json:"revertingTxHashes,omitempty"`     // List of tx hashes in bundle that are allowed to revert.
}

func (SendBundleParam) MarshalJSON

func (s SendBundleParam) MarshalJSON() ([]byte, error)

MarshalJSON marshals as JSON.

func (*SendBundleParam) UnmarshalJSON

func (s *SendBundleParam) UnmarshalJSON(input []byte) error

UnmarshalJSON unmarshals from JSON.

type UserStatsFactory

type UserStatsFactory struct {
	// contains filtered or unexported fields
}

func UserStats

func UserStats(blockNumber *big.Int) *UserStatsFactory

UserStats requests the users Flashbots realy stats. The given block number must be within 20 blocks of the current chain tip.

func (*UserStatsFactory) CreateRequest

func (f *UserStatsFactory) CreateRequest() (rpc.BatchElem, error)

CreateRequest implements the w3/core.RequestCreator interface.

func (*UserStatsFactory) HandleResponse

func (f *UserStatsFactory) HandleResponse(elem rpc.BatchElem) error

HandleResponse implements the w3/core.ResponseHandler interface.

func (*UserStatsFactory) Returns

func (f *UserStatsFactory) Returns(userStats *UserStatsResponse) *UserStatsFactory

type UserStatsResponse

type UserStatsResponse struct {
	IsHighPriority       bool     `json:"is_high_priority"`
	AllTimeMinerPayments *big.Int `json:"all_time_miner_payments"`
	AllTimeGasSimulated  *big.Int `json:"all_time_gas_simulated"`
	Last7dMinerPayments  *big.Int `json:"last_7d_miner_payments"`
	Last7dGasSimulated   *big.Int `json:"last_7d_gas_simulated"`
	Last1dMinerPayments  *big.Int `json:"last_1d_miner_payments"`
	Last1dGasSimulated   *big.Int `json:"last_1d_gas_simulated"`
}

func (UserStatsResponse) MarshalJSON

func (u UserStatsResponse) MarshalJSON() ([]byte, error)

MarshalJSON marshals as JSON.

func (*UserStatsResponse) UnmarshalJSON

func (u *UserStatsResponse) UnmarshalJSON(input []byte) error

UnmarshalJSON unmarshals from JSON.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL