Solana Go SDK

Jul 31, 2024 | Blockchain

GitHub go.mod Go version
GitHub release (latest SemVer)

Guide

Getting Started

Installation

To get started with the Solana Go SDK, you need to install it using Go’s package manager. Open your terminal and execute the command below:

sh
go get -v github.com/blocto/solana-go-sdk

Example: Hello World

The following is a simple example that demonstrates how to connect to the Solana blockchain and fetch the version of the Solana core. Here’s how you can do it:

go
package main

import (
    "context"
    "fmt"
    "log"
    "github.com/blocto/solana-go-sdk/client"
    "github.com/blocto/solana-go-sdk/rpc"
)

func main() {
    c := client.NewClient(rpc.MainnetRPCEndpoint)
    resp, err := c.GetVersion(context.TODO())
    if err != nil {
        log.Fatalf("failed to get version info, err: %v", err)
    }
    fmt.Println("Version:", resp.SolanaCore)
}

Understanding the Code through Analogy

Think of this code as a friendly librarian helping you locate a book in a massive library (the Solana blockchain). The librarian (the client) stands at the front desk and waits for you to ask a question. You ask, “What is the latest version of the library’s catalog?”. Upon receiving your request, the librarian checks the latest version of the catalog (calling GetVersion) and provides you the answer. If the librarian can’t find the information, they will express a polite apology along with the reason.

Working with RPC

The SDK allows you to interact with various Remote Procedure Calls (RPC). All interfaces of the RPC follow the Solana’s JSON-RPC documentation. You can customize the functions to get the results or responses based on your requirements. For example, fetching a wallet balance looks like this:

go
package main

import (
    "context"
    "fmt"
    "log"
    "github.com/blocto/solana-go-sdk/client"
    "github.com/blocto/solana-go-sdk/rpc"
)

func main() {
    c := client.NewClient(rpc.DevnetRPCEndpoint)
    
    // Get balance
    balance, err := c.GetBalance(context.TODO(), "RNfp4xTbBb4C3kcv2KqtAj8mu4YhMHxqm1Skg9uchZ7")
    if err != nil {
        log.Fatalf("failed to get balance, err: %v", err)
    }
    fmt.Printf("Balance: %v\n", balance)
    
    // Get balance with specific commitment
    balance, err = c.GetBalanceWithConfig(context.TODO(), "RNfp4xTbBb4C3kcv2KqtAj8mu4YhMHxqm1Skg9uchZ7", rpc.GetBalanceConfig{Commitment: rpc.CommitmentProcessed})
    if err != nil {
        log.Fatalf("failed to get balance with config, err: %v", err)
    }
    fmt.Printf("Balance with config: %v\n", balance)
    
    // For advanced usage, fetch full RPC response
    res, err := c.RpcClient.GetBalance(context.TODO(), "RNfp4xTbBb4C3kcv2KqtAj8mu4YhMHxqm1Skg9uchZ7")
    if err != nil {
        log.Fatalf("failed to get balance via RPC client, err: %v", err)
    }
    fmt.Printf("Response: %+v\n", res)
}

Programming Models

In the Solana ecosystem, several important concepts need to be understood:

  • Program: This resides in the program folder.
  • Pubkey: This is a unique identification key and is located in the common folder.
  • Instruction: It contains multiple Pubkeys and the program ID.
  • Message: This encompasses numerous instructions.
  • Transaction: It includes a message along with several signatures.
  • Account: This represents a public/private key pair and is found in the types folder.

More Examples

For additional examples, you can follow the examples folder in the GitHub repository.

Troubleshooting

If you run into any issues while using the Solana Go SDK, here are some troubleshooting ideas:

  • Check your Go installation and ensure you are using a compatible version.
  • Ensure you have internet access and can connect to the Solana RPC endpoints.
  • Validate that the public key you are using is correct and exists on the blockchain.
  • Look for typos or syntax errors in your code.
  • Consult the issues section of the GitHub repository for solutions to common problems.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox