web3j调用智能合约是web3j使用的关键,当前区块链的核心
其实原理很简单
https://github.com/OpenZeppelin/openzeppelin-contracts
这是智能合约优秀的框架,经过了无数安全的效验,目前绝大多数项目引用此相关依赖。
有些函数是固定写法例如claim,deposit,approve等等。
目前web3j使用org.web3j.abi.datatypes.Function , org.web3j.abi.TypeReference,两个核心类将智能合约的方法和参数构造,再通过org.web3j.abi.FunctionEncoder,FunctionEncoder.encode()格式化后Transaction指令就可以了。
package com.we3j.demo.wallet;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.FunctionReturnDecoder;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.protocol.Web3j;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.*;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthCall;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
* @Author jambestwick
* @create 2021/12/5 0005 21:04
* @email jambestwick@126.com
* <p>
* ERC20的代币
*/
public class TokenClient {
private static String emptyAddress = "0x0000000000000000000000000000000000000000";
/**
* 查询代币发行总量
*
* @param web3j
* @param contractAddress
* @return
*/
public static BigInteger getTokenTotalSupply(Web3j web3j, String contractAddress) {
if (web3j == null) return null;
String methodName = "totalSupply";
String fromAddr = emptyAddress;
BigInteger totalSupply = BigInteger.ZERO;
List<Type> inputParameters = new ArrayList<>();
List<TypeReference<?>> outputParameters = new ArrayList<>();
TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {
};
outputParameters.add(typeReference);
Function function = new Function(methodName, inputParameters, outputParameters);
String data = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
EthCall ethCall;
try {
ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
totalSupply = (BigInteger) results.get(0).getValue();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return totalSupply;
}
/****
* 调用claim方法抢NFT Token
* ***/
public static boolean claimNFT(Web3j web3j, String contractAddress, Integer tokenId) {
if (web3j == null) return false;
String methodName = "claim";
String fromAddr = emptyAddress;
Function function = new Function(
methodName,
Arrays.asList(new Uint256(tokenId)),
Arrays.asList(new TypeReference<Type>() {
}));
String data = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
EthCall ethCall;
try {
ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
//List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return true;
}
}
我么以查看Loot的总供应量为例,演示一遍。Loot合约地址:0xff9c1b15b16263c61d017ee9f65c50e4ae0113d7
总数7779与ethscan一致,Ok。
项目github地址
https://github.com/jambestwick/we3jdemo