8

I do not see anything in creating accounts and retrieving ether balance in web3j's documentation, I wonder if it is possible.

1
  • look at their github wiki
    – Sig Touri
    Commented Jun 6, 2017 at 18:53

3 Answers 3

12

The "ethGetBalance" method will return the balance of any given account. Pls see example below:

// connect to node
Web3j web3 = Web3j.build(new HttpService());  // defaults to http://localhost:8545/

// send asynchronous requests to get balance
EthGetBalance ethGetBalance = web3
  .ethGetBalance("0xAccountAddress", DefaultBlockParameterName.LATEST)
  .sendAsync()
  .get();

BigInteger wei = ethGetBalance.getBalance();
1
  • 1
    If web3j is built with remote node. And i provide a invalid wallet address what will the getBalance return. Commented Aug 20, 2017 at 7:35
3

You can use CURL command or use request module inside your code to call create account API -

CURL command for your reference:

curl -X POST --data '{"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":74}' http://localhost:8545

Also, you must run the geth node with the "personal" api enabled to make this method available:

geth --rpc --rpcapi "personal,eth,web3"

To check account balance using web3js, use following command -

web3.eth.getBalance('account address hash in quotes')

Hope this helps.

1
  • this is in correct the question is about web3j and how to get it using java
    – Abdullah
    Commented Feb 17, 2020 at 1:53
0

This is how you do it kotlin web3j. I'm using Goerli infura


val web3j = Web3j.build(HttpService("https://goerli.infura.io/v3/YOUR_GOERLI_INFURA_KEY"))
        
var mBalance: EthGetBalance =
            web3j.ethGetBalance(walletAddress, DefaultBlockParameterName.LATEST).sendAsync()
                .get()
        
val unScaledBalance = mBalance.balance
        
val scaledBalance = BigDecimal(unScaledBalance).divide(
            BigDecimal(1000000000000000000L),
            18,
            RoundingMode.HALF_UP
        )

        Log.d(TAG, "checkAddressBalance: ScaledBalance: $scaledBalance")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.