【问题标题】:Etherscan API doesn't display the Ethereum balance correctly in JavascriptEtherscan API 无法在 Javascript 中正确显示以太坊余额
【发布时间】:2022-10-05 09:27:02
【问题描述】:

我想显示 eth 余额“结果”,例如,从 etherscan API 显示

{
status: "1",
message: "OK",
result: "13386321000069000000000069"
}

使用以太坊钱包 0x00000000219ab540356cBB839Cbe05303d7705Fa

但我的代码返回以太坊余额结果为 0, 当我从页面检查中输入 chrome 控制台时。

ether.fetchEther("0x00000000219ab540356cBB839Cbe05303d7705Fa")

我不确定为什么状态和消息工作正常。这是我在 JavaScript 文件中使用的代码。 Html 和 CSS 文件只有骨架代码,因为我首先检查 JS 是否有效。

let ether = {

    fetchEther: function(address)
    {
        fetch("https://api.etherscan.io/api?module=account&action=balance&address="+address +"tag=latest&apikey=RE98FGG6WVI25619AZKGI6B9IPJS6I64N8"
        ).then((response) =>response.json())
        .then((data)=>this.displayBalance(data));
    },
displayBalance: function(data){
const { result } =data;
console.log(result)
}
}

【问题讨论】:

  • API 以{"status":"1","message":"OK","result":"0"} 响应,您的代码没有问题。你确定测试钱包有资金吗?你确定你使用了正确的 API 吗?
  • 是的,钱包是以太坊的顶级持有者,我期待收到回馈(结果:“13386321000069000000000069”)但得到了 0。

标签: javascript json api ethereum etherscan


【解决方案1】:

看起来您在标签之前缺少一个&符号。应该是&tag。

let ether = {

    fetchEther: function(address)
    {
        fetch("https://api.etherscan.io/api?module=account&action=balance&address="+address+"&tag=latest&apikey=RE98FGG6WVI25619AZKGI6B9IPJS6I64N8"
        ).then((response) =>response.json())
        .then((data)=>this.displayBalance(data));
    },
displayBalance: function(data){
const { result } =data;
console.log(result)
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2015-11-25
    • 2022-08-09
    • 2018-09-10
    • 2017-01-27
    • 2021-04-28
    • 2022-01-18
    相关资源
    最近更新 更多