Categorygithub.com/nanmu42/etherscan-api
modulepackage
1.10.0
Repository: https://github.com/nanmu42/etherscan-api.git
Documentation: pkg.go.dev

# README

English | 中文

etherscan-api

GoDoc CI status codecov Go Report Card

Golang client for 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, Goerli, Tobalaba), and only depending on standard library. :wink:

Usage

go get github.com/nanmu42/etherscan-api

Create an API instance and off you go. :rocket:

import (
	"github.com/nanmu42/etherscan-api"
	"fmt"
)

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]")
	
	// or, if you are working with etherscan-family API like BscScan
	//
	// client := etherscan.NewCustomized(etherscan.Customization{
	// Timeout:       15 * time.Second,
	// Key:           "You key here",
	// BaseURL:       "https://api.bscscan.com/api?",
	// Verbose:       false,
	// })

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

	// check account balance
	balance, err := client.AccountBalance("0x281055afc982d96fab65b3a49cac8b878184cb16")
	if err != nil {
		panic(err)
	}
	// balance in wei, in *big.Int type
	fmt.Println(balance.Int())

	// check token balance
	tokenBalance, err := client.TokenBalance("contractAddress", "holderAddress")

	// check ERC20 transactions from/to a specified address
	transfers, err := client.ERC20Transfers("contractAddress", "address", startBlock, endBlock, page, offset)
}

You may find full method list at GoDoc.

Etherscan API Key

You may apply for an API key on etherscan.

The Etherscan Ethereum Developer APIs are provided as a community service and without warranty, so please just use what you need and no more. They support both GET/POST requests and a rate limit of 5 requests/sec (exceed and you will be blocked).

Paperwork Things

I am not from Etherscan and I just find their service really useful, so I implement this. :smile:

License

Use of this work is governed by an MIT License.

You may find a license copy in project root.

# Functions

New initialize a new etherscan API client please use pre-defined network value.
NewCustomized initialize a customized API client, useful when calling against etherscan-family API like BscScan.

# Constants

Goerli Testnet(CLIQUE).
Kovan Testnet(POA).
Mainnet Ethereum mainnet for production.
Rinkby Testnet(CLIQUE).
Ropsten Testnet(POW).
Tobalaba Testnet.

# Variables

ErrPreByzantiumTx transaction before 4,370,000 does not support receipt status check.

# Structs

AccountBalance account and its balance in pair.
BlockRewards holds info from query for block and uncle rewards.
Client etherscan API client Clients are safe for concurrent use by multiple goroutines.
ContractSource holds info from query for contract source code.
Customization is used in NewCustomized().
Envelope is the carrier of nearly every response.
ERC1155Transfer holds info from ERC1155 token transfer event query.
ERC20Transfer holds info from ERC20 token transfer event query.
ERC721Transfer holds info from ERC721 token transfer event query.
ExecutionStatus holds info from query for transaction execution status.
GasPrices holds info for Gas Oracle queries Gas Prices are returned in Gwei.
InternalTx holds info from internal tx query.
LatestPrice holds info from query for latest ether price.
No description provided by the author
MinedBlock holds info from query for mined block by address.
NormalTx holds info from normal tx query.

# Type aliases

BigInt is a wrapper over big.Int to implement only unmarshalText for json decoding.
M is a type shorthand for param input.
Network is ethereum network type (mainnet, ropsten, etc).
Time is a wrapper over big.Int to implement only unmarshalText for json decoding.