-
Notifications
You must be signed in to change notification settings - Fork 10
/
send_bundle.go
104 lines (91 loc) · 3.13 KB
/
send_bundle.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
package flashbots
import (
"encoding/json"
"math/big"
"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/rpc"
"github.com/google/uuid"
"github.com/lmittmann/w3/w3types"
)
type SendBundleRequest struct {
Transactions types.Transactions // List of signed transactions to execute in a bundle.
RawTransactions [][]byte // List of signed raw transactions to execute in a bundle.
BlockNumber *big.Int // Block number for which the bundle is valid
MinTimestamp uint64 // Minimum Unix Timestamp for which the bundle is valid
MaxTimestamp uint64 // Maximum Unix Timestamp for which the bundle is valid
RevertingTxHashes []common.Hash // List of tx hashes in bundle that are allowed to revert.
ReplacementUuid uuid.UUID // UUID that can be used to cancel/replace this bundle
}
type sendBundleRequest struct {
RawTransactions []hexutil.Bytes `json:"txs"`
BlockNumber *hexutil.Big `json:"blockNumber"`
MinTimestamp uint64 `json:"minTimestamp,omitempty"`
MaxTimestamp uint64 `json:"maxTimestamp,omitempty"`
RevertingTxHashes []common.Hash `json:"revertingTxHashes,omitempty"`
ReplacementUuid uuid.UUID `json:"replacementUuid,omitempty"`
}
// MarshalJSON implements the [json.Marshaler].
func (s SendBundleRequest) MarshalJSON() ([]byte, error) {
var enc sendBundleRequest
if len(s.Transactions) > 0 {
enc.RawTransactions = make([]hexutil.Bytes, len(s.Transactions))
for i, tx := range s.Transactions {
rawTx, err := tx.MarshalBinary()
if err != nil {
return nil, err
}
enc.RawTransactions[i] = rawTx
}
} else {
enc.RawTransactions = make([]hexutil.Bytes, len(s.RawTransactions))
for i, rawTx := range s.RawTransactions {
enc.RawTransactions[i] = rawTx
}
}
if s.BlockNumber != nil {
enc.BlockNumber = (*hexutil.Big)(s.BlockNumber)
}
enc.MinTimestamp = s.MinTimestamp
enc.MaxTimestamp = s.MaxTimestamp
enc.RevertingTxHashes = s.RevertingTxHashes
enc.ReplacementUuid = s.ReplacementUuid
return json.Marshal(&enc)
}
type sendBundleResponse struct {
BundleHash common.Hash `json:"bundleHash"`
}
// SendBundle sends the bundle to the client's endpoint.
func SendBundle(r *SendBundleRequest) w3types.RPCCallerFactory[common.Hash] {
return &sendBundleFactory{param: r}
}
type sendBundleFactory struct {
// args
param *SendBundleRequest
// returns
result sendBundleResponse
returns *common.Hash
}
func (f *sendBundleFactory) Returns(hash *common.Hash) w3types.RPCCaller {
f.returns = hash
return f
}
// CreateRequest implements the [w3types.RequestCreator].
func (f *sendBundleFactory) CreateRequest() (rpc.BatchElem, error) {
return rpc.BatchElem{
Method: "eth_sendBundle",
Args: []any{f.param},
Result: &f.result,
}, nil
}
// HandleResponse implements the [w3types.ResponseHandler].
func (f *sendBundleFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
if f.returns != nil {
*f.returns = f.result.BundleHash
}
return nil
}