Skip to content

Commit 85f774e

Browse files
authored
refactor: ♻️ bunder-sender: use marshal binary for set tx (#152)
Signed-off-by: thanhpp <[email protected]>
1 parent 18164ed commit 85f774e

File tree

5 files changed

+67
-22
lines changed

5 files changed

+67
-22
lines changed

pkg/mev/backrun_public_sender.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (b BackrunPublicClient) SendBackrunBundle(
5757
if uuid != nil {
5858
p.SetUUID(*uuid, b.senderType)
5959
}
60+
if err := p.Err(); err != nil {
61+
return SendBundleResponse{}, err
62+
}
63+
6064
req.Params = append(req.Params, p)
6165

6266
reqBody, err := json.Marshal(req)

pkg/mev/blxr_bundle_sender.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"context"
66
"crypto/ecdsa"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"net/http"
1011
"strconv"
1112

1213
"github.com/ethereum/go-ethereum"
1314
"github.com/ethereum/go-ethereum/common"
15+
"github.com/ethereum/go-ethereum/common/hexutil"
1416
"github.com/ethereum/go-ethereum/core/types"
1517
"github.com/ethereum/go-ethereum/ethclient/gethclient"
1618
)
@@ -81,6 +83,9 @@ func (s *BloxrouteClient) SendBundle(
8183
if uuid != nil {
8284
p.SetUUID(*uuid)
8385
}
86+
if err := p.Err(); err != nil {
87+
return SendBundleResponse{}, err
88+
}
8489

8590
mevBuilders := make(map[BlxrBuilder]string)
8691
for _, b := range s.enabledBuilders {
@@ -155,6 +160,8 @@ type BLXRSubmitBundleParams struct {
155160
RevertingHashes *[]string `json:"reverting_hashes,omitempty"`
156161
UUID string `json:"uuid,omitempty"`
157162
MEVBuilders map[BlxrBuilder]string `json:"mev_builders,omitempty"`
163+
164+
Errors []error `json:"-"`
158165
}
159166

160167
func (p *BLXRSubmitBundleParams) SetTransactions(txs ...*types.Transaction) *BLXRSubmitBundleParams {
@@ -164,7 +171,12 @@ func (p *BLXRSubmitBundleParams) SetTransactions(txs ...*types.Transaction) *BLX
164171

165172
transactions := make([]string, 0, len(txs))
166173
for _, tx := range txs {
167-
transactions = append(transactions, "0x"+txToRlp(tx))
174+
txBin, err := tx.MarshalBinary()
175+
if err != nil {
176+
p.Errors = append(p.Errors, err)
177+
} else {
178+
transactions = append(transactions, hexutil.Encode(txBin))
179+
}
168180
}
169181

170182
p.Transaction = transactions
@@ -188,6 +200,14 @@ func (p *BLXRSubmitBundleParams) SetUUID(uuid string) *BLXRSubmitBundleParams {
188200
return p
189201
}
190202

203+
func (p *BLXRSubmitBundleParams) Err() error {
204+
if len(p.Errors) == 0 {
205+
return nil
206+
}
207+
208+
return errors.Join(p.Errors...)
209+
}
210+
191211
type BLXRSubmitBundleResponse struct {
192212
Jsonrpc string `json:"jsonrpc,omitempty"`
193213
ID int `json:"id,string,omitempty"`

pkg/mev/bundle_sender.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"crypto/ecdsa"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"net/http"
1011

@@ -202,6 +203,7 @@ func (s *Client) sendBundle(
202203
if uuid != nil {
203204
p.SetUUID(*uuid, s.senderType)
204205
}
206+
205207
req.Params = append(req.Params, p)
206208

207209
reqBody, err := json.Marshal(req)
@@ -258,11 +260,16 @@ func (s *Client) SendPrivateRawTransaction(
258260
return SendPrivateRawTransactionResponse{}, nil
259261
}
260262

263+
txBin, err := tx.MarshalBinary()
264+
if err != nil {
265+
return SendPrivateRawTransactionResponse{}, fmt.Errorf("marshal tx binary: %w", err)
266+
}
267+
261268
req := SendRequest{
262269
ID: SendBundleID,
263270
JSONRPC: JSONRPC2,
264271
Method: ETHSendPrivateRawTransaction,
265-
Params: []any{"0x" + txToRlp(tx)},
272+
Params: []any{hexutil.Encode(txBin)},
266273
}
267274

268275
reqBody, err := json.Marshal(req)
@@ -352,6 +359,8 @@ type SendBundleParams struct {
352359
// (Optional) String, UUID that can be used to cancel/replace this bundle (For beaverbuild)
353360
UUID string `json:"uuid,omitempty"`
354361
StateBlockNumber string `json:"stateBlockNumber,omitempty"`
362+
363+
Errors []error `json:"-"` // check when building bundle
355364
}
356365

357366
func (p *SendBundleParams) SetStateBlockNumber(stateBlockNumber string) *SendBundleParams {
@@ -383,14 +392,20 @@ func (p *SendBundleParams) SetPendingTxHashes(txHashes ...common.Hash) *SendBund
383392
return p
384393
}
385394

395+
// tested at: https://team-kyber.slack.com/archives/C03P04E6UAW/p1754280128232029?thread_ts=1754063210.955249&cid=C03P04E6UAW
386396
func (p *SendBundleParams) SetTransactions(txs ...*types.Transaction) *SendBundleParams {
387397
if len(txs) == 0 {
388398
return p
389399
}
390400

391401
transactions := make([]string, 0, len(txs))
392402
for _, tx := range txs {
393-
transactions = append(transactions, "0x"+txToRlp(tx))
403+
txBin, err := tx.MarshalBinary()
404+
if err != nil {
405+
p.Errors = append(p.Errors, fmt.Errorf("marshal tx: %w", err))
406+
} else {
407+
transactions = append(transactions, hexutil.Encode(txBin))
408+
}
394409
}
395410

396411
p.Txs = transactions
@@ -420,6 +435,14 @@ func (p *SendBundleParams) SetUUID(uuid string, senderType BundleSenderType) *Se
420435
return p
421436
}
422437

438+
func (p *SendBundleParams) Err() error {
439+
if len(p.Errors) == 0 {
440+
return nil
441+
}
442+
443+
return errors.Join(p.Errors...)
444+
}
445+
423446
type CancelBundleParams struct {
424447
ReplacementUUID string `json:"replacementUuid"`
425448
}

pkg/mev/bundle_sender_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ func TestSendBundle(t *testing.T) {
3434
return
3535
}
3636
var (
37-
endpoint = "https://relay-sepolia.flashbots.net"
37+
endpoint = "https://rpc.titanbuilder.xyz"
3838
ctx = context.Background()
3939
client = http.DefaultClient
40-
sepoliaChainID = 11155111
40+
sepoliaChainID = 1
4141
)
4242
require.NoError(t, err)
4343
address := crypto.PubkeyToAddress(privateKey.PublicKey)
4444

45-
ethClient, err := ethclient.Dial("https://ethereum-sepolia.blockpi.network/v1/rpc/public")
45+
ethClient, err := ethclient.Dial("wss://ethereum.kyberengineering.io")
4646
require.NoError(t, err)
4747

4848
blockNumber, err := ethClient.BlockNumber(ctx)

pkg/mev/pkg.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package mev
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/hex"
75
"encoding/json"
86
"fmt"
97
"io"
@@ -122,23 +120,23 @@ var defaultHeaders = [][2]string{ // nolint: gochecknoglobals
122120
{"Accept", "application/json"},
123121
}
124122

125-
func txToRlp(tx *types.Transaction) string {
126-
var buff bytes.Buffer
127-
_ = tx.EncodeRLP(&buff)
123+
// func txToRlp(tx *types.Transaction) string {
124+
// var buff bytes.Buffer
125+
// _ = tx.EncodeRLP(&buff)
128126

129-
rlp := hex.EncodeToString(buff.Bytes())
127+
// rlp := hex.EncodeToString(buff.Bytes())
130128

131-
switch rlp[:2] {
132-
case "b9":
133-
rlp = rlp[6:]
134-
case "b8":
135-
}
136-
if rlp[:2] == "b9" {
137-
rlp = rlp[4:]
138-
}
129+
// switch rlp[:2] {
130+
// case "b9":
131+
// rlp = rlp[6:]
132+
// case "b8":
133+
// }
134+
// if rlp[:2] == "b9" {
135+
// rlp = rlp[4:]
136+
// }
139137

140-
return rlp
141-
}
138+
// return rlp
139+
// }
142140

143141
func doRequest[T any](c *http.Client, req *http.Request, headers ...[2]string) (T, error) {
144142
var t T

0 commit comments

Comments
 (0)