我试图在Windows 10上部署一个非常简单的契约,使用Web3j 4.2.0。我用Mist创建了一个帐户。我已经在我的机器上设置了一个本地ethereum节点(使用geth),并且我能够使用eg部署和与契约交互。薄雾。但是当我一直在用Java代码做同样的事情时
org.web3j.tx.exceptions.ContractCallException: Empty value (0x) returned from contract
at org.web3j.tx.Contract.executeCallSingleValueReturn(Contract.java:250)
at org.web3j.tx.Contract.lambda$executeRemoteCallSingleValueReturn$1(Contract.java:317)
at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)这是我的稳固契约:
pragma solidity ^0.5.5;
contract SampleContract{
uint256 sampleVariable = 0;
function setSample(uint256 x) public{
sampleVariable = x;
}
function getSample() public view returns(uint256){
return sampleVariable;
}
}我使用solidity编译器和web3j CLI4.2.0创建了一个java包装类:
/**
* Auto generated code.
* Do not modify!
* Please use the web3j command line tools,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* codegen module to update.
*
* Generated with web3j version 4.2.0.
*/
public class SampleContract extends Contract {
private static final String BINARY = "60806040526000805534801561001457600080fd5b5060a2806100236000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c806331c8039b146037578063e221818b14604f575b600080fd5b603d606b565b60408051918252519081900360200190f35b606960048036036020811015606357600080fd5b50356071565b005b60005490565b60005556fea165627a7a72305820de9e3ced16dc30cb74a4215c04b4b6f5f61df43a4bc0c38fbbd23e798b6b805d0029";
public static final String FUNC_GETSAMPLE = "getSample";
public static final String FUNC_SETSAMPLE = "setSample";
@Deprecated
protected SampleContract(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}
protected SampleContract(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, credentials, contractGasProvider);
}
@Deprecated
protected SampleContract(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
protected SampleContract(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
}
public RemoteCall getSample() {
final Function function = new Function(FUNC_GETSAMPLE,
Arrays.asList(),
Arrays.>asList(new TypeReference() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
public RemoteCall setSample(BigInteger x) {
final Function function = new Function(
FUNC_SETSAMPLE,
Arrays.asList(new Uint256(x)),
Collections.>emptyList());
return executeRemoteCallTransaction(function);
}
@Deprecated
public static SampleContract load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new SampleContract(contractAddress, web3j, credentials, gasPrice, gasLimit);
}
@Deprecated
public static SampleContract load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new SampleContract(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
public static SampleContract load(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
return new SampleContract(contractAddress, web3j, credentials, contractGasProvider);
}
public static SampleContract load(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
return new SampleContract(contractAddress, web3j, transactionManager, contractGasProvider);
}
public static RemoteCall deploy(Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider) {
return deployRemoteCall(SampleContract.class, web3j, credentials, contractGasProvider, BINARY, "");
}
@Deprecated
public static RemoteCall deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(SampleContract.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}
public static RemoteCall deploy(Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) {
return deployRemoteCall(SampleContract.class, web3j, transactionManager, contractGasProvider, BINARY, "");
}
@Deprecated
public static RemoteCall deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(SampleContract.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
}
} 下面是创建合同的java代码:Credentials credentials = WalletUtils.loadCredentials(password,
keystorePath);
SampleContract sampleContract= SampleContract.deploy(web3j, credentials, new DefaultGasProvider()).send();
LOGGER.info("Contract deployed under address: " + sampleContract.getContractAddress());
LOGGER.info("Is contract valid : " + sampleContract.isValid());
BigInteger sampleNumber = sampleContract.getSample().send();我可以部署合同,得到它的地址,也可以检查这个合同是否有效返回真。但是,当我试图读取它(最后一行代码)时,它总是抛出org.web3j.tx.exceptions.ContractCallException.你知道这是什么原因吗?谢谢你帮忙。发布于 2019-04-15 10:25:38
使用负载(.)方法,然后调用getSample()
我现在不能测试它,但它应该是这样的
// your code for deploy and wait until it deployed
SampleContract myContract = SampleContract.load(, web3j, credentails, ......etc);
BigInteger sNumber = myContract.getSample().send();
//or BigInteger sNumber = myContract.getSample().sendAsync().get(); https://ethereum.stackexchange.com/questions/69654
复制相似问题