-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcall_bundle.go
217 lines (196 loc) · 6.18 KB
/
call_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
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
package flashbots
import (
"encoding/json"
"errors"
"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/lmittmann/flashbots/internal"
"github.com/lmittmann/w3/w3types"
)
type CallBundleRequest struct {
Transactions types.Transactions // List of signed transactions to simulate in a bundle.
RawTransactions [][]byte // List of signed raw transactions to simulate in a bundle.
BlockNumber *big.Int // Block number for which the bundle is valid.
StateBlockNumber *big.Int // Block number of state to use for simulation, "latest" if nil.
Timestamp uint64 // Timestamp of block used for simulation (Optional).
}
type callBundleRequest struct {
RawTransactions []hexutil.Bytes `json:"txs"`
BlockNumber *hexutil.Big `json:"blockNumber"`
StateBlockNumber string `json:"stateBlockNumber"`
Timestamp uint64 `json:"timestamp,omitempty"`
}
// MarshalJSON implements the [json.Marshaler].
func (c CallBundleRequest) MarshalJSON() ([]byte, error) {
var enc callBundleRequest
if len(c.Transactions) > 0 {
enc.RawTransactions = make([]hexutil.Bytes, len(c.Transactions))
for i, tx := range c.Transactions {
rawTx, err := tx.MarshalBinary()
if err != nil {
return nil, err
}
enc.RawTransactions[i] = rawTx
}
} else {
enc.RawTransactions = make([]hexutil.Bytes, len(c.RawTransactions))
for i, rawTx := range c.RawTransactions {
enc.RawTransactions[i] = rawTx
}
}
enc.BlockNumber = (*hexutil.Big)(c.BlockNumber)
enc.StateBlockNumber = toBlockNumberArg(c.StateBlockNumber)
enc.Timestamp = c.Timestamp
return json.Marshal(&enc)
}
type CallBundleResponse struct {
BundleGasPrice *big.Int
BundleHash common.Hash
CoinbaseDiff *big.Int
EthSentToCoinbase *big.Int
GasFees *big.Int
StateBlockNumber *big.Int
TotalGasUsed uint64
Results []CallBundleResult
}
type callBundleResponse struct {
BundleGasPrice *internal.StrInt `json:"bundleGasPrice"`
BundleHash *common.Hash `json:"bundleHash"`
CoinbaseDiff *internal.StrInt `json:"coinbaseDiff"`
EthSentToCoinbase *internal.StrInt `json:"ethSentToCoinbase"`
GasFees *internal.StrInt `json:"gasFees"`
StateBlockNumber *big.Int `json:"stateBlockNumber"`
TotalGasUsed *uint64 `json:"totalGasUsed"`
Results []callBundleResult `json:"results"`
}
type CallBundleResult struct {
CoinbaseDiff *big.Int
EthSentToCoinbase *big.Int
FromAddress common.Address
GasFees *big.Int
GasPrice *big.Int
GasUsed uint64
ToAddress *common.Address
TxHash common.Hash
Value []byte // Output
Error error
Revert string // Revert reason
}
type callBundleResult struct {
CoinbaseDiff *internal.StrInt `json:"coinbaseDiff"`
EthSentToCoinbase *internal.StrInt `json:"ethSentToCoinbase"`
FromAddress *common.Address `json:"fromAddress"`
GasFees *internal.StrInt `json:"gasFees"`
GasPrice *internal.StrInt `json:"gasPrice"`
GasUsed *uint64 `json:"gasUsed"`
ToAddress *common.Address `json:"toAddress"`
TxHash *common.Hash `json:"txHash"`
Value *hexutil.Bytes `json:"value"`
Error *string `json:"error"`
Revert *string `json:"revert"`
}
// UnmarshalJSON implements the [json.Unmarshaler].
func (c *CallBundleResponse) UnmarshalJSON(input []byte) error {
var dec callBundleResponse
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.BundleGasPrice != nil {
c.BundleGasPrice = (*big.Int)(dec.BundleGasPrice)
}
if dec.BundleHash != nil {
c.BundleHash = *dec.BundleHash
}
if dec.CoinbaseDiff != nil {
c.CoinbaseDiff = (*big.Int)(dec.CoinbaseDiff)
}
if dec.EthSentToCoinbase != nil {
c.EthSentToCoinbase = (*big.Int)(dec.EthSentToCoinbase)
}
if dec.GasFees != nil {
c.GasFees = (*big.Int)(dec.GasFees)
}
if dec.StateBlockNumber != nil {
c.StateBlockNumber = dec.StateBlockNumber
}
if dec.TotalGasUsed != nil {
c.TotalGasUsed = *dec.TotalGasUsed
}
if dec.Results != nil {
c.Results = make([]CallBundleResult, len(dec.Results))
for i, res := range dec.Results {
if res.CoinbaseDiff != nil {
c.Results[i].CoinbaseDiff = (*big.Int)(res.CoinbaseDiff)
}
if res.EthSentToCoinbase != nil {
c.Results[i].EthSentToCoinbase = (*big.Int)(res.EthSentToCoinbase)
}
if res.FromAddress != nil {
c.Results[i].FromAddress = *res.FromAddress
}
if res.GasFees != nil {
c.Results[i].GasFees = (*big.Int)(res.GasFees)
}
if res.GasPrice != nil {
c.Results[i].GasPrice = (*big.Int)(res.GasPrice)
}
if res.GasUsed != nil {
c.Results[i].GasUsed = *res.GasUsed
}
if res.ToAddress != nil {
c.Results[i].ToAddress = res.ToAddress
}
if res.TxHash != nil {
c.Results[i].TxHash = *res.TxHash
}
if res.Value != nil {
c.Results[i].Value = *res.Value
}
if res.Error != nil {
c.Results[i].Error = errors.New(*res.Error)
}
if res.Revert != nil {
c.Results[i].Revert = *res.Revert
}
}
}
return nil
}
// CallBundle simulates a bundle.
func CallBundle(r *CallBundleRequest) w3types.RPCCallerFactory[*CallBundleResponse] {
return &callBundleFactory{param: r}
}
type callBundleFactory struct {
// args
param *CallBundleRequest
// returns
returns **CallBundleResponse
}
func (f *callBundleFactory) Returns(resp **CallBundleResponse) w3types.RPCCaller {
f.returns = resp
return f
}
// CreateRequest implements the [w3types.RequestCreator].
func (f *callBundleFactory) CreateRequest() (rpc.BatchElem, error) {
return rpc.BatchElem{
Method: "eth_callBundle",
Args: []any{f.param},
Result: f.returns,
}, nil
}
// HandleResponse implements the [w3types.ResponseHandler].
func (f *callBundleFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
return nil
}
func toBlockNumberArg(blockNumber *big.Int) string {
if blockNumber == nil || blockNumber.Sign() < 0 {
return "latest"
}
return hexutil.EncodeBig(blockNumber)
}