Skip to content

Commit

Permalink
cleanup, rename from flashbots-rpc to flashbotsrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Sep 23, 2021
1 parent bef7b06 commit 3fba074
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 14 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
Fork of [ethrpc](https://github.com/onrik/ethrpc) with Flashbots RPC calls.
# Flashbots RPC client

* [Flashbots RPC documentation](https://docs.flashbots.net/flashbots-auction/searchers/advanced/rpc-endpoint)
Fork of [ethrpc](https://github.com/onrik/ethrpc) with additional [Flashbots RPC methods](https://docs.flashbots.net/flashbots-auction/searchers/advanced/rpc-endpoint):

* `FlashbotsGetUserStats`
* `FlashbotsCallBundle`
* `FlashbotsSendBundle`
* `FlashbotsSimulateBlock`: simulate a full block

## Usage

```go
rpc := flashbotsrpc.NewFlashbotsRPC("https://relay.flashbots.net")

// Creating a new private key here for testing; you probably would want to use an existing one
privateKey, _ := crypto.GenerateKey()

// Query relay for user stats
result, err := rpc.FlashbotsGetUserStats(privateKey, 13281018)
if err != nil {
log.Fatal(err)
}

// Print result
fmt.Printf("%+v\n", result)
```

You can find [more examples in `/examples/`](https://github.com/metachris/flashbotsrpc/tree/master/examples).
72 changes: 72 additions & 0 deletions examples/simulateblock/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"math/big"
"os"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/metachris/flashbotsrpc"
)

func main() {
mevGethUriPtr := flag.String("mevgeth", os.Getenv("MEVGETH_NODE"), "mev-geth node URI")
blockHash := flag.String("blockhash", "", "hash of block to simulate")
blockNumber := flag.Int64("blocknumber", -1, "number of block to simulate")
debugPtr := flag.Bool("debug", false, "print debug information")
flag.Parse()

if *mevGethUriPtr == "" {
log.Fatal("No mev geth URI provided")
}

if *blockHash == "" && *blockNumber == -1 {
log.Fatal("Either block number or hash is needed")
}

client, err := ethclient.Dial(*mevGethUriPtr)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to", *mevGethUriPtr)

var block *types.Block
if *blockHash != "" {
hash := common.HexToHash(*blockHash)
block, err = client.BlockByHash(context.Background(), hash)
if err != nil {
log.Fatal(err)
}

} else {
block, err = client.BlockByNumber(context.Background(), big.NewInt(*blockNumber))
if err != nil {
log.Fatal(err)
}
}

t := time.Unix(int64(block.Header().Time), 0).UTC()
fmt.Printf("Block %d %s \t %s \t tx=%-4d \t gas=%d \t uncles=%d\n", block.NumberU64(), block.Hash(), t, len(block.Transactions()), block.GasUsed(), len(block.Uncles()))

if len(block.Transactions()) == 0 {
fmt.Println("No transactions in this block")
return
}

rpc := flashbotsrpc.NewFlashbotsRPC(*mevGethUriPtr)
rpc.Debug = *debugPtr

privateKey, _ := crypto.GenerateKey()
result, err := rpc.FlashbotsSimulateBlock(privateKey, block, 0)
if err != nil {
log.Fatal(err)
}
fmt.Println("CoinbaseDiff:", result.CoinbaseDiff)
}
23 changes: 23 additions & 0 deletions examples/userstats/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"

"github.com/ethereum/go-ethereum/crypto"
"github.com/metachris/flashbotsrpc"
)

var privateKey, _ = crypto.GenerateKey() // creating a new private key for testing. you probably want to use an existing key.

func main() {
rpc := flashbotsrpc.NewFlashbotsRPC("https://relay.flashbots.net")

// Query relay for user stats
result, err := rpc.FlashbotsGetUserStats(privateKey, 13281018)
if err != nil {
panic(err)
}

// Print result
fmt.Printf("%+v\n", result)
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/metachris/flashbots-rpc
module github.com/metachris/flashbotsrpc

go 1.16

require (
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/ethereum/go-ethereum v1.10.7
github.com/ethereum/go-ethereum v1.10.8
github.com/jarcoal/httpmock v1.0.8
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.8.1
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 // indirect
)
Loading

0 comments on commit 3fba074

Please sign in to comment.