Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Myrepo #80

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"sync"
"time"
)

// Client etherscan API client
// Clients are safe for concurrent use by multiple goroutines.
type Client struct {
coon *http.Client
key string
baseURL string
coon *http.Client
baseURL string
keys []string
keySelectMutex sync.Mutex
keySelectIdx int

// Verbose when true, talks a lot
Verbose bool
Expand All @@ -37,12 +40,32 @@ type Client struct {
AfterRequest func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error)
}

func (c *Client) getKey() string {
c.keySelectMutex.Lock()
defer c.keySelectMutex.Unlock()
c.keySelectIdx++
if c.keySelectIdx >= len(c.keys) {
c.keySelectIdx = 0
}
return c.keys[c.keySelectIdx]
}

// New initialize a new etherscan API client
// please use pre-defined network value
func New(network Network, APIKeys string) *Client {
return NewCustomized(Customization{
Timeout: 30 * time.Second,
Keys: []string{APIKeys},
BaseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()),
})
}

// New initialize a new etherscan API client
// please use pre-defined network value
func New(network Network, APIKey string) *Client {
func NewMultiKey(network Network, APIKeys []string) *Client {
return NewCustomized(Customization{
Timeout: 30 * time.Second,
Key: APIKey,
Keys: APIKeys,
BaseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()),
})
}
Expand All @@ -51,8 +74,8 @@ func New(network Network, APIKey string) *Client {
type Customization struct {
// Timeout for API call
Timeout time.Duration
// API key applied from Etherscan
Key string
// API keys applied from Etherscan
Keys []string
// Base URL like `https://api.etherscan.io/api?`
BaseURL string
// When true, talks a lot
Expand Down Expand Up @@ -83,7 +106,7 @@ func NewCustomized(config Customization) *Client {
}
return &Client{
coon: httpClient,
key: config.Key,
keys: config.Keys,
baseURL: config.BaseURL,
Verbose: config.Verbose,
BeforeRequest: config.BeforeRequest,
Expand Down Expand Up @@ -117,7 +140,7 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco
err = wrapErr(err, "http.NewRequest")
return
}
req.Header.Set("User-Agent", "etherscan-api(Go)")
req.Header.Set("User-Agent", "etherscan-api-multikey(Go)")
req.Header.Set("Content-Type", "application/json; charset=utf-8")

if c.Verbose {
Expand Down Expand Up @@ -196,7 +219,7 @@ func (c *Client) craftURL(module, action string, param map[string]interface{}) (
q := url.Values{
"module": []string{module},
"action": []string{action},
"apikey": []string{c.key},
"apikey": []string{c.getKey()},
}

for k, v := range param {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/nanmu42/etherscan-api
module github.com/NFEL/etherscan-api-multikey

go 1.13

Expand Down
21 changes: 21 additions & 0 deletions multi_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package etherscan

import "testing"

// TestGetKey may fail is you run test parallel
func TestGetKey(t *testing.T) {
countApiKey, countBackupApiKey, k := 0, 0, ""
for i := 0; i < 10; i++ {
k = api.getKey()
t.Logf("key: %s", k)
if apiKey == k {
countApiKey++
} else if backupApiKey == k {
countBackupApiKey++
}
}
equal := countApiKey == 5 && countBackupApiKey == 5
if !equal {
t.Errorf("api.getKey not working, expected 5 for each key, got main:%d , backup %d", countApiKey, countBackupApiKey)
}
}
14 changes: 12 additions & 2 deletions setup_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,40 @@ package etherscan

import (
"fmt"
"log"
"os"
"testing"
"time"
)

const apiKeyEnvName = "ETHERSCAN_API_KEY"
const (
apiKeyEnvName = "ETHERSCAN_API_KEY"
backupApiKeyEnvName = "BACKUP_ETHERSCAN_API_KEY"
)

var (
// api test client for many test cases
api *Client
// bucket default rate limiter
bucket *Bucket
// apiKey etherscan API key
apiKey string
apiKey string
backupApiKey string
)

func init() {
apiKey = os.Getenv(apiKeyEnvName)
if apiKey == "" {
panic(fmt.Sprintf("API key is empty, set env variable %q with a valid API key to proceed.", apiKeyEnvName))
}
backupApiKey = os.Getenv(backupApiKeyEnvName)
if backupApiKey == "" {
log.Printf("WARN: Backup API key is empty, set env variable %q with a valid API key to proceed.", backupApiKeyEnvName)
}
bucket = NewBucket(500 * time.Millisecond)

api = New(Mainnet, apiKey)
api = NewMultiKey(Mainnet, []string{apiKey, backupApiKey})
api.Verbose = true
api.BeforeRequest = func(module string, action string, param map[string]interface{}) error {
bucket.Take()
Expand Down