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

View all ATA accounts under the account #176

Open
mingtianquan opened this issue Mar 20, 2024 · 0 comments
Open

View all ATA accounts under the account #176

mingtianquan opened this issue Mar 20, 2024 · 0 comments

Comments

@mingtianquan
Copy link

How to search for all tokens under a Sol account,
View all ATA accounts under the account
I need to check the balance of all tokens under my account.
This example seems to be ineffective:

package main

import (
"context"

"github.com/davecgh/go-spew/spew"
bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/token"
"github.com/gagliardetto/solana-go/rpc"
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)

pubKey := solana.MustPublicKeyFromBase58("7HZaCWazgTuuFuajxaaxGYbGnyVKwxvsJKue1W4Nvyro")
out, err := client.GetTokenAccountsByOwner(
context.TODO(),
pubKey,
&rpc.GetTokenAccountsConfig{
Mint: solana.WrappedSol.ToPointer(),
},
&rpc.GetTokenAccountsOpts{
Encoding: solana.EncodingBase64Zstd,
},
)
if err != nil {
panic(err)
}
spew.Dump(out)

{
tokenAccounts := make([]token.Account, 0)
for _, rawAccount := range out.Value {
var tokAcc token.Account

  data := rawAccount.Account.Data.GetBinary()
  dec := bin.NewBinDecoder(data)
  err := dec.Decode(&tokAcc)
  if err != nil {
    panic(err)
  }
  tokenAccounts = append(tokenAccounts, tokAcc)
}
spew.Dump(tokenAccounts)

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant