Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 1370149

Browse files
committed
sendPrivateTransaction + cancelPrivateTransaction
1 parent 04f82fb commit 1370149

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ethereum/go-ethereum/crypto"
7+
"github.com/metachris/flashbotsrpc"
8+
)
9+
10+
var privateKey, _ = crypto.GenerateKey() // creating a new private key for testing. you probably want to use an existing key.
11+
12+
func main() {
13+
rpc := flashbotsrpc.New("https://relay.flashbots.net")
14+
rpc.Debug = true
15+
16+
cancelPrivTxArgs := flashbotsrpc.FlashbotsCancelPrivateTransactionRequest{
17+
TxHash: "0xfb34b88cd77215867aa8e8ff0abc7060178b8fed6519a85d0b22853dfd5e9fec",
18+
}
19+
20+
cancelled, err := rpc.FlashbotsCancelPrivateTransaction(privateKey, cancelPrivTxArgs)
21+
if err != nil {
22+
fmt.Printf("%+v\n", err)
23+
return
24+
}
25+
26+
// Print result
27+
fmt.Printf("was cancelled: %v\n", cancelled)
28+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/ethereum/go-ethereum/crypto"
8+
"github.com/metachris/flashbotsrpc"
9+
)
10+
11+
var privateKey, _ = crypto.GenerateKey() // creating a new private key for testing. you probably want to use an existing key.
12+
13+
func main() {
14+
rpc := flashbotsrpc.New("https://relay.flashbots.net")
15+
rpc.Debug = true
16+
17+
sendPrivTxArgs := flashbotsrpc.FlashbotsSendPrivateTransactionRequest{
18+
Tx: "0xYourTxHash",
19+
}
20+
21+
txHash, err := rpc.FlashbotsSendPrivateTransaction(privateKey, sendPrivTxArgs)
22+
if err != nil {
23+
if errors.Is(err, flashbotsrpc.ErrRelayErrorResponse) {
24+
fmt.Println(err.Error()) // standard error response from relay
25+
} else {
26+
fmt.Printf("unknown error: %+v\n", err)
27+
}
28+
return
29+
}
30+
31+
// Print txHash
32+
fmt.Printf("txHash: %s\n", txHash)
33+
}

flashbotsrpc.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"os"
1313
"time"
1414

15-
"github.com/pkg/errors"
16-
1715
"github.com/ethereum/go-ethereum/accounts"
1816
"github.com/ethereum/go-ethereum/common/hexutil"
1917
"github.com/ethereum/go-ethereum/core/types"
@@ -187,7 +185,7 @@ func (rpc *FlashbotsRPC) CallWithFlashbotsSignature(method string, privKey *ecds
187185
errorResp := new(RelayErrorResponse)
188186
if err := json.Unmarshal(data, errorResp); err == nil && errorResp.Error != "" {
189187
// relay returned an error
190-
return nil, errors.New(errorResp.Error)
188+
return nil, fmt.Errorf("%w: %s", ErrRelayErrorResponse, errorResp.Error)
191189
}
192190

193191
resp := new(rpcResponse)
@@ -621,7 +619,7 @@ func (rpc *FlashbotsRPC) FlashbotsSendBundle(privKey *ecdsa.PrivateKey, param Fl
621619
return res, err
622620
}
623621

624-
// numTx is the maximum number of tx to include (used for troubleshooting). default 0 (all transactions)
622+
// Simulate a full Ethereum block. numTx is the maximum number of tx to include, used for troubleshooting (default: 0 - all transactions)
625623
func (rpc *FlashbotsRPC) FlashbotsSimulateBlock(privKey *ecdsa.PrivateKey, block *types.Block, maxTx int) (res FlashbotsCallBundleResponse, err error) {
626624
if rpc.Debug {
627625
fmt.Printf("Simulating block %s 0x%x %s \t %d tx \t timestamp: %d\n", block.Number(), block.Number(), block.Header().Hash(), len(block.Transactions()), block.Header().Time)
@@ -682,3 +680,23 @@ func (rpc *FlashbotsRPC) FlashbotsSimulateBlock(privKey *ecdsa.PrivateKey, block
682680
res, err = rpc.FlashbotsCallBundle(privKey, params)
683681
return res, err
684682
}
683+
684+
// docs todo
685+
func (rpc *FlashbotsRPC) FlashbotsSendPrivateTransaction(privKey *ecdsa.PrivateKey, param FlashbotsSendPrivateTransactionRequest) (txHash string, err error) {
686+
rawMsg, err := rpc.CallWithFlashbotsSignature("eth_sendPrivateTransaction", privKey, param)
687+
if err != nil {
688+
return "", err
689+
}
690+
err = json.Unmarshal(rawMsg, &txHash)
691+
return txHash, err
692+
}
693+
694+
// docs todo
695+
func (rpc *FlashbotsRPC) FlashbotsCancelPrivateTransaction(privKey *ecdsa.PrivateKey, param FlashbotsCancelPrivateTransactionRequest) (cancelled bool, err error) {
696+
rawMsg, err := rpc.CallWithFlashbotsSignature("eth_sendPrivateTransaction", privKey, param)
697+
if err != nil {
698+
return false, err
699+
}
700+
err = json.Unmarshal(rawMsg, &cancelled)
701+
return cancelled, err
702+
}

types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import (
55
"encoding/json"
66
"math/big"
77
"unsafe"
8+
9+
"github.com/pkg/errors"
810
)
911

12+
var ErrRelayErrorResponse = errors.New("relay error response")
13+
1014
// Syncing - object with syncing data info
1115
type Syncing struct {
1216
IsSyncing bool
@@ -369,6 +373,7 @@ type FlashbotsCallBundleResponse struct {
369373
TotalGasUsed int64 `json:"totalGasUsed"` // 63197
370374
}
371375

376+
// sendBundle
372377
type FlashbotsSendBundleRequest struct {
373378
Txs []string `json:"txs"` // Array[String], A list of signed transactions to execute in an atomic bundle
374379
BlockNumber string `json:"blockNumber"` // String, a hex encoded block number for which this bundle is valid on
@@ -380,3 +385,13 @@ type FlashbotsSendBundleRequest struct {
380385
type FlashbotsSendBundleResponse struct {
381386
BundleHash string `json:"bundleHash"`
382387
}
388+
389+
// sendPrivateTransaction
390+
type FlashbotsSendPrivateTransactionRequest struct {
391+
Tx string `json:"tx"`
392+
}
393+
394+
// cancelPrivateTransaction
395+
type FlashbotsCancelPrivateTransactionRequest struct {
396+
TxHash string `json:"txHash"`
397+
}

0 commit comments

Comments
 (0)