solana-go-sdk

module
v1.18.69 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: MIT

README

Solana Go SDK

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

Guide

Getting Started

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

import (
	"context"
	"fmt"
	"log"

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

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

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

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

WebSocket (In-Progress)

All interfaces of websocket follow the solana subscription websocket docs.

package main

import (
	"context"
	"fmt"
	"log"
	"strings"
	"github.com/0x15d3f/solana-go-sdk/ws"
)

func main() {
	ws := ws.ConnectClient(ws.WebsocketMainnetEndpoint)

	conn, err := ws.LogsSubscribeMentions(
		context.TODO(),
		"M2mx93ekt1fmXSVkTrUL9xVFHkmME8HTUi5Cyc5aF7K"
		&ws.LogSubscribeConfig{
			commitment: rpc.CommitmentProcessed
		}
	)

	/*
	OR

	conn, err := ws.LogsSubscribe(
		context.TODO(),
		ws.AllTransactions,
		&ws.LogSubscribeConfig{
			commitment: rpc.CommitmentProcessed
		}
	)

	*/

	for {
		data, err := conn.Recv()

		if err != nil {
			log.Printf("error while streaming logs: %s", err.Error())
			continue
		}

		log.Printf("signature:%s\nlogs:%s\n"data.Signature, strings.Join(data.Logs, ""))
	}



}

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/0x15d3f/solana-go-sdk/client"
	"github.com/0x15d3f/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.

  • Instruction (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

TODO
  • TPU Client implementation
  • Websocket Pub/Sub implementation
  • More Metaplex Program implementations

Directories

Path Synopsis
docs
pkg
program
rpc
ws

Jump to

Keyboard shortcuts

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