Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 2d9448d

Browse files
authored
Merge pull request metachris#2 from metachris/relay-error
properly handle relay errors
2 parents c4fd592 + 7d2cc43 commit 2d9448d

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

examples/sendbundle/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ethereum/go-ethereum/crypto"
7+
"github.com/metachris/flashbotsrpc"
8+
)
9+
10+
var privateKey, _ = crypto.GenerateKey() // creating a new private key for testing. you probably want to use an existing key.
11+
12+
func main() {
13+
rpc := flashbotsrpc.New("https://relay.flashbots.net")
14+
rpc.Debug = true
15+
16+
sendBundleArgs := flashbotsrpc.FlashbotsSendBundleRequest{
17+
Txs: []string{"YOUR_HASH"},
18+
BlockNumber: fmt.Sprintf("0x%x", 13281018),
19+
}
20+
21+
result, err := rpc.FlashbotsSendBundle(privateKey, sendBundleArgs)
22+
if err != nil {
23+
fmt.Printf("%+v\n", err)
24+
return
25+
}
26+
27+
// Print result
28+
fmt.Printf("%+v\n", result)
29+
}

examples/userstats/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ var privateKey, _ = crypto.GenerateKey() // creating a new private key for testi
1212
func main() {
1313
rpc := flashbotsrpc.New("https://relay.flashbots.net")
1414

15-
// Query relay for user stats
16-
result, err := rpc.FlashbotsGetUserStats(privateKey, 13281018)
15+
sendBundleArgs := flashbotsrpc.FlashbotsSendBundleRequest{
16+
Txs: []string{"0x02f9019d011e843b9aca008477359400830247fa94def1c0ded9bec7f1a1670819833240f027b25eff88016345785d8a0000b90128d9627aa40000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000016345785d8a000000000000000000000000000000000000000000000000001394b63b2cbaea253a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f869584cd00000000000000000000000086003b044f70dac0abc80ac8957305b6370893ed0000000000000000000000000000000000000000000000d3443651ba615f6cd6c001a011a9f58ebe30aa679783b31793f897fdb603dd2ea086845723a22dae85ab2864a0090cf1fcce0f6e85da54f4eccf32a485d71a7d39bc0b43a53a9e64901c656230"},
17+
// BlockNumber: fmt.Sprintf("0x%x", blockNumber),
18+
BlockNumber: "13281018",
19+
}
20+
21+
result, err := rpc.FlashbotsSendBundle(privateKey, sendBundleArgs)
1722
if err != nil {
1823
panic(err)
1924
}

flashbotsrpc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"os"
1313
"time"
1414

15+
"github.com/pkg/errors"
16+
1517
"github.com/ethereum/go-ethereum/accounts"
1618
"github.com/ethereum/go-ethereum/common/hexutil"
1719
"github.com/ethereum/go-ethereum/core/types"
@@ -181,6 +183,13 @@ func (rpc *FlashbotsRPC) CallWithFlashbotsSignature(method string, privKey *ecds
181183
rpc.log.Println(fmt.Sprintf("%s\nRequest: %s\nSignature: %s\nResponse: %s\n", method, body, signature, data))
182184
}
183185

186+
// On error, response looks like this instead of JSON-RPC: {"error":"block param must be a hex int"}
187+
errorResp := new(RelayErrorResponse)
188+
if err := json.Unmarshal(data, errorResp); err == nil && errorResp.Error != "" {
189+
// relay returned an error
190+
return nil, errors.New(errorResp.Error)
191+
}
192+
184193
resp := new(rpcResponse)
185194
if err := json.Unmarshal(data, resp); err != nil {
186195
return nil, err

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/btcsuite/btcd v0.22.0-beta // indirect
77
github.com/ethereum/go-ethereum v1.10.8
88
github.com/jarcoal/httpmock v1.0.8
9+
github.com/pkg/errors v0.9.1
910
github.com/stretchr/testify v1.7.0
1011
github.com/tidwall/gjson v1.8.1
1112
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect

types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ func (proxy *proxyBlockWithoutTransactions) toBlock() Block {
321321
return block
322322
}
323323

324+
type RelayErrorResponse struct {
325+
Error string `json:"error"`
326+
}
327+
324328
type FlashbotsUserStats struct {
325329
IsHighPriority bool `json:"is_high_priority"`
326330
AllTimeMinerPayments string `json:"all_time_miner_payments"`

0 commit comments

Comments
 (0)