我如何获得以太账户的余额? [英] How do I get the balance of an account in Ethereum?

查看:12
本文介绍了我如何获得以太账户的余额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

网上:

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

从geth、eth、pyeth控制台:

使用Java脚本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 + " 	balance: " + web3.fromWei(eth.getBalance(e), "ether") + " ether"); i++; })}; checkAllBalances();

内部合同:

在合同中,稳健性提供了一种简单的平衡方式。每个地址都有一个.Balance属性,该属性以wei为单位返回值。合同样本:

contract ownerbalancereturner {

    address owner;

    function ownerbalancereturner() public {
        owner = msg.sender; 
    }

    function getOwnerBalance() constant returns (uint) {
        return owner.balance;
    }
}

这篇关于我如何获得以太账户的余额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆