forked from chenzhijie/go-web3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflashbots_test.go
160 lines (128 loc) · 3.3 KB
/
flashbots_test.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
package flashbots
import (
"encoding/hex"
"fmt"
"math/big"
"os"
"testing"
"github.com/ethereum/go-ethereum/common"
eTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/laziercoder/go-web3/types"
)
const (
mainnetInfuraProvider = "https://mainnet.infura.io/v3/91ffab09868d430f9ce744c78d7ff427"
goerliInfuraProvider = "https://goerli.infura.io/v3/91ffab09868d430f9ce744c78d7ff427"
goerliFlashbotMintNFTAddr = "0x20EE855E43A7af19E407E39E5110c2C1Ee41F64D"
)
func TestFlashbotSendBundleTx(t *testing.T) {
signerKey := os.Getenv("signerKey")
if len(signerKey) == 0 {
t.Fatal("signer key or sender key is empty")
}
web3, err := web3.NewWeb3(goerliInfuraProvider)
if err != nil {
t.Fatal(err)
}
err = web3.Eth.SetAccount(signerKey)
if err != nil {
t.Fatal(err)
}
web3.Eth.SetChainId(5)
currentBlockNumber, err := web3.Eth.GetBlockNumber()
if err != nil {
t.Fatal(err)
}
fmt.Printf("currentBlockNumber %v\n", currentBlockNumber)
mintValue := web3.Utils.ToWei(0.03)
mintNFTData, err := hex.DecodeString("1249c58b")
if err != nil {
t.Fatal(err)
}
bundleTxs := make([]*eTypes.Transaction, 0)
gasLimit, err := web3.Eth.EstimateGas(&types.CallMsg{
From: web3.Eth.Address(),
To: common.HexToAddress(goerliFlashbotMintNFTAddr),
Data: mintNFTData,
Value: types.NewCallMsgBigInt(mintValue),
})
if err != nil {
t.Fatal(err)
}
nonce, err := web3.Eth.GetNonce(web3.Eth.Address(), nil)
if err != nil {
t.Fatal(err)
}
fmt.Printf("gaslimit %v nonce %v\n", gasLimit, nonce)
mintNFTtx, err := web3.Eth.NewEIP1559Tx(
common.HexToAddress(goerliFlashbotMintNFTAddr),
mintValue, // 6a94d74f430000
gasLimit,
web3.Utils.ToGWei(0), //
web3.Utils.ToGWei(30), // b2d05e00
mintNFTData,
nonce,
)
if err != nil {
t.Fatal(err)
}
bundleTxs = append(bundleTxs, mintNFTtx)
fb, err := NewFlashBot(TestRelayURL, signerKey)
if err != nil {
t.Fatal(err)
}
resp, err := fb.Simulate(
bundleTxs,
big.NewInt(int64(currentBlockNumber)),
"latest",
)
if err != nil {
t.Fatal(err)
}
egp, err := resp.EffectiveGasPrice()
if err != nil {
t.Fatal(err)
}
fmt.Printf("Resp %s EffectiveGasPrice %v\n", resp, web3.Utils.FromGWei(egp))
bundleResp, err := fb.SendBundle(
bundleTxs,
big.NewInt(int64(currentBlockNumber)+1),
)
if err != nil {
t.Fatal(err)
}
fmt.Printf("bundle resp %v\n", bundleResp)
}
func TestGetBundleStats(t *testing.T) {
signerKey := os.Getenv("signerKey")
if len(signerKey) == 0 {
t.Fatal("signer key or sender key is empty")
}
fb, err := NewFlashBot(DefaultRelayURL, signerKey)
if err != nil {
t.Fatal(err)
}
bundleHash := "0x4a11aa0e0bdc321a7bbe5c96f9952cc38e38d8843b293379761d736222f8635b"
targetBlockNumber := big.NewInt(int64(6974433))
stat, err := fb.GetBunderStats(bundleHash, targetBlockNumber)
if err != nil {
t.Fatal(err)
}
fmt.Printf("bundle stat %v\n", stat)
}
func TestGetUserStats(t *testing.T) {
signerKey := os.Getenv("signerKey")
if len(signerKey) == 0 {
t.Fatal("signer key or sender key is empty")
}
fb, err := NewFlashBot(TestRelayURL, signerKey)
if err != nil {
t.Fatal(err)
}
targetBlockNumber := big.NewInt(int64(6974433))
fmt.Printf("%x\n", targetBlockNumber.Int64())
stat, err := fb.GetUserStats(targetBlockNumber)
if err != nil {
t.Fatal(err)
}
fmt.Printf("user stat %v\n", stat)
}