95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package clients
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gagliardetto/solana-go"
|
|
)
|
|
|
|
type AstralaneClient struct {
|
|
sendTxUrl string
|
|
|
|
client *http.Client
|
|
}
|
|
|
|
func NewAstralaneClient(sendTxUrl string) *AstralaneClient {
|
|
// 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 &AstralaneClient{
|
|
sendTxUrl: sendTxUrl,
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (c *AstralaneClient) 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", "zhaozNc5OIadLPI3r9nUVVPpCZcQAUjngO6Tgr5XUJcmBrIisFaaZF81Ijn01Ytn") // 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 *AstralaneClient) SendBundle(ctx context.Context, txs []*solana.Transaction) error {
|
|
return fmt.Errorf("astralane client not support send bundle")
|
|
}
|