[Ethereum] how to get latest block number using web3j java APIs

web3j

I ask the network for current balance of the address i want to keep track. after that i want to detect/listen incoming deposits starting at the latest block number at the start of my program/server. now the problem is how to find the latest block number at that time? it looks like there is an API in web3js (java script) but not in web3j (java). I am connected to rinkeby testnet via infura node.

Best Answer

This is the easiest way

Web3j web3j = Web3j.build(new HttpService("https://rinkeby.infura.io/v3/......."));
Block block = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().getBlock();
System.out.println(block.getNumber().toString());
Related Topic