Go 通过etherscan api访问接囗报错

25 篇文章 1 订阅
16 篇文章 1 订阅

0、前提条件

(1)操作系统

        操作系统Windows10中进行开发测试

(2)参考代码

GitHub - nanmu42/etherscan-api: Golang client for Ethereum Etherscan API (and its families like BscScan) / Golang 以太坊 Etherscan API库(也支持同一家族的BscScan)

注:已使用科学上网

1、测试代码

import (
	"github.com/nanmu42/etherscan-api"
	"fmt"
)

func main() {
	// create a API client for specified ethereum net
	// there are many pre-defined network in package
	client := etherscan.New(etherscan.Mainnet, "xxxxxxxx")
	

	// check account balance
	balance, err := client.AccountBalance("xxxxxxxx")
	if err != nil {
		panic(err)
	}
	// balance in wei, in *big.Int type
	fmt.Println(balance.Int())
}

第一个"xxxxxxxx"表示为API key:在 https://etherscan.io/ 中生成,如下图所示

第二个"xxxxxxxx"表示为以太坊账户地址

以上2个"xxxxxxxx"替换成自己的API key与账户地址即可

 2、错误信息

运行代码后报错,错误信息如下:

panic: Get "https://api-ropsten.etherscan.io/api?action=balance&address=xxxxxxxx&apikey=xxxxxxxx&module=account&tag=latest": dial tcp 199.59.148.8:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

打开浏览器,直接访问链接地址发现是正常的

3、原因分析

浏览器是通过科学上网访问的,但是go访问没有使用科学上网的配置,需要使用代理才能访问

4、解决方案

(1)查看本地代理

打开IE浏览器,工具 -> Internet选项 -> 连接 -> 局域网设置 -> 代理服务器,查看代理地址与端囗

 (2)调整代码,增加http代理设置

import (
	"github.com/nanmu42/etherscan-api"
	"fmt"
)

func main() {
	// create a API client for specified ethereum net
	// there are many pre-defined network in package
	// client := etherscan.New(etherscan.Mainnet, "xxxxxxxx")
	
    url, _ := url.Parse("htttp://127.0.0.1:7890")
	transport := &http.Transport{
		Proxy: http.ProxyURL(url),
	}
	c := &http.Client{
		Transport: transport,
	}

	client := etherscan.NewCustomized(etherscan.Customization{
		Timeout: 15 * time.Second,
		Key:     "xxxxxxxx",
		BaseURL: "https://api-ropsten.etherscan.io/api?",
		Verbose: false,
		Client:  c,
	})

	// check account balance
	balance, err := client.AccountBalance("xxxxxxxx")
	if err != nil {
		panic(err)
	}
	// balance in wei, in *big.Int type
	fmt.Println(balance.Int())
}

5、问题

以上解决方案部署在Windows平台是可以正常运行的,但是部署到Centos7中就报错了

(1)测试代码

只把上面获取账户余额改为获取交易结果(当然交易hash确保是存在的)

import (
	"github.com/nanmu42/etherscan-api"
	"fmt"
)

func main() {
	// create a API client for specified ethereum net
	// there are many pre-defined network in package
	// client := etherscan.New(etherscan.Mainnet, "xxxxxxxx")
	
    url, _ := url.Parse("htttp://127.0.0.1:7890")
	transport := &http.Transport{
		Proxy: http.ProxyURL(url),
	}
	c := &http.Client{
		Transport: transport,
	}

	client := etherscan.NewCustomized(etherscan.Customization{
		Timeout: 15 * time.Second,
		Key:     "xxxxxxxx",
		BaseURL: "https://api-ropsten.etherscan.io/api?",
		Verbose: false,
		Client:  c,
	})

	// 获取交易结果
	status, err := client.ReceiptStatus("0xea3c06eb70a5a77e25bbce478ae0fef34d93b3d7dd3a82d1d64d36d53aa8ed51")
	if err != nil {
		panic(err)
	}

	fmt.Println(status)
}

(2)错误信息 

sending request: Get "https://api-ropsten.etherscan.io/api?action=gettxreceiptstatus&apikey=xxxxxxx&module=transaction&txhash=xxxxx": context deadline exceeded (Client.Timeout exceeded while awaiting headers)"

(3)解决方案(都没成功)

网上找了一些解决方案,以下解决方案只做记录,都没有成功,请勿参考。可直接见(4)终极解决方案,如果小伙伴们遇到相样的问题,有好的解决方案可以分享哈!!!

1)方案一:添加本地代理(未成功)

像windows一样添加代理127.0.0.1:7890

编辑配置文件/etc/profile

[root@Tracy ~]# vi /etc/profile

在文件末尾添加如下代理设置:

export all_proxy=http://127.0.0.1:7890/
export ALL_PROXY=http://127.0.0.1:7890/
export HTTP_PROXY=http://127.0.0.1:7890/
export http_proxy=http://127.0.0.1:7890/
export HTTPS_PROXY=https://127.0.0.1:7890/
export https_proxy=https://127.0.0.1:7890/

按Esc退出编辑,输入:wq保存退出。

使配置文件生效 

[root@Tracy ~]# source /etc/profile

再次运行程序,还是同样的错误,放弃此方案,删除配置文件添加的代理设置,如果通过以下方式查询到还有代理设置

[root@Tracy ~]# export | grep -i proxy
declare -x ALL_PROXY="http://127.0.0.1:7890/"
declare -x HTTPS_PROXY="https://127.0.0.1:7890/"
declare -x HTTP_PROXY="http://127.0.0.1:7890/"
declare -x all_proxy="http://127.0.0.1:7890/"
declare -x http_proxy="http://127.0.0.1:7890/"
declare -x https_proxy="https://127.0.0.1:7890/"

 使用以下方式清除

[root@Tracy ~]# unset ALL_PROXY
[root@Tracy ~]# unset HTTPS_PROXY
[root@Tracy ~]# unset HTTP_PROXY
[root@Tracy ~]# unset all_proxy
[root@Tracy ~]# unset http_proxy
[root@Tracy ~]# unset https_proxy

2)方案二:配置ip映射(未成功)

在使用ping时,发现ping api-ropsten.etherscan.io不通,网上查了对应的IP为104.22.14.57 或是 104.22.15.57,ping时发现104.22.14.57好用些,因此配置这个IP。

查询IP可参见:域名解析查询(A/Txt/Cname/Mx/Srv/Aaaa...)

编辑hosts文件  

[root@Tracy ~]# vi /etc/hosts

在文件末尾添加如下配置: 

104.22.14.57  api-ropsten.etherscan.io        ropsten 

# 配置依次表示 IP地址  主机域名 主机别名 

按Esc退出编辑,输入:wq保存退出。

再次运行程序,发现报了新错:

sending request: Get "https://api-ropsten.etherscan.io/api?action=getstatus&apikey=xxxxxxxxxxxx&module=transaction&txhash=xxxxxxxxxxxx": read tcp 172.17.102.231:35248->104.22.14.57:443: read: connection reset by peer

依然没成功啊,放弃此方法,删除hosts文件中刚刚的配置。

(4)终极解决方案

放弃使用etherscan-api查询交易结果,改为使用go-ethereum,代码如下:

import (
	"context"
	"fmt"
	"github.com/ethereum/go-ethereum"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
)

func main() {
	client, err := ethclient.Dial("https://ropsten.infura.io/v3/xxxxxxxxx")
	if err != nil {
		fmt.Println("ethclient.Dial Error:", err)
		return
	}

    receipt, err := client.TransactionReceipt(context.Background(), common.HexToHash("0xea3c06eb70a5a77e25bbce478ae0fef34d93b3d7dd3a82d1d64d36d53aa8ed51"))	
	if err != nil {
		fmt.Println("client.TransactionReceipt Error:", err)
		return
	}

    fmt.Println(receipt)
}

访问地址使用infura, 官网地址:https://infura.io/

官网有详细说明,直接参考网址即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值