hyperledger-web3j/web3j-maven-plugin

Incorrect Return Type

Closed this issue · 12 comments

I am using Solc v0.6.9 and we I generate the Java code I am getting the incorrect return type for a few of my ERC20 smart contract functions (e.g., balanceOf).

ERC20 balanceOf function (Solidity code):

function balanceOf(address who) external view returns (uint256);

Java generated method (returns TransactionReceipt):

public RemoteFunctionCall<TransactionReceipt> ERC20BalanceOf(String _erc20Address, String _who) { final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function( FUNC_ERC20BALANCEOF, Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, _erc20Address), new org.web3j.abi.datatypes.Address(160, _who)), Collections.<TypeReference<?>>emptyList()); return executeRemoteCallTransaction(function); }

Expected Java generated function (returns BigInteger):

public RemoteFunctionCall<BigInteger> ERC20BalanceOf(String _erc20Address, String _who) { final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function( FUNC_ERC20BALANCEOF, Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, _erc20Address), new org.web3j.abi.datatypes.Address(160, _who)), Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {})); return executeRemoteCallSingleValueReturn(function, BigInteger.class); }

https://ethereum.stackexchange.com/questions/40730/why-is-web3j-java-not-generating-correct-return-types-for-my-contract

In the Stack Exchange example the function is giving the error because is not a view.

Not sure about it but Solidity 0.6.9 but must have a way to mark balanceOf as a view that has changed from prior versions.

Could you attach the generated ABI files?

ABI: Abi.txt

Take a look at ERC20BalanceOf() which has a view attribute on the function.

From solidity 0.6 the property constant included in the ABI has been replaced by the value of stateMutability

The version 4.5.11 of web3j-maven-plugin uses the value of constant to determine if a function is a view

You can wait to the version 4.5.18 of web3j-maven-plugin which probably will solve the problem or make a fork of web3j-maven-plugin and update the version of web3j in pom.xml to 4.5.18
Replace
<web3j.version>4.5.11</web3j.version>
With
<web3j.version>4.5.18</web3j.version>

When do you guys plan to release 4.5.18?

Hi, we just released v4.6.4, can I close this issue?

Absolutely, thank you! Also, do you know how to generate an Ethereum address from a xpub (extended public key) using Web3j?

final String address = org.web3j.crypto.Keys.getAddress("<public key>")

I guess my question is that does Keys.getAddress() support BIP32 or other standards?

Not sure about that, have you tried it? @josh-richardson may have a better answer.