【问题标题】:How do I get the balance of an account in Ethereum?如何获取以太坊账户的余额?
【发布时间】:2015-11-25 14:01:56
【问题描述】:

我如何以编程方式发现以太坊区块链上给定账户中有多少 ETH?

【问题讨论】:

    标签: ethereum


    【解决方案1】:

    在网络上:

    (不是程序化的,而是为了完整性...)如果您只是想获取账户或合约的余额,您可以访问http://etherchain.orghttp://etherscan.io

    来自 geth、eth、pyeth 控制台:

    使用 Javascript API(这是 geth、eth 和 pyeth 控制台使用的),您可以通过以下方式获取帐户余额:

    web3.fromWei(eth.getBalance(eth.coinbase)); 
    

    “web3”是Ethereum-compatible Javascript library web3.js

    “eth”实际上是“web3.eth”的简写(在geth中自动可用)。所以,真的,上面应该写成:

    web3.fromWei(web3.eth.getBalance(web3.eth.coinbase));
    

    “web3.eth.coinbase”是您控制台会话的默认帐户。如果您愿意,您可以为其插入其他值。所有账户余额都在以太坊中开放。例如,如果您有多个帐户:

    web3.fromWei(web3.eth.getBalance(web3.eth.accounts[0]));
    web3.fromWei(web3.eth.getBalance(web3.eth.accounts[1]));
    web3.fromWei(web3.eth.getBalance(web3.eth.accounts[2]));
    

    web3.fromWei(web3.eth.getBalance('0x2910543af39aba0cd09dbb2d50200b3e800a63d2'));
    

    编辑:这是一个方便的脚本,用于列出您所有帐户的余额:

    function checkAllBalances() { var i =0; eth.accounts.forEach( function(e){ console.log("  eth.accounts["+i+"]: " +  e + " \tbalance: " + web3.fromWei(eth.getBalance(e), "ether") + " ether"); i++; })}; checkAllBalances();
    

    内部合同:

    在合约内部,Solidity 提供了一种获取余额的简单方法。每个地址都有一个 .balance 属性,它以 wei 为单位返回值。合同样本:

    contract ownerbalancereturner {
    
        address owner;
    
        function ownerbalancereturner() public {
            owner = msg.sender; 
        }
    
        function getOwnerBalance() constant returns (uint) {
            return owner.balance;
        }
    }
    

    【讨论】:

    • 我可以建议您列出余额的脚本可以更简单: eth.accounts.forEach( function(e, i){ console.log(" eth.accounts["+i+"]: " + e + " \tbalance: " + web3.fromWei(eth.getBalance(e), "ether") + "ether") })
    • eth.getBalance() 接收 2 个参数,而不是 1 个,当你只提供一个时会发生什么?第二个的默认值是什么?
    • web3.fromWei 不是函数
    【解决方案2】:

    对于新版本的 web3 API:

    最新版本的 web3 API(版本 beta 1.xx)使用承诺(异步,如回调)。 文档:web3 beta 1.xx

    因此它是一个 Promise 并返回 wei 中给定地址的字符串。

    我在 Linux (openSUSE)、geth 1.7.3、Rinkeby Ethereum testnet 上,使用 Meteor 1.6.1,并让它通过 IPC Provider 连接到我的 geth 节点以下列方式工作:

    // serverside js file
    
    import Web3 from 'web3';
    
    if (typeof web3 !== 'undefined') {
      web3 = new Web3(web3.currentProvider);
    } else {
      var net = require('net');
      var web3 = new Web3('/home/xxYourHomeFolderxx/.ethereum/geth.ipc', net);
    };
    
      // set the default account
      web3.eth.defaultAccount = '0x123..............';
    
      web3.eth.coinbase = '0x123..............';
    
      web3.eth.getAccounts(function(err, acc) {
        _.each(acc, function(e) {
          web3.eth.getBalance(e, function (error, result) {
            if (!error) {
              console.log(e + ': ' + result);
            };
          });
        });
      });
    

    【讨论】:

    • 那些使用 nodejs(我在 v11.5.0 上)的人不会使这项工作,因为不支持 import。所以你需要改用const Web3 = require('web3');
    • 另外,余额会以wei的形式返回,需要用.fromWei()进行转换
    【解决方案3】:

    'for-each' 循环有效,但获得平衡的一种非常简单的方法是简单地为函数添加 await

    var bal = await web3.eth.getBalance(accounts[0]);
    

    或者如果你想直接显示:

    console.log('balance = : ', await web3.eth.getBalance(accounts[0]));
    

    【讨论】:

    • 当我运行这个我得到 Uncaught SyntaxError: missing ) after argument list
    • SyntaxError: await 仅在异步函数中有效
    【解决方案4】:

    来自docs,(查看链接了解变化)

    web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1")
    .then(console.log);
    > "1000000000000"
    

    【讨论】:

      猜你喜欢
      • 2022-11-16
      • 1970-01-01
      • 2019-04-06
      • 2019-03-29
      • 2011-12-09
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      相关资源
      最近更新 更多