blockscan

package module
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: MIT Imports: 9 Imported by: 0

README

Blockscan


Chains supported
chain id network blockscan
1 ethereum etherscan
10 optimism optimism
56 binance bscscan
128 heco hecoscan
137 polygon polygonscan
250 fantom ftmscan
42161 arbitrum arbiscan
43114 avalanche snowtrace

New a Scanner

How to get an api key?

// You can use "" as your api key, but not recommanded.
// You may suffer from some unpredictabale errors when requesting data form blockscans.
ethScanner, _ := blockscan.New(chainId.EthereumChainName, "YOUR API KEY")
avaxScanner, _ := blockscan.New(chainId.AvalancheChainName, "YOUR API KEY")
polyScanner, _ := blockscan.New(chainId.PolygonChainName, "YOUR API KEY")
...

To see more on etherscan api.

Accounts

Return the chain token(eth for ethereum) balance of an address.

Return the chain token(eth for ethereum) balances of a list of addresses.

Return the erc20 transactions(up to 10000) of an address between sepecific blocks.

Return all the erc20 transactions(no amount limit) of an address from block 0 till now.

Return the internal transactions(up to 10000) of an address between sepecific blocks.

Return all the internal transactions(no amount limit) of an address from block 0 till now.

Return the normal transactions(up to 10000) of an address between sepecific blocks.

Return all the normal transactions(no amount limit) of an address from block 0 till now.


Contracts

Return the contract abi.

Return the contract's source code.

Return the contract's name.

Return whether the address is a contract.

If the contract is not verified, will still return false.


Blocks

Return the block number at specific timestamp.


Logs

Return the events(up to 1000) of a topic0 between sepecific blocks.

Return all the events(no amount limit) of a topic0 from block 0 till now.


Gas

Return the gas price in wei.

Documentation

Overview

Request info from blockscan.

Example:

// To get the balance of address "0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7"
scanner, err:=blockscan.New("avalanche","")
if err != nil { return err }
res, err := scanner.GetBalance("0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7")
if err != nil { return err }
fmt.Println(res)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbiInput added in v0.4.0

type AbiInput struct {
	InternalType string `json:"internalType"`
	Name         string `json:"name"`
	Type         string `json:"type"`
}

type AbiOutput added in v0.4.0

type AbiOutput struct {
	InternalType string `json:"internalType"`
	Name         string `json:"name"`
	Type         string `json:"type"`
}

type ContractAbi added in v0.4.0

type ContractAbi []struct {
	Inputs          []AbiInput  `json:"inputs"`
	StateMutability string      `json:"stateMutability"`
	Type            string      `json:"type"`
	Anonymous       bool        `json:"anonymous"`
	Name            string      `json:"name"`
	Outputs         []AbiOutput `json:"outputs"`
}

type Erc20Txs added in v0.4.1

type Erc20Txs struct {
	BlockNumber       string `json:"blockNumber"`       // block number
	TimeStamp         string `json:"timeStamp"`         // timestamp
	Hash              string `json:"hash"`              // tx hash
	Nonce             string `json:"nonce"`             // tx nonce
	BlockHash         string `json:"blockHash"`         // block hash
	From              string `json:"from"`              // who sent this token
	ContractAddress   string `json:"contractAddress"`   // token contract
	To                string `json:"to"`                // who received this token
	Value             string `json:"value"`             // value in WEI
	TokenName         string `json:"tokenName"`         // token name
	TokenSymbol       string `json:"tokenSymbol"`       // token symbol
	TokenDecimal      string `json:"tokenDecimal"`      // token decimals
	TransactionIndex  string `json:"transactionIndex"`  // tx index
	Gas               string `json:"gas"`               // gas
	GasPrice          string `json:"gasPrice"`          // gas price
	GasUsed           string `json:"gasUsed"`           // gas used
	CumulativeGasUsed string `json:"cumulativeGasUsed"` //
	Input             string `json:"input"`             //
	Confirmations     string `json:"confirmations"`     //
}

type Events added in v0.4.1

type Events struct {
	Address          string   `json:"address"`          // address
	Topics           []string `json:"topics"`           // topics of this event
	Data             string   `json:"data"`             // call data of the event
	BlockNumber      string   `json:"blockNumber"`      // block number
	TimeStamp        string   `json:"timeStamp"`        // timestamp of the event
	GasPrice         string   `json:"gasPrice"`         // gas price when the event emitted
	GasUsed          string   `json:"gasUsed"`          // gas used
	LogIndex         string   `json:"logIndex"`         // log index
	TransactionHash  string   `json:"transactionHash"`  // tx hash
	TransactionIndex string   `json:"transactionIndex"` // tx index
}

Everything in hex. Should convert before further use.

type InternalTxs added in v0.4.1

type InternalTxs struct {
	BlockNumber     string `json:"blockNumber"`     // tx block number
	TimeStamp       string `json:"timeStamp"`       // tx timestamp
	Hash            string `json:"hash"`            // tx hash
	From            string `json:"from"`            // who sent the chain token
	To              string `json:"to"`              // who received the chain token
	Value           string `json:"value"`           // value in WEI
	ContractAddress string `json:"contractAddress"` // contract address
	Input           string `json:"input"`           //
	Type            string `json:"type"`            //
	Gas             string `json:"gas"`             //
	GasUsed         string `json:"gasUsed"`         //
	TraceID         string `json:"traceId"`         //
	IsError         string `json:"isError"`         //
	ErrCode         string `json:"errCode"`         //
}

type NormalTxs added in v0.4.1

type NormalTxs struct {
	BlockNumber       string `json:"blockNumber"`       // tx block number
	TimeStamp         string `json:"timeStamp"`         // tx timestamp
	Hash              string `json:"hash"`              // tx hash
	Nonce             string `json:"nonce"`             // tx nonce
	BlockHash         string `json:"blockHash"`         // tx block hash
	TransactionIndex  string `json:"transactionIndex"`  // tx index
	From              string `json:"from"`              // who call the contract
	To                string `json:"to"`                // who received the call
	Value             string `json:"value"`             // value in WEI
	Gas               string `json:"gas"`               //
	GasPrice          string `json:"gasPrice"`          //
	IsError           string `json:"isError"`           //
	TxreceiptStatus   string `json:"txreceipt_status"`  //
	Input             string `json:"input"`             // call data
	ContractAddress   string `json:"contractAddress"`   //
	CumulativeGasUsed string `json:"cumulativeGasUsed"` //
	GasUsed           string `json:"gasUsed"`           //
	Confirmations     string `json:"confirmations"`     //
}

type Scanner added in v0.2.0

type Scanner struct {
	UrlHead string // Url head based on network.
	ApiKey  string // User's api key, length should be 34.
}

func New added in v0.2.0

func New(network string, apiKey string) (*Scanner, error)

Create a new scanner.

"network" should be the full name of the chain, such as "ethereum", "avalanche", etc.

"apiKey" is the key for blockscan requests. When the key is "", default key will be used. Otherwise, the key length should be 34.

To get an apikey on ethereum, visit https://docs.etherscan.io/getting-started/viewing-api-usage-statistics

Example
// case 1
fmt.Println("\ncase 1:")
scanner, err := New(chainId.AvalancheChainName, "")
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(*scanner)
}
// case 2, wrong key
fmt.Println("\ncase 2:")
scanner, err = New(chainId.AvalancheChainName, "asaasfas")
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(*scanner)
}
Output:


case 1:
You do not have a blockscan api key. Unecpected errors may occur when running.
{https://api.snowtrace.io/api? K6SR1G96B2SQBRWPI4JP8WXM6BCG62EPQ7}

case 2:
api key length should be 34

func (*Scanner) GetBalance added in v0.2.0

func (s *Scanner) GetBalance(address any) (*big.Int, error)

Return the balance of a single address.

func (*Scanner) GetBalances added in v0.2.0

func (s *Scanner) GetBalances(addresses []string) (map[string]*big.Int, error)

Return balances of up to 20 addresses. map[address] = balance.

func (*Scanner) GetBlockNumberByTimestamp added in v0.2.0

func (s *Scanner) GetBlockNumberByTimestamp(timestamp string) (int64, error)

Return the block number.

func (*Scanner) GetContractAbi added in v0.2.0

func (s *Scanner) GetContractAbi(address any) (ContractAbi, error)

Return the contract's abi.

func (*Scanner) GetContractName added in v0.2.0

func (s *Scanner) GetContractName(address any) (string, error)

Return the contract's name.

func (*Scanner) GetErc20Transactions added in v0.2.0

func (s *Scanner) GetErc20Transactions(address any, startBlock int, endBlock any) ([]Erc20Txs, error)

Return up to 10000 erc20 txs of an address.

func (*Scanner) GetErc20TransactionsAll added in v0.2.0

func (s *Scanner) GetErc20TransactionsAll(address any) ([]Erc20Txs, error)

Return all the erc20 txs of an address.

func (*Scanner) GetEvents added in v0.2.0

func (s *Scanner) GetEvents(topic0 string, address any, startBlock int, endBlock any) ([]Events, error)

Return up to 1000 events of an address.

Param:

topic0:     The topic0 of the event. In most cases means the hash of the function name.
address:    The address. Can be string or common.Address
startBlock: The block to start from.
endBlock:   The block to end. If greater than latest block number, or use "latest", will use the latest block number.

func (*Scanner) GetEventsAll added in v0.2.0

func (s *Scanner) GetEventsAll(topic0 string, address string) ([]Events, error)

Return all the events of an address.

Param:

topic0:     The topic0 of the event. In most cases means the hash of the function name.
address:    The address. Can be string or common.Address

func (*Scanner) GetGasPrice added in v0.2.0

func (s *Scanner) GetGasPrice() (*big.Int, error)

Return gas price in WEI.

func (*Scanner) GetInternalTransactions added in v0.2.0

func (s *Scanner) GetInternalTransactions(address any, startBlock int, endBlock any) ([]InternalTxs, error)

Return up to 10000 internal txs of an address.

func (*Scanner) GetInternalTransactionsAll added in v0.2.0

func (s *Scanner) GetInternalTransactionsAll(address any) ([]InternalTxs, error)

Return all the internal txs of an address.

func (*Scanner) GetNormalTransactions added in v0.2.0

func (s *Scanner) GetNormalTransactions(address any, startBlock int, endBlock any) ([]NormalTxs, error)

Return up to 10000 txs of an address.

func (*Scanner) GetNormalTransactionsAll added in v0.2.0

func (s *Scanner) GetNormalTransactionsAll(address any) ([]NormalTxs, error)

Return all the txs of an address.

func (*Scanner) GetSourceCode added in v0.2.0

func (s *Scanner) GetSourceCode(address any) (SourceCode, error)

Return the source code of a contract.

func (*Scanner) IsVerifiedContract added in v0.2.0

func (s *Scanner) IsVerifiedContract(address any) (bool, error)

Return whether the address is a verified contract.

Some contracts may not be verified, will be considered not contract.

type SourceCode added in v0.4.1

type SourceCode struct {
	SourceCode           string `json:"SourceCode"`
	Abi                  string `json:"ABI"`
	ContractName         string `json:"ContractName"`
	CompilerVersion      string `json:"CompilerVersion"`
	OptimizationUsed     string `json:"OptimizationUsed"`
	Runs                 string `json:"Runs"`
	ConstructorArguments string `json:"ConstructorArguments"`
	EVMVersion           string `json:"EVMVersion"`
	Library              string `json:"Library"`
	LicenseType          string `json:"LicenseType"`
	Proxy                string `json:"Proxy"`
	Implementation       string `json:"Implementation"`
	SwarmSource          string `json:"SwarmSource"`
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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