flashbots_client

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: MIT Imports: 21 Imported by: 0

README

Flashbots Client (WIP)

This project is a work in progress. The goal is to provide a high-level wrapper for interacting with Flashbots, abstracting away most of the complexity involved.

Overview

Flashbots Client aims to simplify the process of sending and simulating bundles on the Flashbots network. It provides a set of easy-to-use functions to interact with Flashbots, making it accessible for developers to integrate Flashbots into their applications.

Note: This project targets similar functionality as the ethers-provider-flashbots-bundle TypeScript package, but for the Go programming language.

Features

  • Send bundles to Flashbots
  • Simulate bundle execution
  • Wait for bundle inclusion
  • Update fee refund recipient
  • Duplicate bundles
  • Get bundle statistics
  • Cancel bundles
  • To be extended...

Installation

To install the Flashbots Client, use the following command:

go get github.com/yourusername/flashbots_client

Usage

Creating a Client

To create a new Flashbots client, you need to provide an Ethereum RPC URL and a searcher secret (ECDSA private key):

package main

import (
    "log"
    "github.com/yourusername/flashbots_client"
    "github.com/ethereum/go-ethereum/crypto"
)

func main() {
    ethRpcUrl := "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
    searcherSecret, err := crypto.HexToECDSA("YOUR_PRIVATE_KEY")
    if err != nil {
        log.Fatalf("Failed to create private key: %v", err)
    }

    client, err := flashbots_client.NewClient(ethRpcUrl, searcherSecret)
    if err != nil {
        log.Fatalf("Failed to create Flashbots client: %v", err)
    }

    // Use the client...
}
Sending a Bundle

To send a bundle, create a Bundle object and use the SendBundle method:

bundle := flashbots_client.NewBundle()
// Add transactions to the bundle
// bundle.AddTransaction(tx)

bundleHash, err := client.SendBundle(bundle)
if err != nil {
    log.Fatalf("Failed to send bundle: %v", err)
}

log.Printf("Bundle sent with hash: %s", bundleHash.Hex())
Simulating a Bundle

To simulate a bundle, use the SimulateBundle method:

simulationResult, success, err := client.SimulateBundle(bundle, 0)
if err != nil {
    log.Fatalf("Failed to simulate bundle: %v", err)
}

if success {
    log.Println("Simulation successful")
} else {
    log.Println("Simulation failed")
}
Getting Bundle Statistics

To get statistics for a bundle, use the GetBundleStatsV2 method:

stats, err := client.GetBundleStatsV2(bundle)
if err != nil {
    log.Fatalf("Failed to get bundle stats: %v", err)
}

log.Printf("Bundle stats: %+v", stats)
Canceling a Bundle

To cancel a bundle, use the CancelBundle method:

err := client.CancelBundle(bundle.ReplacementUuid())
if err != nil {
    log.Fatalf("Failed to cancel bundle: %v", err)
}

log.Println("Bundle canceled successfully")

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is open source and available under the MIT License.

Documentation

Index

Constants

View Source
const (
	JsonRpcParseError     = -32700
	JsonRpcInvalidRequest = -32600
	JsonRpcMethodNotFound = -32601
	JsonRpcInvalidParams  = -32602
	JsonRpcInternalError  = -32603
)

Variables

View Source
var AllBuilders = []string{
	"flashbots",
	"f1b.io",
	"rsync",
	"beaverbuild.org",
	"builder0x69",
	"Titan",
	"EigenPhi",
	"boba-builder",
	"Gambit Labs",
	"payload",
	"Loki",
	"BuildAI",
	"JetBuilder",
	"tbuilder",
	"penguinbuild",
	"bobthebuilder",
	"BTCS",
	"bloXroute",
	"Blockbeelder",
}
View Source
var FlashbotsUrlPerNetwork = map[uint64]string{
	1:        "https://relay.flashbots.net",
	5:        "https://relay-goerli.flashbots.net",
	11155111: "https://relay-sepolia.flashbots.net",
	17000:    "https://relay-holesky.flashbots.net",
}

Functions

This section is empty.

Types

type Bundle

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

func NewBundle

func NewBundle() *Bundle

func NewBundleWithTransactions

func NewBundleWithTransactions(transactions []*types.Transaction) *Bundle

func (*Bundle) AddTransaction

func (b *Bundle) AddTransaction(tx *types.Transaction)

func (*Bundle) AddTransactions

func (b *Bundle) AddTransactions(txs []*types.Transaction)

func (*Bundle) Builders

func (b *Bundle) Builders() []string

func (*Bundle) BundleHash

func (b *Bundle) BundleHash() common.Hash

func (*Bundle) Copy added in v0.2.0

func (b *Bundle) Copy() *Bundle

func (*Bundle) GetBundelsForNextNBlocks added in v0.2.0

func (b *Bundle) GetBundelsForNextNBlocks(n uint64) ([]*Bundle, error)

func (*Bundle) MaxTimestamp

func (b *Bundle) MaxTimestamp() int64

func (*Bundle) MaximumGasFeePaid

func (b *Bundle) MaximumGasFeePaid() (feePaid *big.Int)

func (*Bundle) MinTimestamp

func (b *Bundle) MinTimestamp() int64

func (*Bundle) ReplacementUuid

func (b *Bundle) ReplacementUuid() string

func (*Bundle) RevertingTxHashes

func (b *Bundle) RevertingTxHashes() []string

func (*Bundle) SetMaxTimestamp

func (b *Bundle) SetMaxTimestamp(maxTimestamp int64) error

SetMaxTimestamp sets the maximum timestamp for which this bundle is valid, in seconds since the unix epoch

func (*Bundle) SetMinTimestamp

func (b *Bundle) SetMinTimestamp(minTimestamp int64) error

SetMinTimestamp sets the minimum timestamp for which this bundle is valid, in seconds since the unix epoch

func (*Bundle) SetReplacementUuid

func (b *Bundle) SetReplacementUuid(replacementUuid string) error

SetReplacementUuid sets the replacement UUID for this bundle that can be used to cancel/replace this bundle

func (*Bundle) SetRevertingTxHash

func (b *Bundle) SetRevertingTxHash(revertingTxHash string)

SetRevertingTxHash sets one transaction hash that is allowed to revert

func (*Bundle) SetRevertingTxHashes

func (b *Bundle) SetRevertingTxHashes(revertingTxHashes []string)

SetRevertingTxHashes sets the list of transaction hashes that are allowed to revert

func (*Bundle) SetTargetBlockNumber added in v0.2.0

func (b *Bundle) SetTargetBlockNumber(blocknumber uint64) error

SetBlockNumber sets the block number for which this bundle is valid If set to 0, the bundle is valid for the next block

func (*Bundle) TargetBlockNumber added in v0.2.0

func (b *Bundle) TargetBlockNumber() uint64

func (*Bundle) Transactions

func (b *Bundle) Transactions() []*types.Transaction

func (*Bundle) UseAllBuilders added in v0.1.1

func (b *Bundle) UseAllBuilders(networkId uint64)

type BundleStats added in v0.3.0

type BundleStats struct {
	IsHighPriority       bool   `json:"isHighPriority"`
	IsSimulated          bool   `json:"isSimulated"`
	SimulatedAt          string `json:"simulatedAt"`
	ReceivedAt           string `json:"receivedAt"`
	ConsideredByBuilders []struct {
		Pubkey    string `json:"pubkey"`
		Timestamp string `json:"timestamp"`
	} `json:"consideredByBuildersAt"`
	SealedByBuilders []struct {
		Pubkey    string `json:"pubkey"`
		Timestamp string `json:"timestamp"`
	} `json:"sealedByBuildersAt"`
}

type BundleStatsV2 added in v0.2.0

type BundleStatsV2 struct {
	IsHighPriority       bool   `json:"isHighPriority"`
	IsSimulated          bool   `json:"isSimulated"`
	SimulatedAt          string `json:"simulatedAt"`
	ReceivedAt           string `json:"receivedAt"`
	ConsideredByBuilders []struct {
		Pubkey    string `json:"pubkey"`
		Timestamp string `json:"timestamp"`
	} `json:"consideredByBuildersAt"`
	SealedByBuilders []struct {
		Pubkey    string `json:"pubkey"`
		Timestamp string `json:"timestamp"`
	} `json:"sealedByBuildersAt"`
}

type ErrorType

type ErrorType struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

type FlasbotsNetork

type FlasbotsNetork string

type FlashbotsClient

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

func NewClient

func NewClient(logger *slog.Logger, ethRpc *ethclient.Client, searcherSecret *ecdsa.PrivateKey) (*FlashbotsClient, error)

func NewClientRpcString added in v0.2.0

func NewClientRpcString(logger *slog.Logger, ethRpcStr string, searcherSecret *ecdsa.PrivateKey) (*FlashbotsClient, error)

func (*FlashbotsClient) Call

func (client *FlashbotsClient) Call(method string, params ...interface{}) (json.RawMessage, error)

func (*FlashbotsClient) CallWithAditionalHeaders

func (client *FlashbotsClient) CallWithAditionalHeaders(method string, headers map[string]string, params ...interface{}) (json.RawMessage, error)

func (*FlashbotsClient) CancelBundle added in v0.2.0

func (client *FlashbotsClient) CancelBundle(uuid string) error

func (*FlashbotsClient) CheckBundleIncusion added in v0.2.0

func (client *FlashbotsClient) CheckBundleIncusion(ctx context.Context, bundle *Bundle) (bool, error)

func (*FlashbotsClient) GetBundleStats added in v0.3.0

func (client *FlashbotsClient) GetBundleStats(bundle *Bundle) (*BundleStats, error)

func (*FlashbotsClient) GetBundleStatsV2 added in v0.2.0

func (client *FlashbotsClient) GetBundleStatsV2(bundle *Bundle) (*BundleStatsV2, error)

func (*FlashbotsClient) GetSbundleStats added in v0.3.0

func (client *FlashbotsClient) GetSbundleStats(bundle *Bundle) (*BundleStats, error)

func (*FlashbotsClient) SendBundle

func (client *FlashbotsClient) SendBundle(bundle *Bundle) (common.Hash, bool, error)

func (*FlashbotsClient) SendBundleAndWait added in v0.3.0

func (client *FlashbotsClient) SendBundleAndWait(ctx context.Context, bundle *Bundle) (bool, error)

func (*FlashbotsClient) SendBundleNTimes added in v0.3.0

func (client *FlashbotsClient) SendBundleNTimes(originalBundle *Bundle, n uint64) (bundlesToSend []*Bundle, hash common.Hash, smart bool, err error)

func (*FlashbotsClient) SendNBundleAndWait added in v0.3.0

func (client *FlashbotsClient) SendNBundleAndWait(ctx context.Context, bundle *Bundle, n uint64) (bool, error)

func (*FlashbotsClient) SimulateBundle

func (client *FlashbotsClient) SimulateBundle(bundle *Bundle, stateBlocknumber uint64) (*SimulationResultBundle, bool, error)

SimulateBundle simulates the execution of a bundle The stateBlocknumber parameter is the block number at which the simulation should start, 0 for the current block

func (*FlashbotsClient) UpdateFeeRefundRecipient

func (client *FlashbotsClient) UpdateFeeRefundRecipient(newFeeRefundRecipient common.Address) error

func (*FlashbotsClient) WaitForBundleInclusion

func (client *FlashbotsClient) WaitForBundleInclusion(ctx context.Context, bundle *Bundle) (bool, error)

WaitForBundleInclusion waits for a bundle to be included in a block

type RawSimulationResultBundle

type RawSimulationResultBundle struct {
	BundleGasPrice    string
	BundleHash        string
	CoinbaseDiff      string
	EthSentToCoinbase string
	GasFees           string
	Results           []RawSimulationResultTransaction
	StateBlockNumber  uint64
	TotalGasUsed      uint64
	FirstRevert       string
}

type RawSimulationResultTransaction

type RawSimulationResultTransaction struct {
	CoinbaseDiff      string `json:"coinbaseDiff"`
	EthSentToCoinbase string `json:"ethSentToCoinbase"`
	FromAddress       string `json:"fromAddress"`
	GasFees           string `json:"gasFees"`
	GasPrice          string `json:"gasPrice"`
	GasUsed           uint64 `json:"gasUsed"`
	ToAddress         string `json:"toAddress"`
	TxHash            string `json:"txHash"`
	Value             string `json:"value"`

	// revert related
	Error        string `json:"error"`
	RevertReason string `json:"revert"`
}

type Request

type Request struct {
	ID      int           `json:"id"`
	JSONRPC string        `json:"jsonrpc"`
	Method  string        `json:"method"`
	Params  []interface{} `json:"params"`
}

type Response

type Response struct {
	ID      int             `json:"id"`
	JSONRPC string          `json:"jsonrpc"`
	Result  json.RawMessage `json:"result"`
	Error   ErrorType       `json:"error"`
}

type SimulationResultBundle

type SimulationResultBundle struct {
	BundleGasPrice    *big.Int
	BundleHash        common.Hash
	CoinbaseDiff      *big.Int
	EthSentToCoinbase *big.Int
	GasFees           *big.Int
	Results           []SimulationResultTransaction
	StateBlockNumber  uint64
	TotalGasUsed      uint64
	FirstRevert       common.Hash
}

type SimulationResultTransaction

type SimulationResultTransaction struct {
	CoinbaseDiff      *big.Int       `json:"coinbaseDiff"`
	EthSentToCoinbase *big.Int       `json:"ethSentToCoinbase"`
	FromAddress       common.Address `json:"fromAddress"`
	GasFees           *big.Int       `json:"gasFees"`
	GasPrice          *big.Int       `json:"gasPrice"`
	GasUsed           uint64         `json:"gasUsed"`
	ToAddress         common.Address `json:"toAddress"`
	TxHash            common.Hash    `json:"txHash"`
	Value             *big.Int       `json:"value"`

	// revert related
	Error        string `json:"error"`
	RevertReason string `json:"revert"`
}

Jump to

Keyboard shortcuts

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