forked from weeaa/jito-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relayer.go
85 lines (72 loc) 路 2.42 KB
/
relayer.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
package relayer_client
import (
"context"
"crypto/tls"
"fmt"
"github.com/QuantumLeaper/jito-go/pkg"
"github.com/QuantumLeaper/jito-go/proto"
"github.com/gagliardetto/solana-go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func New(ctx context.Context, grpcDialURL string, privateKey solana.PrivateKey, tlsConfig *tls.Config, opts ...grpc.DialOption) (*Client, error) {
if tlsConfig != nil {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
} else {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
}
chErr := make(chan error)
conn, err := pkg.CreateAndObserveGRPCConn(ctx, chErr, grpcDialURL, opts...)
if err != nil {
return nil, err
}
relayerClient := proto.NewRelayerClient(conn)
authService := pkg.NewAuthenticationService(conn, privateKey)
if err = authService.AuthenticateAndRefresh(proto.Role_RELAYER); err != nil {
return nil, err
}
return &Client{
GrpcConn: conn,
Relayer: relayerClient,
Auth: authService,
ErrChan: chErr,
}, nil
}
func (c *Client) GetTpuConfigs(opts ...grpc.CallOption) (*proto.GetTpuConfigsResponse, error) {
return c.Relayer.GetTpuConfigs(c.Auth.GrpcCtx, &proto.GetTpuConfigsRequest{}, opts...)
}
func (c *Client) NewPacketsSubscription(opts ...grpc.CallOption) (proto.Relayer_SubscribePacketsClient, error) {
return c.Relayer.SubscribePackets(c.Auth.GrpcCtx, &proto.SubscribePacketsRequest{}, opts...)
}
// SubscribePackets is a wrapper around NewPacketsSubscription.
func (c *Client) SubscribePackets(ctx context.Context) (<-chan []*solana.Transaction, <-chan error, error) {
chTx := make(chan []*solana.Transaction)
chErr := make(chan error)
sub, err := c.NewPacketsSubscription()
if err != nil {
return nil, nil, err
}
go func() {
for {
select {
case <-ctx.Done():
return
case <-c.Auth.GrpcCtx.Done():
return
default:
var packet *proto.SubscribePacketsResponse
packet, err = sub.Recv()
if err != nil {
chErr <- fmt.Errorf("SubscribePackets: failed to receive packet information: %w", err)
}
var txns = make([]*solana.Transaction, 0, len(packet.Batch.GetPackets()))
txns, err = pkg.ConvertBatchProtobufPacketToTransaction(packet.Batch.GetPackets())
if err != nil {
chErr <- fmt.Errorf("SubscribePackets: failed to convert protobuf packet to transaction: %w", err)
}
chTx <- txns
}
}
}()
return chTx, chErr, nil
}