Skip to content

Commit be93bc0

Browse files
authored
Merge pull request #5 from gagliardetto/token-2
- Add client for token program. - Add SendAndConfirmTransaction. - Improve formatting of parsed transactions.
2 parents 17d74ec + 18f8518 commit be93bc0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+5715
-610
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Solana SDK library for Go
22

3-
[![GoDoc](https://pkg.go.dev/badge/github.com/gagliardetto/solana-go?status.svg)](https://pkg.go.dev/github.com/gagliardetto/[email protected].3?tab=doc)
3+
[![GoDoc](https://pkg.go.dev/badge/github.com/gagliardetto/solana-go?status.svg)](https://pkg.go.dev/github.com/gagliardetto/[email protected].4?tab=doc)
44
[![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)
55
[![Build Status](https://github.com/gagliardetto/solana-go/workflows/tests/badge.svg?branch=main)](https://github.com/gagliardetto/solana-go/actions?query=branch%3Amain)
66
[![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)
@@ -41,13 +41,23 @@ More contracts to come.
4141
- [x] Full WebSocket JSON streaming API
4242
- [ ] Wallet, account, and keys management
4343
- [ ] Clients for native programs
44-
- [ ] Clients for Solana Program Library
44+
- [x] system
45+
- [ ] config
46+
- [ ] stake
47+
- [ ] vote
48+
- [ ] BPF Loader
49+
- [ ] Secp256k1
50+
- [ ] Clients for Solana Program Library (SPL)
51+
- [ ] token: WIP
52+
- [ ] memo
53+
- [ ] name-service
54+
- [ ] ...
4555
- [ ] Client for Serum
4656
- [ ] More programs
4757

4858
## Current development status
4959

50-
There is currently **no stable release**. The SDK is actively developed and latest is `v0.4.3` which is an `alpha` release.
60+
There is currently **no stable release**. The SDK is actively developed and latest is `v0.4.4` which is an `alpha` release.
5161

5262
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).
5363

@@ -61,7 +71,7 @@ The RPC and WS client implementation is based on [this RPC spec](https://github.
6171
6272
```bash
6373
$ cd my-project
64-
$ go get github.com/gagliardetto/[email protected].3
74+
$ go get github.com/gagliardetto/[email protected].4
6575
$ go get github.com/gagliardetto/[email protected]
6676
$ go get github.com/gagliardetto/[email protected]
6777
```

account.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,39 @@ func (slice AccountMetaSlice) GetSigners() []*AccountMeta {
110110
}
111111
return signers
112112
}
113+
114+
func (slice AccountMetaSlice) Len() int {
115+
return len(slice)
116+
}
117+
118+
func (slice AccountMetaSlice) SplitFrom(index int) (AccountMetaSlice, AccountMetaSlice) {
119+
if index < 0 {
120+
panic("negative index")
121+
}
122+
if index == 0 {
123+
return AccountMetaSlice{}, slice
124+
}
125+
if index > len(slice)-1 {
126+
return slice, AccountMetaSlice{}
127+
}
128+
129+
firstLen, secondLen := calcSplitAtLengths(len(slice), index)
130+
131+
first := make(AccountMetaSlice, firstLen)
132+
copy(first, slice[:index])
133+
134+
second := make(AccountMetaSlice, secondLen)
135+
copy(second, slice[index:])
136+
137+
return first, second
138+
}
139+
140+
func calcSplitAtLengths(total int, index int) (int, int) {
141+
if index == 0 {
142+
return 0, total
143+
}
144+
if index > total-1 {
145+
return total, 0
146+
}
147+
return index, total - index
148+
}

account_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,64 @@ func TestMeta(t *testing.T) {
140140
require.True(t, meta.IsSigner)
141141
require.True(t, meta.IsWritable)
142142
}
143+
144+
func TestSplitFrom(t *testing.T) {
145+
slice := make(AccountMetaSlice, 0)
146+
slice = append(slice, Meta(BPFLoaderDeprecatedProgramID))
147+
slice = append(slice, Meta(TokenProgramID))
148+
slice = append(slice, Meta(TokenLendingProgramID))
149+
slice = append(slice, Meta(SPLAssociatedTokenAccountProgramID))
150+
slice = append(slice, Meta(MemoProgramID))
151+
152+
require.Len(t, slice, 5)
153+
154+
{
155+
part1, part2 := slice.SplitFrom(0)
156+
require.Len(t, part1, 0)
157+
require.Len(t, part2, 5)
158+
}
159+
{
160+
part1, part2 := slice.SplitFrom(1)
161+
require.Len(t, part1, 1)
162+
require.Len(t, part2, 4)
163+
require.Equal(t, Meta(BPFLoaderDeprecatedProgramID), part1[0])
164+
require.Equal(t, Meta(TokenProgramID), part2[0])
165+
require.Equal(t, Meta(TokenLendingProgramID), part2[1])
166+
require.Equal(t, Meta(SPLAssociatedTokenAccountProgramID), part2[2])
167+
require.Equal(t, Meta(MemoProgramID), part2[3])
168+
}
169+
{
170+
part1, part2 := slice.SplitFrom(2)
171+
require.Len(t, part1, 2)
172+
require.Len(t, part2, 3)
173+
}
174+
{
175+
part1, part2 := slice.SplitFrom(3)
176+
require.Len(t, part1, 3)
177+
require.Len(t, part2, 2)
178+
}
179+
{
180+
part1, part2 := slice.SplitFrom(4)
181+
require.Len(t, part1, 4)
182+
require.Len(t, part2, 1)
183+
}
184+
{
185+
part1, part2 := slice.SplitFrom(5)
186+
require.Len(t, part1, 5)
187+
require.Len(t, part2, 0)
188+
}
189+
{
190+
part1, part2 := slice.SplitFrom(6)
191+
require.Len(t, part1, 5)
192+
require.Len(t, part2, 0)
193+
}
194+
{
195+
part1, part2 := slice.SplitFrom(10000)
196+
require.Len(t, part1, 5)
197+
require.Len(t, part2, 0)
198+
}
199+
require.Panics(t,
200+
func() {
201+
slice.SplitFrom(-1)
202+
})
203+
}

cmd/slnc/cmd/token_get_mint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ var tokenGetMintCmd = &cobra.Command{
5757
out = append(out, fmt.Sprintf("Supply | %d", mint.Supply))
5858
out = append(out, fmt.Sprintf("Decimals | %d", mint.Decimals))
5959

60-
if mint.MintAuthorityOption != 0 {
60+
if mint.MintAuthority != nil {
6161
out = append(out, fmt.Sprintf("Token Authority | %s", mint.MintAuthority))
6262
} else {
6363
out = append(out, "No mint authority")
6464
}
6565

66-
if mint.FreezeAuthorityOption != 0 {
66+
if mint.FreezeAuthority != nil {
6767
out = append(out, fmt.Sprintf("Freeze Authority | %s", mint.FreezeAuthority))
6868
} else {
6969
out = append(out, "No freeze authority")

cmd/slnc/cmd/token_list_mints.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,15 @@ var tokenListMintsCmd = &cobra.Command{
3636
out := []string{"Mint | Decimals | Supply | Token Authority | Freeze Authority"}
3737
for _, m := range mints {
3838
line := []string{
39-
fmt.Sprintf("%d", m.MintAuthorityOption),
4039
fmt.Sprintf("%d", m.Supply),
4140
fmt.Sprintf("%d", m.Decimals),
4241
}
43-
if m.MintAuthorityOption != 0 {
42+
if m.MintAuthority != nil {
4443
line = append(line, fmt.Sprintf("%s", m.MintAuthority))
4544
} else {
4645
line = append(line, "No mint authority")
4746
}
48-
if m.FreezeAuthorityOption != 0 {
47+
if m.FreezeAuthority != nil {
4948
line = append(line, fmt.Sprintf("%s", m.FreezeAuthority))
5049
} else {
5150
line = append(line, "No freeze authority")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/fatih/color v1.7.0
2020
github.com/gagliardetto/binary v0.4.1
2121
github.com/gagliardetto/gofuzz v1.2.2
22-
github.com/gagliardetto/treeout v0.1.2
22+
github.com/gagliardetto/treeout v0.1.4
2323
github.com/google/go-cmp v0.5.1
2424
github.com/gorilla/rpc v1.2.0
2525
github.com/gorilla/websocket v1.4.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ github.com/gagliardetto/binary v0.4.1 h1:3HXeBOfmR2HHIrF1YBnoqgFKQBtN8+rWA770KyX
9393
github.com/gagliardetto/binary v0.4.1/go.mod h1:peJR9PvwamL4YOh1nHWCPLry2VEfeeD1ADvewka7HnQ=
9494
github.com/gagliardetto/gofuzz v1.2.2 h1:XL/8qDMzcgvR4+CyRQW9UGdwPRPMHVJfqQ/uMvSUuQw=
9595
github.com/gagliardetto/gofuzz v1.2.2/go.mod h1:bkH/3hYLZrMLbfYWA0pWzXmi5TTRZnu4pMGZBkqMKvY=
96-
github.com/gagliardetto/treeout v0.1.2 h1:WXO7LDJTwINO37OQfNlf7s095Z1bAiwN2ACaZQic33Q=
97-
github.com/gagliardetto/treeout v0.1.2/go.mod h1:loUefvXTrlRG5rYmJmExNryyBRh8f89VZhmMOyCyqok=
96+
github.com/gagliardetto/treeout v0.1.4 h1:ozeYerrLCmCubo1TcIjFiOWTTGteOOHND1twdFpgwaw=
97+
github.com/gagliardetto/treeout v0.1.4/go.mod h1:loUefvXTrlRG5rYmJmExNryyBRh8f89VZhmMOyCyqok=
9898
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
9999
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
100100
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=

keys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func findAssociatedTokenAddressAndBumpSeed(
332332
) (PublicKey, uint8, error) {
333333
return FindProgramAddress([][]byte{
334334
walletAddress[:],
335-
SPLTokenProgramID[:],
335+
TokenProgramID[:],
336336
splTokenMintAddress[:],
337337
},
338338
programID,

program_ids.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ var (
2929
var (
3030
// A Token program on the Solana blockchain.
3131
// This program defines a common implementation for Fungible and Non Fungible tokens.
32-
SPLTokenProgramID = MustPublicKeyFromBase58("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
32+
TokenProgramID = MustPublicKeyFromBase58("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
3333

3434
// A Uniswap-like exchange for the Token program on the Solana blockchain,
3535
// implementing multiple automated market maker (AMM) curves.
36-
SPLTokenSwapProgramID = MustPublicKeyFromBase58("SwaPpA9LAaLfeLi3a68M4DjnLqgtticKg6CnyNwgAC8")
37-
SPLTokenSwapFeeOwner = MustPublicKeyFromBase58("HfoTxFR1Tm6kGmWgYWD6J7YHVy1UwqSULUGVLXkJqaKN")
36+
TokenSwapProgramID = MustPublicKeyFromBase58("SwaPpA9LAaLfeLi3a68M4DjnLqgtticKg6CnyNwgAC8")
37+
TokenSwapFeeOwner = MustPublicKeyFromBase58("HfoTxFR1Tm6kGmWgYWD6J7YHVy1UwqSULUGVLXkJqaKN")
3838

3939
// A lending protocol for the Token program on the Solana blockchain inspired by Aave and Compound.
40-
SPLTokenLendingProgramID = MustPublicKeyFromBase58("LendZqTs8gn5CTSJU1jWKhKuVpjJGom45nnwPb2AMTi")
40+
TokenLendingProgramID = MustPublicKeyFromBase58("LendZqTs8gn5CTSJU1jWKhKuVpjJGom45nnwPb2AMTi")
4141

4242
// This program defines the convention and provides the mechanism for mapping
4343
// the user's wallet address to the associated token accounts they hold.
@@ -49,5 +49,14 @@ var (
4949
// to the transaction log, so that anyone can easily observe memos
5050
// and know they were approved by zero or more addresses
5151
// by inspecting the transaction log from a trusted provider.
52-
SPLMemoProgramID = MustPublicKeyFromBase58("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr")
52+
MemoProgramID = MustPublicKeyFromBase58("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr")
53+
)
54+
55+
var (
56+
// The Mint for native SOL Token accounts
57+
SolMint = MustPublicKeyFromBase58("So11111111111111111111111111111111111111112")
58+
)
59+
60+
var (
61+
TokenMetadataProgramID = MustPublicKeyFromBase58("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s")
5362
)

programs/system/AdvanceNonceAccount.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ func (inst *AdvanceNonceAccount) EncodeToTree(parent ag_treeout.Branches) {
103103

104104
// Accounts of the instruction:
105105
instructionBranch.Child("Accounts").ParentFunc(func(accountsBranch ag_treeout.Branches) {
106-
accountsBranch.Child(ag_format.Meta("NonceAccount", inst.AccountMetaSlice[0]))
107-
accountsBranch.Child(ag_format.Meta("$(SysVarRecentBlockHashesPubkey)", inst.AccountMetaSlice[1]))
108-
accountsBranch.Child(ag_format.Meta("NonceAuthorityAccount", inst.AccountMetaSlice[2]))
106+
accountsBranch.Child(ag_format.Meta(" Nonce", inst.AccountMetaSlice[0]))
107+
accountsBranch.Child(ag_format.Meta("SysVarRecentBlockHashes", inst.AccountMetaSlice[1]))
108+
accountsBranch.Child(ag_format.Meta(" NonceAuthority", inst.AccountMetaSlice[2]))
109109
})
110110
})
111111
})

0 commit comments

Comments
 (0)