Skip to content

Commit

Permalink
feat: use a custom token account struct in client (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau authored Aug 10, 2023
1 parent 093a419 commit 0f36048
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 48 deletions.
29 changes: 19 additions & 10 deletions client/rpc_get_token_accounts_by_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package client
import (
"context"

"github.com/blocto/solana-go-sdk/common"
"github.com/blocto/solana-go-sdk/program/token"
"github.com/blocto/solana-go-sdk/rpc"
)

func (c *Client) GetTokenAccountsByOwnerByMint(ctx context.Context, owner, mintAddr string) ([]token.TokenAccount, error) {
type TokenAccount struct {
token.TokenAccount
PublicKey common.PublicKey
}

func (c *Client) GetTokenAccountsByOwnerByMint(ctx context.Context, owner, mintAddr string) ([]TokenAccount, error) {
return process(
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.GetProgramAccounts]], error) {
return c.RpcClient.GetTokenAccountsByOwnerWithConfig(
Expand All @@ -25,7 +31,7 @@ func (c *Client) GetTokenAccountsByOwnerByMint(ctx context.Context, owner, mintA
)
}

func (c *Client) GetTokenAccountsByOwnerByProgram(ctx context.Context, owner, programId string) ([]token.TokenAccount, error) {
func (c *Client) GetTokenAccountsByOwnerByProgram(ctx context.Context, owner, programId string) ([]TokenAccount, error) {
return process(
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.GetProgramAccounts]], error) {
return c.RpcClient.GetTokenAccountsByOwnerWithConfig(
Expand All @@ -43,7 +49,7 @@ func (c *Client) GetTokenAccountsByOwnerByProgram(ctx context.Context, owner, pr
)
}

func (c *Client) GetTokenAccountsByOwnerWithContextByMint(ctx context.Context, owner, mintAddr string) (rpc.ValueWithContext[[]token.TokenAccount], error) {
func (c *Client) GetTokenAccountsByOwnerWithContextByMint(ctx context.Context, owner, mintAddr string) (rpc.ValueWithContext[[]TokenAccount], error) {
return process(
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.GetProgramAccounts]], error) {
return c.RpcClient.GetTokenAccountsByOwnerWithConfig(
Expand All @@ -61,7 +67,7 @@ func (c *Client) GetTokenAccountsByOwnerWithContextByMint(ctx context.Context, o
)
}

func (c *Client) GetTokenAccountsByOwnerWithContextByProgram(ctx context.Context, owner, programId string) (rpc.ValueWithContext[[]token.TokenAccount], error) {
func (c *Client) GetTokenAccountsByOwnerWithContextByProgram(ctx context.Context, owner, programId string) (rpc.ValueWithContext[[]TokenAccount], error) {
return process(
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.GetProgramAccounts]], error) {
return c.RpcClient.GetTokenAccountsByOwnerWithConfig(
Expand All @@ -79,8 +85,8 @@ func (c *Client) GetTokenAccountsByOwnerWithContextByProgram(ctx context.Context
)
}

func convertGetTokenAccountsByOwner(v rpc.ValueWithContext[rpc.GetProgramAccounts]) ([]token.TokenAccount, error) {
tokenAccounts := make([]token.TokenAccount, 0, len(v.Value))
func convertGetTokenAccountsByOwner(v rpc.ValueWithContext[rpc.GetProgramAccounts]) ([]TokenAccount, error) {
tokenAccounts := make([]TokenAccount, 0, len(v.Value))
for _, v := range v.Value {
accountInfo, err := convertAccountInfo(v.Account)
if err != nil {
Expand All @@ -90,17 +96,20 @@ func convertGetTokenAccountsByOwner(v rpc.ValueWithContext[rpc.GetProgramAccount
if err != nil {
return nil, err
}
tokenAccounts = append(tokenAccounts, tokenAccount)
tokenAccounts = append(tokenAccounts, TokenAccount{
TokenAccount: tokenAccount,
PublicKey: common.PublicKeyFromString(v.Pubkey),
})
}
return tokenAccounts, nil
}

func convertGetTokenAccountsByOwnerAndContext(v rpc.ValueWithContext[rpc.GetProgramAccounts]) (rpc.ValueWithContext[[]token.TokenAccount], error) {
func convertGetTokenAccountsByOwnerAndContext(v rpc.ValueWithContext[rpc.GetProgramAccounts]) (rpc.ValueWithContext[[]TokenAccount], error) {
tokenAccounts, err := convertGetTokenAccountsByOwner(v)
if err != nil {
return rpc.ValueWithContext[[]token.TokenAccount]{}, err
return rpc.ValueWithContext[[]TokenAccount]{}, err
}
return rpc.ValueWithContext[[]token.TokenAccount]{
return rpc.ValueWithContext[[]TokenAccount]{
Context: v.Context,
Value: tokenAccounts,
}, nil
Expand Down
88 changes: 50 additions & 38 deletions client/rpc_get_token_accounts_by_owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ func TestClient_GetTokenAccountsByOwnerByMint(t *testing.T) {
"4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3",
)
},
ExpectedValue: []token.TokenAccount{
ExpectedValue: []TokenAccount{
{
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
Amount: 9000000000,
Delegate: nil,
State: token.TokenAccountStateInitialized,
IsNative: nil,
DelegatedAmount: 0,
CloseAuthority: nil,
TokenAccount: token.TokenAccount{
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
Amount: 9000000000,
Delegate: nil,
State: token.TokenAccountStateInitialized,
IsNative: nil,
DelegatedAmount: 0,
CloseAuthority: nil,
},
PublicKey: common.PublicKeyFromString("AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ"),
},
},
ExpectedError: nil,
Expand All @@ -58,16 +61,19 @@ func TestClient_GetTokenAccountsByOwnerByProgram(t *testing.T) {
common.TokenProgramID.ToBase58(),
)
},
ExpectedValue: []token.TokenAccount{
ExpectedValue: []TokenAccount{
{
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
Amount: 9000000000,
Delegate: nil,
State: token.TokenAccountStateInitialized,
IsNative: nil,
DelegatedAmount: 0,
CloseAuthority: nil,
TokenAccount: token.TokenAccount{
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
Amount: 9000000000,
Delegate: nil,
State: token.TokenAccountStateInitialized,
IsNative: nil,
DelegatedAmount: 0,
CloseAuthority: nil,
},
PublicKey: common.PublicKeyFromString("AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ"),
},
},
ExpectedError: nil,
Expand All @@ -91,21 +97,24 @@ func TestClient_GetTokenAccountsByOwnerWithContextByMint(t *testing.T) {
"4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3",
)
},
ExpectedValue: rpc.ValueWithContext[[]token.TokenAccount]{
ExpectedValue: rpc.ValueWithContext[[]TokenAccount]{
Context: rpc.Context{
ApiVersion: "1.14.17",
Slot: 219416878,
},
Value: []token.TokenAccount{
Value: []TokenAccount{
{
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
Amount: 9000000000,
Delegate: nil,
State: token.TokenAccountStateInitialized,
IsNative: nil,
DelegatedAmount: 0,
CloseAuthority: nil,
TokenAccount: token.TokenAccount{
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
Amount: 9000000000,
Delegate: nil,
State: token.TokenAccountStateInitialized,
IsNative: nil,
DelegatedAmount: 0,
CloseAuthority: nil,
},
PublicKey: common.PublicKeyFromString("AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ"),
},
},
},
Expand All @@ -130,21 +139,24 @@ func TestClient_GetTokenAccountsByOwnerWithContextByProgram(t *testing.T) {
common.TokenProgramID.ToBase58(),
)
},
ExpectedValue: rpc.ValueWithContext[[]token.TokenAccount]{
ExpectedValue: rpc.ValueWithContext[[]TokenAccount]{
Context: rpc.Context{
ApiVersion: "1.14.17",
Slot: 219416878,
},
Value: []token.TokenAccount{
Value: []TokenAccount{
{
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
Amount: 9000000000,
Delegate: nil,
State: token.TokenAccountStateInitialized,
IsNative: nil,
DelegatedAmount: 0,
CloseAuthority: nil,
TokenAccount: token.TokenAccount{
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
Amount: 9000000000,
Delegate: nil,
State: token.TokenAccountStateInitialized,
IsNative: nil,
DelegatedAmount: 0,
CloseAuthority: nil,
},
PublicKey: common.PublicKeyFromString("AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ"),
},
},
},
Expand Down

0 comments on commit 0f36048

Please sign in to comment.