JAVA WEB3J与ganache以太坊环境交互

mac2025-06-19  2

1. 下载Web3j库

在github官网  https://github.com/web3j/web3j/releases  下载web3j-4.5.5.zip文件

在命令行中输入加压命令 unzip web3j-4.5.5.zip 进行解压

直接输入命令 ./web3j-4.5.5.zip/bin/web3j 即可运行,显示图像如下所示说明运行成功

参考官方文档: https://web3j.readthedocs.io/en/latest/command_line_tools.html#manual-installation

 

2. 编译生成所需的辅助类文件

先安装solc,输入命令 npm install -g solc

把合约代码编译生成需要的bin和abi文件,注意指定一个输出路径,这里是我新建的 JAVA-ETH 文件夹

输入命令 solcjs MyContract.sol --bin --abi --optimize -o JAVA-ETH/  

会在JAVA-ETH 文件夹下 生成 MyContract.bin 和 MyContract.abi 文件。

这两个文件用于生成JAVA访问以太坊的合约辅助类,当前文件夹是myApp/JAVA-ETH,而Web3j在myApp/web3j-4.5.5文件夹,因此命令如下:

$ ./../web3j-4.5.5/bin/web3j solidity generate -a=MyContract.abi -b=MyContract.bin -o=Wrapper -p=My.test

然后就能在myApp/JAVA-ETH/Wrapper/My/test 文件夹下看到生成的 MyContract_sol_MyContract.java

 

3. 访问区块链

把这个类文件复制到JAVA工程中,在JAVA工程里添加myApp/web3j-4.5.5/lib/console-4.5.5-all.jar 这个库

参考官方示例,即可与以太坊交互

https://web3j.readthedocs.io/en/latest/smart_contracts.html

https://kauri.io/article/14dc434d11ef4ee18bf7d57f079e246e/interacting-with-an-ethereum-smart-contract-in-java

https://www.baeldung.com/web3j

以下是我写的简单例子,可以访问ganache里的账户信息和加载合约

/** * FileName: Test * Author: star * Date: 2019/10/22 16:40 * Description: 测试JAVA连接以太坊和调用合约函数 * History: * <author> <time> <version> <desc> * 作者姓名 修改时间 版本号 描述 */ package Test.blockchain; import org.web3j.crypto.Credentials; import org.web3j.protocol.Web3j; import org.web3j.protocol.core.DefaultBlockParameter; import org.web3j.protocol.core.methods.response.*; import org.web3j.protocol.http.HttpService; import java.math.BigInteger; import java.util.List; /** * 〈一句话功能简述〉<br> * 〈测试JAVA连接以太坊和调用合约函数〉 * * @author star * @create 2019/10/22 * @since 1.0.0 */ public class Test { private Web3j web3j; private Credentials credentials; //第一个账户的私钥 private String CONTRACT_ADDRESS = "0x187c81F78Faf7EBE68AF673ED2ab35b640dc6bA9"; //部署的合约地址 private static MyContract_sol_MyContract contract; private BigInteger ethBase = BigInteger.valueOf(10).pow(18); // 1 eth = 10^18 wei public Test(){ try { web3j = Web3j.build(new HttpService("http://localhost:7545")); //本地的ganache gui credentials = Credentials.create("4e1b6ab1b2a5f477c416eb275ac5aac4e1257002782f0c3e74f3e8747db30f05"); //直接填写第一个账户的私钥 }catch(Exception e){ e.printStackTrace(); } } //加载已经部署的合约 public void loadContract(){ System.out.println("Going to load smart contract"); try { contract = MyContract_sol_MyContract.load( CONTRACT_ADDRESS, web3j, credentials, new BigInteger("22000000000"), new BigInteger("510000")); System.out.println("Load smart contract done!"); } catch(Exception e){ e.printStackTrace(); } } //===================== 测试以太坊环境的方法 ===================== public void GetClientVersion(){ try { Web3ClientVersion version = web3j.web3ClientVersion().sendAsync().get(); System.out.println("version : " + version.getWeb3ClientVersion()); }catch(Exception e){ e.printStackTrace(); } } //获得最新的块数,大整数类型 public BigInteger getLatestBlockNumber() { EthBlockNumber result = new EthBlockNumber(); try { result = this.web3j.ethBlockNumber() .sendAsync() .get(); } catch (Exception e) { e.printStackTrace(); } return result.getBlockNumber(); } //获得所有以太坊账户地址,这里是10个测试账户 public List<String> getEthAccounts() { EthAccounts result = new EthAccounts(); try { result = this.web3j.ethAccounts() .sendAsync() .get(); } catch (Exception e) { e.printStackTrace(); } return result.getAccounts(); } //获得交易总数,是大整数类型 public BigInteger getTransactionCount() { EthGetTransactionCount result = new EthGetTransactionCount(); try { result = this.web3j.ethGetTransactionCount(this.CONTRACT_ADDRESS, DefaultBlockParameter.valueOf("latest")) .sendAsync() .get(); } catch (Exception e) { e.printStackTrace(); } return result.getTransactionCount(); } //获得某个账户余额,大整数类型 public BigInteger getAccountBalance(String contractAddress) { EthGetBalance result = new EthGetBalance(); try { this.web3j.ethGetBalance(contractAddress, DefaultBlockParameter.valueOf("latest")) .sendAsync() .get(); } catch (Exception e) { e.printStackTrace(); } return result.getBalance(); //报错:org.web3j.exceptions.MessageDecodingException: Value must be in format 0x[1-9]+[0-9]* or 0x0 } public static void main(String args[]){ Test test = new Test(); // 测试ganache的功能 test.GetClientVersion(); //查看测试环境版本 System.out.println("最新块数:" + test.getLatestBlockNumber()); //查看最新块数 System.out.println("交易总数:" + test.getTransactionCount()); //查看交易总数 System.out.println("所有账户地址如下:"); List<String> accs = test.getEthAccounts(); //打印所有账户 for (int i = 0; i < accs.size(); i ++) System.out.println(accs.get(i)); //System.out.println("第10个账户余额" + test.getAccountBalance(accs.get(9))); //查看第10个账户余额 test.loadContract(); //加载已经部署的MyContract合约 } }

 

最新回复(0)