83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
|
|
package clients
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
pb "github.com/BlockRazorinc/solana-trader-client-go/pb/serverpb"
|
||
|
|
"github.com/gagliardetto/solana-go"
|
||
|
|
"google.golang.org/grpc"
|
||
|
|
"google.golang.org/grpc/credentials/insecure"
|
||
|
|
)
|
||
|
|
|
||
|
|
type BlockRazorClient struct {
|
||
|
|
conn *grpc.ClientConn
|
||
|
|
client pb.ServerClient
|
||
|
|
}
|
||
|
|
|
||
|
|
type Authentication struct {
|
||
|
|
apiKey string
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
|
||
|
|
return map[string]string{"apikey": a.apiKey}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *Authentication) RequireTransportSecurity() bool {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewBlockRazorClient(ctx context.Context, endpoint string) (*BlockRazorClient, error) {
|
||
|
|
if endpoint == "" {
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// setup grpc connect
|
||
|
|
conn, err := grpc.NewClient(
|
||
|
|
endpoint,
|
||
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||
|
|
grpc.WithKeepaliveParams(kacp),
|
||
|
|
grpc.WithPerRPCCredentials(&Authentication{apiKey: "xhMIRxybodR6U35cDdTjQVIkUPPVVjKC5ynKWQMdL9fm8DnwoEFFAlj1E4ySBADo4xLh3RVTRgLQI2BTzegxb3N5CIXThEEJ"}), // TODO: maybe config?
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("connect error: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// use the Gateway client connection interface
|
||
|
|
client := pb.NewServerClient(conn)
|
||
|
|
|
||
|
|
// grpc request warmup
|
||
|
|
_, err = client.GetHealth(ctx, &pb.HealthRequest{})
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return &BlockRazorClient{
|
||
|
|
conn: conn,
|
||
|
|
client: client,
|
||
|
|
}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *BlockRazorClient) SendTransaction(ctx context.Context, tx *solana.Transaction) error {
|
||
|
|
if c == nil {
|
||
|
|
return errors.New("block razor client is nil")
|
||
|
|
}
|
||
|
|
|
||
|
|
txBase64, _ := tx.ToBase64()
|
||
|
|
_, err := c.client.SendTransaction(ctx, &pb.SendRequest{
|
||
|
|
Transaction: txBase64,
|
||
|
|
Mode: "fast",
|
||
|
|
SafeWindow: 3,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (c *BlockRazorClient) SendBundle(ctx context.Context, txs []*solana.Transaction) error {
|
||
|
|
return fmt.Errorf("block razor client not support send bundle")
|
||
|
|
}
|