Etherscan

Etherscan is a block explorer and it implements some read compatible endpoint with the JsonRPC spec. It requires an apiKey to use the service and not be rate limited.

Create an instance of Etherscan from a network id:

package main
import (
"github.com/cloudwalk/ethgo/etherscan"
"github.com/cloudwalk/ethgo"
)
func main() {
ethscan, err := etherscan.NewEtherscanFromNetwork(ethgo.Mainnet, "apiKey")
}

The package will resolve the name of the network to the specific endpoint in Etherscan, at this point it only works for Mainnet, Ropsten, Rinkeby and Goerli.

For a custom url use:

ethscan, err := etherscan.NewEtherscan("https://api.polygonscan.com", "apiKey")

BlockNumber

BlockNumber returns the current block number.

num, err := ethscan.BlockNumber()

Output

  • num (uint64): Last block number.

GetBlockByNumber

GetBlockByNumber returns a specific block by its number.

block, err := ethscan.GetBlockByNumber(ethgo.BlockNumber(100), true)

Input:

  • block number (BlockTag): Block number selection
  • full (bool): Whether to return the full block with transactions.

Output:

  • block: (): Block object

GetContractCode

GetContractCode returns the contract of a given address (if any).

code, err := ethscan.GetContractCode(address)

Input:

  • address (Address): Address of the contract.

Output:

  • code ([]byte): Code of the contract.

GetLogs

GetLogs returns the logs given a log filter.

filter := &ethgo.LogFilter{
Address: []ethgo.Address{
ethgo.HexToAddress("..."),
},
}
logs, err := ethscan.GetLogs(filter)

Input:

Output:

  • logs ([]Log): List of logs that match the filter.

GasPrice

GasPrice returns the gas price of the latest block.

Output:

  • gasPrice (uint64): Gas price of the latest block.