Go
Transaction Construction Example
package main
import (
"context"
pb "example/pb/serverpb"
"fmt"
"math/rand"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/system"
"github.com/gagliardetto/solana-go/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
// BlockRazor relay test endpoint address
blzRelayEndpoint = "frankfurt.solana-grpc.blockrazor.xyz:80"
// replace your solana rpc endpoint
mainNetRPC = ""
// replace your authKey
authKey = ""
// relace your private key(base58)
privateKey = ""
// publicKey(base58)
publicKey = ""
// transfer amount
amount = 200_000
// tip amount
tipAmount = 1_000_000
)
var tipAccounts = []string{
"Gywj98ophM7GmkDdaWs4isqZnDdFCW7B46TXmKfvyqSm",
"FjmZZrFvhnqqb9ThCuMVnENaM3JGVuGWNyCAxRJcFpg9",
"6No2i3aawzHsjtThw81iq1EXPJN6rh8eSJCLaYZfKDTG",
"A9cWowVAiHe9pJfKAj3TJiN9VpbzMUq6E4kEvf5mUT22",
}
func main() {
var err error
account, err := solana.WalletFromPrivateKeyBase58(privateKey)
receivePub := solana.MustPublicKeyFromBase58(publicKey)
// setup grpc connect
conn, err := grpc.NewClient(blzRelayEndpoint,
// grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithPerRPCCredentials(&Authentication{authKey}),
)
if err != nil {
panic(fmt.Sprintf("connect error: %v", err))
}
// use the Gateway client connection interface
client := pb.NewServerClient(conn)
// grpc request warmup
client.GetHealth(context.Background(), &pb.HealthRequest{})
// new rpc client and get latest block hash
rpcClient := rpc.New(mainNetRPC)
blockhash, err := rpcClient.GetLatestBlockhash(context.TODO(), rpc.CommitmentFinalized)
if err != nil {
panic(fmt.Sprintf("[get latest block hash] error: %v", err))
}
tipAccount := tipAccounts[rand.Intn(len(tipAccounts))]
// construct instruction
transferIx := system.NewTransferInstruction(amount, account.PublicKey(), receivePub).Build()
tipIx := system.NewTransferInstruction(tipAmount, account.PublicKey(), solana.MustPublicKeyFromBase58(tipAccount)).Build()
// construct transation, replace your transation
tx, err := solana.NewTransaction(
[]solana.Instruction{transferIx, tipIx},
blockhash.Value.Blockhash,
solana.TransactionPayer(account.PublicKey()),
)
if err != nil {
panic(fmt.Sprintf("new tx error: %v", err))
}
// transaction sign
_, err = tx.Sign(
func(key solana.PublicKey) *solana.PrivateKey {
if account.PublicKey().Equals(key) {
return &account.PrivateKey
}
return nil
},
)
if err != nil {
panic(fmt.Sprintf("sign tx error: %v", err))
}
txBase64, _ := tx.ToBase64()
sendRes, err := client.SendTransaction(context.TODO(), &pb.SendRequest{
Transaction: txBase64,
Mode: "fast",
})
if err != nil {
panic(fmt.Sprintf("[send tx] error: %v", err))
}
fmt.Printf("[send tx] response: %+v \n", sendRes)
return
}
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
}
Proto
syntax = "proto3";
package serverpb;
option go_package = "./pb/serverpb";
service Server {
rpc SendTransaction(SendRequest) returns(SendResponse) {};
rpc GetHealth(HealthRequest) returns(HealthResponse) {};
}
message SendRequest {
string transaction = 1;
string mode = 2;
}
message SendResponse {
string signature = 1;
}
message HealthRequest {
}
message HealthResponse {
string status = 1;
}
Response Example
Success
signature:"2PCgCdD5gm4852ooyT2LQqiTgcau28hRNWCqBHCJ33EhhBsuAzAxYGLbS8kAmAMu7DW8JSrrKtaCGMZqQbtbGpgx"
Error
error: rpc error: code = Unknown desc = Insufficient tip, please increase the tip amount and try again, at least 1000000 lamports
Last updated