-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundles.go
137 lines (116 loc) · 3.3 KB
/
bundles.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
package bundles
import (
"bytes"
"crypto/ecdsa"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
// New create new rpc client with given url
func NewRPC(url string) *RPC {
rpc := &RPC{
url: url,
Headers: make(map[string]string),
Timeout: 30 * time.Second,
}
rpc.client = &http.Client{
Timeout: rpc.Timeout,
}
return rpc
}
func (rpc *RPC) newRPCRequest(method string, params ...interface{}) rpcRequest {
return rpcRequest{
ID: 1,
JSONRPC: "2.0",
Method: method,
Params: params,
}
}
func (rpc *RPC) buildSignature(method string, privKey *ecdsa.PrivateKey, params ...interface{}) error {
request := rpc.newRPCRequest(method, params...)
body, err := json.Marshal(request)
if err != nil {
return err
}
hashedBody := crypto.Keccak256Hash([]byte(body)).Hex()
sig, err := crypto.Sign(accounts.TextHash([]byte(hashedBody)), privKey)
if err != nil {
return err
}
rpc.Signature = crypto.PubkeyToAddress(privKey.PublicKey).Hex() + ":" + hexutil.Encode(sig)
rpc.RequestBody = body
return nil
}
func (rpc *RPC) buildRPCRequest() error {
req, err := http.NewRequest("POST", rpc.url, bytes.NewBuffer(rpc.RequestBody))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("X-Auction-Signature", rpc.Signature) //sould be X-Flashbots-Signature for other builders like Flashbots
req.Header.Add("X-Flashbots-Signature", rpc.Signature)
for k, v := range rpc.Headers {
req.Header.Add(k, v)
}
rpc.Request = req
return nil
}
func (rpc *RPC) SendBundle(privKey *ecdsa.PrivateKey, params ...interface{}) (*SendBundleResponse, error) {
response, err := rpc.makeRpcCall("eth_sendBundle", privKey, params...)
if err != nil {
return nil, err
}
var sbResponse = new(SendBundleResponse)
err = json.Unmarshal(response.Result, &sbResponse)
return sbResponse, err
}
func (rpc *RPC) CallBundle(privKey *ecdsa.PrivateKey, params ...interface{}) (*CallBundleResponse, error) {
response, err := rpc.makeRpcCall("eth_callBundle", privKey, params...)
if err != nil {
return nil, err
}
var cbResponse = new(CallBundleResponse)
err = json.Unmarshal(response.Result, &cbResponse)
if err != nil {
return nil, err
}
return cbResponse, nil
}
// CallWithFlashbotsSignature is like Call but also signs the request
func (rpc *RPC) makeRpcCall(method string, privKey *ecdsa.PrivateKey, params ...interface{}) (*rpcResponse, error) {
if err := rpc.buildSignature(method, privKey, params...); err != nil {
return nil, err
}
if err := rpc.buildRPCRequest(); err != nil {
return nil, err
}
// fmt.Println(rpc)
response, err := rpc.client.Do(rpc.Request)
// fmt.Println(response)
if err != nil {
return nil, err
}
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
errorResp := new(RelayErrorResponse)
if err := json.Unmarshal(data, errorResp); err == nil && errorResp.Error != "" {
// relay returned an error
return nil, fmt.Errorf("%w: %s", ErrRelayErrorResponse, errorResp.Error)
}
resp := new(rpcResponse)
if err := json.Unmarshal(data, resp); err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
return resp, nil
}