以太坊Web3j开发实例,从零开始与以太坊区块链交互

时间: 2026-04-08 18:03 阅读数: 2人阅读

以太坊作为全球领先的智能合约平台,为去中心化应用(DApps)的开发提供了强大的基础设施,对于Java开发者而言,Web3j是一个轻量级、高度模块化的库,它使得与以太坊区块链的交互变得异常简单,本文将通过一个具体的开发实例,带你了解如何使用Web3j进行以太坊应用开发。

Web3j简介

Web3j是一个开源的Java和Android库,用于与以太坊节点进行交互,它提供了以下核心功能:

  1. 连接以太坊节点:支持连接到本地或远程的以太坊节点(如Geth, Parity, Infura等)。
  2. 账户管理:创建、导入、导出以太坊账户(钱包)。
  3. 随机配图
trong>交易发送:发送以太币(ETH)和智能合约交互(调用函数、发送交易)。
  • 智能合约交互:编译、部署、加载以及与已部署的智能合约进行方法调用和事件监听。
  • 事件监听:监听区块链上的特定事件。
  • 区块链数据查询:查询区块、交易、账户余额等信息。
  • Web3j的优势在于其纯Java实现,不依赖外部守护进程,易于集成到现有Java项目中,并且提供了丰富的API和文档。

    开发环境准备

    在开始之前,确保你的开发环境已准备就绪:

    1. Java Development Kit (JDK):推荐JDK 8或更高版本。
    2. Maven:用于项目管理和依赖下载。
    3. 以太坊客户端:可以选择安装本地的Geth或Parity节点,或者使用Infura等远程节点服务(本实例以Infura为例)。
    4. 测试ETH:从以太坊测试网络(如Ropsten, Goerli)获取一些测试ETH,用于交易,可以通过水龙头获取。

    Web3j项目创建与依赖配置

    1. 创建一个新的Maven项目(可以使用IDE如IntelliJ IDEA或Eclipse)。
    2. pom.xml文件中添加Web3j依赖:
    <dependencies>
        <!-- Web3j Core -->
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>4.9.8</version> <!-- 请使用最新版本 -->
        </dependency>
        <!-- 如果需要Android支持,可以添加web3j-android -->
        <!-- <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>android</artifactId>
            <version>4.9.8</version>
        </dependency> -->
        <!-- 日志依赖,可选 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.36</version>
        </dependency>
    </dependencies>

    连接以太坊节点

    我们需要建立一个与以太坊节点的连接,这里以使用Infura的Goerli测试网络为例。

    1. 在Infura官网(infura.io)注册并创建一个新的项目,获取你的项目ID。
    2. 编写代码连接节点:
    import org.web3j.protocol.Web3j;
    import org.web3j.protocol.http.HttpService;
    import java.io.IOException;
    public class Web3jConnection {
        public static void main(String[] args) {
            // 替换为你的Infura项目ID和选择的网络(如Goerli)
            String infuraUrl = "https://goerli.infura.io/v3/YOUR_INFURA_PROJECT_ID";
            // 创建Web3j实例
            Web3j web3j = Web3j.build(new HttpService(infuraUrl));
            try {
                // 检查连接是否成功
                String clientVersion = web3j.web3ClientVersion().send().getWeb3ClientVersion();
                System.out.println("Connected to Ethereum client: " + clientVersion);
                // 获取最新区块号
                String latestBlockNumber = web3j.ethBlockNumber().send().getBlockNumber().toString();
                System.out.println("Latest block number: " + latestBlockNumber);
            } catch (IOException e) {
                System.err.println("Failed to connect to Ethereum client: " + e.getMessage());
                e.printStackTrace();
            } finally {
                // 关闭连接
                web3j.shutdown();
            }
        }
    }

    运行上述代码,如果成功连接,你将看到客户端版本信息和最新区块号。

    账户管理:查询账户余额

    假设我们有一个测试账户地址,我们可以查询其ETH余额。

    import org.web3j.protocol.core.methods.response.EthGetBalance;
    import java.math.BigInteger;
    public class AccountBalance {
        public static void main(String[] args) {
            String infuraUrl = "https://goerli.infura.io/v3/YOUR_INFURA_PROJECT_ID";
            Web3j web3j = Web3j.build(new HttpService(infuraUrl));
            // 替换为你要查询的测试账户地址
            String address = "0xYourTestAccountAddress";
            try {
                // 查询账户余额,单位是Wei
                EthGetBalance balance = web3j.ethGetBalance(address, org.web3j.protocol.core.DefaultBlockParameterName.LATEST).send();
                BigInteger balanceInWei = balance.getBalance();
                // 将Wei转换为ETH (1 ETH = 10^18 Wei)
                double balanceInEth = balanceInWei.doubleValue() / Math.pow(10, 18);
                System.out.println("Address: " + address);
                System.out.println("Balance in Wei: " + balanceInWei);
                System.out.println("Balance in ETH: " + balanceInEth);
            } catch (IOException e) {
                System.err.println("Failed to get balance: " + e.getMessage());
                e.printStackTrace();
            } finally {
                web3j.shutdown();
            }
        }
    }

    发送交易(转账ETH)

    发送ETH需要使用账户的私钥进行签名。注意:私钥敏感,切勿硬编码在代码中或提交到版本控制系统! 本例仅作演示,实际应用中应从安全的地方(如密钥库文件、环境变量、硬件钱包)读取私钥。

    import org.web3j.protocol.core.methods.response.EthSendTransaction;
    import org.web3j.protocol.core.methods.transaction.Transaction;
    import org.web3j.utils.Convert;
    import org.web3j.utils.Convert.Unit;
    import org.web3j.utils.Numeric;
    import java.io.IOException;
    import java.math.BigDecimal;
    import java.math.BigInteger;
    import java.util.concurrent.ExecutionException;
    public class SendTransaction {
        public static void main(String[] args) {
            String infuraUrl = "https://goerli.infura.io/v3/YOUR_INFURA_PROJECT_ID";
            Web3j web3j = Web3j.build(new HttpService(infuraUrl));
            // 发送方账户地址和私钥(演示用,实际请勿如此!)
            String fromAddress = "0xYourSenderAddress";
            String privateKey = "YOUR_PRIVATE_KEY"; // 替换为发送方私钥
            // 接收方账户地址
            String toAddress = "0xRecipientAddress";
            // 转账金额(例如0.01 ETH)
            BigDecimal amountInEth = BigDecimal.valueOf(0.01);
            BigInteger amountInWei = Convert.toWei(amountInEth, Unit.ETH).toBigIntegerExact();
            // 获取当前gas价格
            BigInteger gasPrice = null;
            try {
                gasPrice = web3j.ethGasPrice().send().getGasPrice();
            } catch (IOException e) {
                System.err.println("Failed to get gas price: " + e.getMessage());
                e.printStackTrace();
                return;
            }
            // 设置gas限制(简单起见,可以设置一个较高的值,如21000)
            BigInteger gasLimit = BigInteger.valueOf(21000);
            // 创建交易对象
            Transaction transaction = Transaction.createEtherTransaction(
                    fromAddress,
                    getNonce(web3j, fromAddress), // 获取nonce
                    gasPrice,
                    gasLimit,
                    toAddress,
                    Numeric.encodeQuantity(amountInWei)
            );
            try {
                // 使用私钥签名并发送交易
                EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(
                        Sign.signTransaction(transaction, Credentials.create(privateKey))
                ).send();
                if (ethSendTransaction.getTransactionHash() != null) {
                    System.out.println("Transaction sent! Hash: " + ethSendTransaction.getTransactionHash());
                } else {
                    System.err.println("Failed to send transaction: " + ethSendTransaction.getError().getMessage());
                }
            } catch (IOException | ExecutionException | InterruptedException e) {
                System.err.println("Failed to send transaction: " + e.getMessage());
                e.printStackTrace();
            } finally {
                web3j.shutdown();
            }
        }
        // 获取账户的nonce值
        private

    上一篇:

    下一篇: