Ethereum web3余额始终为0

Ethereum web3余额始终为0,ethereum,solidity,web3,Ethereum,Solidity,Web3,我正在尝试使用web3获取智能合约上地址的余额,但余额始终为0。在Rinkeby上使用metamask,因为我的合同部署在Rinkeby上 您可以将钱包放入etherScan的balanceOf功能中,以验证钱包是否有余额。使用地址0x8b54A82a12bD5A7bA33B4842cA677E55f78a8612 let provider = web3.currentProvider; web3 = new Web3(provider); let abi = 'too long of a st

我正在尝试使用web3获取智能合约上地址的余额,但余额始终为0。在Rinkeby上使用metamask,因为我的合同部署在Rinkeby上

您可以将钱包放入etherScan的balanceOf功能中,以验证钱包是否有余额。使用地址0x8b54A82a12bD5A7bA33B4842cA677E55f78a8612

let provider = web3.currentProvider;
web3 = new Web3(provider);
let abi = 'too long of a string to post here';
let MyContract = web3.eth.contract(JSON.parse(abi));
let myContractInstance = MyContract.at('0x8e3a88be716ce7c8119c36558ec97bc634592255');
let address = '0x8b54A82a12bD5A7bA33B4842cA677E55f78a8612';

function balanceOf(address) {
    if (!address) {
        return;
    }

    this.myContractInstance.balanceOf.call(address, function(error, balance) {
      if (error) {
        return;
      }

      alert(balance.c[0] + ' RHC');
    });
}

balanceOf(address);
这是我合同上的getBalance函数

function balanceOf(address _owner) public view returns (uint256 balance) {
    return balances[_owner];
}
网站在
代码

编辑 当我将提供程序更改为以下内容时:

var web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/'));
我能得到平衡。因此,使用metamask作为提供程序有问题。

处的行有一个输入错误。内容如下:

self.myContractInstance = self.MyContract.at(self.address);
但该地址存储在self.contractAddress中,因此应为:

self.myContractInstance = self.MyContract.at(self.contractAddress);

修复后,MetaMask对我来说很好。

你可能没有MetaMask指向Rinkeby吗?是的,我使用MetaMask测试了Rinkeby和mainnet。你链接的网站似乎坏了;它给出了一个错误,因为
ropsten
是一个未声明的变量。是的,我只有rinkeby和mainnet设置。哦,对不起,我误读了。