Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
jordipainan committed Apr 11, 2024
1 parent 293335f commit 696d31f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 deletions census3/census3.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
const (
// maxRetries is the maximum number of retries for a request.
maxRetries = 3
// cooldown is the time to wait between retries.
cooldown = time.Second * 2
// retryTime is the time to wait between retries.
retryTime = time.Second * 2
)

// Client wraps a client for the Census3 API and other revelant data.
type Client struct {
c3client.HTTPclient
c *c3client.HTTPclient
}

// NewClient creates a new client for the Census3 API.
Expand All @@ -37,7 +37,7 @@ func NewClient(endpoint string, bearerToken string) (*Client, error) {
return nil, fmt.Errorf("error creating HTTP client: %v", err)
}
c3 := &Client{
HTTPclient: *httpClient,
c: httpClient,
}
return c3, nil
}
Expand All @@ -51,7 +51,7 @@ func requestWithRetry[T any](fn reqFunc[T]) (T, error) {
for i := 0; i < maxRetries; i++ {
result, err := fn()
if err != nil {
time.Sleep(cooldown)
time.Sleep(retryTime)
continue
}
return result, nil
Expand All @@ -62,7 +62,7 @@ func requestWithRetry[T any](fn reqFunc[T]) (T, error) {
// SupportedChains returns the information of the Census3 endpoint supported chains.
func (c3 *Client) SupportedChains() ([]c3types.SupportedChain, error) {
return requestWithRetry(func() ([]c3types.SupportedChain, error) {
info, err := c3.Info()
info, err := c3.c.Info()
if err != nil {
return nil, fmt.Errorf("failed to get supported chains: %w", err)
}
Expand All @@ -73,7 +73,7 @@ func (c3 *Client) SupportedChains() ([]c3types.SupportedChain, error) {
// Tokens returns the list of tokens registered in the Census3 endpoint.
func (c3 *Client) Tokens() ([]*c3types.TokenListItem, error) {
return requestWithRetry(func() ([]*c3types.TokenListItem, error) {
tokens, err := c3.GetTokens(-1, "", "")
tokens, err := c3.c.Tokens(-1, "", "")
if err != nil {
return nil, fmt.Errorf("failed to get tokens: %w", err)
}
Expand All @@ -85,7 +85,7 @@ func (c3 *Client) Tokens() ([]*c3types.TokenListItem, error) {
// Token returns the token with the given ID.
func (c3 *Client) Token(tokenID, externalID string, chainID uint64) (*c3types.Token, error) {
return requestWithRetry(func() (*c3types.Token, error) {
token, err := c3.GetToken(tokenID, chainID, externalID)
token, err := c3.c.Token(tokenID, chainID, externalID)
if err != nil {
return nil, fmt.Errorf("failed to get token: %w", err)
}
Expand All @@ -96,7 +96,7 @@ func (c3 *Client) Token(tokenID, externalID string, chainID uint64) (*c3types.To
// SupportedTokens returns the list of tokens supported by the Census3 endpoint.
func (c3 *Client) SupportedTokens() ([]string, error) {
return requestWithRetry(func() ([]string, error) {
tokens, err := c3.GetTokenTypes()
tokens, err := c3.c.TokenTypes()
if err != nil {
return nil, fmt.Errorf("failed to get supported tokens: %w", err)
}
Expand All @@ -107,7 +107,7 @@ func (c3 *Client) SupportedTokens() ([]string, error) {
// Strategies returns the list of strategies registered in the Census3 endpoint.
func (c3 *Client) Strategies() ([]*c3types.Strategy, error) {
return requestWithRetry(func() ([]*c3types.Strategy, error) {
strategies, err := c3.GetStrategies(-1, "", "")
strategies, err := c3.c.Strategies(-1, "", "")
if err != nil {
return nil, fmt.Errorf("failed to get strategies: %w", err)
}
Expand All @@ -118,19 +118,19 @@ func (c3 *Client) Strategies() ([]*c3types.Strategy, error) {
// Strategy returns the strategy with the given ID.
func (c3 *Client) Strategy(strategyID uint64) (*c3types.Strategy, error) {
return requestWithRetry(func() (*c3types.Strategy, error) {
strategy, err := c3.GetStrategy(strategyID)
strategy, err := c3.c.Strategy(strategyID)
if err != nil {
return nil, fmt.Errorf("failed to get strategy: %w", err)
}
return strategy, nil
})
}

// GetStrategyHolders returns the list of holders for the given strategy.
// StrategyHolders returns the list of holders for the given strategy.
// The returned map has the holder's address as key and the holder's balance as a string encoded big.Int
func (c3 *Client) GetStrategyHolders(strategyID uint64) (map[string]string, error) {
func (c3 *Client) StrategyHolders(strategyID uint64) (map[string]string, error) {
return requestWithRetry(func() (map[string]string, error) {
holders, err := c3.GetHoldersByStrategy(strategyID)
holders, err := c3.c.HoldersByStrategy(strategyID)
if err != nil {
return nil, fmt.Errorf("failed to get strategy holders: %w", err)
}
Expand All @@ -141,7 +141,7 @@ func (c3 *Client) GetStrategyHolders(strategyID uint64) (map[string]string, erro
// Census returns the census with the given ID.
func (c3 *Client) Census(censusID uint64) (*c3types.Census, error) {
return requestWithRetry(func() (*c3types.Census, error) {
census, err := c3.GetCensus(censusID)
census, err := c3.c.Census(censusID)
if err != nil {
return nil, fmt.Errorf("failed to get census: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
github.com/vocdoni/census3 v0.1.4-0.20240409153009-cb9df91453c7
github.com/vocdoni/census3 v0.1.4-0.20240411135514-e434723b7840
github.com/zeebo/blake3 v0.2.3
go.mongodb.org/mongo-driver v1.14.0
go.vocdoni.io/dvote v1.10.2-0.20240313095944-f5790a5af0ed
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1548,8 +1548,8 @@ github.com/vektah/gqlparser/v2 v2.5.1 h1:ZGu+bquAY23jsxDRcYpWjttRZrUz07LbiY77gUO
github.com/vektah/gqlparser/v2 v2.5.1/go.mod h1:mPgqFBu/woKTVYWyNk8cO3kh4S/f4aRFZrvOnp3hmCs=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/vocdoni/census3 v0.1.4-0.20240409153009-cb9df91453c7 h1:DJr/cvnV3QfErKhblAYihdmbrLqRcJl2bgENE6xysNA=
github.com/vocdoni/census3 v0.1.4-0.20240409153009-cb9df91453c7/go.mod h1:Ml5LlJQLbbd5GYttVCKu4kN2mjvrdLSxXfjoc/okNRY=
github.com/vocdoni/census3 v0.1.4-0.20240411135514-e434723b7840 h1:1iw2/8sSZAyXioxG7y4bf7D59DVrFtfYuVu1UNoocps=
github.com/vocdoni/census3 v0.1.4-0.20240411135514-e434723b7840/go.mod h1:Ml5LlJQLbbd5GYttVCKu4kN2mjvrdLSxXfjoc/okNRY=
github.com/vocdoni/storage-proofs-eth-go v0.1.6 h1:mF6VNGudgCjjgjxs5Z2GGMM2tS79+xFBWcr7BnPuhAY=
github.com/vocdoni/storage-proofs-eth-go v0.1.6/go.mod h1:aoD9pKg8kDFQ5I6b3obGPTwjC+DsaW2h4wt2UQEjQrg=
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE=
Expand Down

0 comments on commit 696d31f

Please sign in to comment.