152 lines
3.0 KiB
Go
152 lines
3.0 KiB
Go
package clients
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gagliardetto/solana-go"
|
|
)
|
|
|
|
type SendTransactionParam struct {
|
|
Encoding string `json:"encoding"`
|
|
SkipPreflight bool `json:"skipPreflight"`
|
|
}
|
|
|
|
type JsonRpcRequest struct {
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
Method string `json:"method"`
|
|
Params []any `json:"params"`
|
|
Id int `json:"id"`
|
|
}
|
|
|
|
type JsonRpcResponse struct {
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
Error any `json:"error"`
|
|
Result string `json:"result"`
|
|
Id int `json:"id"`
|
|
}
|
|
|
|
type HttpClient struct {
|
|
sendTxUrl string
|
|
sendBundleUrl string
|
|
|
|
client *http.Client
|
|
}
|
|
|
|
func NewHttpClient(sendTxUrl string, sendBundleUrl string) *HttpClient {
|
|
// 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 &HttpClient{
|
|
sendTxUrl: sendTxUrl,
|
|
sendBundleUrl: sendBundleUrl,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (c *HttpClient) 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")
|
|
|
|
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 *HttpClient) SendBundle(ctx context.Context, txs []*solana.Transaction) error {
|
|
if c.sendBundleUrl == "" {
|
|
return fmt.Errorf("send bundle url is empty")
|
|
}
|
|
|
|
txns := make([]string, 0, len(txs))
|
|
for _, tx := range txs {
|
|
txns = append(txns, tx.MustToBase64())
|
|
}
|
|
|
|
request := JsonRpcRequest{
|
|
Jsonrpc: "2.0",
|
|
Method: "sendBundle",
|
|
Params: []any{txns, SendTransactionParam{Encoding: "base64", SkipPreflight: true}},
|
|
Id: 1,
|
|
}
|
|
|
|
jsonData, err := json.Marshal(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := c.client.Post(c.sendBundleUrl, "application/json", bytes.NewReader(jsonData))
|
|
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
|
|
}
|