chore: add swqos client
This commit is contained in:
94
pkg/swqos/clients/node1_client.go
Normal file
94
pkg/swqos/clients/node1_client.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gagliardetto/solana-go"
|
||||
)
|
||||
|
||||
type Node1Client struct {
|
||||
sendTxUrl string
|
||||
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewNode1Client(sendTxUrl string) *Node1Client {
|
||||
// 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 &Node1Client{
|
||||
sendTxUrl: sendTxUrl,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Node1Client) 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 := JsonRpcRequest{
|
||||
Jsonrpc: "2.0",
|
||||
Method: "sendTransaction",
|
||||
Params: []any{encoded, SendTransactionParam{Encoding: "base64", SkipPreflight: true}},
|
||||
Id: 1,
|
||||
}
|
||||
|
||||
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("api-key", "9b8e1f04-6518-40da-b60a-3c4e9c7e39eb") // 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 *Node1Client) SendBundle(ctx context.Context, txs []*solana.Transaction) error {
|
||||
return fmt.Errorf("node1 client not support send bundle")
|
||||
}
|
||||
Reference in New Issue
Block a user