Go 通过etherscan api访问接囗报错

本文讲述了在Windows和Centos7环境下使用Golang的etherscan-api库进行以太坊账户余额和交易查询时遇到的问题,涉及科学上网、http代理设置、以及为何在Centos中失败。最终提供了解决方案,转向使用go-ethereum替代获取交易结果。

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&a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值