1 min read - Posted 18 Dec 19

Retrieving an Ethereum Account Balance in Web3j

Balance at Current Block

To obtain the balance of an account in java with web3j at the current block, the following code snippet can be used:

//Connect to node.
//Defaults to http://localhost:8545
Web3j web3 = HttpService());

//Get balance result synchronously
EthGetBalance balanceResult = web3.ethGetBalance("0xF0f15Cedc719B5A55470877B0710d5c7816916b1", 

//Obtain the BigInteger balance representation, in the wei unit.
BigInteger balanceInWei = balanceResult.getBalance();

Balance at a Specific Block

The below code snippet will obtain the account balance at block 300000:

Web3j web3 = HttpService());

EthGetBalance balanceResult = web3.ethGetBalance("0xF0f15Cedc719B5A55470877B0710d5c7816916b1", 
new DefaultBlockParameterNumber(300000)).send();

BigInteger balanceInWei = balanceResult.getBalance();

Balances are returned in the smallest denomination, called wei. To convert to other denominations, such as ether, see this cheetsheet article.

Created with Sketch.Content is"CC-BY-SA 4.0" licensed
Article On-chain
Related Articles
Manage an Ethereum account with Java and Web3j

Other articles in this series: - Connecting to an Ethereum client with Java, Eclipse and Web3j - Generate a Java Wrapper from your Smart Contract - Interacting with an Ethereum Smart Contract in Java - Listening for Ethereum Smart Contract Events in Java - Using Pantheon, the Java Ethereum Client with Linux The Ethereum blockchain is often compared to a World Computer with a global state. The global state grows after each new block and cosists of many accounts organised in a Merkle tree. Each ac

Signing an Ethereum Transaction in Web3j

Web3j provides a bunch of helper classes to enable you to create and sign a transaction within your ethereum java code. The transaction creation process involve