/chain-explorer-api

Golang client for main blockchain explorer api, include etherscan, blockscout, oklink and so on

Primary LanguageGoMIT LicenseMIT

chain-explorer-api repo

Version License: Apache-2.0 GoDoc

Chain Explorer API is a statistical browser interface gateway that simplifies the calling process of browser APIs, provides a unified imported parameter exported parameter underlying call library, and helps third-party projects simplify the development process.

Tips: need Go 1.18+

Install

Use this Library

1.New ChainExplorerAdaptor

var (
	EtherscanBaseUrl = "https://api.etherscan.io/api?"
	EtherscanApiKey  = ""
	EtherscanTimeout = time.Second * 20

	OklinkBaseUrl = "https://www.oklink.com/"
	OklinkApiKey  = ""
	OkTimeout     = time.Second * 20
)

func NewChainExplorerAdaptor() (*oklink.ChainExplorerAdaptor, *etherscan.ChainExplorerAdaptor, error) {
	var err error
	oklinkCli, err := oklink.NewChainExplorerAdaptor(OklinkApiKey, OklinkBaseUrl, false, time.Duration(OkTimeout))
	if err != nil {
		fmt.Println("Mock oklink client fail", "err", err)
		return nil, nil, err
	}

	etherscanCli, err := etherscan.NewChainExplorerAdaptor(EtherscanApiKey, EtherscanBaseUrl, false, time.Duration(EtherscanTimeout))
	if err != nil {
		fmt.Println("Mock etherscan client fail", "err", err)
		return nil, nil, err
	}
	return oklinkCli, etherscanCli, nil
}

2. Example Case

func GetAccountBalance() {
	oklinkClient, etherscanClient, err := NewChainExplorerAdaptor()
	if err != nil {
		fmt.Println("new chain explorer adaptor fail", "err", err)
	}
	accountItem := []string{"0x4838B106FCe9647Bdf1E7877BF73cE8B0BAD5f97"}
	symbol := []string{"ETH"}
	contractAddress := []string{"0x00"}
	protocolType := []string{""}
	page := []string{"1"}
	limit := []string{"10"}
	acbr := &account.AccountBalanceRequest{
		ChainShortName:  "ETH",
		ExplorerName:    "etherescan",
		Account:         accountItem,
		Symbol:          symbol,
		ContractAddress: contractAddress,
		ProtocolType:    protocolType,
		Page:            page,
		Limit:           limit,
	}
	etherscanResp, err := etherscanClient.GetAccountBalance(acbr)
	if err != nil {
		t.Error(err)
	}
	fmt.Println("==========etherscanResp============")
	fmt.Println(etherscanResp.Balance.Int())
	fmt.Println(etherscanResp.Account)
	fmt.Println(etherscanResp.Symbol)
	fmt.Println("===========etherscanResp===========")

	oklinkResp, err := oklinkClient.GetAccountBalance(acbr)
	if err != nil {
		t.Error(err)
	}
	fmt.Println("==========oklinkResp============")
	fmt.Println(oklinkResp.BalanceStr)
	fmt.Println(oklinkResp.Account)
	fmt.Println(oklinkResp.Symbol)
	fmt.Println("==========oklinkResp============")
}

Contribute

1.fork repo

fork chain-explorer-api to your github

2.clone repo

git@github.com:guoshijiang/chain-explorer-api.git

3. create new branch and commit code

git branch -C xxx
git checkout xxx

coding

git add .
git commit -m "xxx"
git push origin xxx

4.commit PR

Have a pr on your github and submit it to the chain-explorer-api repository

5.review

After the chain-explorer-api code maintainer has passed the review, the code will be merged into the chain-explorer-api library. At this point, your PR submission is complete

6.Disclaimer

This code has not yet been audited, and should not be used in any production systems.