|
| 1 | +# etherscan-api |
| 2 | + |
| 3 | +[](https://travis-ci.org/nanmu42/etherscan-api) |
| 4 | +[](https://goreportcard.com/report/github.com/nanmu42/etherscan-api) |
| 5 | +[](https://codecov.io/gh/nanmu42/etherscan-api) |
| 6 | +[](https://godoc.org/github.com/nanmu42/etherscan-api) |
| 7 | +[English Readme](https://github.com/nanmu42/etherscan-api/blob/master/README.md) |
| 8 | + |
| 9 | +Etherscan.io的Golang实现, |
| 10 | +支持几乎所有功能(accounts, transactions, tokens, contracts, blocks, stats), |
| 11 | +所有公共网络(Mainnet, Ropsten, Kovan, Rinkby, Tobalaba)。 |
| 12 | +本项目只依赖于官方库。 :wink: |
| 13 | + |
| 14 | +# Usage |
| 15 | + |
| 16 | +填入网络选项和API Key即可开始使用。 :rocket: |
| 17 | + |
| 18 | +```go |
| 19 | +import ( |
| 20 | + "github.com/nanmu42/etherscan-api" |
| 21 | + "fmt" |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + // 创建连接指定网络的客户端 |
| 26 | + client := etherscan.New(etherscan.Mainnet, "[your API key]") |
| 27 | + |
| 28 | + // (可选)按需注册钩子函数,例如用于速率控制 |
| 29 | + client.BeforeRequest = func(module, action string, param map[string]interface{}) error { |
| 30 | + // ... |
| 31 | + } |
| 32 | + client.AfterRequest = func(module, action string, param map[string]interface{}, outcome interface{}, requestErr error) { |
| 33 | + // ... |
| 34 | + } |
| 35 | + |
| 36 | + // 查询账户以太坊余额 |
| 37 | + balance, err := client.AccountBalance("0x281055afc982d96fab65b3a49cac8b878184cb16") |
| 38 | + if err != nil { |
| 39 | + panic(err) |
| 40 | + } |
| 41 | + // 余额以 *big.Int 的类型呈现,单位为 wei |
| 42 | + fmt.Println(balance.Int()) |
| 43 | + |
| 44 | + // 查询token余额 |
| 45 | + tokenBalance, err := client.TokenBalance("contractAddress", "holderAddress") |
| 46 | + |
| 47 | + // 查询出入指定地址的ERC20转账列表 |
| 48 | + transfers, err := client.ERC20Transfers("contractAddress", "address", startBlock, endBlock, page, offset) |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +客户端方法列表可在[GoDoc](https://godoc.org/github.com/nanmu42/etherscan-api)查询。 |
| 53 | + |
| 54 | +# Etherscan API Key |
| 55 | + |
| 56 | +API Key可以在[etherscan](https://etherscan.io/apis)申请。 |
| 57 | + |
| 58 | +Etherscan的API服务是一个公开的社区无偿服务,请避免滥用。 |
| 59 | +API的调用速率不能高于5次/秒,否则会遭到封禁。 |
| 60 | + |
| 61 | +# 利益声明 |
| 62 | + |
| 63 | +我和Etherscan没有任何联系。我仅仅是觉得他们的服务很棒,而自己又恰好需要这样一个库。 :smile: |
| 64 | + |
| 65 | +# 许可证 |
| 66 | + |
| 67 | +MIT |
| 68 | + |
| 69 | +请自由享受开源,欢迎贡献开源。 |
0 commit comments