:2026-03-23 21:36 点击:1
以太坊作为全球领先的智能合约平台,为去中心化应用(DApps)的开发提供了强大的基础设施,对于Java开发者而言,Web3j是一个轻量级、高度模块化的库,它使得与以太坊区块链的交互变得异常简单,本文将通过一个具体的开发实例,带你了解如何使用Web3j进行以太坊应用开发。
Web3j是一个开源的Java和Android库,用于与以太坊节点进行交互,它提供了以下核心功能:
Web3j的优势在于其纯Java实现,不依赖外部守护进程,易于集成到现有Java项目中,并且提供了丰富的API和文档。
在开始之前,确保你的开发环境已准备就绪:
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测试网络为例。
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需要使用账户的私钥进行签名。注意:私钥敏感,切勿硬编码在代码中或提交到版本控制系统! 本例仅作演示,实际应用中应从安全的地方(如密钥库文件、环境变量、硬件钱包)读取私钥。
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
本文由用户投稿上传,若侵权请提供版权资料并联系删除!