solana

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: Apache-2.0 Imports: 19 Imported by: 21

README

StreamingFast Solana library for Go

reference License

Go library to interface with Solana nodes's JSON-RPC interface, Solana's SPL tokens various instructions decoding for popular programs.

solana-go works using SemVer but in 0 version, which means that the 'minor' will be changed when some broken changes are introduced into the application, and the 'patch' will be changed when a new feature with new changes is added or for bug fixing. As soon as v1.0.0 be released, solana-go will start to use SemVer as usual.

Installation

go get github.com/streamingfast/solana-go

All development happens on the develop branch so if you need to check if there is any fixes in there, try updating to it with go get github.com/streamingfast/solana-go@develop

Usage

Loading an SPL mint


import "github.com/streamingfast/solana-go/rpc"
import "github.com/streamingfast/solana-go/token"

	addr := solana.MustPublicKeyFromBase58("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")
	cli := rpc.NewClient("https://api.mainnet-beta.solana.com")

	var m token.Mint
	err := cli.GetAccountDataIn(context.Background(), addr, &m)
	// handle `err`

	json.NewEncoder(os.Stdout).Encode(m)
	// {"OwnerOption":1,
	//  "Owner":"2wmVCSfPxGPjrnMMn7rchp4uaeoTqN39mXFC2zhPdri9",
	//  "Decimals":128,
	//  "IsInitialized":true}

Getting any account's data:


import "github.com/streamingfast/solana-go/rpc"
import "github.com/streamingfast/solana-go/token"

	addr := solana.MustPublicKeyFromBase58("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")
	cli := rpc.NewClient("https://api.mainnet-beta.solana.com")

	acct, err := cli.GetAccountInfo(context.Background(), addr)
	// handle `err`

	json.NewEncoder(os.Stdout).Encode(m)
// {
//   "context": {
//     "Slot": 47836700
//   },
//   "value": {
//     "lamports": 1461600,
//     "data": {
//       "data": "AQAAABzjWe1aAS4E+hQrnHUaHF6Hz9CgFhuchf/TG3jN/Nj2gCa3xLwWAAAGAQEAAAAqnl7btTwEZ5CY/3sSZRcUQ0/AjFYqmjuGEQXmctQicw==",
//       "encoding": "base64"
//     },
//     "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
//     "executable": false,
//     "rentEpoch": 109
//   }
// }

Examples

Reference
Running

The easiest way to see the actual output for a given example is to add a line // Output: any at the very end of the test, looks like this for ExampleRPC_GetRecentBlockhash file (example_rpc_get_recent_blockhash_test.go):

	...

    fmt.Println(string(bytes))
    // Output: any
}

This tells go test that it can execute this test correctly. Then, simply run only this example:

go test -run ExampleRPC_GetRecentBlockhash

Replacing ExampleRPC_GetRecentBlockhash with the actual example name you want to try out where line // Output: any was added.

This will run the example and compares the standard output with the any which will fail. But it's ok an expected, so you can see the actual output printed to your terminal.

WebSocket examples runs for a 1 minute then exits, you will not see anything until the example finish RPC URL to use can be specified by using environment variable SOLANA_GO_RPC_URL, defaults to https://api.mainnet-beta.solana.com. WS URL to use can be specified by using environment variable SOLANA_GO_WS_URL, defaults to ws://api.mainnet-beta.solana.com.

Contributing

Issues and PR in this repo related strictly to the solana go library.

Report any protocol-specific issues in their respective repositories

Please first refer to the general StreamingFast contribution guide, if you wish to contribute to this code base.

This codebase uses unit tests extensively, please write and run tests.

License

Apache 2.0

Documentation

Index

Constants

View Source
const MAX_SEED_LENGTH = 32

Variables

View Source
var InstructionDecoderRegistry = map[string]InstructionDecoder{}

Functions

func DecimalsInBigInt

func DecimalsInBigInt(decimal uint32) *big.Int

func DecodeInstruction

func DecodeInstruction(programID PublicKey, accounts []*AccountMeta, data []byte) (interface{}, error)

func NewRandomPrivateKey

func NewRandomPrivateKey() (PublicKey, PrivateKey, error)

func RegisterInstructionDecoder

func RegisterInstructionDecoder(programID PublicKey, decoder InstructionDecoder)

Types

type Account

type Account struct {
	PrivateKey PrivateKey
}

func AccountFromPrivateKeyBase58

func AccountFromPrivateKeyBase58(privateKey string) (*Account, error)

func NewAccount

func NewAccount() *Account

func (*Account) PublicKey

func (a *Account) PublicKey() PublicKey

type AccountMeta

type AccountMeta struct {
	PublicKey  PublicKey
	IsSigner   bool
	IsWritable bool
}

type AccountSettable

type AccountSettable interface {
	SetAccounts(accounts []*AccountMeta) error
}

type Base58

type Base58 []byte

/

func (Base58) MarshalJSON

func (t Base58) MarshalJSON() ([]byte, error)

func (Base58) String

func (t Base58) String() string

func (*Base58) UnmarshalJSON

func (t *Base58) UnmarshalJSON(data []byte) (err error)

type ByteWrapper

type ByteWrapper struct {
	io.Reader
}

/

func (*ByteWrapper) ReadByte

func (w *ByteWrapper) ReadByte() (byte, error)

type CompiledInstruction

type CompiledInstruction struct {
	ProgramIDIndex uint8         `json:"programIdIndex"`
	AccountCount   bin.Varuint16 `json:"-" bin:"sizeof=Accounts"`
	Accounts       []uint8       `json:"accounts"`
	DataLength     bin.Varuint16 `json:"-" bin:"sizeof=Data"`
	Data           Base58        `json:"data"`
}

func (*CompiledInstruction) ResolveInstructionAccounts

func (ci *CompiledInstruction) ResolveInstructionAccounts(message *Message) (out []*AccountMeta)

type Data

type Data []byte

func (Data) MarshalJSON

func (t Data) MarshalJSON() ([]byte, error)

func (Data) String

func (t Data) String() string

func (*Data) UnmarshalJSON

func (t *Data) UnmarshalJSON(data []byte) (err error)

type Hash

type Hash PublicKey

type Instruction

type Instruction interface {
	Accounts() []*AccountMeta // returns the list of accounts the instructions requires
	ProgramID() PublicKey     // the programID the instruction acts on
	Data() ([]byte, error)    // the binary encoded instructions
}

type InstructionDecoder

type InstructionDecoder func(instructionAccounts []*AccountMeta, data []byte) (interface{}, error)

InstructionDecoder receives the AccountMeta FOR THAT INSTRUCTION, and not the accounts of the *Message object. Resolve with CompiledInstruction.ResolveInstructionAccounts(message) beforehand.

type Message

type Message struct {
	Header          MessageHeader         `json:"header"`
	AccountKeys     []PublicKey           `json:"accountKeys"`
	RecentBlockhash PublicKey             `json:"recentBlockhash"`
	Instructions    []CompiledInstruction `json:"instructions"`
}

func (*Message) AccountMetaList

func (m *Message) AccountMetaList() (out []*AccountMeta)

func (*Message) IsSigner

func (m *Message) IsSigner(account PublicKey) bool

func (*Message) IsWritable

func (m *Message) IsWritable(account PublicKey) bool

func (*Message) ResolveProgramIDIndex

func (m *Message) ResolveProgramIDIndex(programIDIndex uint8) (PublicKey, error)

func (*Message) TouchAccount

func (m *Message) TouchAccount(account PublicKey) bool

type MessageHeader

type MessageHeader struct {
	NumRequiredSignatures       uint8 `json:"numRequiredSignatures"`
	NumReadonlySignedAccounts   uint8 `json:"numReadonlySignedAccounts"`
	NumReadonlyUnsignedAccounts uint8 `json:"numReadonlyUnsignedAccounts"`
}

type Padding

type Padding []byte

type PrivateKey

type PrivateKey []byte

func MustPrivateKeyFromBase58

func MustPrivateKeyFromBase58(in string) PrivateKey

func PrivateKeyFromBase58

func PrivateKeyFromBase58(privkey string) (PrivateKey, error)

func PrivateKeyFromSolanaKeygenFile

func PrivateKeyFromSolanaKeygenFile(file string) (PrivateKey, error)

func (PrivateKey) PublicKey

func (k PrivateKey) PublicKey() PublicKey

func (PrivateKey) Sign

func (k PrivateKey) Sign(payload []byte) (Signature, error)

func (PrivateKey) String

func (k PrivateKey) String() string

type PublicKey

type PublicKey [32]byte

func MustPublicKeyFromBase58

func MustPublicKeyFromBase58(in string) PublicKey

func PublicKeyFindProgramAddress

func PublicKeyFindProgramAddress(path [][]byte, programId PublicKey) (PublicKey, byte, error)

func PublicKeyFromBase58

func PublicKeyFromBase58(in string) (out PublicKey, err error)

func PublicKeyFromBytes

func PublicKeyFromBytes(in []byte) (out PublicKey)

func (PublicKey) Equals

func (p PublicKey) Equals(pb PublicKey) bool

func (PublicKey) IsZero

func (p PublicKey) IsZero() bool

func (PublicKey) MarshalJSON

func (p PublicKey) MarshalJSON() ([]byte, error)

func (PublicKey) String

func (p PublicKey) String() string

func (PublicKey) ToSlice added in v0.5.0

func (p PublicKey) ToSlice() []byte

func (*PublicKey) UnmarshalJSON

func (p *PublicKey) UnmarshalJSON(data []byte) (err error)

type Signature

type Signature [64]byte

func MustSignatureFromString added in v0.5.0

func MustSignatureFromString(in string) (out Signature)

func NewSignatureFromBase58

func NewSignatureFromBase58(in string) (out Signature, err error)

func NewSignatureFromBytes

func NewSignatureFromBytes(in []byte) (out Signature, err error)

func NewSignatureFromString

func NewSignatureFromString(in string) (out Signature, err error)

func SignatureFromBase58 deprecated

func SignatureFromBase58(in string) (out Signature, err error)

Deprecated: Use NewSignatureFromBase58 instead

func (Signature) MarshalJSON

func (s Signature) MarshalJSON() ([]byte, error)

func (Signature) String

func (s Signature) String() string

func (Signature) ToSlice added in v0.5.0

func (s Signature) ToSlice() []byte

func (*Signature) UnmarshalJSON

func (s *Signature) UnmarshalJSON(data []byte) (err error)

func (Signature) Verify

func (s Signature) Verify(publicKey PublicKey, message []byte) bool

type Transaction

type Transaction struct {
	Signatures []Signature `json:"signatures"`
	Message    Message     `json:"message"`
}

func NewTransaction

func NewTransaction(instructions []Instruction, blockHash PublicKey, opts ...TransactionOption) (*Transaction, error)

func TransactionFromData

func TransactionFromData(in []byte) (*Transaction, error)

func (*Transaction) AccountMetaList

func (t *Transaction) AccountMetaList() (out []*AccountMeta)

func (*Transaction) IsSigner

func (t *Transaction) IsSigner(account PublicKey) bool

func (*Transaction) IsWritable

func (t *Transaction) IsWritable(account PublicKey) bool

func (*Transaction) ResolveProgramIDIndex

func (t *Transaction) ResolveProgramIDIndex(programIDIndex uint8) (PublicKey, error)

func (*Transaction) Sign

func (t *Transaction) Sign(getter privateKeyGetter) (out []Signature, err error)

func (*Transaction) TouchAccount

func (t *Transaction) TouchAccount(account PublicKey) bool

type TransactionOption

type TransactionOption interface {
	// contains filtered or unexported methods
}

func TransactionPayer

func TransactionPayer(payer PublicKey) TransactionOption

Directories

Path Synopsis
programs
serum
Code generated by rice embed-go; DO NOT EDIT.
Code generated by rice embed-go; DO NOT EDIT.
token
Code generated by rice embed-go; DO NOT EDIT.
Code generated by rice embed-go; DO NOT EDIT.
rpc
ws

Jump to

Keyboard shortcuts

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