web3j icon indicating copy to clipboard operation
web3j copied to clipboard

Issue when generate contract java file using web3j.codegen

Open thanghv97 opened this issue 2 years ago • 1 comments

Issue_title

Issue when generating contract java file using web3j.codegen

Issue_description

Contract file java generated by web3j.codegen error, cannot compile.

java: incompatible types: inference variable T has incompatible bounds
    equality constraints: java.util.List
    upper bounds: org.web3j.abi.datatypes.Type

Enviroment & Tool

ubuntu 20.04 solc version: 0.8.14+commit.80d49f37.Linux.g++ web3j: core + codegen: 4.8.7

                <dependency>
			<groupId>org.web3j</groupId>
			<artifactId>core</artifactId>
			<version>4.8.7</version>
		</dependency>
		<dependency>
			<groupId>org.web3j</groupId>
			<artifactId>codegen</artifactId>
			<version>4.8.7</version>
		</dependency>

Step

  1. I use solc to generate abi & bin file from contract.sol. I have a function like
    struct ContractInfo {
        string contractCode;
        uint256 quantity;
    }
  
    function getAllContract(address account) public override view returns (IBond.ContractInfo[] memory) {
        string[] memory contracts = allAccounts[account].contracts;
        IBond.ContractInfo[] memory data = new IBond.ContractInfo[](contracts.length);
        for (uint i = 0; i < contracts.length; i++) {
            data[i].contractCode = contracts[i];
            data[i].quantity = balanceByContractCode[account][contracts[i]];
        }
        return data;
    }
  1. Then I use a function from codegen to gen java file
        SolidityFunctionWrapperGenerator generator = new SolidityFunctionWrapperGenerator(
                new File(BIN_FILE), new File(ABI_FILE), new File(DEST_DIR), "Bond",
                "middleware.contract", false, true, 20);

        try {
            generator.generate();
        } catch (IOException e) {
            logger.info("IOException: {}", e.getMessage());
        } catch (ClassNotFoundException e) {
            logger.info("ClassNotFoundException: {}", e.getMessage());
        }
  1. The function generated for function getAllContract() in file java like
    public RemoteFunctionCall<List> getAllContract(Address account) {
        final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(FUNC_GETALLCONTRACT, 
                Arrays.<Type>asList(account), 
                Arrays.<TypeReference<?>>asList(new TypeReference<List<ContractInfo>>() {}));
        return executeRemoteCallSingleValueReturn(function);
    }

=> I found executeRemoteCallSingleValueReturn return <T extends Type> RemoteFunctionCall<T>, but List is a native type of Java, it's not extends from Type. `

thanghv97 avatar Jun 07 '22 09:06 thanghv97

I also encountered this problem. No one knows how to solve it

420154195 avatar Jul 26 '22 01:07 420154195