-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtip.go
322 lines (251 loc) · 6.83 KB
/
tip.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"time"
jito_go "github.com/1fge/pump-fun-sniper-bot/pkg/jito-go"
"github.com/1fge/pump-fun-sniper-bot/pkg/jito-go/clients/searcher_client"
util "github.com/1fge/pump-fun-sniper-bot/pkg/jito-go/pkg"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
)
type validatorAPIResponse struct {
Validators []*jitoValidator `json:"validators"`
}
type jitoValidator struct {
VoteAccount string `json:"vote_account"`
RunningJito bool `json:"running_jito"`
}
// JitoManager acts as the struct where we store important information for interacting
// with Jito. This includes keeping track of the leaders, determining tip amount based on percentile,
// and building the tip instruction.
type JitoManager struct {
client *http.Client
rpcClient *rpc.Client
privateKey solana.PrivateKey
slotIndex uint64
epoch uint64
// jitoValidators is a map of validator IDs that are running Jito.
jitoValidators map[string]bool
// slotLeader maps slot to validator ID.
slotLeader map[uint64]string
// voteAccounts maps nodeAccount to voteAccount
voteAccounts map[string]string
lock *sync.Mutex
// tipInfo maps the latest tip information from Jito.
tipInfo *util.TipStreamInfo
jitoClient *searcher_client.Client
}
func newJitoManager(rpcClient *rpc.Client, privateKey solana.PrivateKey) (*JitoManager, error) {
jitoClient, err := searcher_client.New(
context.Background(),
jito_go.NewYork.BlockEngineURL,
rpcClient,
rpcClient,
privateKey,
nil,
)
if err != nil {
return nil, err
}
return &JitoManager{
client: &http.Client{},
rpcClient: rpcClient,
jitoClient: jitoClient,
jitoValidators: make(map[string]bool),
slotLeader: make(map[uint64]string),
voteAccounts: make(map[string]string),
lock: &sync.Mutex{},
privateKey: privateKey,
}, nil
}
func (j *JitoManager) status(msg string) {
log.Println("Jito Manager", msg)
}
func (j *JitoManager) statusr(msg string) {
log.Println("Jito Manager (R)", msg)
}
func (j *JitoManager) generateTipInstruction() (solana.Instruction, error) {
tipAmount := j.generateTipAmount()
j.status(fmt.Sprintf("Generating tip instruction for %.5f SOL", float64(tipAmount)/1e9))
return j.jitoClient.GenerateTipRandomAccountInstruction(tipAmount, j.privateKey.PublicKey())
}
func (j *JitoManager) generateTipAmount() uint64 {
if j.tipInfo == nil {
return 2000000
}
return uint64(j.tipInfo.LandedTips75ThPercentile * 1e9)
}
func (j *JitoManager) manageTipStream() {
go func() {
for {
if err := j.subscribeTipStream(); err != nil {
j.statusr("Error reading tip stream: " + err.Error())
}
}
}()
}
func (j *JitoManager) subscribeTipStream() error {
infoChan, errChan, err := util.SubscribeTipStream(context.TODO())
if err != nil {
return err
}
for {
select {
case info := <-infoChan:
j.status(fmt.Sprintf("Received tip stream (75th percentile=%.3fSOL, 95th percentile=%.3fSOL, 99th percentile=%.3fSOL)", info.LandedTips75ThPercentile, info.LandedTips95ThPercentile, info.LandedTips99ThPercentile))
j.tipInfo = info
case err = <-errChan:
return err
}
}
}
func (j *JitoManager) start() error {
if j.jitoClient == nil {
return nil
}
j.manageTipStream()
if err := j.fetchJitoValidators(); err != nil {
return err
}
if err := j.fetchLeaderSchedule(); err != nil {
return err
}
if err := j.fetchVoteAccounts(); err != nil {
return err
}
if err := j.fetchEpochInfo(); err != nil {
return err
}
go func() {
for {
if err := j.fetchEpochInfo(); err != nil {
fmt.Println("Failed to fetch epoch info: ", err)
}
time.Sleep(10 * time.Millisecond)
}
}()
go func() {
for {
if err := j.fetchLeaderSchedule(); err != nil {
fmt.Println("Failed to fetch epoch info: ", err)
}
time.Sleep(10 * time.Minute)
}
}()
go func() {
for {
if err := j.fetchJitoValidators(); err != nil {
fmt.Println("Failed to fetch epoch info: ", err)
}
time.Sleep(10 * time.Minute)
}
}()
go func() {
for {
if err := j.fetchVoteAccounts(); err != nil {
fmt.Println("Failed to fetch epoch info: ", err)
}
time.Sleep(10 * time.Minute)
}
}()
return nil
}
func (j *JitoManager) isJitoLeader() bool {
j.lock.Lock()
defer j.lock.Unlock()
validator, ok := j.slotLeader[j.slotIndex]
if !ok {
return false
}
j.status("Checking if validator is a Jito leader: " + validator)
isLeader := j.jitoValidators[j.voteAccounts[validator]]
return isLeader
}
func (j *JitoManager) fetchLeaderSchedule() error {
j.status("Fetching leader schedule")
scheduleResult, err := j.rpcClient.GetLeaderSchedule(context.Background())
if err != nil {
return err
}
j.buildLeaderSchedule(&scheduleResult)
return nil
}
func (j *JitoManager) buildLeaderSchedule(scheduleResult *rpc.GetLeaderScheduleResult) {
j.lock.Lock()
defer j.lock.Unlock()
j.slotLeader = make(map[uint64]string)
for validator, slots := range *scheduleResult {
for _, slot := range slots {
j.slotLeader[slot] = validator.String()
}
}
}
func (j *JitoManager) fetchVoteAccounts() error {
j.status("Fetching vote accounts")
voteAccounts, err := j.rpcClient.GetVoteAccounts(context.Background(), nil)
if err != nil {
return err
}
j.buildVoteAccounts(voteAccounts.Current)
return nil
}
func (j *JitoManager) buildVoteAccounts(voteAccounts []rpc.VoteAccountsResult) {
j.lock.Lock()
defer j.lock.Unlock()
for _, account := range voteAccounts {
j.voteAccounts[account.NodePubkey.String()] = account.VotePubkey.String()
}
}
func (j *JitoManager) fetchEpochInfo() error {
schedule, err := j.rpcClient.GetEpochInfo(context.Background(), rpc.CommitmentFinalized)
if err != nil {
return err
}
j.slotIndex = schedule.SlotIndex
if j.epoch != schedule.Epoch {
if err = j.fetchLeaderSchedule(); err != nil {
return err
}
j.epoch = schedule.Epoch
}
return nil
}
// fetchJitoValidators fetches the list of validators from the Jito network.
func (j *JitoManager) fetchJitoValidators() error {
j.status("Fetching jito-enabled validators")
req, err := http.NewRequest("GET", "https://kobe.mainnet.jito.network/api/v1/validators", nil)
if err != nil {
return err
}
req.Header.Set("accept", "application/json")
resp, err := j.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("failed to fetch validators: %s", resp.Status)
}
var validators validatorAPIResponse
err = json.NewDecoder(resp.Body).Decode(&validators)
if err != nil {
return err
}
j.buildJitoValidators(validators.Validators)
return nil
}
func (j *JitoManager) buildJitoValidators(validators []*jitoValidator) {
j.lock.Lock()
defer j.lock.Unlock()
j.jitoValidators = make(map[string]bool)
for i := range validators {
if validators[i].RunningJito {
j.jitoValidators[validators[i].VoteAccount] = true
}
}
}