blocto / solana-go-sdk

Solana Golang SDK
https://blocto.github.io/solana-go-sdk/
MIT License
394 stars 104 forks source link

why GetTokenAccountsByOwnerByMint return slice #139

Closed yangyile1990 closed 1 year ago

yangyile1990 commented 1 year ago

code:

package main

import (
    "context"
    "fmt"

    "github.com/blocto/solana-go-sdk/client"
    "github.com/blocto/solana-go-sdk/rpc"
    "gitlab.yyle.com/golang/uvyyle.git/utils_assert"
)

func main() {
    // 创建上下文对象
    ctx := context.Background()
    // 要查询余额的 Solana 钱包地址
    accountAddress := "BmFdpraQhkiDQE6SnfG5omcA1VwzqfXrwtNYBwWTymy6"

    client2 := client.NewClient(rpc.MainnetRPCEndpoint)

    tokenAccounts, err := client2.GetTokenAccountsByOwnerByMint(ctx, accountAddress, "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB")
    utils_assert.Done(err)
    // 打印余额
    fmt.Println("account balance:", tokenAccounts)
    fmt.Println(len(tokenAccounts))
}

I don't know why it return a slice. slice[0] is the data, what about slice[1] slice[2] ? no info in doc. no info in comment. please help me.

yangyile1990 commented 1 year ago

why why why?

curl https://api.mainnet-beta.solana.com -X POST -H "Content-Type: application/json" -d '
  {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getTokenAccountsByOwner",
    "params": [
      "BmFdpraQhkiDQE6SnfG5omcA1VwzqfXrwtNYBwWTymy6",
      {
        "mint": "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"
      },
      {
        "encoding": "jsonParsed"
      }
    ]
  }
'
yihau commented 1 year ago

The reason we use a slice here is that a wallet can own not only one token account for a mint. It means if you have a wallet A, the A may have token accounts B, C and D and all of them are USDT token accounts.

yihau commented 1 year ago

I sent you an example in #140. could you please have a look at it?

yangyile1990 commented 1 year ago

I sent you an example in #140. could you please have a look at it?

thank you.