Skip to content

Commit

Permalink
refactor: fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
loikg committed Oct 9, 2023
1 parent 04259aa commit 4162278
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
14 changes: 7 additions & 7 deletions internal/cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func tokenCreateAction(ctx *cli.Context) error {
})
}

type TokenInfo struct {
type tokenInfo struct {
TokenID string `json:"tokenId"`
TotalSupply uint64 `json:"totalSupply"`
Decimals uint32 `json:"decimals"`
Expand All @@ -199,16 +199,16 @@ func tokenShowAction(ctx *cli.Context) error {
return err
}

tokenInfo, err := hedera.NewTokenInfoQuery().
info, err := hedera.NewTokenInfoQuery().
SetTokenID(tokenID).Execute(client)
if err != nil {
return err
}

return internal.ConsolePrint(ctx.App.Writer, &TokenInfo{
TokenID: tokenInfo.TokenID.String(),
TotalSupply: tokenInfo.TotalSupply,
Decimals: tokenInfo.Decimals,
Treasury: tokenInfo.Treasury.String(),
return internal.ConsolePrint(ctx.App.Writer, &tokenInfo{
TokenID: info.TokenID.String(),
TotalSupply: info.TotalSupply,
Decimals: info.Decimals,
Treasury: info.Treasury.String(),
})
}
15 changes: 2 additions & 13 deletions internal/hedera.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ const (
HederaNetworkMainnet HederaNetwork = "mainnet"
)

// BuildHederaClientOptions are options passed to BuildHederaClient
type BuildHederaClientOptions struct {
Network HederaNetwork
OperatorID string
OperatorKey string
}

// BuildHederaClient create a new *hedera.Client
func BuildHederaClient(opts BuildHederaClientOptions) (*hedera.Client, error) {
parsedOperatorID, err := hedera.AccountIDFromString(opts.OperatorID)
if err != nil {
Expand Down Expand Up @@ -61,16 +63,3 @@ func buildLocalHederaClient() *hedera.Client {
func buildMainnetClient() *hedera.Client {
return hedera.ClientForMainnet()
}

// func parseNetworkString(network string) (HederaNetwork, error) {
// switch {
// case network == "local":
// return HederaNetworkLocal, nil
// case network == "testnet":
// return HederaNetworkTestNet, nil
// case network == "mainnet":
// return HederaNetworkMainnet, nil
// default:
// return "", fmt.Errorf("unsupported network string: %s", network)
// }
// }
2 changes: 2 additions & 0 deletions internal/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
)

// M is shothand alias for map[string]interface{}
type M map[string]interface{}

func (m M) String() string {
Expand All @@ -16,6 +17,7 @@ func (m M) String() string {
return string(bytes)
}

// ConsolePrint pretty print the given data as json to w io.Writer
func ConsolePrint(w io.Writer, data any) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
Expand Down
9 changes: 9 additions & 0 deletions internal/testutils/hedera.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"github.com/hashgraph/hedera-sdk-go/v2"
)

// HederaTestClient is a simple wrapper around a *hedera.Client use to make assertions
// about the state of the chain in tests.
type HederaTestClient struct {
client *hedera.Client
t *testing.T
}

// NewHederaTestClient create a new HederaTestClient. Once client should be instanciated for each tests.
func NewHederaTestClient(t *testing.T) *HederaTestClient {
t.Helper()

Expand Down Expand Up @@ -40,6 +43,7 @@ func NewHederaTestClient(t *testing.T) *HederaTestClient {
}
}

// GetAccount query an hedera account by account id.
func (c HederaTestClient) GetAccount(accountID string) (hedera.AccountInfo, error) {
id, err := hedera.AccountIDFromString(accountID)
if err != nil {
Expand All @@ -48,6 +52,7 @@ func (c HederaTestClient) GetAccount(accountID string) (hedera.AccountInfo, erro
return hedera.NewAccountInfoQuery().SetAccountID(id).Execute(c.client)
}

// MustCreateAccount attempt to create an hedera account, it fails the current tests if any error occur.
func (c HederaTestClient) MustCreateAccount(balance float64) (*hedera.AccountID, hedera.PrivateKey) {
privateKey, err := hedera.PrivateKeyGenerateEd25519()
if err != nil {
Expand All @@ -71,6 +76,7 @@ func (c HederaTestClient) MustCreateAccount(balance float64) (*hedera.AccountID,
return receipt.AccountID, privateKey
}

// GetToken query a token by token id.
func (c HederaTestClient) GetToken(tokenIDStr string) (hedera.TokenInfo, error) {
tokenID, err := hedera.TokenIDFromString(tokenIDStr)
if err != nil {
Expand All @@ -80,6 +86,7 @@ func (c HederaTestClient) GetToken(tokenIDStr string) (hedera.TokenInfo, error)
return hedera.NewTokenInfoQuery().SetTokenID(tokenID).Execute(c.client)
}

// CreateTokenOptions are options pass to HederaTestClient.MustCreateToken to create a new token.
type CreateTokenOptions struct {
Name string
Symbol string
Expand All @@ -92,6 +99,7 @@ type CreateTokenOptions struct {
SupplyKey hedera.PrivateKey
}

// MustCreateToken creates a new token with the given params. It fails the current test if any error occur.
func (c HederaTestClient) MustCreateToken(opts *CreateTokenOptions) *hedera.TokenID {
tokenCreateTx, err := hedera.NewTokenCreateTransaction().
SetTokenName(opts.Name).
Expand Down Expand Up @@ -125,6 +133,7 @@ func (c HederaTestClient) MustCreateToken(opts *CreateTokenOptions) *hedera.Toke
return tokenCreateRx.TokenID
}

// MustGenerateKey generate a new key pair. It fails the current test if any error occur.
func (c HederaTestClient) MustGenerateKey() hedera.PrivateKey {
key, err := hedera.PrivateKeyGenerateEd25519()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions internal/testutils/hedera_assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ import (
"github.com/stretchr/testify/assert"
)

// AssertValidAccountID tries to parse the given accountID as an hedera.AccountID
func AssertValidAccountID(t *testing.T, accountID interface{}) {
t.Helper()

assert.IsType(t, "string", accountID)
_, err := hedera.AccountIDFromString(accountID.(string))
if err != nil {
t.Fatalf("expected %s to be a valid account id: %v", accountID, err)
}
}

// AssertValidKeyPair tries to parse the given key pair as an hedera.PrivateKey
func AssertValidKeyPair(t *testing.T, privateKey interface{}, publicKey interface{}) {
t.Helper()

assert.IsType(t, "string", privateKey)
assert.IsType(t, "string", publicKey)
key, err := hedera.PrivateKeyFromString(privateKey.(string))
_, err := hedera.PrivateKeyFromString(privateKey.(string))
if err != nil {
t.Fatalf("failed parse private key %s: %v", privateKey, err)
}
assert.Equal(t, key.PublicKey().String(), publicKey)
}

0 comments on commit 4162278

Please sign in to comment.