配合视频教程一块看:的个人空间_哔哩哔哩_Bilibili
pom.xml
<dependency>
<groupId>org.web3j</groupId>
<artifactId>codegen</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.6</version>
</dependency>
SolidityERC721Utils.java
package com.zscq.common.erc.utils;
import cn.hutool.core.io.file.FileReader;
import org.web3j.codegen.SolidityFunctionWrapperGenerator;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;
public class SolidityERC721Utils {
//文件基础路径
private static String basePath = "F:\\IdeaProjects\\cazql\\zscq\\zscq-common\\src\\main\\java\\com\\zscq\\common\\erc\\";
// 私有实例,类初始化就加载
private static SolidityERC721Utils solidityGenerateUtil = new SolidityERC721Utils();
// 私有构造方法
private SolidityERC721Utils() {}
// 公共的、静态的获取实例方法
public static SolidityERC721Utils getInstance() {
return solidityGenerateUtil;
}
/**
* 获取abi josn字符串
* @return
*/
public static String getAbiJson(){
String abiPath = basePath + "utils/abi.json";
FileReader abiReader = new FileReader(abiPath);
String abiResult = abiReader.readString();
return abiResult;
}
/**
* 获取bytecode json字符串
* @return
*/
public static String getBytecodeJson(){
String bytecodePath = basePath + "utils/bytecode.json";
FileReader bytecodePathReader = new FileReader(bytecodePath);
String bytecodeResult = bytecodePathReader.readString();
return bytecodeResult;
}
public static void genAbiAndBin(){
String abiJson = SolidityERC721Utils.getInstance().getAbiJson();
String bytecodeJson = SolidityERC721Utils.getInstance().getBytecodeJson();
String abiFileName = "SolidityERC721.abi";
String binFileName = "SolidityERC721.bin";
generateAbiAndBin(abiJson,bytecodeJson,abiFileName,binFileName);
}
public static void generateAbiAndBin(String abi,String bin,String abiFileName,String binFileName){
File abiFile = new File(basePath + "contract/"+abiFileName);
File binFile = new File(basePath + "contract/"+binFileName);
BufferedOutputStream abiBos = null;
BufferedOutputStream binBos = null;
try{
FileOutputStream abiFos = new FileOutputStream(abiFile);
FileOutputStream binFos = new FileOutputStream(binFile);
abiBos = new BufferedOutputStream(abiFos);
binBos = new BufferedOutputStream(binFos);
abiBos.write(abi.getBytes());
abiBos.flush();
binBos.write(bin.getBytes());
binBos.flush();
generateJavaFile(abiFile.getPath(),binFile.getPath());
}catch (Exception e){
e.printStackTrace();
}finally {
if(abiBos != null){
try{
abiBos.close();;
}catch (IOException e){
e.printStackTrace();
}
}
if(binBos != null){
try {
binBos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
public static void generateJavaFile(String abiFile, String binFile){
String generateFile = basePath + "/contract/";
generateClass(abiFile,binFile,generateFile);
}
public static void generateClass(String abiFile,String binFile,String generateFile){
String[] args = Arrays.asList(
"-a",abiFile,
"-b",binFile,
"-p","",
"-o",generateFile
).toArray(new String[0]);
Stream.of(args).forEach(System.out::println);
SolidityFunctionWrapperGenerator.main(args);
}
public static void main(String[] args) {
genAbiAndBin();
}
}
SolidityRequestService.java
package com.zscq.common.erc;
import com.zscq.common.erc.contract.SolidityERC721;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.RemoteFunctionCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.tx.RawTransactionManager;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.StaticGasProvider;
import java.math.BigInteger;
public class SolidityRequestService {
private static SolidityRequestService contractUtil = new SolidityRequestService();
private SolidityRequestService(){
}
public static SolidityRequestService getInstance() {
return contractUtil;
}
// 网络地址
private static String netWorkUrl = "HTTP://127.0.0.1:8545";
// 钱包私钥
private static String walletKey = "0bfcd46fd5a97d1c1a540889a808bb441f3d13c2fc6d3bd497e3c9e554472e2e";
// 合约地址
private static String contractAddress = "0xa549579E04Bfc4539AD2Ab23eC7d4588780c27a5";
public static void useContract(){
try {
//连接对应节点
Web3j web3 = Web3j.build(new HttpService(netWorkUrl));
Credentials credentials = Credentials.create(walletKey);
TransactionManager transactionManager = new RawTransactionManager(
web3, credentials,3);
BigInteger gasPrice = web3.ethGasPrice().send().getGasPrice();
SolidityERC721 contract = SolidityERC721.load(contractAddress,web3,
transactionManager,new StaticGasProvider(gasPrice, Contract.GAS_LIMIT));
// //调用合约方法
RemoteFunctionCall<String> setWord = contract.name();
String send = setWord.send();
System.out.println("名称:"+send);
// RemoteFunctionCall<BigInteger> bigIntegerRemoteFunctionCall = contract.balanceOf("0x657cF811dBB5Ebdd83ba0Aa616A5a097f3C5387E");
// BigInteger send1 = bigIntegerRemoteFunctionCall.send();
// System.out.println("余额:"+send1);
// RemoteCall<TransactionReceipt> setWord = contract.newGreeting("hello world");
// TransactionReceipt transactionReceipt = setWord.send();
// String transactionHash = transactionReceipt.getTransactionHash();
// System.out.println(transactionHash);
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
SolidityRequestService instance = SolidityRequestService.getInstance();
instance.useContract();
}
}