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.
2 Answers
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());
This is my implementation of getting the latest block number. I set up the project using https://docs.web3j.io/getting_started/#gradle
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.http.HttpService;
import java.util.concurrent.ExecutionException;
public class App {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Web3j web3 = Web3j.build(new HttpService(__rpcURL__));
EthBlockNumber result = web3.ethBlockNumber().sendAsync().get();
System.out.println(" result: " + result.getBlockNumber().toString());
} }