For the complete documentation index, see llms.txt. This page is also available as Markdown.

Solana Send Transaction in Binary

介紹BlockRazor Solana Transaction Sending模式下Send Transaction in Binary的集成方法

介紹

Send in Binary 用於在Solana上發送已簽名的交易。與 Send Transaction 方式相比,它支持已簽名交易以binary形式提交,而不用先轉成Base64。這樣做的好處是少了一層編碼與解碼開銷,而且由於數據包更小交易在傳輸時不容易分包,可以降低因此導致的高延遲。

端點

  • POST /v2/sendBinaryTransaction

請求示例

curl --location 'http://frankfurt.solana.blockrazor.xyz:443/v2/sendBinaryTransaction?auth=<auth_token>' \
--data-binary @transaction.bin
package main

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"math/rand"
	"net/http"
	"time"

	"github.com/gagliardetto/solana-go"
	"github.com/gagliardetto/solana-go/programs/system"
	"github.com/gagliardetto/solana-go/rpc"
)

const (
	httpEndpoint = "http://frankfurt.solana.blockrazor.xyz:443/v2/sendBinaryTransaction?auth=<auth_token>"
	mainNetRPC   = ""
	privateKey   = ""
	publicKey    = ""
	amount       = 200_000
	tipAmount    = 1_000_000
)

var tipAccounts = []string{
	"Gywj98ophM7GmkDdaWs4isqZnDdFCW7B46TXmKfvyqSm",
	"FjmZZrFvhnqqb9ThCuMVnENaM3JGVuGWNyCAxRJcFpg9",
	"6No2i3aawzHsjtThw81iq1EXPJN6rh8eSJCLaYZfKDTG",
	"A9cWowVAiHe9pJfKAj3TJiN9VpbzMUq6E4kEvf5mUT22",
	"68Pwb4jS7eZATjDfhmTXgRJjCiZmw1L7Huy4HNpnxJ3o",
	"4ABhJh5rZPjv63RBJBuyWzBK3g9gWMUQdTZP2kiW31V9",
	"B2M4NG5eyZp5SBQrSdtemzk5TqVuaWGQnowGaCBt8GyM",
	"5jA59cXMKQqZAVdtopv8q3yyw9SYfiE3vUCbt7p8MfVf",
	"5YktoWygr1Bp9wiS1xtMtUki1PeYuuzuCF98tqwYxf61",
	"295Avbam4qGShBYK7E9H5Ldew4B3WyJGmgmXfiWdeeyV",
	"EDi4rSy2LZgKJX74mbLTFk4mxoTgT6F7HxxzG2HBAFyK",
	"BnGKHAC386n4Qmv9xtpBVbRaUTKixjBe3oagkPFKtoy6",
	"Dd7K2Fp7AtoN8xCghKDRmyqr5U169t48Tw5fEd3wT9mq",
	"AP6qExwrbRgBAVaehg4b5xHENX815sMabtBzUzVB4v8S",
}

var httpClient = &http.Client{
	Timeout: 10 * time.Second,
}

func main() {
	if err := sendTx(); err != nil {
		fmt.Printf("send tx failed: %v\n", err)
	}
}

func sendTx() error {
	account, err := solana.WalletFromPrivateKeyBase58(privateKey)
	if err != nil {
		return err
	}

	receivePub := solana.MustPublicKeyFromBase58(publicKey)
	tipPub := solana.MustPublicKeyFromBase58(tipAccounts[rand.Intn(len(tipAccounts))])

	rpcClient := rpc.New(mainNetRPC)
	blockhash, err := rpcClient.GetLatestBlockhash(context.TODO(), rpc.CommitmentFinalized)
	if err != nil {
		return fmt.Errorf("[get blockhash] %v", err)
	}

	transferIx := system.NewTransferInstruction(amount, account.PublicKey(), receivePub).Build()
	tipIx := system.NewTransferInstruction(tipAmount, account.PublicKey(), tipPub).Build()

	tx, err := solana.NewTransaction(
		[]solana.Instruction{tipIx, transferIx},
		blockhash.Value.Blockhash,
		solana.TransactionPayer(account.PublicKey()),
	)
	if err != nil {
		return fmt.Errorf("build tx error: %v", err)
	}

	_, err = tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
		if account.PublicKey().Equals(key) {
			return &account.PrivateKey
		}
		return nil
	})
	if err != nil {
		return fmt.Errorf("sign tx error: %v", err)
	}

	binTx, err := tx.MarshalBinary()
	if err != nil {
		return fmt.Errorf("marshal tx error: %v", err)
	}

	req, err := http.NewRequest("POST", httpEndpoint, bytes.NewReader(binTx))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/octet-stream")

	resp, err := httpClient.Do(req)
	if err != nil {
		return fmt.Errorf("send http error: %v", err)
	}
	defer resp.Body.Close()

	bodyBytes, _ := io.ReadAll(resp.Body)
	fmt.Printf("[send tx] response: %s\n", string(bodyBytes))
	return nil
}

注意:

  • 認證 (auth) 和 請求 (request) 參數必須以 URI 參數的形式填入URL,比如http://frankfurt.solana.blockrazor.xyz:443/v2/sendBinaryTransaction?auth=<auth_token>&mode=fast&revertProtection=true

請求參數

字段
必填
示例
備注

binaryTransaction

transaction.bin

已簽名的交易,binary格式

mode

"fast" "sandwichMitigation"

BlockRazor支持fast和sandwichMitigation兩種模式,默認為fast模式。 在fast模式中,交易會基於全球分布式高性能網絡和高質量SWQoS質押鏈路被飽和式發送,以最低延遲到達Leader節點。 在sandwichMitigation模式中,交易會被發往BlockRazor高度信任的SWQoS質押鏈路,同時交易會跳過黑名單Leader(經BlockRazor三明治監測機制動態精確識別)的slot。在此模式下,請不要用durable nonce發送交易,這會使三明治保護失效。

safeWindow

3

sandwichMitigation模式中用於確定交易發送時機的參數,數字代表從當前slot起連續白名單驗證者的slot數量,比如設定3,則交易會僅在當前起連續3個slot都屬於白名單驗證者時發送。 safeWindow的參數範圍是3-13,數字越大防治三明治攻擊效果越好,但可能會對上鏈速度有一定影響。如不設定,則默認為3。

revertProtection

false

默認為false。如設置為true,交易不會在鏈上執行失敗,但上鏈速度会受到影响且存在无法上链的可能,請根據實際需求謹慎選擇開啓。

返回參數

狀態碼
消息
釋義

200

OK

請求正常

400

BadRequest

請求參數不合法

403

Forbidden

請求被拒絕,auth為空、無效或過期

500

InternalServerError

內部服務錯誤

最後更新於

這有幫助嗎?