Skip to content

Commit

Permalink
feat: add sendBackrunBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
datluongductuan committed Mar 12, 2024
1 parent 86b9e2b commit 2173936
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
50 changes: 50 additions & 0 deletions pkg/mev/bundle_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,56 @@ func (s *Client) SendBundle(
return s.sendBundle(ctx, ETHSendBundleMethod, uuid, blockNumber, txs...)
}

func (s *Client) SendBackrunBundle(
ctx context.Context,
uuid *string,
blockNumber uint64,
pendingTxHash common.Hash,
txs ...*types.Transaction,
) (SendBundleResponse, error) {
req := SendBundleRequest{
ID: SendBundleID,
JSONRPC: JSONRPC2,
Method: ETHSendBundleMethod,
}
p := new(SendBundleParams).SetBlockNumber(blockNumber).SetTransactions(txs...).SetPendingTxHash(pendingTxHash)
if uuid != nil {
p.SetUUID(*uuid, s.senderType)
}
req.Params = append(req.Params, p)

reqBody, err := json.Marshal(req)
if err != nil {
return SendBundleResponse{}, fmt.Errorf("marshal json error: %w", err)
}

var headers [][2]string
if s.flashbotKey != nil {
signature, err := requestSignature(s.flashbotKey, reqBody)
if err != nil {
return SendBundleResponse{}, fmt.Errorf("sign flashbot request error: %w", err)
}
headers = append(headers, [2]string{"X-Flashbots-Signature", signature})
}

httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, s.endpoint, bytes.NewBuffer(reqBody))
if err != nil {
return SendBundleResponse{}, fmt.Errorf("new http request error: %w", err)
}

resp, err := doRequest[SendBundleResponse](s.c, httpReq, headers...)
if err != nil {
return SendBundleResponse{}, err
}

if len(resp.Error.Messange) != 0 {
return SendBundleResponse{}, fmt.Errorf("response error, code: [%d], message: [%s]",
resp.Error.Code, resp.Error.Messange)
}

return resp, nil
}

func (s *Client) CancelBundle(
ctx context.Context, bundleUUID string,
) error {
Expand Down
8 changes: 8 additions & 0 deletions pkg/mev/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net/http"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

Expand Down Expand Up @@ -40,6 +41,13 @@ type IBundleSender interface {
blockNumber uint64,
tx ...*types.Transaction,
) (SendBundleResponse, error)
SendBackrunBundle(
ctx context.Context,
uuid *string,
blockNumber uint64,
pendingTxHash common.Hash,
tx ...*types.Transaction,
) (SendBundleResponse, error)
CancelBundle(
ctx context.Context, bundleUUID string,
) error
Expand Down

0 comments on commit 2173936

Please sign in to comment.