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 the Go client
git clone https://github.com/nextblock-ag/nextblock-proto
# Follow the Go generation instructions in that repositoryExample
package main
import (
"context"
"crypto/x509"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
)
type ApiKeyCredentials struct {
apiKey string
requireTLS bool
}
func NewApiKeyCredentials(apiKey string, requireTLS bool) *ApiKeyCredentials {
return &ApiKeyCredentials{
apiKey: apiKey,
requireTLS: requireTLS,
}
}
func (a *ApiKeyCredentials) RequireTransportSecurity() bool {
return a.requireTLS
}
func (a *ApiKeyCredentials) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{
"authorization": a.apiKey,
}, nil
}
var keepAliveParams = keepalive.ClientParameters{
Time: time.Minute,
Timeout: 15 * time.Second,
PermitWithoutStream: true,
}
func ConnectToNextblock(address string, apiKey string, useTLS bool) (*grpc.ClientConn, error) {
var opts []grpc.DialOption
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()))
}
opts = append(opts, grpc.WithKeepaliveParams(keepAliveParams))
opts = append(opts, grpc.WithPerRPCCredentials(NewApiKeyCredentials(apiKey, useTLS)))
return grpc.NewClient(address, opts...)
}Usage Example
Connection Best Practices
Available Endpoints
Last updated