Sample Code
package main
import (
"bytes"
"context"
"encoding/json"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/system"
"github.com/gagliardetto/solana-go/rpc"
"io"
"net/http"
"testing"
)
type Payload struct {
Transaction TransactionMessage `json:"transaction"`
}
type TransactionMessage struct {
Content string `json:"content"`
}
func TestSendTx(T *testing.T) {
cli := http.Client{}
privateKey := solana.MustPrivateKeyFromBase58("")
rpcClient := rpc.New("")
bh, _ := rpcClient.GetLatestBlockhash(context.TODO(), rpc.CommitmentFinalized)
txBuilder := solana.NewTransactionBuilder()
txBuilder.SetRecentBlockHash(bh.Value.Blockhash)
txBuilder.AddInstruction(system.NewTransferInstruction(1000000, privateKey.PublicKey(), solana.MustPublicKeyFromBase58("nextBLoCkPMgmG8ZgJtABeScP35qLa2AMCNKntAP7Xc")).Build())
tx, _ := txBuilder.Build()
tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
return &privateKey
})
tx64Str := tx.MustToBase64()
txMsg := TransactionMessage{
Content: tx64Str,
}
p := Payload{
Transaction: txMsg,
}
t, _ := json.Marshal(p)
req, err := http.NewRequest("POST", "https://fra.nextblock.io/api/v2/submit", bytes.NewBuffer(t))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("authorization", "")
if err != nil {
T.Error(err)
return
}
do, err := cli.Do(req)
if err != nil {
T.Error(err)
return
}
defer do.Body.Close()
b, _ := io.ReadAll(do.Body)
T.Log(string(b))
if do.StatusCode == 200 {
T.Log("Sent transaction with signature ", tx.Signatures[0].String())
}
}
CURL Example
curl -XPOST -H 'authorization: api key' -H 'Content-Type: application/json' 'https://fra.nextblock.io/api/v2/submit' -d '{ "transaction": {"content": "AjF+B...BCQ=="} }'
Last updated