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

如何Function构建二维数组? #117

Open
qfengthree opened this issue Mar 11, 2024 · 5 comments
Open

如何Function构建二维数组? #117

qfengthree opened this issue Mar 11, 2024 · 5 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@qfengthree
Copy link

使用trident-java中

  1. Function1: methodName(address[] path) returns (uint256)
    可以用这样构建:
    DynamicArray dynamicArrayPath = new DynamicArray(Address.class, pathList);
  2. methodName2(address[][] path) returns (uint256)
    请教一下methodName2,如何构建二维数组?
@endiaoekoe
Copy link
Contributor

hi @qfengthree
you can try this, as below:

   ```
    // Construct a two-dimensional array of addresses
    Address[][] twoDimensionalPath = new Address[][] {
        {new Address("0x123"), new Address("0x456")},
        {new Address("0x789"), new Address("0xabc")}
    };

    // Building two-dimensional arrays with DynamicArray
    DynamicArray dynamicArrayPath = new DynamicArray(Address.class, twoDimensionalPath);

    // invoke methodName2 method
    uint256 result = methodName2(dynamicArrayPath);

@endiaoekoe
Copy link
Contributor

endiaoekoe commented Mar 11, 2024

@qfengthree
I just checked the source code of DynamicArray.java, only public constructors can be used,

  public DynamicArray(Class<T> type, List<T> values) {
        super(type, values);
    }

guess you want to construct a Function object, like this

final Function function =
              new Function(
                      "addParticipants",
                      Arrays.asList(
                              new DynamicArray<>(
                                      Bytes32.class, Utils.typeMap(participants, Bytes32.class))),
                      Collections.emptyList());

if you insist on using two-d arrays, I suggest you transform the 2-D arrays to a 2-D List Object, then pass to the constructor function.
which may be like this,

    int[][] twoDimensionalArray = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        List<List<Integer>> twoDimensionalList = Arrays.stream(twoDimensionalArray)
                .map(row -> Arrays.stream(row).boxed().collect(Collectors.toList()))
                .collect(Collectors.toList());

hope it helps.

@endiaoekoe
Copy link
Contributor

AbiTypes

that's right. currently, it can not use 2-D array as parameters when constructing function. you can only pass limited types, eg: Uint256, Address, and BigInteger.

@endiaoekoe
Copy link
Contributor

@qfengthree

line ’Utils.typeMap(paths, DynamicArray.class, Address.class)‘ which should return a list contains two DynamicArray object.
image

so

DynamicArray _versionLens = new DynamicArray<>(Uint256.class, Utils.typeMap(versionLens, DynamicArray.class, Uint256.class));
should be
DynamicArray _versionLens = new DynamicArray<>(DynamicArray.class, Utils.typeMap(versionLens, DynamicArray.class, Uint256.class));
same for other lines.

but I just debug your code in my local machine,when encode the DynamicArray(which contains a list have 2 DynamicArray object),form a nested loops。and the output is wrong and be different from your result running with js.

@qfengthree
Copy link
Author

已经解决了,参考同步更新web3j

@eodiandie eodiandie added enhancement New feature or request good first issue Good for newcomers labels Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants