hyperledger-web3j/web3j

Generated Contract Error: Cannot cast from List to byte[]

Opened this issue · 0 comments

After generating a contract from the abi and bin the class has a casting error.

  • Web3j version: CLI 1.5.0, web3j 4.9.8
  • Java version: 17.0.9
  • Operating System: MacOS M2

The contract is probably trying to parse this solidity event:

event GameEnded(bytes32 indexed gameId, bytes32[] indexed winners, uint256 time);

And it is having an hard time to convert the array of bytes32.
This is the code where the error:

Cannot cast from List to byte[]Java(16777372)
List org.web3j.abi.datatypes.Array.getNativeValueCopy()

occurs at lines:

typedResponse.winners = (byte[]) ((Array) eventValues.getIndexedValues().get(1)).getNativeValueCopy();

and

typedResponse.winners = (byte[]) ((Array) eventValues.getIndexedValues().get(1)).getNativeValueCopy();
    public static List<GameEndedEventResponse> getGameEndedEvents(TransactionReceipt transactionReceipt) {
        List<Contract.EventValuesWithLog> valueList = staticExtractEventParametersWithLog(GAMEENDED_EVENT,
                transactionReceipt);
        ArrayList<GameEndedEventResponse> responses = new ArrayList<GameEndedEventResponse>(valueList.size());
        for (Contract.EventValuesWithLog eventValues : valueList) {
            GameEndedEventResponse typedResponse = new GameEndedEventResponse();
            typedResponse.log = eventValues.getLog();
            typedResponse.gameId = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.winners = (byte[]) ((Array) eventValues.getIndexedValues().get(1)).getNativeValueCopy();

            typedResponse.time = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
            responses.add(typedResponse);
        }
        return responses;
    }
    public static List<GameEndedEventResponse> getGameEndedEvents(TransactionReceipt transactionReceipt) {
        List<Contract.EventValuesWithLog> valueList = staticExtractEventParametersWithLog(GAMEENDED_EVENT,
                transactionReceipt);
        ArrayList<GameEndedEventResponse> responses = new ArrayList<GameEndedEventResponse>(valueList.size());
        for (Contract.EventValuesWithLog eventValues : valueList) {
            GameEndedEventResponse typedResponse = new GameEndedEventResponse();
            typedResponse.log = eventValues.getLog();
            typedResponse.gameId = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.winners = (byte[]) ((Array) eventValues.getIndexedValues().get(1)).getNativeValueCopy();

            typedResponse.time = (BigInteger) eventValues.getNonIndexedValues().get(0).getValue();
            responses.add(typedResponse);
        }
        return responses;
    }

it can be solved by writing it like the byte32 gameId event but I think that it get only one value of the array.

typedResponse.winners = (byte[]) eventValues.getIndexedValues().get(1).getValue();

Hope this is just a quick simple error, thank you for your time.