Skip to content

Add the ability to use bscscan #23

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

Closed
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
func main() {
// create a API client for specified ethereum net
// there are many pre-defined network in package
client := etherscan.New(etherscan.Mainnet, "[your API key]")
client := etherscan.New(etherscan.Mainnet, "[your api key]")

// Alternatively, to use bscscan.com for bsc transactions, use:
// client := etherscan.New(etherscan.Mainnet, "[your api key]", etherscan.WithChain(etherscan.ChainBsc))

// (optional) add hooks, e.g. for rate limit
client.BeforeRequest = func(module, action string, param map[string]interface{}) error {
Expand Down
24 changes: 22 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,36 @@ type Client struct {
AfterRequest func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error)
}

type Option struct {
chain Chain
}

type ClientOption func(o *Option)

func WithChain(chain Chain) ClientOption {
return func(o *Option) {
o.chain = chain
}
}

// New initialize a new etherscan API client
// please use pre-defined network value
func New(network Network, APIKey string) *Client {
func New(network Network, APIKey string, opts ...ClientOption) *Client {
option := Option{
chain: ChainEthereum,
}

for _, opt := range opts {
opt(&option)
}

return &Client{
coon: &http.Client{
Timeout: 30 * time.Second,
},
network: network,
key: APIKey,
baseURL: fmt.Sprintf(`https://%s.etherscan.io/api?`, network.SubDomain()),
baseURL: fmt.Sprintf(string(option.chain), network.SubDomain()),
}
}

Expand Down
5 changes: 5 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ const (
Rinkby Network = "api-rinkeby"
// Tobalaba Testnet
Tobalaba Network = "api-tobalaba"

ChainEthereum Chain = "https://%s.etherscan.io/api?"
ChainBsc Chain = "https://%s.bscscan.com/api?"
)

type Chain string

// Network is ethereum network type (mainnet, ropsten, etc)
type Network string

Expand Down