NextBlock
SupportWebsiteDashboard
  • What is NextBlock?
  • Pricing & Rate Limits
  • Getting Started
    • Quickstart
      • GoLang Sample Code
      • Bundle Sending Sample
    • Contact
  • API
    • Basics
    • Authentication
    • Submit Transaction
    • Submit Batched Transactions
    • Examples
      • Golang
        • Connection
        • Keepalive
        • Tip floor stream
        • Submit single transactions
        • Submit batched transactions
      • Http
      • Rust
Powered by GitBook
On this page
Export as PDF
  1. Getting Started
  2. Quickstart

Bundle Sending Sample

package main

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"github.com/gagliardetto/solana-go"
	"github.com/gagliardetto/solana-go/programs/system"
	"github.com/gagliardetto/solana-go/rpc"
	"io"
	"net/http"
	"testing"
)

type Payload struct {
	Transaction TransactionMessage `json:"transaction"`
}

type TransactionMessage struct {
	Content string `json:"content"`
}

func TestSendTx(T *testing.T) {
	cli := http.Client{}
	privateKey := solana.MustPrivateKeyFromBase58("")
	rpcClient := rpc.New("")
	bh, _ := rpcClient.GetLatestBlockhash(context.TODO(), rpc.CommitmentFinalized)
	txBuilder := solana.NewTransactionBuilder()
	txBuilder.SetRecentBlockHash(bh.Value.Blockhash)
	txBuilder.AddInstruction(system.NewTransferInstruction(1000001, privateKey.PublicKey(), solana.MustPublicKeyFromBase58("nEXTBLockYgngeRmRrjDV31mGSekVPqZoMGhQEZtPVG")).Build())

	tx, _ := txBuilder.Build()
	tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
		return &privateKey
	})
	tx64Str := tx.MustToBase64()

	txMsg := TransactionMessage{
		Content: tx64Str,
	}
	entry := Entry{
		Transaction: txMsg,
	}

	p := BatchPayload{
		Transactions: []Entry{entry},
	}

	fmt.Println("Sending transaction", tx.Signatures[0].String())

	txBuilder = solana.NewTransactionBuilder()
	txBuilder.SetRecentBlockHash(bh.Value.Blockhash)
	txBuilder.AddInstruction(system.NewTransferInstruction(1000002, privateKey.PublicKey(), solana.MustPublicKeyFromBase58("nEXTBLockYgngeRmRrjDV31mGSekVPqZoMGhQEZtPVG")).Build())

	tx, _ = txBuilder.Build()
	tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
		return &privateKey
	})
	tx64Str = tx.MustToBase64()

	txMsg = TransactionMessage{
		Content: tx64Str,
	}
	entry = Entry{
		Transaction: txMsg,
	}

	p.Transactions = append(p.Transactions, entry)

	t, _ := json.Marshal(p)

	fmt.Println(string(t))
	fmt.Println(privateKey.PublicKey().String())

	req, err := http.NewRequest("POST", "https://direct-ny.nextblock.io/api/v2/submit-batch", bytes.NewBuffer(t))
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("authorization", "advanced123456789-ABCD%2FyfHouQJyiWObY1ntyFSBI3G3NNCczaWKRJPvc%3D")
	if err != nil {
		T.Error(err)
		return
	}
	do, err := cli.Do(req)
	if err != nil {
		T.Error(err)
		return
	}
	defer do.Body.Close()
	b, _ := io.ReadAll(do.Body)
	T.Log(string(b))
	if do.StatusCode == 200 {
		T.Log("Sent transaction with signature ", tx.Signatures[0].String())
	} else {
		fmt.Println(do.StatusCode)
		T.Error("Failed to send transaction")
	}
}

CURL Example

curl -XPOST -H 'authorization: api key' -H 'Content-Type: application/json' 'https://fra.nextblock.io/api/v2/submit-batch' -d '{"entries": [ { "transaction": { "content": "ASrTNkPOT...." } } ] }'

PreviousGoLang Sample CodeNextContact

Last updated 2 months ago