Search code examples
javaethereumweb3-java

Conversion from int to DeafultBlockParameter


I am using Infura and web3j to query the Ethereum blockchain. My aim is to get information about certain blocks so i'm using the function web3.eth.getBlockByNumber, however it gives me incompatible types error: int cannot be converted to DefaultBlockParameter. How can we convert an integer to default block parameter? Current code:

System.out.println("here " +web3j.ethGetBlockByNumber(6777, true));

I have also tried converting 6777 to BigInteger but it gives me incompatible types error on that too.


Solution

  • web3j uses a DefaultBlockParameter type to encode the block number in such calls, likely to handle the "latest" and "pending" block state special cases. Try:

    System.out.println("here " +web3j.ethGetBlockByNumber(new DefaultBlockParameter(6777), true));