开发环境:

windows

java 17.0.1

maven 3.8.3

node 14.17.0

实验步骤:

1.启动以太坊客户端节点

安装ganache-cli(用于搭建以太坊私链),并启动ganache-cli

npm install -g ganache-cli

ganache-cli

 启动后可以看到如下信息

2.测试以太坊通用的RPC接口,获取区块,交易,余额什么的

新建一个maven项目,加上下面两个依赖

org.web3j

core

4.8.7

org.slf4j

slf4j-simple

1.6.6

代码如下(暂时没有智能合约,只是简单的获取区块链信息和普通交易):

package com.example;

import org.web3j.crypto.Credentials;

import org.web3j.protocol.Web3j;

import org.web3j.protocol.core.DefaultBlockParameterName;

import org.web3j.protocol.core.methods.request.Transaction;

import org.web3j.protocol.core.methods.response.*;

import org.web3j.protocol.core.methods.response.EthBlock.Block;

import org.web3j.protocol.core.methods.response.EthBlock.TransactionResult;

import org.web3j.protocol.http.HttpService;

import org.web3j.tx.Transfer;

import org.web3j.utils.Convert;

import org.web3j.utils.Convert.Unit;

import java.math.BigDecimal;

import java.math.BigInteger;

import java.util.List;

/**

* Hello world!

*

*/

public class App

{

public static void main( String[] args )

{

try {

//连接以太坊客户端节点,可以是ganache,geth等,注意地址端口和RPC协议(ws ,http)

Web3j web3j = Web3j.build(new HttpService("http://127.0.0.1:8545"));

//获取客户端版本号,通常用来判断是否连接上

Web3ClientVersion web3clientversion = web3j.web3ClientVersion().send();

String clientVersion = web3clientversion.getWeb3ClientVersion();

System.out.println("web3clientVersion : " + clientVersion);

//获取节点上所有的账户,在ganache中,每个账户有100ETH,允许你用上面用户交易

//注意绝大多数以太坊客户端节点不会允许你用它上面的账户交易,你要自己创建钱包(见后面的Cpp类main函数)

List addressList = web3j.ethAccounts().send().getAccounts();

System.out.println("addressList : "+addressList);

//交易前,获取前两个账户余额

BigInteger balance0 = web3j.ethGetBalance(addressList.get(0), DefaultBlockParameterName.LATEST).send().getBalance();

BigInteger balance1 = web3j.ethGetBalance(addressList.get(1), DefaultBlockParameterName.LATEST).send().getBalance();

System.out.println("before Transaction:");

System.out.println("balance0 : " + addressList.get(0) + " : " + balance0);

System.out.println("balance1 : " + addressList.get(1) + " : " + balance1);

//生成交易,参数包括谁发送,发给谁,发多少,给矿工的小费等等,由于ganache允许我们操作它上面的账户,所以不用签名。

BigInteger mynounce = web3j.ethGetTransactionCount(addressList.get(0), DefaultBlockParameterName.LATEST).send().getTransactionCount();

BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();

BigInteger gasLimit = BigInteger.valueOf(21000);

BigInteger value = Convert.toWei(BigDecimal.valueOf(0.5), Unit.ETHER).toBigInteger() ;

Transaction mytesttransaction= Transaction.createEtherTransaction(addressList.get(0),mynounce, gasPrice, gasLimit, addressList.get(1),value);

System.out.println("mytesttransaction : ");

printTransaction(mytesttransaction);

//发送交易,获取交易哈希

String txhash = web3j.ethSendTransaction(mytesttransaction).send().getTransactionHash();

System.out.println("txhash : " + txhash);

//交易对应的收据,这个对于智能合约交易非常有用,对普通交易没多大用

TransactionReceipt transactionReceipt = web3j.ethGetTransactionReceipt(txhash).send().getTransactionReceipt().get();

System.out.println("transactionReceipt : " + transactionReceipt);

//交易后的余额

balance0 = web3j.ethGetBalance(addressList.get(0), DefaultBlockParameterName.LATEST).send().getBalance();

balance1 = web3j.ethGetBalance(addressList.get(1), DefaultBlockParameterName.LATEST).send().getBalance();

System.out.println("after Transaction:");

System.out.println("balance0 : " + addressList.get(0) + " : " + balance0);

System.out.println("balance1 : " + addressList.get(1) + " : " + balance1);

//获取当前最新区块,并获取它上面的所有交易

Block latestBlock = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().getBlock();

System.out.println("latestBlock: " + latestBlock.getHash());

//获取区块上的所有交易

List txlist = latestBlock.getTransactions();

System.out.println("get TransactionBy Block :" );

for (TransactionResult transactionResult : txlist) {

System.out.println(transactionResult.get());

}

} catch (Exception e) {

System.out.println(e);

e.printStackTrace();

}

}

public static void printTransaction(Transaction x) {

//打印交易信息

System.out.println("From:"+x.getFrom());

System.out.println("GasLimit:"+x.getGas());

System.out.println("Nonce:"+x.getNonce());

System.out.println("GasPrice:"+x.getGasPrice());

System.out.println("To:"+x.getTo());

System.out.println("Value:"+x.getValue());

}

}

class Cpp {

public static void main(String[] args) {

try {

Web3j web3j = Web3j.build(new HttpService("http://127.0.0.1:8545"));

//通常情况下,以太坊节点是不会让其他人用它上面的账户,我们要自己创建钱包,用自己的私钥对交易签名,

//下面的私钥是ganache上第一个账户的私钥,虽然交易发起者和上面的交易一样,但两者本质是不同的。

Credentials mysigner = Credentials.create("0xfd468e650d82240e739c18ea1aaa0a7316febec3e7bb056ff1437bd3d0ede99b");

List addressList = web3j.ethAccounts().send().getAccounts();

String txhash = Transfer.sendFunds(web3j, mysigner, addressList.get(1),BigDecimal.valueOf(1.0), Unit.ETHER).send().getTransactionHash();

System.out.println("txhash:"+txhash);

TransactionReceipt receipt = web3j.ethGetTransactionReceipt(txhash).send().getTransactionReceipt().get();

System.out.println(receipt);

} catch (Exception e) {

e.printStackTrace();

}

}

}

App中main执行结果:

Cpp中main执行结果:

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。