91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
|
|
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
|
||
|
|
}
|