Connection
Prerequisites
go mod init nextblock-example
go get google.golang.org/grpc
go get github.com/gagliardetto/solana-go
# Clone the proto specs and generate Go client
git clone https://github.com/nextblock-ag/nextblock-proto
# Generate the Go client using protoc (see proto repo for instructions)Example
import (
"context"
"time"
"crypto/x509"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
// Import your generated proto client here
// "path/to/your/generated/api"
)
// ApiKeyCredentials implements gRPC credentials interface for API key authentication
type ApiKeyCredentials struct {
apiKey string
}
func NewApiKeyCredentials(apiKey string) *ApiKeyCredentials {
return &ApiKeyCredentials{apiKey: apiKey}
}
func (a *ApiKeyCredentials) RequireTransportSecurity() bool {
return false // Set to true if using TLS
}
func (a *ApiKeyCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"authorization": a.apiKey,
}, nil
}
// Keep-alive parameters for maintaining persistent connections
var keepAliveParams = keepalive.ClientParameters{
Time: time.Minute, // Send ping every minute
Timeout: 15 * time.Second, // Wait 15 seconds for ping response
PermitWithoutStream: true, // Allow pings without active streams
}
// ConnectToNextblock establishes a connection to NextBlock API
func ConnectToNextblock(address string, apiKey string, useTLS bool) (*grpc.ClientConn, error) {
var opts []grpc.DialOption
// Configure transport credentials
if useTLS {
pool, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
creds := credentials.NewClientTLSFromCert(pool, "")
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
// Add keep-alive parameters for better connection management
opts = append(opts, grpc.WithKeepaliveParams(keepAliveParams))
// Add API key authentication
opts = append(opts, grpc.WithPerRPCCredentials(NewApiKeyCredentials(apiKey)))
// Establish the connection
conn, err := grpc.NewClient(address, opts...)
if err != nil {
return nil, err
}
return conn, nil
}Usage Example
Connection Best Practices
Available Endpoints
Last updated