Skip to content

Commit 0f36048

Browse files
authored
feat: use a custom token account struct in client (#141)
1 parent 093a419 commit 0f36048

File tree

2 files changed

+69
-48
lines changed

2 files changed

+69
-48
lines changed

client/rpc_get_token_accounts_by_owner.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ package client
33
import (
44
"context"
55

6+
"github.com/blocto/solana-go-sdk/common"
67
"github.com/blocto/solana-go-sdk/program/token"
78
"github.com/blocto/solana-go-sdk/rpc"
89
)
910

10-
func (c *Client) GetTokenAccountsByOwnerByMint(ctx context.Context, owner, mintAddr string) ([]token.TokenAccount, error) {
11+
type TokenAccount struct {
12+
token.TokenAccount
13+
PublicKey common.PublicKey
14+
}
15+
16+
func (c *Client) GetTokenAccountsByOwnerByMint(ctx context.Context, owner, mintAddr string) ([]TokenAccount, error) {
1117
return process(
1218
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.GetProgramAccounts]], error) {
1319
return c.RpcClient.GetTokenAccountsByOwnerWithConfig(
@@ -25,7 +31,7 @@ func (c *Client) GetTokenAccountsByOwnerByMint(ctx context.Context, owner, mintA
2531
)
2632
}
2733

28-
func (c *Client) GetTokenAccountsByOwnerByProgram(ctx context.Context, owner, programId string) ([]token.TokenAccount, error) {
34+
func (c *Client) GetTokenAccountsByOwnerByProgram(ctx context.Context, owner, programId string) ([]TokenAccount, error) {
2935
return process(
3036
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.GetProgramAccounts]], error) {
3137
return c.RpcClient.GetTokenAccountsByOwnerWithConfig(
@@ -43,7 +49,7 @@ func (c *Client) GetTokenAccountsByOwnerByProgram(ctx context.Context, owner, pr
4349
)
4450
}
4551

46-
func (c *Client) GetTokenAccountsByOwnerWithContextByMint(ctx context.Context, owner, mintAddr string) (rpc.ValueWithContext[[]token.TokenAccount], error) {
52+
func (c *Client) GetTokenAccountsByOwnerWithContextByMint(ctx context.Context, owner, mintAddr string) (rpc.ValueWithContext[[]TokenAccount], error) {
4753
return process(
4854
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.GetProgramAccounts]], error) {
4955
return c.RpcClient.GetTokenAccountsByOwnerWithConfig(
@@ -61,7 +67,7 @@ func (c *Client) GetTokenAccountsByOwnerWithContextByMint(ctx context.Context, o
6167
)
6268
}
6369

64-
func (c *Client) GetTokenAccountsByOwnerWithContextByProgram(ctx context.Context, owner, programId string) (rpc.ValueWithContext[[]token.TokenAccount], error) {
70+
func (c *Client) GetTokenAccountsByOwnerWithContextByProgram(ctx context.Context, owner, programId string) (rpc.ValueWithContext[[]TokenAccount], error) {
6571
return process(
6672
func() (rpc.JsonRpcResponse[rpc.ValueWithContext[rpc.GetProgramAccounts]], error) {
6773
return c.RpcClient.GetTokenAccountsByOwnerWithConfig(
@@ -79,8 +85,8 @@ func (c *Client) GetTokenAccountsByOwnerWithContextByProgram(ctx context.Context
7985
)
8086
}
8187

82-
func convertGetTokenAccountsByOwner(v rpc.ValueWithContext[rpc.GetProgramAccounts]) ([]token.TokenAccount, error) {
83-
tokenAccounts := make([]token.TokenAccount, 0, len(v.Value))
88+
func convertGetTokenAccountsByOwner(v rpc.ValueWithContext[rpc.GetProgramAccounts]) ([]TokenAccount, error) {
89+
tokenAccounts := make([]TokenAccount, 0, len(v.Value))
8490
for _, v := range v.Value {
8591
accountInfo, err := convertAccountInfo(v.Account)
8692
if err != nil {
@@ -90,17 +96,20 @@ func convertGetTokenAccountsByOwner(v rpc.ValueWithContext[rpc.GetProgramAccount
9096
if err != nil {
9197
return nil, err
9298
}
93-
tokenAccounts = append(tokenAccounts, tokenAccount)
99+
tokenAccounts = append(tokenAccounts, TokenAccount{
100+
TokenAccount: tokenAccount,
101+
PublicKey: common.PublicKeyFromString(v.Pubkey),
102+
})
94103
}
95104
return tokenAccounts, nil
96105
}
97106

98-
func convertGetTokenAccountsByOwnerAndContext(v rpc.ValueWithContext[rpc.GetProgramAccounts]) (rpc.ValueWithContext[[]token.TokenAccount], error) {
107+
func convertGetTokenAccountsByOwnerAndContext(v rpc.ValueWithContext[rpc.GetProgramAccounts]) (rpc.ValueWithContext[[]TokenAccount], error) {
99108
tokenAccounts, err := convertGetTokenAccountsByOwner(v)
100109
if err != nil {
101-
return rpc.ValueWithContext[[]token.TokenAccount]{}, err
110+
return rpc.ValueWithContext[[]TokenAccount]{}, err
102111
}
103-
return rpc.ValueWithContext[[]token.TokenAccount]{
112+
return rpc.ValueWithContext[[]TokenAccount]{
104113
Context: v.Context,
105114
Value: tokenAccounts,
106115
}, nil

client/rpc_get_token_accounts_by_owner_test.go

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ func TestClient_GetTokenAccountsByOwnerByMint(t *testing.T) {
2525
"4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3",
2626
)
2727
},
28-
ExpectedValue: []token.TokenAccount{
28+
ExpectedValue: []TokenAccount{
2929
{
30-
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
31-
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
32-
Amount: 9000000000,
33-
Delegate: nil,
34-
State: token.TokenAccountStateInitialized,
35-
IsNative: nil,
36-
DelegatedAmount: 0,
37-
CloseAuthority: nil,
30+
TokenAccount: token.TokenAccount{
31+
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
32+
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
33+
Amount: 9000000000,
34+
Delegate: nil,
35+
State: token.TokenAccountStateInitialized,
36+
IsNative: nil,
37+
DelegatedAmount: 0,
38+
CloseAuthority: nil,
39+
},
40+
PublicKey: common.PublicKeyFromString("AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ"),
3841
},
3942
},
4043
ExpectedError: nil,
@@ -58,16 +61,19 @@ func TestClient_GetTokenAccountsByOwnerByProgram(t *testing.T) {
5861
common.TokenProgramID.ToBase58(),
5962
)
6063
},
61-
ExpectedValue: []token.TokenAccount{
64+
ExpectedValue: []TokenAccount{
6265
{
63-
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
64-
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
65-
Amount: 9000000000,
66-
Delegate: nil,
67-
State: token.TokenAccountStateInitialized,
68-
IsNative: nil,
69-
DelegatedAmount: 0,
70-
CloseAuthority: nil,
66+
TokenAccount: token.TokenAccount{
67+
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
68+
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
69+
Amount: 9000000000,
70+
Delegate: nil,
71+
State: token.TokenAccountStateInitialized,
72+
IsNative: nil,
73+
DelegatedAmount: 0,
74+
CloseAuthority: nil,
75+
},
76+
PublicKey: common.PublicKeyFromString("AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ"),
7177
},
7278
},
7379
ExpectedError: nil,
@@ -91,21 +97,24 @@ func TestClient_GetTokenAccountsByOwnerWithContextByMint(t *testing.T) {
9197
"4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3",
9298
)
9399
},
94-
ExpectedValue: rpc.ValueWithContext[[]token.TokenAccount]{
100+
ExpectedValue: rpc.ValueWithContext[[]TokenAccount]{
95101
Context: rpc.Context{
96102
ApiVersion: "1.14.17",
97103
Slot: 219416878,
98104
},
99-
Value: []token.TokenAccount{
105+
Value: []TokenAccount{
100106
{
101-
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
102-
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
103-
Amount: 9000000000,
104-
Delegate: nil,
105-
State: token.TokenAccountStateInitialized,
106-
IsNative: nil,
107-
DelegatedAmount: 0,
108-
CloseAuthority: nil,
107+
TokenAccount: token.TokenAccount{
108+
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
109+
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
110+
Amount: 9000000000,
111+
Delegate: nil,
112+
State: token.TokenAccountStateInitialized,
113+
IsNative: nil,
114+
DelegatedAmount: 0,
115+
CloseAuthority: nil,
116+
},
117+
PublicKey: common.PublicKeyFromString("AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ"),
109118
},
110119
},
111120
},
@@ -130,21 +139,24 @@ func TestClient_GetTokenAccountsByOwnerWithContextByProgram(t *testing.T) {
130139
common.TokenProgramID.ToBase58(),
131140
)
132141
},
133-
ExpectedValue: rpc.ValueWithContext[[]token.TokenAccount]{
142+
ExpectedValue: rpc.ValueWithContext[[]TokenAccount]{
134143
Context: rpc.Context{
135144
ApiVersion: "1.14.17",
136145
Slot: 219416878,
137146
},
138-
Value: []token.TokenAccount{
147+
Value: []TokenAccount{
139148
{
140-
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
141-
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
142-
Amount: 9000000000,
143-
Delegate: nil,
144-
State: token.TokenAccountStateInitialized,
145-
IsNative: nil,
146-
DelegatedAmount: 0,
147-
CloseAuthority: nil,
149+
TokenAccount: token.TokenAccount{
150+
Mint: common.PublicKeyFromString("4UyUTBdhPkFiu7ZE8zfxnE6hbbzf8LKo1uR5wSi5MYE3"),
151+
Owner: common.PublicKeyFromString("27kVX7JpPZ1bsrSckbR76mV6GeRqtrjoddubfg2zBpHZ"),
152+
Amount: 9000000000,
153+
Delegate: nil,
154+
State: token.TokenAccountStateInitialized,
155+
IsNative: nil,
156+
DelegatedAmount: 0,
157+
CloseAuthority: nil,
158+
},
159+
PublicKey: common.PublicKeyFromString("AyHWro8zumyZN68Mhuk6mhNUUQ2VX5qux2pMD4HnN3aJ"),
148160
},
149161
},
150162
},

0 commit comments

Comments
 (0)