blocto / solana-go-sdk

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

Allowing to specify a different Encoding on GetAccountInfoWithConfig #122

Open pegartillo95 opened 1 year ago

pegartillo95 commented 1 year ago

This will keep the base GetAccount info to still use base64 encoding but allow users to specify a different one if they want to do so.

yihau commented 1 year ago

Thanks for the contributing. GetAccountInfoWithConfig will convert encoded data into []byte. If we would like to make this happen, we will need to update here as well

Also, could you share the use case with me? I'm not sure why users need to specify the attribute cuz the data will be transferred into bytes eventually. No matter what users pass, the output data always be []byte

If you would like to get the encoded data, I think you can either

  1. encode the data by yourself or
  2. use c.RpcClient. GetAccountInfoWithConfig to query
pedro-seedcx commented 1 year ago

So maybe I understood wrong the Encoding field for that RPC method. Does it change the way the whole returned payload is encoded or it only does for the Data field of the response?

yihau commented 1 year ago

yeah. you can take a look at https://docs.solana.com/developing/clients/jsonrpc-api#getaccountinfo

pedro-seedcx commented 1 year ago

And out of curiosity how would you get the json information from the base64 data if you do not specify parsing to be jsonParsed? What I need to get is the owner inside the data. This is an example of the result of the call when executing it with jsonParsed and I would need to get the inner owner, the one with value DhEsUaJkT1DzkFUWLCkU21VruJQZk1es4zBRhU9QjK9R in this case: {"context":{"apiVersion":"1.13.5","slot":173125432},"value":{"data":{"parsed":{"info":{"isNative":false,"mint":"Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB","owner":"DhEsUaJkT1DzkFUWLCkU21VruJQZk1es4zBRhU9QjK9R","state":"initialized","tokenAmount":{"amount":"5000000","decimals":6,"uiAmount":5.0,"uiAmountString":"5"}},"type":"account"},"program":"spl-token","space":165},"executable":false,"lamports":2039280,"owner":"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA","rentEpoch":361}},"id":1}

pedro-seedcx commented 1 year ago

Added the parsing of the response when you specify it to be jsonParsed. I still think it is needed but I might be missing something

yihau commented 1 year ago

You can decode data to get it. Seems you would like to get a token account. You can either https://portto.github.io/solana-go-sdk/tour/get-token-account.html or use this function https://github.com/portto/solana-go-sdk/blob/main/client/token.go#L9 directlly

jsonParsed not only decodes for token account but also mint, vote, stake ... so your InfoStruct only works when the account is token account.

pedro-seedcx commented 1 year ago

I got it, thanks a lot for your help. Then you are completely right and there is no need to add my code.