Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add token metadat #276

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions rpc/getTokenMetaData.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package rpc

import (
"bytes"
"context"

"github.com/gagliardetto/solana-go"
"github.com/mr-tron/base58"
)

// Metaplex Token Metadata
const (
metaplex = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"
meta = "metadata"
)

func (cl *Client) GetTokenMetaData(mint string) (*TokenMetaData, error) {
b, err := base58.Decode(metaplex)
if err != nil {
return nil, err
}
seeds := [][]byte{
[]byte(meta),
b,
solana.MustPublicKeyFromBase58(mint).Bytes(),
}
pb, _, err := solana.FindProgramAddress(seeds, solana.MustPublicKeyFromBase58(metaplex))
if err != nil {
return nil, err
}
resp, err := cl.GetAccountInfoWithOpts(
context.TODO(),
pb,
&GetAccountInfoOpts{
Encoding: solana.EncodingBase64,
Commitment: CommitmentFinalized,
},
)
if err != nil {
return nil, err
}
metadata := &TokenMetaData{}
data := resp.Value.Data.GetBinary()
metadata.Name = string(bytes.Trim(data[69:69+32], "\x00"))
metadata.Symbol = string(bytes.Trim(data[105:115], "\x00"))
metadata.URI = string(bytes.Trim(data[116:200], "\x00"))

return metadata, err
}
6 changes: 6 additions & 0 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,12 @@ type InstructionInfo struct {
InstructionType string `json:"type"`
}

type TokenMetaData struct {
Name string
Symbol string
URI string
}

type TransactionOpts struct {
Encoding solana.EncodingType `json:"encoding,omitempty"`
SkipPreflight bool `json:"skipPreflight,omitempty"`
Expand Down