Skip to content

Commit

Permalink
Merge pull request #2 from gagliardetto/native-programs
Browse files Browse the repository at this point in the history
Refactor program utilities; refactor system program client
  • Loading branch information
gagliardetto authored Aug 31, 2021
2 parents 7e0eaa6 + 44559ca commit ed736e9
Show file tree
Hide file tree
Showing 73 changed files with 4,119 additions and 376 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Solana SDK library for Go

[![GoDoc](https://pkg.go.dev/badge/github.com/gagliardetto/solana-go?status.svg)](https://pkg.go.dev/github.com/gagliardetto/solana-go@v0.3.6?tab=doc)
[![GoDoc](https://pkg.go.dev/badge/github.com/gagliardetto/solana-go?status.svg)](https://pkg.go.dev/github.com/gagliardetto/solana-go@v0.4.0?tab=doc)
[![GitHub tag (latest SemVer pre-release)](https://img.shields.io/github/v/tag/gagliardetto/solana-go?include_prereleases&label=release-tag)](https://github.com/gagliardetto/solana-go/releases)
[![Build Status](https://github.com/gagliardetto/solana-go/workflows/tests/badge.svg?branch=main)](https://github.com/gagliardetto/solana-go/actions?query=branch%3Amain)
[![TODOs](https://badgen.net/https/api.tickgit.com/badgen/github.com/gagliardetto/solana-go/main)](https://www.tickgit.com/browse?repo=github.com/gagliardetto/solana-go&branch=main)
Expand Down Expand Up @@ -110,7 +110,7 @@ More contracts to come.

## Current development status

There is currently **no stable release**. The SDK is actively developed and latest is `v0.3.6` which is an `alpha` release.
There is currently **no stable release**. The SDK is actively developed and latest is `v0.4.0` which is an `alpha` release.

The RPC and WS client implementation is based on [this RPC spec](https://github.com/solana-labs/solana/blob/dff9c88193da142693cabebfcd3bf68fa8e8b873/docs/src/developing/clients/jsonrpc-api.md).

Expand All @@ -124,7 +124,7 @@ The RPC and WS client implementation is based on [this RPC spec](https://github.
```bash
$ cd my-project
$ go get github.com/gagliardetto/solana-go@latest
$ go get github.com/gagliardetto/solana-go@v0.4.0
```

## Examples
Expand Down
61 changes: 60 additions & 1 deletion account.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,41 @@ func (a *Account) PublicKey() PublicKey {

type AccountMeta struct {
PublicKey PublicKey
IsSigner bool
IsWritable bool
IsSigner bool
}

// Meta intializes a new AccountMeta with the provided pubKey.
func Meta(
pubKey PublicKey,
) *AccountMeta {
return &AccountMeta{
PublicKey: pubKey,
}
}

// WRITE sets IsWritable to true.
func (meta *AccountMeta) WRITE() *AccountMeta {
meta.IsWritable = true
return meta
}

// SIGNER sets IsSigner to true.
func (meta *AccountMeta) SIGNER() *AccountMeta {
meta.IsSigner = true
return meta
}

func NewAccountMeta(
pubKey PublicKey,
WRITE bool,
SIGNER bool,
) *AccountMeta {
return &AccountMeta{
PublicKey: pubKey,
IsWritable: WRITE,
IsSigner: SIGNER,
}
}

func (a *AccountMeta) less(act *AccountMeta) bool {
Expand All @@ -50,3 +83,29 @@ func (a *AccountMeta) less(act *AccountMeta) bool {
}
return false
}

type AccountMetaSlice []*AccountMeta

func (slice *AccountMetaSlice) Append(account *AccountMeta) {
*slice = append(*slice, account)
}

func (slice *AccountMetaSlice) SetAccounts(accounts []*AccountMeta) error {
*slice = accounts
return nil
}

func (slice AccountMetaSlice) GetAccounts() []*AccountMeta {
return slice
}

// GetSigners returns the accounts that are signers.
func (slice AccountMetaSlice) GetSigners() []*AccountMeta {
signers := make([]*AccountMeta, 0)
for _, ac := range slice {
if ac.IsSigner {
signers = append(signers, ac)
}
}
return signers
}
61 changes: 61 additions & 0 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,66 @@ func Test_AccountMeta_less(t *testing.T) {
assert.Equal(t, test.expect, test.left.less(test.right))
})
}
}

func TestAccountMetaSlice(t *testing.T) {
pkey1 := MustPublicKeyFromBase58("SysvarS1otHashes111111111111111111111111111")

var slice AccountMetaSlice

setting := []*AccountMeta{
{PublicKey: pkey1, IsSigner: true, IsWritable: false},
}
err := slice.SetAccounts(setting)
require.NoError(t, err)

require.Len(t, slice, 1)
require.Equal(t, setting[0], slice[0])
require.Equal(t, setting, slice.GetAccounts())

{
pkey2 := MustPublicKeyFromBase58("BPFLoaderUpgradeab1e11111111111111111111111")

meta := NewAccountMeta(pkey2, true, false)
slice.Append(meta)

require.Len(t, slice, 2)
require.Equal(t, meta, slice[1])
require.Equal(t, meta, slice.GetAccounts()[1])
}
}

func TestNewAccountMeta(t *testing.T) {
pkey := MustPublicKeyFromBase58("SysvarS1otHashes111111111111111111111111111")

isWritable := false
isSigner := true

out := NewAccountMeta(pkey, isWritable, isSigner)

require.NotNil(t, out)

require.Equal(t, isSigner, out.IsSigner)
require.Equal(t, isWritable, out.IsWritable)
}

func TestMeta(t *testing.T) {
pkey := MustPublicKeyFromBase58("SysvarS1otHashes111111111111111111111111111")

meta := Meta(pkey)
require.NotNil(t, meta)
require.Equal(t, pkey, meta.PublicKey)

require.False(t, meta.IsSigner)
require.False(t, meta.IsWritable)

meta.SIGNER()

require.True(t, meta.IsSigner)
require.False(t, meta.IsWritable)

meta.WRITE()

require.True(t, meta.IsSigner)
require.True(t, meta.IsWritable)
}
4 changes: 2 additions & 2 deletions cmd/slnc/cmd/decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ func decodeAsToken(data []byte) (out interface{}, err error) {
case 120:
var tokenAcct token.Account

if err := bin.NewDecoder(data).Decode(&tokenAcct); err != nil {
if err := bin.NewBinDecoder(data).Decode(&tokenAcct); err != nil {
return nil, fmt.Errorf("failed unpacking: %w", err)
}

return tokenAcct, nil
case 40:
var mint token.Mint
if err := bin.NewDecoder(data).Decode(&mint); err != nil {
if err := bin.NewBinDecoder(data).Decode(&mint); err != nil {
log.Fatalln("failed unpack", err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/slnc/cmd/get_spl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var getSPLTokenCmd = &cobra.Command{
acct := keyedAcct.Account
//fmt.Println("Data len:", len(acct.Data), keyedAcct.Pubkey)
var mint *token.Mint
if err := bin.NewDecoder(acct.Data.GetBinary()).Decode(&mint); err != nil {
if err := bin.NewBinDecoder(acct.Data.GetBinary()).Decode(&mint); err != nil {
log.Fatalln("failed unpack", err)
}

Expand Down
18 changes: 16 additions & 2 deletions cmd/slnc/cmd/token_registry_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,24 @@ var tokenRegistryRegisterCmd = &cobra.Command{

tokenRegistryProgramID := tokenregistry.ProgramID()

createAccountInstruction := system.NewCreateAccountInstruction(uint64(lamport), tokenregistry.TOKEN_META_SIZE, tokenRegistryProgramID, registrarPubKey, tokenMetaAccount.PublicKey())
createAccountInstruction := system.NewCreateAccountInstruction(
lamport,
tokenregistry.TOKEN_META_SIZE,
tokenRegistryProgramID,
registrarPubKey,
tokenMetaAccount.PublicKey(),
).
Build()
registerTokenInstruction := tokenregistry.NewRegisterTokenInstruction(logo, name, symbol, website, tokenMetaAccount.PublicKey(), registrarPubKey, tokenAddress)

trx, err := solana.NewTransaction([]solana.Instruction{createAccountInstruction, registerTokenInstruction}, blockHashResult.Value.Blockhash, solana.TransactionPayer(registrarPubKey))
trx, err := solana.NewTransaction(
[]solana.Instruction{
createAccountInstruction,
registerTokenInstruction,
},
blockHashResult.Value.Blockhash,
solana.TransactionPayer(registrarPubKey),
)
if err != nil {
return fmt.Errorf("unable to craft transaction: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package solana

const (
// There are 1-billion lamports in one SOL.
LAMPORTS_PER_SOL uint64 = 1000000000
)
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ retract (

require (
contrib.go.opencensus.io/exporter/stackdriver v0.13.4 // indirect
filippo.io/edwards25519 v1.0.0-rc.1
github.com/AlekSi/pointer v1.1.0
github.com/GeertJohan/go.rice v1.0.0
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59
github.com/buger/jsonparser v1.1.1
github.com/davecgh/go-spew v1.1.1
github.com/dfuse-io/binary v0.0.0-20210216024852-4ae6830a495d
github.com/dfuse-io/logging v0.0.0-20210109005628-b97a57253f70
github.com/fatih/color v1.7.0
github.com/gagliardetto/treeout v0.1.2
github.com/google/go-cmp v0.5.1
github.com/google/gofuzz v1.0.0
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.2
github.com/json-iterator/go v1.1.11
github.com/klauspost/compress v1.13.1
github.com/kr/pretty v0.2.1 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/magiconair/properties v1.8.1
github.com/mr-tron/base58 v1.2.0
Expand All @@ -45,5 +48,8 @@ require (
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
golang.org/x/tools v0.0.0-20200601175630-2caf76543d99 // indirect
google.golang.org/api v0.29.0
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/dfuse-io/binary => github.com/gagliardetto/binary v0.4.0

replace github.com/google/gofuzz => github.com/gagliardetto/gofuzz v1.2.1
14 changes: 11 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ contrib.go.opencensus.io/exporter/stackdriver v0.12.6/go.mod h1:8x999/OcIPy5ivx/
contrib.go.opencensus.io/exporter/stackdriver v0.13.4 h1:ksUxwH3OD5sxkjzEqGxNTl+Xjsmu3BnC/300MhSVTSc=
contrib.go.opencensus.io/exporter/stackdriver v0.13.4/go.mod h1:aXENhDJ1Y4lIg4EUaVTwzvYETVNZk10Pu26tevFKLUc=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
github.com/AlekSi/pointer v1.1.0 h1:SSDMPcXD9jSl8FPy9cRzoRaMJtm9g9ggGTxecRUbQoI=
github.com/AlekSi/pointer v1.1.0/go.mod h1:y7BvfRI3wXPWKXEBhU71nbnIEEZX0QTSB2Bj48UJIZE=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
Expand All @@ -46,6 +48,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-sdk-go v1.22.1/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.23.20/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
Expand All @@ -72,8 +76,6 @@ github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CL
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dfuse-io/binary v0.0.0-20210216024852-4ae6830a495d h1:EvEAQZ38olo+sALrqqCo345IeB2HH4im1deNOcjbi0s=
github.com/dfuse-io/binary v0.0.0-20210216024852-4ae6830a495d/go.mod h1:GDFX6qH3BQZPWTeYaA4ZW98T94zs2skRoG3oMz/0jw0=
github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79/go.mod h1:V+ED4kT/t/lKtH99JQmKIb0v9WL3VaYkJ36CfHlVECI=
github.com/dfuse-io/logging v0.0.0-20210109005628-b97a57253f70 h1:CuJS05R9jmNlUK8GOxrEELPbfXm0EuGh/30LjkjN5vo=
github.com/dfuse-io/logging v0.0.0-20210109005628-b97a57253f70/go.mod h1:EoK/8RFbMEteaCaz89uessDTnCWjbbcr+DXcBh4el5o=
Expand All @@ -87,6 +89,12 @@ github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gagliardetto/binary v0.4.0 h1:dxLndYArHtdZYbLYwnpLY86mlAa9gWgxG2zSDPFINjM=
github.com/gagliardetto/binary v0.4.0/go.mod h1:55fxN6CKhVnsBhSr3Hmyn7i2igseIzN9/NC+gHvv42k=
github.com/gagliardetto/gofuzz v1.2.1 h1:fHBiDgCYYb8kBRqyI+bhU59/IKATHArFUAY3iVUrdPA=
github.com/gagliardetto/gofuzz v1.2.1/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gagliardetto/treeout v0.1.2 h1:WXO7LDJTwINO37OQfNlf7s095Z1bAiwN2ACaZQic33Q=
github.com/gagliardetto/treeout v0.1.2/go.mod h1:loUefvXTrlRG5rYmJmExNryyBRh8f89VZhmMOyCyqok=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -130,7 +138,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down Expand Up @@ -490,6 +497,7 @@ golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20191216173652-a0e659d51361/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200117161641-43d50277825c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
Expand Down
6 changes: 5 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package solana

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

type AccountsGettable interface {
GetAccounts() (accounts []*AccountMeta)
}
Loading

0 comments on commit ed736e9

Please sign in to comment.