Solana Send Transaction in Binary
Introduce the integration of 'Send Transaction in Binary' in BlockRazor Solana Transaction Sending mode
Introduction
Solana's transaction sending service is not bound to the subscription plan, with rate limit default to 3 TPS. If you need to increase the TPS limit, please contact us and we will handle it as soon as possible.
Send in Binary is used to send signed transactions on Solana. Compared with the Send Transaction, it allows signed transactions to be submitted in binary format instead of being converted to Base64 first. The benefits are that it removes one layer of encoding and decoding overhead, and because the payload is smaller, the transaction is less likely to be split into multiple packets during transmission, which can help reduce the resulting latency.
Endpoint
POST /v2/sendBinaryTransaction
Request Example
curl --location 'http://frankfurt.solana.blockrazor.xyz:443/v2/sendBinaryTransaction?auth=<auth_token>' \
--data-binary @transaction.binpackage 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
}Request Parameter
transaction
Mandatory
transaction.bin
Fully signed transactions, binary format
mode
Optional
"fast" "sandwichMitigation"
BlockRazor offers two modes: Fast and SandwichMitigation, with Fast as the default. In fast mode, transactions are sent based on globally distributed high-performance network and high-quality SWQoS, reaching the Leader node with the lowest latency. In sandwichMitigation mode, BlockRazoz will route transactions to the trusted SWQoS and skip the slot of the blacklisted Leader (dynamically identified by the BlockRazor sandwich monitoring mechanism). In this mode, DO NOT send transactions using durable nonce, as it will cause the sandwich protection to become ineffective.
safeWindow
Optional
3
safeWindow is used to determine the timing of transaction sending in sandwichMitigation mode and represents the number of consecutive slots of whitelist validators. For example, if it is set to 3, the transaction will only be sent when 3 consecutive slots from the current slot belong to whitelist validators. The range of safeWindow is 3-13. The larger the number, the better the effect of mitigating the sandwich attack, but it may have a certain impact on the rate of inclusion. If not set, the default is 3.
revertProtection
Optional
false
The default value is false. If set to true, the transaction will not fail on chain, but the speed of inclusion will be affected and there is a possibility that it cannot be included. Please choose to enable it carefully according to actual needs.
Response
200
OK
The request is normal
400
BadRequest
Invalid parameter
403
Forbidden
Request denied, as the authentication (auth) is empty, invalid, or expired.
500
InternalServerError
The server encountered an unexpected condition that prevented it from fulfilling the request
Last updated
Was this helpful?