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

Allowing to specify a different Encoding on GetAccountInfoWithConfig #122

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions client/rpc_get_account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"

"github.com/portto/solana-go-sdk/common"
Expand Down Expand Up @@ -41,14 +42,35 @@ func convertAccountInfo(v rpc.AccountInfo) (AccountInfo, error) {
}, nil
}

func convertAccountInfoWithData(v rpc.AccountInfoWithData) (AccountInfo, error) {
if v == (rpc.AccountInfoWithData{}) {
return AccountInfo{}, nil
}
rawData, err := json.Marshal(v.Data)
if err != nil {
return AccountInfo{}, fmt.Errorf("failed to base64 decode data")
}
return AccountInfo{
Lamports: v.Lamports,
Owner: common.PublicKeyFromString(v.Owner),
Executable: v.Executable,
RentEpoch: v.RentEpoch,
Data: rawData,
}, nil
}

type GetAccountInfoConfig struct {
Encoding rpc.AccountEncoding
Commitment rpc.Commitment
DataSlice *rpc.DataSlice
}

func (c GetAccountInfoConfig) toRpc() rpc.GetAccountInfoConfig {
if c.Encoding == "" {
c.Encoding = rpc.AccountEncodingBase64
}
return rpc.GetAccountInfoConfig{
Encoding: rpc.AccountEncodingBase64,
Encoding: c.Encoding,
Commitment: c.Commitment,
DataSlice: c.DataSlice,
}
Expand All @@ -66,6 +88,14 @@ func (c *Client) GetAccountInfo(ctx context.Context, base58Addr string) (Account

// GetAccountInfoWithConfig return account's info
func (c *Client) GetAccountInfoWithConfig(ctx context.Context, base58Addr string, cfg GetAccountInfoConfig) (AccountInfo, error) {
if cfg.Encoding == rpc.AccountEncodingJsonParsed {
return process(
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.AccountInfo]], error) {
return c.RpcClient.GetAccountInfoWithConfig(ctx, base58Addr, cfg.toRpc())
},
convertGetAccountInfoWithData,
)
}
return process(
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.AccountInfo]], error) {
return c.RpcClient.GetAccountInfoWithConfig(ctx, base58Addr, cfg.toRpc())
Expand All @@ -78,7 +108,7 @@ func (c *Client) GetAccountInfoWithConfig(ctx context.Context, base58Addr string
func (c *Client) GetAccountInfoAndContext(ctx context.Context, base58Addr string) (rpc.ValueWithContext[AccountInfo], error) {
return process(
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.AccountInfo]], error) {
return c.RpcClient.GetAccountInfoWithConfig(ctx, base58Addr, GetAccountInfoConfig{}.toRpc())
return c.RpcClient.GetAccountInfoWithConfig(ctx, base58Addr, GetAccountInfoConfig{Encoding: rpc.AccountEncodingBase64}.toRpc())
},
convertGetAccountInfoAndContext,
)
Expand All @@ -98,6 +128,10 @@ func convertGetAccountInfo(v rpc.ValueWithContext[rpc.AccountInfo]) (AccountInfo
return convertAccountInfo(v.Value)
}

func convertGetAccountInfoWithData(v rpc.ValueWithContext[rpc.AccountInfoWithData]) (AccountInfo, error) {
return convertAccountInfoWithData(v.Value)
}

func convertGetAccountInfoAndContext(v rpc.ValueWithContext[rpc.AccountInfo]) (rpc.ValueWithContext[AccountInfo], error) {
accountInfo, err := convertGetAccountInfo(v)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ type AccountInfo struct {
Executable bool `json:"executable"`
}

type AccountInfoWithData struct {
Lamports uint64 `json:"lamports"`
Owner string `json:"owner"`
Executable bool `json:"executable"`
RentEpoch uint64 `json:"rentEpoch"`
Data DataResultValue `json:"data"`
}

type DataResultValue struct {
Program string `json:"program"`
Space uint64 `json:"space"`
Parsed ParseStruct `json:"parsed"`
}

type ParseStruct struct {
Type string `json:"type"`
Info InfoStruct `json:"info"`
}

type InfoStruct struct {
IsNative bool `json:"isNative"`
Mint string `json:"mint"`
Owner string `json:"owner"`
State string `json:"state"`
TokenAmount interface{} `json:"tokenAmount"`
}

type TokenAccountBalance struct {
Amount string `json:"amount"`
Decimals uint8 `json:"decimals"`
Expand Down