-
Notifications
You must be signed in to change notification settings - Fork 210
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
how to get token owner of a token account #206
Comments
Here is how you do it @BillInUK: package main
import (
"context"
"fmt"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
)
func main() {
rpcClient := rpc.New(rpc.MainNetBeta_RPC)
tokenAccountPubKey := solana.MustPublicKeyFromBase58("3XRNhFEXkV7itJEJMDBN8iRHBgwhgk83XB5K3s3re3Ew")
// Fetch account info
accountInfo, err := rpcClient.GetAccountInfo(context.Background(), tokenAccountPubKey)
if err != nil {
fmt.Printf("Error retrieving account info: %v\n", err)
return
}
owner := accountInfo.Value.Owner
// Output the owner of the token account
fmt.Printf("Token account owner: %v\n", owner.String())
// OUTPUT: Token account owner: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
// Ensure that data is available
if accountInfo.Value == nil || accountInfo.Value.Data.GetBinary() == nil {
fmt.Println("Account data is not available")
return
}
fmt.Println("Trying the second method...")
data := accountInfo.Value.Data.GetBinary()
// Check if data length is sufficient to contain Mint and Owner public keys
if len(data) < 64 {
fmt.Println("Account data is too short to contain required information")
return
}
// Owner public key starts at byte 32 and spans 32 bytes
ownerPubKey := solana.PublicKeyFromBytes(data[32:64])
// Output the owner of the token account
fmt.Printf("Token account owner from metadata: %v\n", ownerPubKey)
// OUTPUT: Token account owner: GmKsGRytiVoeMZGmBVCWPcUzJGHVqcvzhP5K9cstdr3E
} |
thanks for your help, i will try it !!! |
I implemented this functionality by defining my own structure to parse the results of GetAccountInfoWithOpts. The code is as follows: const (
SPLTokenProgramId = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
)
type SPLTokenAccountInfo struct {
IsNative bool `json:"isNative"`
Mint solana.PublicKey `json:"mint"`
Owner solana.PublicKey `json:"owner"`
State string `json:"state"`
TokenAmount struct {
Amount string `json:"amount"`
Decimals int `json:"decimals"`
UIAmount float64 `json:"uiAmount"`
UIAmountStr string `json:"uiAmountString"`
} `json:"tokenAmount"`
}
type SPLTokenAccountData struct {
TokenAccountInfo SPLTokenAccountInfo `json:"info"`
Type string `json:"type"`
}
type ParsedData struct {
Parsed SPLTokenAccountData `json:"parsed"`
Program string `json:"program"`
Space int `json:"space"`
}
|
Overall, the code you provided is simpler, avoiding unnecessary struct definitions and JSON parsing. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use the following code to get owner of token account, but returned me a program id 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'。
how can i get the token owner of this token account? like below:
The text was updated successfully, but these errors were encountered: