Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when pass empty list to the smart contract #733

Closed
Hirama opened this issue Oct 10, 2018 · 2 comments
Closed

Exception when pass empty list to the smart contract #733

Hirama opened this issue Oct 10, 2018 · 2 comments
Assignees
Labels
bug A bug in behaviour or functionality
Milestone

Comments

@Hirama
Copy link

Hirama commented Oct 10, 2018

Contract with function addAddresses(address point, address[] list) external;

When I try to execute contract method.

public RemoteCall<TransactionReceipt> addAddresses(String point, List<String> list) {
        final Function function = new Function(
                FUNC_ADDADDRESSES, 
                Arrays.<Type>asList(new Address(point),
                new DynamicArray<Address>(
                        org.web3j.abi.Utils.typeMap(list, Address.class))),
                Collections.<TypeReference<?>>emptyList());
        return executeRemoteCallTransaction(function);
    }

I got exception java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

image
Problem is in this part of the code when trying to get the type from the first element of the empty list.

@MarkOSullivan94
Copy link
Contributor

@Hirama curious what you are wanting done here

@iikirilov iikirilov added the bug A bug in behaviour or functionality label Nov 8, 2018
@iikirilov
Copy link
Contributor

iikirilov commented Nov 9, 2018

Considering the Arrays.sol contract in web3j-codegen there is a method:

    function dynamicReverse(uint[] input) public pure returns (uint[] result) {
        uint length = input.length;
        result = new uint[](length);

        for (uint i = 0; i < length; i++) {
            result[i] = input[length - (i + 1)];
        }
        return result;
    }

The web3j generator will generate:

    public RemoteCall<List> dynamicReverse(List<BigInteger> input) {
        final Function function = new Function(FUNC_DYNAMICREVERSE, 
                java.util.Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.generated.Uint256>(
                        org.web3j.abi.Utils.typeMap(input, org.web3j.abi.datatypes.generated.Uint256.class))), 
                java.util.Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Uint256>>() {}));
        return new RemoteCall<List>(
                new Callable<List>() {
                    @Override
                    @SuppressWarnings("unchecked")
                    public List call() throws Exception {
                        List<Type> result = (List<Type>) executeCallSingleValueReturn(function, List.class);
                        return convertToNative(result);
                    }
                });
    }

If one was to call arrays.dynamicReverse(new Arraylist<BigInteger>()) web3j will complain.
The DynamicArray constructor is called:

    public DynamicArray(List<T> values) {
        super(values.get(0).getTypeAsString() + "[]", values);
    }

As the list is empty we should get the type of the list via reflection?

@snazha-blkio snazha-blkio added this to the 4.1.x milestone Jan 18, 2019
@snazha-blkio snazha-blkio added awaiting-release On release branch, awaiting build and removed awaiting-release On release branch, awaiting build labels Jan 18, 2019
snazha-blkio added a commit that referenced this issue Feb 10, 2019
Fix for isse #733 Add explicit component type in constructors
snazha-blkio added a commit that referenced this issue Mar 7, 2019
snazha-blkio added a commit that referenced this issue Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug in behaviour or functionality
Projects
None yet
Development

No branches or pull requests

6 participants