依赖:
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>5.0.0</version>
</dependency>
代码:
package com.mathieu.blockchain;
import com.alibaba.fastjson.JSONObject;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterNumber;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
import java.math.BigInteger;
public class Web3jClientDemo {
public static void main(String[] args) throws IOException {
String url = "https://bsc-mainnet.nodereal.io/v1/64a9df0874fb4a93b9d0a3849de012d3";
// 第二个参数表示是否返回原始response数据,原始response中的数据为16进制
Web3j web3j = Web3j.build(new HttpService(url, false));

该代码示例展示了如何通过Web3j库连接到BinanceSmartChain的节点,获取最新的区块号并详细查询指定区块的信息。使用HttpService构建Web3j客户端,然后调用ethBlockNumber()获取区块号,ethGetBlockByNumber()获取区块详情,最后关闭客户端。
最低0.47元/天 解锁文章
3284

被折叠的 条评论
为什么被折叠?



