2

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.

hd312
  • 81
  • 1
  • 3

2 Answers2

10

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());
Majd TL
  • 3,217
  • 3
  • 17
  • 36
4

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());
    } }
Ashish Mishra
  • 313
  • 1
  • 8