sendTransaction
介紹
sendTransaction
用於在Solana上發送已簽名的交易,支持gRPC協議。
端點
地区
URL
法蘭克福
frankfurt.solana-grpc.blockrazor.xyz:80
香港
hongkong.solana-grpc.blockrazor.xyz:80
紐約
newyork.solana-grpc.blockrazor.xyz:80
流控說明
Tier 4
Tier 3
Tier 2
Tier 1
Tier 0
10 TPM
5 TPS
50 TPS
100 TPS
Custom
請求參數
字段
必選
格式
示例
備注
signedtx
是
String
"AVXo5X……gEAAAA="
已完全簽名的交易,兼容base 64和base 58的編碼格式,建議用base 64
skipPreflight
否
Bool
false
默认为false,不跳过交易preflight检查,如设置为true,则跳过交易preflight检查
preflightCommitment
否
String
confirmed
構建交易
Priority Fee & Tip
賬戶地址
Gywj98ophM7GmkDdaWs4isqZnDdFCW7B46TXmKfvyqSm
交易構建示例
package main
import (
"context"
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/insecure"
)
const (
// BlockRazor relay 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 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(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",
SkipPreflight: false,
PreflightCommitment: "confirmed",
})
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;
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
Last updated