Skip to content

Commit

Permalink
fix Dynamic Arrays encode
Browse files Browse the repository at this point in the history
  • Loading branch information
gtebrean committed Sep 12, 2023
1 parent e4e1dd7 commit fbc64f2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
14 changes: 8 additions & 6 deletions abi/src/main/java/org/web3j/abi/TypeEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static String removePadding(String encodedValue, Type parameter) {
* @return
*/
private static <T extends Type> String encodeStaticArrayWithDynamicStruct(Array<T> value) {
String valuesOffsets = encodeStructsArraysOffsets(value);
String valuesOffsets = encodeDynamicsTypesArraysOffsets(value);
String encodedValues = encodeArrayValues(value);

StringBuilder result = new StringBuilder();
Expand Down Expand Up @@ -334,6 +334,8 @@ private static <T extends Type> String encodeArrayValuesOffsets(DynamicArray<T>
!value.getValue().isEmpty() && value.getValue().get(0) instanceof Utf8String;
boolean arrayOfDynamicStructs =
!value.getValue().isEmpty() && value.getValue().get(0) instanceof DynamicStruct;
boolean arrayOfDynamicArrays =
!value.getValue().isEmpty() && value.getValue().get(0) instanceof DynamicArray;
if (arrayOfBytes || arrayOfString) {
long offset = 0;
for (int i = 0; i < value.getValue().size(); i++) {
Expand All @@ -353,20 +355,20 @@ private static <T extends Type> String encodeArrayValuesOffsets(DynamicArray<T>
Numeric.toBytesPadded(
new BigInteger(Long.toString(offset)), MAX_BYTE_LENGTH)));
}
} else if (arrayOfDynamicStructs) {
result.append(encodeStructsArraysOffsets(value));
} else if (arrayOfDynamicArrays || arrayOfDynamicStructs) {
result.append(encodeDynamicsTypesArraysOffsets(value));
}
return result.toString();
}

/**
* Encodes arrays of structs elements offsets. To be used when encoding a dynamic array or a
* static array containing dynamic structs,
* Encodes arrays of structs or dynamic arrays elements offsets. To be used when encoding a
* dynamic arrays or a static array containing dynamic structs,
*
* @param value DynamicArray or StaticArray containing dynamic structs
* @return encoded array offset
*/
private static <T extends Type> String encodeStructsArraysOffsets(Array<T> value) {
private static <T extends Type> String encodeDynamicsTypesArraysOffsets(Array<T> value) {
StringBuilder result = new StringBuilder();
long offset = value.getValue().size();
List<String> tailsEncoding =
Expand Down
31 changes: 31 additions & 0 deletions abi/src/test/java/org/web3j/abi/TypeEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.web3j.abi;

import java.math.BigInteger;
import java.util.Arrays;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -85,6 +86,7 @@
import org.web3j.abi.datatypes.generated.Uint24;
import org.web3j.abi.datatypes.generated.Uint240;
import org.web3j.abi.datatypes.generated.Uint248;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.generated.Uint32;
import org.web3j.abi.datatypes.generated.Uint40;
import org.web3j.abi.datatypes.generated.Uint48;
Expand Down Expand Up @@ -1217,6 +1219,35 @@ public void testDynamicStringsArray() {
TypeEncoder.encodeDynamicArray(array));
}

@Test
public void testDynamicArrayOfDynamicArraysOfStaticStructs() {
DynamicArray<DynamicArray<Bar>> array =
new DynamicArray(
DynamicArray.class,
Arrays.asList(
new DynamicArray(
Bar.class,
new Bar(
new Uint256(BigInteger.ZERO),
new Uint256(BigInteger.ZERO))),
new DynamicArray(
Bar.class,
new Bar(
new Uint256(BigInteger.ONE),
new Uint256(BigInteger.ZERO)))));
assertEquals(
("0000000000000000000000000000000000000000000000000000000000000002"
+ "0000000000000000000000000000000000000000000000000000000000000040"
+ "00000000000000000000000000000000000000000000000000000000000000a0"
+ "0000000000000000000000000000000000000000000000000000000000000001"
+ "0000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000001"
+ "0000000000000000000000000000000000000000000000000000000000000001"
+ "0000000000000000000000000000000000000000000000000000000000000000"),
TypeEncoder.encodeDynamicArray(array));
}

@Test
public void testStructsDynamicArray() {
DynamicArray<Foo> array =
Expand Down

0 comments on commit fbc64f2

Please sign in to comment.