package main
import (
"context"
"crypto/tls"
pb "example/pb/serverpb"
"fmt"
"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"
"google.golang.org/grpc/credentials/insecure"
)
const (
// BlockRazor relay test endpoint address
blzRelayEndpoint = "grpc.solana-rpc.blockrazor.xyz:443"
// 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 account
tipAccount = "Gywj98ophM7GmkDdaWs4isqZnDdFCW7B46TXmKfvyqSm"
tipAmount = 1_000_000
)
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{})), // 對接通用端點時開啟tls配置
// 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))
}
// 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
}
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;
bool skipPreflight = 3;
string preflightCommitment = 4;
}
message SendResponse {
string signature = 1;
}
message HealthRequest {
}
message HealthResponse {
string status = 1;
}
signature:"2PCgCdD5gm4852ooyT2LQqiTgcau28hRNWCqBHCJ33EhhBsuAzAxYGLbS8kAmAMu7DW8JSrrKtaCGMZqQbtbGpgx"
error: rpc error: code = Unknown desc = Insufficient tip, please increase the tip amount and try again, at least 1000000 lamports