赞
踩
操作系统Windows10中进行开发测试
注:已使用科学上网
- 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与账户地址即可
运行代码后报错,错误信息如下:
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.
打开浏览器,直接访问链接地址发现是正常的
浏览器是通过科学上网访问的,但是go访问没有使用科学上网的配置,需要使用代理才能访问
打开IE浏览器,工具 -> Internet选项 -> 连接 -> 局域网设置 -> 代理服务器,查看代理地址与端囗
- 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())
- }
以上解决方案部署在Windows平台是可以正常运行的,但是部署到Centos7中就报错了
只把上面获取账户余额改为获取交易结果(当然交易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)
- }
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)"
网上找了一些解决方案,以下解决方案只做记录,都没有成功,请勿参考。可直接见(4)终极解决方案,如果小伙伴们遇到相样的问题,有好的解决方案可以分享哈!!!
像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
在使用ping时,发现ping api-ropsten.etherscan.io不通,网上查了对应的IP为104.22.14.57 或是 104.22.15.57,ping时发现104.22.14.57好用些,因此配置这个IP。
编辑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文件中刚刚的配置。
放弃使用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/
官网有详细说明,直接参考网址即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。