From 0f36048b6b3a2f2a6e84a2a2a99d02784e9b3aa2 Mon Sep 17 00:00:00 2001 From: Yihau Chen Date: Thu, 10 Aug 2023 13:30:35 +0800 Subject: [PATCH] feat: use a custom token account struct in client (#141) --- client/rpc_get_token_accounts_by_owner.go | 29 +++--- .../rpc_get_token_accounts_by_owner_test.go | 88 +++++++++++-------- 2 files changed, 69 insertions(+), 48 deletions(-) diff --git a/client/rpc_get_token_accounts_by_owner.go b/client/rpc_get_token_accounts_by_owner.go index 9d5de110..51ab491d 100644 --- a/client/rpc_get_token_accounts_by_owner.go +++ b/client/rpc_get_token_accounts_by_owner.go @@ -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( @@ -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( @@ -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( @@ -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( @@ -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 { @@ -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 diff --git a/client/rpc_get_token_accounts_by_owner_test.go b/client/rpc_get_token_accounts_by_owner_test.go index 10c24710..9fc6a2ea 100644 --- a/client/rpc_get_token_accounts_by_owner_test.go +++ b/client/rpc_get_token_accounts_by_owner_test.go @@ -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, @@ -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, @@ -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"), }, }, }, @@ -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"), }, }, },