blocto / solana-go-sdk

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

how to getTokenAccountOwner #146

Closed yangyile1990 closed 1 year ago

yangyile1990 commented 1 year ago

I see the https://solscan.io/account/7N6Y6cYRdVuD2ViCk6imDMpjsvd8L83jM2hmY7GXVVXX

address = 7N6Y6cYRdVuD2ViCk6imDMpjsvd8L83jM2hmY7GXVVXX

const publicKey = new solanaWeb3.PublicKey(address); const tokenAccountInfo = await connection.getAccountInfo(publicKey); const owner = tokenAccountInfo.data.parsed.info.owner;

want owner = g6C2kzzxGa653aAiHmN8HTntAAPjeSD5C191h8rdqrQ

while when i see the golang sdk.

type AccountInfo struct { Lamports uint64 Owner common.PublicKey Executable bool RentEpoch uint64 Data []byte }

how can I get data.parsed.info.owner?

too hard to use.

yangyile1990 commented 1 year ago

aha. I knew it.

toSub, erc := GetTxnInxAccount(txn, inx, 2)
            if erc != nil {
                return nil, erc
            }
            toAcc, err := s.client3.GetAccountInfoWithOpts(context.TODO(), toSub, &rpc3.GetAccountInfoOpts{
                Encoding: solana.EncodingJSONParsed,
            })
            if err != nil {
                return nil, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, "GetAccountInfo", toSub.String(), err.Error())
            }
            simJson, err := simplejson.NewJson(toAcc.Value.Data.GetRawJSON())
            if err != nil {
                return nil, cerror.NewError(cerror.CODE_NODE_RESP_ERROR, err.Error())
            }
            toOwner, err := simJson.Get("parsed").Get("info").Get("owner").String()
            if err != nil {
                return nil, cerror.NewError(cerror.CODE_UNKNOWN, err.Error())
            }

but I still think it is a little too hard to use.

yihau commented 1 year ago

https://blocto.github.io/solana-go-sdk/tour/get-token-account.html

yangyile1990 commented 1 year ago

cool! very useful.