Code Monkey home page Code Monkey logo

etherscan-api's Introduction

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. 😉

Usage

go get github.com/nanmu42/etherscan-api

Create an API instance and off you go. 🚀

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. 😄

License

Use of this work is governed by an MIT License.

You may find a license copy in project root.

etherscan-api's People

Contributors

7ac avatar avislash avatar benknigge avatar coolestowl avatar jojorb avatar krasi-georgiev avatar nanmu42 avatar pashteto avatar will7200 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

etherscan-api's Issues

Allow for an empty contract in ERC20Transfers function

pull request #10

This would allow for an empty string pointer to be passed in to ERC20Transfers
resulting in the parameter of the contract address being omitted from the underlying api call.

The result being all coin transfers being returned instead of a list filtered by contract address.

NIL parameters pass to rpc-call

They are not pass, if the code in helper.go change to:

// compose adds input to param, whose key is tag
// if input is nil, compose is a no-op.
func compose(param map[string]interface{}, tag string, input interface{}) {
	if  reflect.ValueOf(input).IsNil() {
		return
	}
	param[tag] = input
}

Etherscan api can return the string "0_0" as the traceId for InternalTx requests

I've found a bug.

[Error] json unmarshal outcome: json: cannot unmarshal number 0_0 into Go struct field InternalTx.traceId of type int

To recreate this issue

internalTX, err := etherScanClient.InternalTxByAddress(address, &startBlock, &bestBlock, 1, 0, true)

Address "0x8b7B6C61238088593BF75eEC8FBF58D0a615d30c"

where startBlock is 0 and bestBlock is the current best block of around 6.3 million

The Json returned is huge so I'll only quote the relevant node.

{
  "status": "1",
  "message": "OK",
  "result": [
    {
      "blockNumber": "4244642",
      "timeStamp": "1504704720",
      "hash": "0x67419842db50db416b035163ccefca0a06908677f642a93063c5104d2f8dd011",
      "from": "0x1522900b6dafac587d499a862861c0869be6e428",
      "to": "0x8b7b6c61238088593bf75eec8fbf58d0a615d30c",
      "value": "30000000000000000",
      "contractAddress": "",
      "input": "",
      "type": "call",
      "gas": "403263",
      "gasUsed": "0",
      "traceId": "0_0",
      "isError": "1",
      "errCode": "Bad instruction"
    },

The InternalTx.traceId will need to be a string or the string "0_0" will need to be serialized to an int.

Which method do you prefer?

wsarecv error

Code:
client := etherscan.NewCustomized(etherscan.Customization{
Timeout: 15 * time.Second,
Key: "2N9DPZFRMK2B9MNK17JX******", // my bscscan api key
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 {
	return nil
}

client.AfterRequest = func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error) {
	// ...
}

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

Result: read tcp 192.168.31.24:59215->23.92.68.154:443: wsarecv: 远程主机强迫关闭了一个现有的连接。

etherScanClient.ERC20Transfers unmarshal error

etherScanClient.ERC20Transfers(&contract, &address, &startBlock, &bestBlock, 1, 0)

Address : 0xd46d54fdac2bf746dfd97d699810c3774a427b3c

Contract : 0x5eac95ad5b287cf44e058dcf694419333b796123

StartBlock : 0

BestBlock : Around 6.3 million

JSON response

{
  "status": "1",
  "message": "OK",
  "result": [
    {
      "blockNumber": "5802549",
      "timeStamp": "1529204204",
      "hash": "0x4a9430b79d072f22bdeb553cbee94148b03b71173ceaa14c6eb317dcaf49f377",
      "nonce": "9755",
      "blockHash": "0x3828622f018be7635a6a42afda5725efc133ae7a3bedc122e32e7e3644deb156",
      "from": "0x2881badc252165ee04c697306fea572cd19d8549",
      "contractAddress": "0x5eac95ad5b287cf44e058dcf694419333b796123",
      "to": "0xd46d54fdac2bf746dfd97d699810c3774a427b3c",
      "value": "600000000000000000000",
      "tokenName": "",
      "tokenSymbol": "",
      "tokenDecimal": "",
      "transactionIndex": "108",
      "gas": "1500000",
      "gasPrice": "3000000000",
      "gasUsed": "1446427",
      "cumulativeGasUsed": "6025951",
      "input": "0xbadcb92a000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000002086ac35105260000000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000e0060462c47ff9679baef07159cae08c29f274a90000000000000000000000002d03fbf63a78d0eb83873d9159d0e20a171cbbdd000000000000000000000000da0c2012be2ce446911a304db5df62e39f6a1f8a00000000000000000000000078b978a9d7e91ee529ea4fc4b76feaf8762f698c00000000000000000000000044f62f2aaabc29ad3a6b04e1ff6f9ce452d1c140000000000000000000000000889713a4c501f65e369a4ea497ea7f9af871d395000000000000000000000000d46d54fdac2bf746dfd97d699810c3774a427b3c0000000000000000000000006fa1aff85c0c9aedf387ec8a3f4e13fe8ea70cde000000000000000000000000d2a061247cbbc6bf9ac8b2d0b7a24381418ba40e000000000000000000000000714ee0584d1e88d0d9f510b5c55d3bb286a41ab00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000005bd9c36f000000000000000000000000000000000000000000000000000000005c530d6f",
      "confirmations": "586769"
    }
  ]
}

Error : "json unmarshal outcome: json: invalid use of ,string struct tag, trying to unmarshal "" into uint8"

I'll look into resolving this when I wake up. I'm guessing that the empty string should be deserialized into a 0. If you have another idea let me know.

Error handling ERC-1155

There is an issue with error handling while using the ERC-1155 function. With err, result or any other error type the code only logs: etherscan server: NOTOK instead of providing an actual error message.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.