etherscan

package module
v0.0.0-...-5d5dc01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

README

Golang client for the etherscan.io and bscscan.com

Usage

go get github.com/AlexanderMint/crypto-explorer

Create an API instance and off you go. 🚀

import (
	"github.com/AlexanderMint/crypto-explorer"
	"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)
}

Etherscan API Key

You may apply for an API key on etherscan.

Documentation

Index

Constants

View Source
const (
	DefaultTimeout = 15 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccountBalance

type AccountBalance struct {
	Account string `json:"account"`
	Balance BigInt `json:"balance"`
}

AccountBalance account and its balance in pair

type BigInt

type BigInt big.Int

BigInt is a wrapper over big.Int to implement only unmarshalText for json decoding.

func (*BigInt) Int

func (b *BigInt) Int() *big.Int

Int returns b's *big.Int form

func (*BigInt) MarshalText

func (b *BigInt) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler

func (*BigInt) UnmarshalText

func (b *BigInt) UnmarshalText(text []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Client

type Client struct {
	// contains filtered or unexported fields
}

func New

func New(service Service, APIKey string) *Client

func NewClient

func NewClient(config Config) *Client

func (*Client) AccountBalance

func (c *Client) AccountBalance(address string) (balance *BigInt, err error)

AccountBalance gets ether balance for a single address

func (*Client) GetLogs

func (c *Client) GetLogs(fields LogFields) (logs []Log, err error)

GetLogs gets logs that match "topic" emitted by the specified "address" between the "fromBlock" and "toBlock"

type Config

type Config struct {
	// Timeout for API call
	Timeout time.Duration
	// API key applied from Etherscan
	Key string
	// Base URL like `https://api.etherscan.io/api?`
	BaseURL string
	// HTTP Client to be used. Specifying this value will ignore the Timeout value set
	// Set your own timeout.
	Client *http.Client
}

type Log

type Log struct {
	Address         string   `json:"address"`
	Topics          []string `json:"topics"`
	Data            string   `json:"data"`
	BlockNumber     BigInt   `json:"blockNumber"`
	TransactionHash string   `json:"transactionHash"`
	BlockHash       string   `json:"blockHash"`
	LogIndex        BigInt   `json:"logIndex"`
	Timestamp       Time     `json:"timestamp"`
	Removed         bool     `json:"removed"`
}

type LogFields

type LogFields struct {
	FromBlock *int64
	ToBlock   *int64
	Topic0    *string
	Address   *string
}

func (LogFields) ToParams

func (l LogFields) ToParams() map[string]string

type Response

type Response struct {
	// 1 for good, 0 for error
	Status int `json:"status,string"`
	// OK for good, other words when Status equals 0
	Message string `json:"message"`
	// where response lies
	Payload json.RawMessage `json:"result"`
}

Response is the carrier of nearly every response

type Service

type Service string
const (
	Etherscan Service = "https://api.etherscan.io/api"
	BSCscan   Service = "https://api.bscscan.com/api"
)

func (Service) String

func (s Service) String() string

type Time

type Time time.Time

Time is a wrapper over big.Int to implement only unmarshalText for json decoding.

func (Time) MarshalText

func (t Time) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler

func (Time) Time

func (t Time) Time() time.Time

Time returns t's time.Time form

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(text []byte) (err error)

UnmarshalText implements the encoding.TextUnmarshaler interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL