-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
63 lines (52 loc) · 1.64 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package token
import (
"bytes"
"crypto/ed25519"
"github.com/code-payments/code-server/pkg/solana"
"github.com/pkg/errors"
)
var (
// ErrAccountNotFound indicates there is no account for the given address.
ErrAccountNotFound = errors.New("account not found")
// ErrInvalidTokenAccount indicates that a Solana account exists at the
// given address, but it is either not initialized, or not configured correctly.
ErrInvalidTokenAccount = errors.New("invalid token account")
)
// Client provides utilities for accessing token accounts for a given token.
type Client struct {
sc solana.Client
token ed25519.PublicKey
}
// NewClient creates a new Client.
func NewClient(sc solana.Client, token ed25519.PublicKey) *Client {
return &Client{
sc: sc,
token: token,
}
}
func (c *Client) Token() ed25519.PublicKey {
return c.token
}
// GetAccount returns the token account info for the specified account.
//
// If the account is not initialized, or belongs to a different
// mint, then ErrInvalidTokenAccount is returned.
func (c *Client) GetAccount(accountID ed25519.PublicKey, commitment solana.Commitment) (*Account, error) {
accountInfo, err := c.sc.GetAccountInfo(accountID, commitment)
if err == solana.ErrNoAccountInfo {
return nil, ErrAccountNotFound
} else if err != nil {
return nil, errors.Wrap(err, "failed to get account info")
}
if !bytes.Equal(accountInfo.Owner, ProgramKey) {
return nil, ErrInvalidTokenAccount
}
var account Account
if !account.Unmarshal(accountInfo.Data) {
return nil, ErrInvalidTokenAccount
}
if !bytes.Equal(c.token, account.Mint) {
return nil, ErrInvalidTokenAccount
}
return &account, nil
}