Files
libsam/pkg/swqos/clients/bloxroute_client.go

111 lines
2.4 KiB
Go
Raw Permalink Normal View History

2025-12-26 11:13:31 +08:00
package clients
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/gagliardetto/solana-go"
)
// TODO: SubmitProtection
type BloxrouteSendTransactionRequest struct {
Transaction struct {
Content string `json:"content"`
} `json:"transaction"`
SkipPreFlight bool `json:"skipPreFlight"`
FrontRunningProtection bool `json:"frontRunningProtection"`
RevertProtection bool `json:"revertProtection"`
UseStakedRPCs bool `json:"useStakedRPCs"`
}
type BloxrouteClient struct {
sendTxUrl string
client *http.Client
}
func NewBloxrouteClient(sendTxUrl string) *BloxrouteClient {
// create custom transport with keep-alive enabled
transport := &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 65 * time.Second,
DisableKeepAlives: false, // enable keep-alive
}
client := &http.Client{
Transport: transport,
Timeout: 30 * time.Second,
}
return &BloxrouteClient{
sendTxUrl: sendTxUrl,
client: client,
}
}
func (c *BloxrouteClient) SendTransaction(ctx context.Context, tx *solana.Transaction) error {
if c.sendTxUrl == "" {
return fmt.Errorf("send tx url is empty")
}
raw, err := tx.MarshalBinary()
if err != nil {
return err
}
encoded := base64.StdEncoding.EncodeToString(raw)
request := BloxrouteSendTransactionRequest{
Transaction: struct {
Content string `json:"content"`
}{
Content: encoded,
},
SkipPreFlight: true,
FrontRunningProtection: false,
RevertProtection: false,
UseStakedRPCs: true,
}
jsonData, err := json.Marshal(request)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.sendTxUrl, bytes.NewReader(jsonData))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "OTA2NzI4ZWMtNWJjZC00YTgzLTg4ODctNjZlOTFjMDUyMGNlOmIwYWQyNGJhYjlhNzVlZDQyYTQwMjA5MWJlZjMyMmRl") // TODO: maybe config?
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code: %d, body: %s", resp.StatusCode, string(body))
}
return nil
}
func (c *BloxrouteClient) SendBundle(ctx context.Context, txs []*solana.Transaction) error {
return fmt.Errorf("bloxroute client not support send bundle")
}