chore: add swqos client
This commit is contained in:
94
pkg/swqos/clients/astralane_client.go
Normal file
94
pkg/swqos/clients/astralane_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 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")
|
||||
}
|
||||
82
pkg/swqos/clients/block_razor_client.go
Normal file
82
pkg/swqos/clients/block_razor_client.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
pb "github.com/BlockRazorinc/solana-trader-client-go/pb/serverpb"
|
||||
"github.com/gagliardetto/solana-go"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
type BlockRazorClient struct {
|
||||
conn *grpc.ClientConn
|
||||
client pb.ServerClient
|
||||
}
|
||||
|
||||
type Authentication struct {
|
||||
apiKey string
|
||||
}
|
||||
|
||||
func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
|
||||
return map[string]string{"apikey": a.apiKey}, nil
|
||||
}
|
||||
|
||||
func (a *Authentication) RequireTransportSecurity() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func NewBlockRazorClient(ctx context.Context, endpoint string) (*BlockRazorClient, error) {
|
||||
if endpoint == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// setup grpc connect
|
||||
conn, err := grpc.NewClient(
|
||||
endpoint,
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithKeepaliveParams(kacp),
|
||||
grpc.WithPerRPCCredentials(&Authentication{apiKey: "xhMIRxybodR6U35cDdTjQVIkUPPVVjKC5ynKWQMdL9fm8DnwoEFFAlj1E4ySBADo4xLh3RVTRgLQI2BTzegxb3N5CIXThEEJ"}), // TODO: maybe config?
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("connect error: %v", err)
|
||||
}
|
||||
|
||||
// use the Gateway client connection interface
|
||||
client := pb.NewServerClient(conn)
|
||||
|
||||
// grpc request warmup
|
||||
_, err = client.GetHealth(ctx, &pb.HealthRequest{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &BlockRazorClient{
|
||||
conn: conn,
|
||||
client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *BlockRazorClient) SendTransaction(ctx context.Context, tx *solana.Transaction) error {
|
||||
if c == nil {
|
||||
return errors.New("block razor client is nil")
|
||||
}
|
||||
|
||||
txBase64, _ := tx.ToBase64()
|
||||
_, err := c.client.SendTransaction(ctx, &pb.SendRequest{
|
||||
Transaction: txBase64,
|
||||
Mode: "fast",
|
||||
SafeWindow: 3,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *BlockRazorClient) SendBundle(ctx context.Context, txs []*solana.Transaction) error {
|
||||
return fmt.Errorf("block razor client not support send bundle")
|
||||
}
|
||||
110
pkg/swqos/clients/bloxroute_client.go
Normal file
110
pkg/swqos/clients/bloxroute_client.go
Normal file
@@ -0,0 +1,110 @@
|
||||
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")
|
||||
}
|
||||
13
pkg/swqos/clients/common.go
Normal file
13
pkg/swqos/clients/common.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/keepalive"
|
||||
)
|
||||
|
||||
var kacp = keepalive.ClientParameters{
|
||||
Time: 10 * time.Second, // send pings every 10 seconds if there is no activity
|
||||
Timeout: time.Second, // wait 1 second for ping ack before considering the connection dead
|
||||
PermitWithoutStream: true, // send pings even without active streams
|
||||
}
|
||||
90
pkg/swqos/clients/flash_block_client.go
Normal file
90
pkg/swqos/clients/flash_block_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 FlashBlockBundleRequest struct {
|
||||
Transactions []string `json:"transactions"`
|
||||
}
|
||||
|
||||
type FlashBlockClient struct {
|
||||
sendTxUrl string
|
||||
sendBundleUrl string
|
||||
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewFlashBlockClient(sendTxUrl string, sendBundleUrl string) *FlashBlockClient {
|
||||
// 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 &FlashBlockClient{
|
||||
sendTxUrl: sendTxUrl,
|
||||
sendBundleUrl: sendBundleUrl,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *FlashBlockClient) SendTransaction(ctx context.Context, tx *solana.Transaction) error {
|
||||
return c.SendBundle(ctx, []*solana.Transaction{tx})
|
||||
}
|
||||
|
||||
func (c *FlashBlockClient) SendBundle(ctx context.Context, txs []*solana.Transaction) error {
|
||||
request := FlashBlockBundleRequest{
|
||||
Transactions: make([]string, 0, len(txs)),
|
||||
}
|
||||
|
||||
for _, tx := range txs {
|
||||
request.Transactions = append(request.Transactions, tx.MustToBase64())
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.sendBundleUrl, bytes.NewReader(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "0b1ef3abade04426") // 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
|
||||
}
|
||||
151
pkg/swqos/clients/http_client.go
Normal file
151
pkg/swqos/clients/http_client.go
Normal file
@@ -0,0 +1,151 @@
|
||||
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
|
||||
}
|
||||
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")
|
||||
}
|
||||
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