How do I get the balance from the Ethereum account?

Asked 2 years ago, Updated 2 years ago, 148 views

How do I know how many ETHs are in a given account on the Ethereum blockchain programmatically?

ethereum

2022-09-22 12:37

2 Answers

For the web

Although it is not programmed, you can find out the balance of your account or contract by entering http://etherchain.org or http://etherscan.io.

geth, eth, pyeth console:

You can use the JavaScript API used in the geth, eth, or pyeth console to find out the balance of your account.

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

"web3" is a JavaScript library web3.js compatible with Ethereum.

"eth" is actually an abbreviation for "web3.eth" and is automatically available in geth. So you actually have to write the above:

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

"web3.eth.coinbase" is the default account for the console session. You can associate other values if you want. All account balances are available in Ethereum. For example, when you have multiple accounts:

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])); 

Or

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

In addition, the following convenient script code lists the balance of all accounts.

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(); 

Internal contract:

Within the contract, Solidity makes it easy to get the balance. All addresses have a .balance property that returns a value of wei. For example,

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


2022-09-22 12:37

New release of web3 API:

The latest version of the web3 API (beta 1.xx) takes advantage of promised actions, such as asynchronous, like callback. Document: web3 beta 1.xx

So this is a fixed commitment and it returns a string of addresses given to wei.

I first work on Linux (openSUSE), geth 1.7.3, Rinkby Ethereum testnet using Meteor 1.6.1, and then connect the following code to the geth node via IPC provider.

 // 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); 
            };
         });
    }); 
}); 


2022-09-22 12:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.