-
Notifications
You must be signed in to change notification settings - Fork 10
/
private_tx.go
118 lines (99 loc) · 2.9 KB
/
private_tx.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
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/lmittmann/w3/w3types"
)
type SendPrivateTxRequest struct {
Tx *types.Transaction // Signed transaction to send.
RawTx []byte // Raw signed transaction to send.
MaxBlockNumber *big.Int // Max block number for which the tx should be included (Optional).
Fast bool // Enable fast mode (Optional). See https://docs.flashbots.net/flashbots-protect/rpc/fast-mode
}
type sendPrivateTxRequest struct {
RawTx hexutil.Bytes `json:"tx"`
MaxBlockNumber *hexutil.Big `json:"maxBlockNumber"`
Preferences struct {
Fast bool `json:"fast"`
} `json:"preferences"`
}
// MarshalJSON implements the [json.Marshaler].
func (c SendPrivateTxRequest) MarshalJSON() ([]byte, error) {
var enc sendPrivateTxRequest
if c.Tx != nil {
rawTx, err := c.Tx.MarshalBinary()
if err != nil {
return nil, err
}
enc.RawTx = rawTx
} else {
enc.RawTx = c.RawTx
}
enc.MaxBlockNumber = (*hexutil.Big)(c.MaxBlockNumber)
enc.Preferences.Fast = c.Fast
return json.Marshal(&enc)
}
// SendPrivateTx sends a private transaction to the Flashbots relay.
func SendPrivateTx(r *SendPrivateTxRequest) w3types.RPCCallerFactory[common.Hash] {
return &sendPrivateTxFactory{params: r}
}
type sendPrivateTxFactory struct {
// args
params *SendPrivateTxRequest
// returns
returns *common.Hash
}
func (f *sendPrivateTxFactory) Returns(txHash *common.Hash) w3types.RPCCaller {
f.returns = txHash
return f
}
func (f *sendPrivateTxFactory) CreateRequest() (rpc.BatchElem, error) {
return rpc.BatchElem{
Method: "eth_sendPrivateTransaction",
Args: []any{f.params},
Result: f.returns,
}, nil
}
func (f *sendPrivateTxFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
return nil
}
type cancelPrivateTxRequest struct {
TxHash common.Hash `json:"txHash"`
}
// CancelPrivateTx stops the private transactions with the given hash
// from being submitted for future blocks by the Flashbots relay.
func CancelPrivateTx(hash common.Hash) w3types.RPCCallerFactory[bool] {
return &cancelPrivateTxFactory{hash: hash}
}
type cancelPrivateTxFactory struct {
// args
hash common.Hash
// returns
returns *bool
}
func (f *cancelPrivateTxFactory) Returns(success *bool) w3types.RPCCaller {
f.returns = success
return f
}
func (f *cancelPrivateTxFactory) CreateRequest() (rpc.BatchElem, error) {
return rpc.BatchElem{
Method: "eth_cancelPrivateTransaction",
Args: []any{&cancelPrivateTxRequest{
TxHash: f.hash,
}},
Result: &f.returns,
}, nil
}
func (f *cancelPrivateTxFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
return nil
}