Here is a very simple smart contract:
pragma solidity ^0.7.0;
contract Name {
string name = "Tom";
function getName() public view returns (string memory) {
return name;
}
}
Then I have converted this into a java file using web3j and the getName() function looks like this:
public RemoteCall<TransactionReceipt> getName() {
final Function function = new Function(
FUNC_GETNAME,
Arrays.<Type>asList(),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
I am a little bit confused about how to get the return value of getName() function.
I am not sure which version of web3j you are using. Currently I am using Web3j 1.4.1. When I created wrapper class it has different return type than what you mentioned.
public RemoteFunctionCall<String> getName() {
final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(FUNC_GETNAME,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}
Below is the code, I tried to work around. It is returning the name that is mentioned in the public variable.
nameContract = Name.deploy(this.web3j, this.credential, (new ContractGasProviderImplementations())).send();
String name = nameContract.getName().send();
System.out.println(name);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.