文章是本人学习过程翻译,原文来自官方文档:https://web3j.readthedocs.io/en/latest/#
官网:https://web3j.io/
官方GitHub:https://github.com/web3j/web3j
官方demo:https://github.com/web3j/web3j/tree/master/integration-tests
文档版本v3.4.0。
一个快速开始的简单demo,地址:https://github.com/web3j/sample-project-gradle
包括:
- Connecting to a node on the Ethereum network
- Loading an Ethereum wallet file
- Sending Ether from one address to another
- Deploying a smart contract to the network
- Reading a value from the deployed smart contract
- Updating a value in the deployed smart contract
- Viewing an event logged by the smart contract
入门指南
Maven引入web3j依赖包
org.web3j
core
3.4.0
启动geth客户端
$ geth --rpcapi personal,db,eth,net,web3 --rpc --rinkeby
初始化web3对象
可以使用http或者ipc连接方式,如下:
Web3j web3 = Web3j.build(new WindowsIpcService("\\\\.\\pipe\\geth.ipc"));
Web3j web3 = Web3j.build(new HttpService("http://192.168.171.58:8545"));
发送同步请求
Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
发送异步请求(使用java的CompletableFuture)
CompletableFuture completableFuture = web3.web3ClientVersion().sendAsync();
Future future = completableFuture.whenComplete((v, e) -> {
System.out.println();
System.out.println("call back function print: " + v.getWeb3ClientVersion());
System.out.println(e);
web3.shutdown();
});
关闭web3对象
web3.shutdown()
命令行工具(Command Line Tools)
web3j的命令行工具有如下功能:
- 创建钱包
- 钱包密码管理
- 交易以太币
- 生成智能合约类包
生成钱包:
$ web3j wallet create
更新钱包密码
$ web3j wallet update
发送以太币到另外一个地址:
$ web3j wallet send
0x |
生成智能合约类包
- 支持从solidity abi文件
$ web3j solidity generate [--javaTypes|--solidityTypes] /path/to/.bin /path/to/.abi -o /path/to/src/main/java -p com.your.organisation.name
2.支持从Truffle json文件生成
$ web3j truffle generate [--javaTypes|--solidityTypes] /path/to/.json -o /path/to/src/main/java -p com.your.organisation.name
在web3j 3.x版本,生成的智能合约类包默认使用 --solidityTypes ,在web3j 3.x版本前默认使用 --javaTypes,可以使用[ --javaTypes| --solidityTypes]参数改变设置。
与智能合约交互
利用命令行工具生成java类包后,你可以创建和部署你的智能合约
Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/
Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile");
YourSmartContract contract = YourSmartContract.deploy(
, ,
GAS_PRICE, GAS_LIMIT,
, ..., ).send(); // constructor params
或者使用一个存在的合约
YourSmartContract contract = YourSmartContract.load( "0x
|
", , , GAS_PRICE, GAS_LIMIT);
向合约发起交易(需要消耗gas)
TransactionReceipt transactionReceipt = contract.someMethod(,...).send();
call一个智能合约(不需要消耗gas)
Type result = contract.someMethod(, ...).send();
Filters
待补充...
交易(Transactions)
发送以太币
Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/ Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile"); TransactionReceipt transactionReceipt = Transfer.sendFunds( web3, credentials, "0x
|
", BigDecimal.valueOf(1.0), Convert.Unit.ETHER) .send();
创建自定义的交易
Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/
Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile");
// get the next available nonce
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
address, DefaultBlockParameterName.LATEST).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
// create our transaction
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
nonce, , , , );
// sign & send our transaction
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send();
// ...
使用客户端节点的admin命令,解锁账户
Admin web3j = Admin.build(new HttpService()); // defaults to http://localhost:8545/
PersonalUnlockAccount personalUnlockAccount = web3j.personalUnlockAccount("0x000...", "a password").sendAsync().get();
if (personalUnlockAccount.accountUnlocked()) {
// send a transaction
}