-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashbots.go
391 lines (339 loc) · 11.7 KB
/
flashbots.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package flashbots
import (
"bytes"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
type Bundle struct {
Transactions []*types.Transaction `json:"-"`
TxsByteString []string `json:"txs"` // TxsByteString is the same as Transactions, but in raw hex formatted strings
BlockNumber string `json:"blockNumber"` // BlockNumber is the earliest block number where this bundle will be valid (stored as hex string)
StateBlockNumber string `json:"stateBlockNumber,omitempty"` // StateBlockNumber must be provided if simulating bundle (stored as hex string)
MinTimestamp *int `json:"minTimestamp,omitempty"` // MinTimestamp in which the bundle will be valid, nil to allow any time
MaxTimestamp *int `json:"maxTimestamp,omitempty"` // MaxTimestamp in which the bundle will be valid, nil to allow any time
// RevertingTxHashes contain list of transaction hashes that are "allowed to revert" - bundle will still land on chain if these transactions revert
RevertingTxHashes []string `json:"revertingTxHashes,omitempty"`
}
// NewBundle creates a new bundle.
// blockNumber: block number for which this bundle is valid on
// stateBlockNumber: block number or a block tag for which state to base this simulation on. will be "latest" if 0
// minTimestamp: min timestamp that bundle will be valid, nil for any
// maxTimestamp: max timestamp that bundle will be valid, nil for any
// revertingTxHashes: list of transaction hashes that are "allowed to fail"
func NewBundle(transactions []*types.Transaction, blockNumber, stateBlockNumber uint64,
minTimestamp *int, maxTimestamp *int, revertingTxHashes []common.Hash) (b Bundle, retErr error) {
b.Transactions = transactions
b.TxsByteString = make([]string, len(b.Transactions))
for i, tx := range b.Transactions {
txBytes, err := tx.MarshalBinary()
if err != nil {
retErr = fmt.Errorf("failed tx.MarshalBinary() for tx: %s\nerrors: %w", tx.Hash().Hex(), err)
return
}
b.TxsByteString[i] = "0x" + common.Bytes2Hex(txBytes)
}
b.BlockNumber = "0x" + strconv.FormatUint(blockNumber, 16)
if stateBlockNumber == 0 {
b.StateBlockNumber = "latest"
} else {
b.StateBlockNumber = "0x" + strconv.FormatUint(stateBlockNumber, 16)
}
b.MinTimestamp = minTimestamp
b.MaxTimestamp = maxTimestamp
b.RevertingTxHashes = make([]string, len(revertingTxHashes))
for i, tx := range revertingTxHashes {
b.RevertingTxHashes[i] = tx.Hex()
}
return
}
// AddTransaction to existing bundle
func (b *Bundle) AddTransaction(tx *types.Transaction) {
b.Transactions = append(b.Transactions, tx)
}
type rpcPaylod struct {
JsonRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
ID int64 `json:"id"`
}
type RelayClient struct {
name string // name used to identify this RelayClient
signingPrivateKey *ecdsa.PrivateKey // signingPrivateKey signs bundles for flashbots
signingPublicAddress common.Address // signingPublicAddress is the public Ethereum address of signingPrivateKey
mainEndpoint string // mainEndpoint of the relay server, bundles are sent to this server
// simulationEndpoint is used for simulating bundles
// this is useful if you run a local version of mev-geth and don't want to wait for the slow public relays to respond
simulationEndpoint string
}
// NewRelayClient creates a new relay client
// signingPrivateKey: the key used to sign bundles (this can be any valid private key)
// mainEndpoint: the relay server endpoint used for sending bundles
// simulationEndpoint: the relay server endpoint used for bundle simulation
func NewRelayClient(signingPrivateKey *ecdsa.PrivateKey, name, mainEndpoint, simulationEndpoint string) (r *RelayClient, retErr error) {
if signingPrivateKey == nil {
retErr = errors.New("must provide a signingPrivateKey")
return
}
r = &RelayClient{
name: name,
signingPrivateKey: signingPrivateKey,
signingPublicAddress: crypto.PubkeyToAddress(signingPrivateKey.PublicKey),
mainEndpoint: mainEndpoint,
simulationEndpoint: simulationEndpoint,
}
return
}
func (r RelayClient) Name() string { return r.name }
func (r RelayClient) SigningAddress() common.Address { return r.signingPublicAddress }
func (r RelayClient) MainEndpoint() string { return r.mainEndpoint }
func (r RelayClient) SimulationEndpoint() string { return r.simulationEndpoint }
func (r *RelayClient) prepareBundlePayload(b Bundle, method string) (payloadBytes []byte, retErr error) {
payload := rpcPaylod{
JsonRPC: "2.0",
Method: method,
Params: []Bundle{b},
ID: 1,
}
payloadBytes, retErr = json.Marshal(payload)
return
}
// prepareBundleStatsPayload prepares a payload for requesting stats for a single bundle. The signign address must be
// the same as the one who submitted the bundle
func (r *RelayClient) prepareBundleStatsPayload(bundleHash, blockNumber, method string) (payloadBytes []byte, retErr error) {
payload := rpcPaylod{
JsonRPC: "2.0",
Method: method,
Params: []map[string]string{
{
"bundleHash": bundleHash,
"blockNumber": blockNumber,
},
},
ID: 1,
}
payloadBytes, retErr = json.Marshal(payload)
return
}
func (r *RelayClient) signPayload(payload []byte) (signature string, retErr error) {
hashedBody := crypto.Keccak256Hash(payload).Hex()
payloadHash := crypto.Keccak256([]byte("\x19Ethereum Signed Message:\n" + strconv.Itoa(len(hashedBody)) + hashedBody))
signatureBytes, err := crypto.Sign(payloadHash, r.signingPrivateKey)
if err != nil {
return "", err
}
return hexutil.Encode(signatureBytes), nil
}
func (r *RelayClient) fbRequest(endpoint string, payload []byte) (responseBytes []byte, duration time.Duration, retErr error) {
signature, err := r.signPayload(payload)
if err != nil {
retErr = err
return
}
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(payload))
if err != nil {
retErr = err
return
}
req.Header.Add("X-Flashbots-Signature", r.signingPublicAddress.Hex()+":"+signature)
req.Header.Set("Content-Type", "application/json")
start := time.Now()
resp, err := http.DefaultClient.Do(req)
duration = time.Since(start)
if err != nil {
retErr = err
return
}
responseBytes, err = ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
retErr = err
return
}
return
}
func unmarshalBodyBytesToGenericMap(bytes []byte) (response map[string]interface{}, retErr error) {
err := json.Unmarshal(bytes, &response)
if err != nil {
retErr = fmt.Errorf("failed to unmarshal body to a generic map: %w", err)
return
}
return
}
type SendBundleResponse struct {
ResponseBytes []byte
Duration time.Duration
Error error
}
// SendBundle sends a Bundle on RelayClient.
func (r *RelayClient) SendBundle(b Bundle) (resp SendBundleResponse) {
payload, err := r.prepareBundlePayload(b, "eth_sendBundle")
if err != nil {
return SendBundleResponse{
Error: err,
}
}
responseBytes, duration, err := r.fbRequest(r.mainEndpoint, payload)
return SendBundleResponse{
ResponseBytes: responseBytes,
Duration: duration,
Error: err,
}
}
func (r *RelayClient) SimulateBundle(b Bundle) (responseBytes []byte, duration time.Duration, retErr error) {
if r.simulationEndpoint == "" {
retErr = errors.New("no simulation endpoint for relay " + r.name)
return
}
if b.StateBlockNumber == "" || b.StateBlockNumber == "0x0" {
b.StateBlockNumber = "latest"
}
payload, err := r.prepareBundlePayload(b, "eth_callBundle")
if err != nil {
retErr = err
return
}
return r.fbRequest(r.simulationEndpoint, payload)
}
type BundleStats struct {
ID int `json:"id"`
JsonRPC string `json:"jsonrpc"`
Result struct {
IsHighPriority bool `json:"isHighPriority"`
IsSentToMiners bool `json:"isSentToMiners"`
IsSimulated bool `json:"isSimulated"`
SentToMinersAt time.Time `json:"sentToMinersAt"`
SimulatedAt time.Time `json:"simulatedAt"`
SubmittedAt time.Time `json:"submittedAt"`
} `json:"result"`
}
// GetBundleStats queries flashbots_getBundleStats for stats on a single bundle. BundleHash and blockNumber must be a hexadecimal strings
func (r *RelayClient) GetBundleStats(bundleHash, blockNumber string) (bundleStats BundleStats, duration time.Duration, retErr error) {
payload, err := r.prepareBundleStatsPayload(bundleHash, blockNumber, "flashbots_getBundleStats")
if err != nil {
retErr = err
return
}
var bodyBytes []byte
bodyBytes, duration, err = r.fbRequest(r.MainEndpoint(), payload)
if err != nil {
retErr = fmt.Errorf("failed to make fbRequest: %w", err)
return
}
err = json.Unmarshal(bodyBytes, &bundleStats)
if err != nil {
retErr = fmt.Errorf("failed to unmarshal into BundleStats: %s\nerror: %w", string(bodyBytes), err)
return
}
return
}
func ExtractGasUsedFromBundleResponse(bytes []byte) (gasUsed float64) {
resp, err := unmarshalBodyBytesToGenericMap(bytes)
if err != nil {
panic(fmt.Errorf("body bytes: %s, %w", string(bytes), err))
}
gasUsed = resp["result"].(map[string]interface{})["totalGasUsed"].(float64)
return
}
func ExtractBundleHashFromBundleResponse(bytes []byte) (bundleHash string) {
resp, err := unmarshalBodyBytesToGenericMap(bytes)
if err != nil {
panic(fmt.Errorf("body bytes: %s, %w", string(bytes), err))
}
bundleHash = resp["result"].(map[string]interface{})["bundleHash"].(string)
return
}
func ExtractExecutionErrorFromSendBundleResponse(bytes []byte) (errs []error) {
errs = make([]error, 0)
resp, err := unmarshalBodyBytesToGenericMap(bytes)
if err != nil {
errs = append(
errs,
fmt.Errorf("failed to unmarshal response: %s, %w", string(bytes), err))
return
}
var resultOk bool
var txResultsI map[string]interface{}
txResultsI, resultOk = resp["result"].(map[string]interface{})
if !resultOk {
errResponse, errorOk := resp["error"]
if errorOk {
errs = append(
errs,
fmt.Errorf("%s", errResponse))
return
}
errs = append(
errs,
errors.New(txResultsI["message"].(string)))
return
}
txResults, ok := txResultsI["results"].([]interface{})
if !ok {
return
}
for _, txI := range txResults {
tx := txI.(map[string]interface{})
errString, errOk := tx["error"]
revertString, revertOk := tx["revert"]
if errOk || revertOk {
errs = append(
errs,
fmt.Errorf("err: %s, revertString: %s", errString, revertString))
}
}
return
}
type BatchRelayClient struct {
relayClients []*RelayClient
}
func NewBatchRelayClient(
signingKeys []*ecdsa.PrivateKey,
names, mainEndpoints []string,
) (b *BatchRelayClient, retErr error) {
if (len(signingKeys) != len(names)) || (len(signingKeys) != len(mainEndpoints)) {
retErr = errors.New("must initialize with same length slices")
return
}
r := make([]*RelayClient, 0)
for idx := range signingKeys {
c, err := NewRelayClient(
signingKeys[idx],
names[idx],
mainEndpoints[idx],
"",
)
if err != nil {
retErr = fmt.Errorf(
"failed to initialize relay client, name: %s, endpoint: %s, error: %w",
names[idx],
mainEndpoints[idx],
err,
)
return
}
r = append(r, c)
}
b = &BatchRelayClient{
relayClients: r,
}
return
}
// BatchSendBundle sends a Bundle on all connected relay clients
func (r *BatchRelayClient) BatchSendBundle(b Bundle) (resps map[string]SendBundleResponse) {
resps = make(map[string]SendBundleResponse)
for _, client := range r.relayClients {
resp := client.SendBundle(b)
resps[client.Name()] = resp
}
return
}