Hi,When i use java build contract on Windows 10, web3j 3.2.0. It's response me Empty value (0x) returned from contract.But i got the contract address and credentials.
The step is :
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
## the contract is :
`
pragma solidity ^0.4.19;
contract HelloWorld{
address creator;
string greeting;
function HelloWorld(string _greeting) public{
creator = msg.sender;
greeting = _greeting;
}
function greet() constant returns (string){
return greeting;
}
function setGreeting(string _newgreeting){
greeting = _newgreeting;
}
function kill(){
if (msg.sender == creator)
suicide(creator); // kills this contract and sends remaining funds back to creator
}
}
`
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
/**
Auto generated code.
Do not modify!
Please use the web3j command line tools,
Generated with web3j version 3.2.0.
*/
public class HelloWorld extends Contract {
private static final String BINARY = "6060604052341561000f57600080fd5b6040516104d03803806104d083398101604052808051820191905050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610081929190610088565b505061012d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c957805160ff19168380011785556100f7565b828001600101855582156100f7579182015b828111156100f65782518255916020019190600101906100db565b5b5090506101049190610108565b5090565b61012a91905b8082111561012657600081600090555060010161010e565b5090565b90565b6103948061013c6000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b51461005c578063a413686214610071578063cfae3217146100ce575b600080fd5b341561006757600080fd5b61006f61015c565b005b341561007c57600080fd5b6100cc600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101ed565b005b34156100d957600080fd5b6100e1610207565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610121578082015181840152602081019050610106565b50505050905090810190601f16801561014e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156101eb576000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b565b80600190805190602001906102039291906102af565b5050565b61020f61032f565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102a55780601f1061027a576101008083540402835291602001916102a5565b820191906000526020600020905b81548152906001019060200180831161028857829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102f057805160ff191683800117855561031e565b8280016001018555821561031e579182015b8281111561031d578251825591602001919060010190610302565b5b50905061032b9190610343565b5090565b602060405190810160405280600081525090565b61036591905b80821115610361576000816000905550600101610349565b5090565b905600a165627a7a7230582054721911b522185a0d4c0bcf50b95120f763f5c87f5c55c82b7da738247863ac0029\rn";
protected HelloWorld(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}
protected HelloWorld(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
public RemoteCall<TransactionReceipt> kill() {
Function function = new Function(
"kill",
Arrays.<Type>asList(),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCall<TransactionReceipt> setGreeting(String _newgreeting) {
Function function = new Function(
"setGreeting",
Arrays.<Type>asList(new Utf8String(_newgreeting)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCall<String> greet() {
Function function = new Function("greet",
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}
public static RemoteCall<HelloWorld> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String _greeting) {
String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new Utf8String(_greeting)));
return deployRemoteCall(HelloWorld.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor);
}
public static RemoteCall<HelloWorld> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String _greeting) {
String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new Utf8String(_greeting)));
return deployRemoteCall(HelloWorld.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor);
}
public static HelloWorld load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new HelloWorld(contractAddress, web3j, credentials, gasPrice, gasLimit);
}
public static HelloWorld load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new HelloWorld(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
}
`//1.get credentials
Credentials credentials=WalletUtils.loadCredentials("123",walletPath);
//2.build contract
HelloWorld contract=HelloWorld.deploy(web3,credentials, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT,"ni hao").send();
//3.get contract address
String contractAddress=contract.getContractAddress();
System.out.println("contract address is :"+contractAddress);
//5.call the contract
TransactionReceipt transactionReceipt=contract.setGreeting("hello1").send();
System.out.println("set:"+transactionReceipt.getTransactionHash());
System.out.println("contract is valid:"+contract.isValid());
String result=contract.greet().send();
System.out.println("result :"+result);
`
contract address is :0x78d12ebe81703c347d3a03b66601c7180fcc5c2c
set:0xc9cd2ef57752eacde42d78fc9a30201c09220a2a4dc1debe5a2f1f0987adfea3
contract is valid::false
Exception in thread "main" org.web3j.tx.exceptions.ContractCallException: Empty value (0x) returned from contract
at org.web3j.tx.Contract.executeCallSingleValueReturn(Contract.java:178)
at org.web3j.tx.Contract.lambda$executeRemoteCallSingleValueReturn$1(Contract.java:232)
at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
at Test.BuildContract(Test.java:136)
at Test.main(Test.java:47)
And the line 136 is :
String result=contract.greet().send();
Thank you!!
Maybe wait until the contract is indeed valid?
@fcorneli what's the problem with it ? I have waitted for 10+ blocks.
the problem has been solved。 I built a wrong contract。
Web3j 3.4.0
public void deploy() throws Exception
{
log.info("Deploying smart contract");
String greetMessage = "Hi this is a greeter app";
gasPrice=BigInteger.valueOf(100);
this.contract = Greeter.deploy(web3j, credentials, gasPrice, gasLimit, greetMessage).send();
log.info("contract deployed " );
String gr = this.contract.greet().send();
}
works fine.
however
log.info("loading contract");
this.contract = (Greeter) Greeter.load(
this.contactAddress, web3j, credentials, gasPrice, gasLimit);
String gr = this.contract.greet().send();
always return the above error.
Eventhough the contract binary returned from both contract are the same.
This is reproducible from many ethereum networks
this.contract = (Greeter) Greeter.load(
this.contractAddress, web3jAdmin, credentials, gasPrice, gasLimit);
log.info("isContractValid=" + contract.isValid());
while the contract returned from deploy worked just fine, the loaded contract always has contract.isValid() false,
isContractValid=false
the
String gr = this.contract.greet().send();
always through the following exceptions:
I have tried a variety of ethereum testnet and local private networks, single or multiple.
It must be a bug. Please fix it.
Exception in thread "main" org.web3j.tx.exceptions.ContractCallException: Empty value (0x) returned from contract
at org.web3j.tx.Contract.executeCallSingleValueReturn(Contract.java:230)
at org.web3j.tx.Contract.lambda$executeRemoteCallSingleValueReturn$1(Contract.java:297)
at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
i got the same error with glomarch
Don't know if it can help out, but I had the same issue and in my wrapped compiled solidity script, it was :
private static final String BINARY = "[{\"constant\":true,\"inputs\":[],\"name\":\"chars\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}, ( ... )se,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]";
instead of :
private static final String BINARY = "608060405234801561001057600080fd5b5060008054600160a060020a0319163317815560015561027f806100356000 (...) ffffffffffffffffffff163314156102515760005473ffffffffffffffffffffffffffffffffffffffff16ff5b5600a165627a7a72305820545e60010ee0d4b8f1315f9743499c370f058e1ab280aab90d9795cd074ff9e60029";
Which means, that web3j or solidity compiler does not correctly do its job.
Personal issue, but it was not obvious.
Don't know if it can help out, but I had the same issue and in my wrapped compiled solidity script, it was :
private static final String BINARY = "[{\"constant\":true,\"inputs\":[],\"name\":\"chars\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}, ( ... )se,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]";instead of :
private static final String BINARY = "608060405234801561001057600080fd5b5060008054600160a060020a0319163317815560015561027f806100356000 (...) ffffffffffffffffffff163314156102515760005473ffffffffffffffffffffffffffffffffffffffff16ff5b5600a165627a7a72305820545e60010ee0d4b8f1315f9743499c370f058e1ab280aab90d9795cd074ff9e60029";Which means, that web3j or solidity compiler does not correctly do its job.
Personal issue, but it was not obvious.
I had the same problem with wrong binary string.
Contract can deploy successfully in remix,but it can not be deploy with web3j.Finally,I found the binary string was wrong ,if I get .bin file by solcjs . I don't not why if I choose to get .bin from remix ,that is right.
I got the same error. In my case it was about issues of Gas Price and Gas Limit.