Skip to content

Commit 4fb3ccd

Browse files
authored
Merge pull request nanmu42#24 from nanmu42/develop
add NewCustomized() so that client works with etherscan-family API like BscScan
2 parents 3ddd9ca + c453d3c commit 4fb3ccd

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![GoDoc](https://godoc.org/github.com/nanmu42/etherscan-api?status.svg)](https://godoc.org/github.com/nanmu42/etherscan-api)
77
[中文文档](https://github.com/nanmu42/etherscan-api/blob/master/README_ZH.md)
88

9-
Go bindings to the Etherscan.io API, with nearly Full implementation(accounts, transactions, tokens, contracts, blocks, stats), full network support(Mainnet, Ropsten, Kovan, Rinkby, Tobalaba), and only depending on standard library. :wink:
9+
Go bindings to the Etherscan.io API(and its families like BscScan), with nearly Full implementation(accounts, transactions, tokens, contracts, blocks, stats), full network support(Mainnet, Ropsten, Kovan, Rinkby, Tobalaba), and only depending on standard library. :wink:
1010

1111
# Usage
1212

@@ -22,6 +22,15 @@ func main() {
2222
// create a API client for specified ethereum net
2323
// there are many pre-defined network in package
2424
client := etherscan.New(etherscan.Mainnet, "[your API key]")
25+
26+
// or, if you are working with etherscan-family API like BscScan
27+
//
28+
// client := etherscan.NewCustomized(etherscan.Customization{
29+
// Timeout: 15 * time.Second,
30+
// Key: "You key here",
31+
// BaseURL: "https://api.bscscan.com/api?",
32+
// Verbose: false,
33+
// })
2534

2635
// (optional) add hooks, e.g. for rate limit
2736
client.BeforeRequest = func(module, action string, param map[string]interface{}) error {

client.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
// Clients are safe for concurrent use by multiple goroutines.
2323
type Client struct {
2424
coon *http.Client
25-
network Network
2625
key string
2726
baseURL string
2827

@@ -41,13 +40,45 @@ type Client struct {
4140
// New initialize a new etherscan API client
4241
// please use pre-defined network value
4342
func New(network Network, APIKey string) *Client {
43+
return NewCustomized(Customization{
44+
Timeout: 30 * time.Second,
45+
Key: APIKey,
46+
BaseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()),
47+
})
48+
}
49+
50+
// Customization is used in NewCustomized()
51+
type Customization struct {
52+
// Timeout for API call
53+
Timeout time.Duration
54+
// API key applied from Etherscan
55+
Key string
56+
// Base URL like `https://api.etherscan.io/api?`
57+
BaseURL string
58+
// When true, talks a lot
59+
Verbose bool
60+
61+
// BeforeRequest runs before every client request, in the same goroutine.
62+
// May be used in rate limit.
63+
// Request will be aborted, if BeforeRequest returns non-nil err.
64+
BeforeRequest func(module, action string, param map[string]interface{}) error
65+
66+
// AfterRequest runs after every client request, even when there is an error.
67+
AfterRequest func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error)
68+
}
69+
70+
// NewCustomized initialize a customized API client,
71+
// useful when calling against etherscan-family API like BscScan.
72+
func NewCustomized(config Customization) *Client {
4473
return &Client{
4574
coon: &http.Client{
46-
Timeout: 30 * time.Second,
75+
Timeout: config.Timeout,
4776
},
48-
network: network,
49-
key: APIKey,
50-
baseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()),
77+
key: config.Key,
78+
baseURL: config.BaseURL,
79+
Verbose: config.Verbose,
80+
BeforeRequest: config.BeforeRequest,
81+
AfterRequest: config.AfterRequest,
5182
}
5283
}
5384

0 commit comments

Comments
 (0)