chore: add swqos client
This commit is contained in:
90
pkg/swqos/clients/next_block_http_client.go
Normal file
90
pkg/swqos/clients/next_block_http_client.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gagliardetto/solana-go"
|
||||
)
|
||||
|
||||
type NextBlockSendTransactionRequest struct {
|
||||
Transaction struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"transaction"`
|
||||
}
|
||||
|
||||
type NextBlockHttpClient struct {
|
||||
sendTxUrl string
|
||||
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewNextBlockHttpClient(sendTxUrl string) *NextBlockHttpClient {
|
||||
// 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 &NextBlockHttpClient{
|
||||
sendTxUrl: sendTxUrl,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *NextBlockHttpClient) SendTransaction(ctx context.Context, tx *solana.Transaction) error {
|
||||
request := NextBlockSendTransactionRequest{
|
||||
Transaction: struct {
|
||||
Content string `json:"content"`
|
||||
}{
|
||||
Content: tx.MustToBase64(),
|
||||
},
|
||||
}
|
||||
|
||||
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", "trial1759030481-pveRwfZNuyvrnrqvx7Lz559s9tR51pt7%2B1Sbv32wgcM%3D")
|
||||
|
||||
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 *NextBlockHttpClient) SendBundle(ctx context.Context, txs []*solana.Transaction) error {
|
||||
return fmt.Errorf("next block http client not support send bundle")
|
||||
}
|
||||
Reference in New Issue
Block a user