0

1D and 2D arrays generate the same code parameters, Obviously, the resulting code is incorrect.

function setOrders(Order[] calldata) external {}
function setOrderss(Order[][] calldata) external {}

Solidity code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TestArray {
    struct Order {
        address from;
        address to;
    }

    function setOrder(Order calldata) external {}

    function setOrders(Order[] calldata) external {}

    function setOrderss(Order[][] calldata) external {}

    // function get(uint256[][] calldata) external {}
}

use mvn web3j:generate-sources get java code

public RemoteFunctionCall<TransactionReceipt> setOrder(Order param0) {
    final Function function = new Function(
            FUNC_SETORDER, 
            Arrays.<Type>asList(param0), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<TransactionReceipt> setOrders(List<Order> param0) {
    final Function function = new Function(
            FUNC_SETORDERS, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicArray<Order>(Order.class, param0)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<TransactionReceipt> setOrderss(List<Order> param0) {
    final Function function = new Function(
            FUNC_SETORDERSS, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicArray<Order>(Order.class, param0)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}

How should I write this code?

public RemoteFunctionCall<TransactionReceipt> setOrderss(List<List<Order>> param0) {
    // code
}

I'm using solidity 0.8.17, web3j 4.10.0.

dbc
  • 104,963
  • 20
  • 228
  • 340
EthanOK
  • 1
  • 1

1 Answers1

-1

In web3j, which is a Java library for working with Ethereum, there is no direct representation of a two-dimensional array in the Ethereum Virtual Machine (EVM). However, you can achieve a similar functionality by using an array of arrays or a nested dynamic array structure.

Here's an example of how you can create and access a two-dimensional array-like structure using web3j:

import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.NumericType;
import org.web3j.abi.datatypes.generated.Uint256;

// Create a two-dimensional array-like structure
DynamicArray<DynamicArray<Uint256>> twoDimensionalArray = new DynamicArray<>(
        new DynamicArray<>(new Uint256(1), new Uint256(2)),
        new DynamicArray<>(new Uint256(3), new Uint256(4))
);

// Accessing values in the two-dimensional array
Uint256 value = twoDimensionalArray.getValue(1).getValue(0);
System.out.println(value.getValue()); // Output: 3

In the example above, we use the DynamicArray class from web3j to create a nested structure. Each element in the outer DynamicArray represents a row, and each element in the inner DynamicArray represents a value within that row.

To access the values in the two-dimensional array-like structure, you can use the getValue() method to retrieve the desired row and value based on their indices.

Keep in mind that the actual storage and handling of arrays in Ethereum smart contracts can differ based on how you design your contract. It's important to consider gas costs and efficiency when working with arrays in a decentralized environment.

I hope this helps you understand how to achieve a two-dimensional array-like structure using web3j! I have done this on my website apkbeyond. Let me know if you have any further questions.

  • This might be a good workaround solution, but I can't get it to work. throw: `Exception in thread "main" java.lang.UnsupportedOperationException: Unsupported type encountered: uint256[]` – EthanOK Jun 12 '23 at 08:49