合约功能没有问题,通过remix可以正常执行,但是Java调用的时候出现了invalid sender错误 1:合约部署在https://sepolia.etherscan.io/ ETH测试链上 这个测试链的ID我找的是:11155111 RPC URL是:https://eth.getblock.io/d83c4ddb-23e8-4487-b8d7-8de29c7a3f1b/sepolia/
2:合约地址:0x09466b1d1161c870C6E48791519ac1F20C8737Ee 出现的问题如下图:
Java代码如下:
package com.block.chain.utils;
import com.blockchain.tools.eth.codec.EthAbiCodecTool;
import com.blockchain.tools.eth.contract.util.EthContractUtil;
import com.blockchain.tools.eth.contract.util.model.SendResultModel;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Numeric;
import java.math.BigInteger;
import java.util.List;
public class ContractUtils {
private Web3j web3j;
public static void main(String args[]) throws Exception {
String privateKey = ""; // 私钥
Web3j web3j = Web3j.build(new HttpService("https://eth.getblock.io/d83c4ddb-23e8-4487-b8d7-8de29c7a3f1b/sepolia/")); // 链的RPC地址
String contractAddress = "0x09466b1d1161c870C6E48791519ac1F20C8737Ee";
String senderAddress = "0xb147533b5953EF2Ae351A1237Ab75279265db499";
SendResultModel sendResultModel = sendRawTransaction(
senderAddress, // 调用者的地址
contractAddress, // 合约地址
privateKey, // senderAddress的私钥
new BigInteger("3000000"), // gasPrice,如果想用默认值 可以直接传null,或者不传这个参数
new BigInteger("3000000"), // gasLimit,如果想用默认值 可以直接传null,或者不传这个参数
EthAbiCodecTool.getInputData(
"addAddress", // 要调用的方法名称
new Address("0x2D85B9F92a2774779C37c2f8b5E5c7367a36052f")
) // 要调用的方法的inputData
);
sendResultModel.getEthSendTransaction(); // 发送交易后的结果
sendResultModel.getEthGetTransactionReceipt(); // 交易成功上链后的结果
String s="";
}
private static SendResultModel sendRawTransaction(String fromAddress, String toAddress, String privateKey, BigInteger gasPrice, BigInteger gasLimit, String inputData) throws Exception {
Web3j web3j = Web3j.build(new HttpService("https://eth.getblock.io/d83c4ddb-23e8-4487-b8d7-8de29c7a3f1b/sepolia/")); // 链的RPC地址
EthGetTransactionCount ethGetTransactionCount = (EthGetTransactionCount)web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
if (gasPrice == null) {
gasPrice = ((EthGasPrice)web3j.ethGasPrice().send()).getGasPrice();
}
if (gasLimit == null) {
gasLimit = new BigInteger("8000000");
}
// RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, toAddress, inputData);
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, toAddress, inputData);
// 通过私钥创建credentials对象
Credentials credentials = Credentials.create(privateKey);
String send = credentials.getAddress();
int chainId = 11155111;
// 使用TransactionEncoder类的signMessage方法签名交易,并设置EIP155为true
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, (byte) chainId, credentials);
// byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, Credentials.create(privateKey));
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction ethSendTransaction = (EthSendTransaction)web3j.ethSendRawTransaction(hexValue).send();
if (ethSendTransaction.hasError()) {
throw new Exception(ethSendTransaction.getError().getMessage());
} else {
SendResultModel sendResultModel = new SendResultModel();
sendResultModel.setEthSendTransaction(ethSendTransaction);
for(int i = 0; i < 60; ++i) {
EthGetTransactionReceipt ethGetTransactionReceipt = (EthGetTransactionReceipt)web3j.ethGetTransactionReceipt(ethSendTransaction.getTransactionHash()).send();
sendResultModel.setEthGetTransactionReceipt(ethGetTransactionReceipt);
if (ethGetTransactionReceipt != null && ethGetTransactionReceipt.getResult() != null) {
break;
}
Thread.sleep(3000L);
}
return sendResultModel;
}
}}