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

Commit 86a488b

Browse files
committed
allow custom headers
1 parent 8e1c35b commit 86a488b

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

flashbotsrpc.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,22 @@ type rpcRequest struct {
4444

4545
// FlashbotsRPC - Ethereum rpc client
4646
type FlashbotsRPC struct {
47-
url string
48-
client httpClient
49-
log logger
50-
Debug bool
47+
url string
48+
client httpClient
49+
log logger
50+
Debug bool
51+
Headers map[string]string // Additional headers to send with the request
52+
Timeout time.Duration
5153
}
5254

5355
// New create new rpc client with given url
5456
func New(url string, options ...func(rpc *FlashbotsRPC)) *FlashbotsRPC {
5557
rpc := &FlashbotsRPC{
56-
url: url,
57-
client: http.DefaultClient,
58-
log: log.New(os.Stderr, "", log.LstdFlags),
58+
url: url,
59+
client: http.DefaultClient,
60+
log: log.New(os.Stderr, "", log.LstdFlags),
61+
Headers: make(map[string]string),
62+
Timeout: 30 * time.Second,
5963
}
6064
for _, option := range options {
6165
option(rpc)
@@ -101,7 +105,21 @@ func (rpc *FlashbotsRPC) Call(method string, params ...interface{}) (json.RawMes
101105
return nil, err
102106
}
103107

104-
response, err := rpc.client.Post(rpc.url, "application/json", bytes.NewBuffer(body))
108+
req, err := http.NewRequest("POST", rpc.url, bytes.NewBuffer(body))
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
req.Header.Add("Content-Type", "application/json")
114+
req.Header.Add("Accept", "application/json")
115+
for k, v := range rpc.Headers {
116+
req.Header.Add(k, v)
117+
}
118+
httpClient := &http.Client{
119+
Timeout: rpc.Timeout,
120+
}
121+
122+
response, err := httpClient.Do(req)
105123
if response != nil {
106124
defer response.Body.Close()
107125
}
@@ -160,8 +178,11 @@ func (rpc *FlashbotsRPC) CallWithFlashbotsSignature(method string, privKey *ecds
160178
req.Header.Add("Content-Type", "application/json")
161179
req.Header.Add("Accept", "application/json")
162180
req.Header.Add("X-Flashbots-Signature", signature)
181+
for k, v := range rpc.Headers {
182+
req.Header.Add(k, v)
183+
}
163184
httpClient := &http.Client{
164-
Timeout: time.Second * 30,
185+
Timeout: rpc.Timeout,
165186
}
166187

167188
response, err := httpClient.Do(req)

0 commit comments

Comments
 (0)