solana-go-sdk

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2024 License: MIT

README

Solana Go SDK

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

Guide

Getting Started

Installing
go get -v github.com/HFFP/solana-go-sdk
Example
Hello World
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/HFFP/solana-go-sdk/client"
	"github.com/HFFP/solana-go-sdk/rpc"
)

func main() {
	c := client.NewClient(rpc.MainnetRPCEndpoint)

	// If you would like to customize the http client used to make the
	// requests you could do something like this
	// c := client.New(rpc.WithEndpoint(rpc.MainnetRPCEndpoint),rpc.WithHTTPClient(customHTTPClient))

	resp, err := c.GetVersion(context.TODO())
	if err != nil {
		log.Fatalf("failed to version info, err: %v", err)
	}

	fmt.Println("version", resp.SolanaCore)
}

RPC

All interfaces of rpc follow the solana's json-rpc docs.

The implementation of client in this project separate into two parts, rpc and wrapped. The wrapped only returns main result value and the rpc returns whole rpc response. You can switch it by yourself for different situation. Take getBalance as example:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/HFFP/solana-go-sdk/client"
	"github.com/HFFP/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 sepcific commitment
	balance, err = c.GetBalanceWithConfig(
		context.TODO(),
		"RNfp4xTbBb4C3kcv2KqtAj8mu4YhMHxqm1Skg9uchZ7",
		rpc.GetBalanceConfig{
			Commitment: rpc.CommitmentProcessed,
		},
	)
	if err != nil {
		log.Fatalf("failed to get balance with cfg, err: %v", err)
	}
	fmt.Printf("balance: %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)
}

Programing model & Program

There are some important tpyes in solana.

  • Program

resides in the program/ folder.

  • Pubkey (a basic identity of key)

resides in the common/ folder.

  • Insturciton (contain many pubkeys and program ID)
  • Message (contain many instructions)
  • Transaction (contain a message and many signatures)
  • Account (a pub/pri keypair )

reside in the types/ folder.

More Example

for more examples, follow examples/ folder

Directories

Path Synopsis
docs
internal
pkg
program
assotokenprog
Deprecated: please use github.com/HFFP/solana-go-sdk/program/associated_token_account
Deprecated: please use github.com/HFFP/solana-go-sdk/program/associated_token_account
cmptbdgprog
Deprecated: please use github.com/HFFP/solana-go-sdk/program/compute_budget
Deprecated: please use github.com/HFFP/solana-go-sdk/program/compute_budget
memoprog
Deprecated: please use github.com/HFFP/solana-go-sdk/program/memo
Deprecated: please use github.com/HFFP/solana-go-sdk/program/memo
metaplex/tokenmeta
Deprecated: please use github.com/HFFP/solana-go-sdk/program/metaplex/token_metadata
Deprecated: please use github.com/HFFP/solana-go-sdk/program/metaplex/token_metadata
nsprog
Deprecated: please use github.com/HFFP/solana-go-sdk/program/name_service
Deprecated: please use github.com/HFFP/solana-go-sdk/program/name_service
stakeprog
Deprecated: please use github.com/HFFP/solana-go-sdk/program/stake
Deprecated: please use github.com/HFFP/solana-go-sdk/program/stake
sysprog
Deprecated: please use github.com/HFFP/solana-go-sdk/program/system
Deprecated: please use github.com/HFFP/solana-go-sdk/program/system
tokenprog
Deprecated: please use github.com/HFFP/solana-go-sdk/program/token
Deprecated: please use github.com/HFFP/solana-go-sdk/program/token

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL