import org.web3j.protocol.Web3j; import org.web3j.protocol.core.DefaultBlockParameter; import org.web3j.protocol.core.methods.response.EthBlock; import org.web3j.protocol.http.HttpService; public class Web3jExample { public static void main(String[] args) { Web3j web3j = Web3j.build(new HttpService("https://mainnet.infura.io/v3/your-infura-api-key")); try { EthBlock.Block latestBlock = web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf("latest"), true) .send().getBlock(); System.out.println("Latest Block: " + latestBlock.getNumber()); System.out.println("Timestamp: " + latestBlock.getTimestamp()); System.out.println("Transactions: " + latestBlock.getTransactions().size()); } catch (Exception e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文