Skip to content
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

Use Github Action and clean up code #59

Merged
merged 4 commits into from
Oct 15, 2021
Merged
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
31 changes: 31 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: "42 6 * * 0"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.x"

- name: Test
env:
ETHERSCAN_API_KEY: ${{ secrets.ETHERSCAN_API_KEY }}
run: go test -v -coverprofile=coverage.txt -covermode=count ./...

- name: Codecov
uses: codecov/[email protected]

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
**English** | [中文](https://github.com/nanmu42/etherscan-api/blob/master/README_ZH.md)

# etherscan-api

[![Build Status](https://travis-ci.org/nanmu42/etherscan-api.svg?branch=master)](https://travis-ci.org/nanmu42/etherscan-api)
[![Go Report Card](https://goreportcard.com/badge/github.com/nanmu42/etherscan-api)](https://goreportcard.com/report/github.com/nanmu42/etherscan-api)
[![codecov](https://codecov.io/gh/nanmu42/etherscan-api/branch/master/graph/badge.svg)](https://codecov.io/gh/nanmu42/etherscan-api)
[![GoDoc](https://godoc.org/github.com/nanmu42/etherscan-api?status.svg)](https://godoc.org/github.com/nanmu42/etherscan-api)
[中文文档](https://github.com/nanmu42/etherscan-api/blob/master/README_ZH.md)
[![CI status](https://github.com/nanmu42/etherscan-api/workflows/build/ci.svg)](https://github.com/nanmu42/etherscan-api/actions)
[![codecov](https://codecov.io/gh/nanmu42/etherscan-api/branch/master/graph/badge.svg)](https://codecov.io/gh/nanmu42/etherscan-api)
[![Go Report Card](https://goreportcard.com/badge/github.com/nanmu42/etherscan-api)](https://goreportcard.com/report/github.com/nanmu42/etherscan-api)

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

Create a API instance and off you go. :rocket:
```bash
go get github.com/nanmu42/etherscan-api
```

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

```go
import (
Expand Down Expand Up @@ -70,6 +75,6 @@ I am not from Etherscan and I just find their service really useful, so I implem

# License

Use of this work is governed by a MIT License.
Use of this work is governed by an MIT License.

You may find a license copy in project root.
17 changes: 11 additions & 6 deletions README_ZH.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
[English](https://github.com/nanmu42/etherscan-api/blob/master/README.md) | **中文**

# etherscan-api

[![Build Status](https://travis-ci.org/nanmu42/etherscan-api.svg?branch=master)](https://travis-ci.org/nanmu42/etherscan-api)
[![Go Report Card](https://goreportcard.com/badge/github.com/nanmu42/etherscan-api)](https://goreportcard.com/report/github.com/nanmu42/etherscan-api)
[![codecov](https://codecov.io/gh/nanmu42/etherscan-api/branch/master/graph/badge.svg)](https://codecov.io/gh/nanmu42/etherscan-api)
[![GoDoc](https://godoc.org/github.com/nanmu42/etherscan-api?status.svg)](https://godoc.org/github.com/nanmu42/etherscan-api)
[English Readme](https://github.com/nanmu42/etherscan-api/blob/master/README.md)
[![CI status](https://github.com/nanmu42/etherscan-api/workflows/build/ci.svg)](https://github.com/nanmu42/etherscan-api/actions)
[![codecov](https://codecov.io/gh/nanmu42/etherscan-api/branch/master/graph/badge.svg)](https://codecov.io/gh/nanmu42/etherscan-api)
[![Go Report Card](https://goreportcard.com/badge/github.com/nanmu42/etherscan-api)](https://goreportcard.com/report/github.com/nanmu42/etherscan-api)

Etherscan.io的Golang实现
Etherscan API的Golang客户端
支持几乎所有功能(accounts, transactions, tokens, contracts, blocks, stats),
所有公共网络(Mainnet, Ropsten, Kovan, Rinkby, Goerli, Tobalaba)。
本项目只依赖于官方库。 :wink:

# Usage
# 使用方法

```bash
go get github.com/nanmu42/etherscan-api
```

填入网络选项和API Key即可开始使用。 :rocket:

Expand Down
24 changes: 17 additions & 7 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

package etherscan

import "strconv"
import (
"fmt"
"strconv"
)

// BlockReward gets block and uncle rewards by block number
func (c *Client) BlockReward(blockNum int) (rewards BlockRewards, err error) {
Expand All @@ -19,21 +22,28 @@ func (c *Client) BlockReward(blockNum int) (rewards BlockRewards, err error) {
return
}

// BlockNumber gets closest block number by UNIX timestamp
// BlockNumber gets the closest block number by UNIX timestamp
//
// valid closest option: before, after
func (c *Client) BlockNumber(timestamp int64, closest string) (blockNumber int, err error) {
var result string
var blockNumberStr string

param := M{
"timestamp": strconv.Itoa(int(timestamp)),
"timestamp": strconv.FormatInt(timestamp, 10),
"closest": closest,
}

err = c.call("block", "getblocknobytime", param, &result)
err = c.call("block", "getblocknobytime", param, &blockNumberStr)

if err != nil {
return
}

blockNumber, err = strconv.Atoi(blockNumberStr)
if err != nil {
err = fmt.Errorf("parsing block number %q: %w", blockNumberStr, err)
return
}

blockNum, err := strconv.ParseInt(result, 10, 64)
blockNumber = int(blockNum)
return
}
14 changes: 7 additions & 7 deletions block_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ func TestClient_BlockReward(t *testing.T) {
}

func TestClient_BlockNumber(t *testing.T) {
//Note: All values taken from docs.etherscan.io/api-endpoints/blocks
const ans_before = 9251482
const ans_after = 9251483
// Note: All values taken from docs.etherscan.io/api-endpoints/blocks
const ansBefore = 9251482
const ansAfter = 9251483

blockNumber, err := api.BlockNumber(1578638524, "before")
noError(t, err, "api.BlockNumber")

if blockNumber != ans_before {
t.Errorf(`api.BlockNumber(1578638524, "before") not working, got %d, want %d`, blockNumber, ans_before)
if blockNumber != ansBefore {
t.Errorf(`api.BlockNumber(1578638524, "before") not working, got %d, want %d`, blockNumber, ansBefore)
}

blockNumber, err = api.BlockNumber(1578638524, "after")
noError(t, err, "api.BlockNumber")

if blockNumber != ans_after {
t.Errorf(`api.BlockNumber(1578638524,"after") not working, got %d, want %d`, blockNumber, ans_after)
if blockNumber != ansAfter {
t.Errorf(`api.BlockNumber(1578638524,"after") not working, got %d, want %d`, blockNumber, ansAfter)
}
}