org.web3j.abi.datatypes.StaticArray Java Examples

The following examples show how to use org.web3j.abi.datatypes.StaticArray. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UtilsTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetTypeName() throws ClassNotFoundException {
    assertThat(Utils.getTypeName(new TypeReference<Uint>(){}), is("uint256"));
    assertThat(Utils.getTypeName(new TypeReference<Int>(){}), is("int256"));
    assertThat(Utils.getTypeName(new TypeReference<Ufixed>(){}), is("ufixed256"));
    assertThat(Utils.getTypeName(new TypeReference<Fixed>(){}), is("fixed256"));

    assertThat(Utils.getTypeName(new TypeReference<Uint64>(){}), is("uint64"));
    assertThat(Utils.getTypeName(new TypeReference<Int64>(){}), is("int64"));
    assertThat(Utils.getTypeName(new TypeReference<Bool>(){}), is("bool"));
    assertThat(Utils.getTypeName(new TypeReference<Utf8String>(){}), is("string"));
    assertThat(Utils.getTypeName(new TypeReference<DynamicBytes>(){}), is("bytes"));

    assertThat(Utils.getTypeName(
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5){}),
            is("uint256[5]"));
    assertThat(Utils.getTypeName(
            new TypeReference<DynamicArray<Uint>>(){}),
            is("uint256[]"));
}
 
Example #2
Source File: UtilsTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTypeName() {
    assertEquals(Utils.getTypeName(new TypeReference<Uint>() {}), ("uint256"));
    assertEquals(Utils.getTypeName(new TypeReference<Int>() {}), ("int256"));
    assertEquals(Utils.getTypeName(new TypeReference<Ufixed>() {}), ("ufixed256"));
    assertEquals(Utils.getTypeName(new TypeReference<Fixed>() {}), ("fixed256"));

    assertEquals(Utils.getTypeName(new TypeReference<Uint64>() {}), ("uint64"));
    assertEquals(Utils.getTypeName(new TypeReference<Int64>() {}), ("int64"));
    assertEquals(Utils.getTypeName(new TypeReference<Bool>() {}), ("bool"));
    assertEquals(Utils.getTypeName(new TypeReference<Utf8String>() {}), ("string"));
    assertEquals(Utils.getTypeName(new TypeReference<DynamicBytes>() {}), ("bytes"));

    assertEquals(
            Utils.getTypeName(
                    new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5) {}),
            ("uint256[5]"));
    assertEquals(Utils.getTypeName(new TypeReference<DynamicArray<Uint>>() {}), ("uint256[]"));
}
 
Example #3
Source File: TypeEncoder.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static String encode(Type parameter) {
    if (parameter instanceof NumericType) {
        return encodeNumeric(((NumericType) parameter));
    } else if (parameter instanceof Address) {
        return encodeAddress((Address) parameter);
    } else if (parameter instanceof Bool) {
        return encodeBool((Bool) parameter);
    } else if (parameter instanceof Bytes) {
        return encodeBytes((Bytes) parameter);
    } else if (parameter instanceof DynamicBytes) {
        return encodeDynamicBytes((DynamicBytes) parameter);
    } else if (parameter instanceof Utf8String) {
        return encodeString((Utf8String) parameter);
    } else if (parameter instanceof StaticArray) {
        return encodeArrayValues((StaticArray) parameter);
    } else if (parameter instanceof DynamicArray) {
        return encodeDynamicArray((DynamicArray) parameter);
    } else {
        throw new UnsupportedOperationException(
                "Type cannot be encoded: " + parameter.getClass());
    }
}
 
Example #4
Source File: FunctionReturnDecoderTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testDecodeStaticArrayValue() {
    List<TypeReference<Type>> outputParameters = new ArrayList<>(1);
    outputParameters.add(
            (TypeReference)
                    new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(2) {});
    outputParameters.add((TypeReference) new TypeReference<Uint256>() {});

    List<Type> decoded =
            FunctionReturnDecoder.decode(
                    "0x0000000000000000000000000000000000000000000000000000000000000037"
                            + "0000000000000000000000000000000000000000000000000000000000000001"
                            + "000000000000000000000000000000000000000000000000000000000000000a",
                    outputParameters);

    StaticArray2<Uint256> uint256StaticArray2 =
            new StaticArray2<>(
                    new Uint256(BigInteger.valueOf(55)), new Uint256(BigInteger.ONE));

    List<Type> expected = Arrays.asList(uint256StaticArray2, new Uint256(BigInteger.TEN));
    assertEquals(decoded, (expected));
}
 
Example #5
Source File: TypeDecoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticArray() {
    assertThat(TypeDecoder.decodeStaticArray(
            "000000000000000000000000000000000000000000000000000000000000000a"
            + "0000000000000000000000000000000000000000000000007fffffffffffffff",
            0,
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(2) {},
            2),
            is(new StaticArray<Uint256>(
                    new Uint256(BigInteger.TEN),
                    new Uint256(BigInteger.valueOf(Long.MAX_VALUE)))));

    assertThat(TypeDecoder.decodeStaticArray(
                    "000000000000000000000000000000000000000000000000000000000000000d"
                    + "48656c6c6f2c20776f726c642100000000000000000000000000000000000000"
                    + "000000000000000000000000000000000000000000000000000000000000000d"
                    + "776f726c64212048656c6c6f2c00000000000000000000000000000000000000",
            0,
            new TypeReference.StaticArrayTypeReference<StaticArray<Utf8String>>(2){},
            2
            ),
            equalTo(new StaticArray<Utf8String>(
                    new Utf8String("Hello, world!"),
                    new Utf8String("world! Hello,"))));
}
 
Example #6
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testDecodeStaticArrayValue() {
    List<TypeReference<Type>> outputParameters = new ArrayList<>(1);
    outputParameters.add((TypeReference)
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(2) {});
    outputParameters.add((TypeReference) new TypeReference<Uint256>() {});


    List<Type> decoded = FunctionReturnDecoder.decode(
            "0x0000000000000000000000000000000000000000000000000000000000000037"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "000000000000000000000000000000000000000000000000000000000000000a",
            outputParameters);

    List<Type> expected = Arrays.asList(
            new StaticArray<>(new Uint256(BigInteger.valueOf(55)), new Uint256(BigInteger.ONE)),
            new Uint256(BigInteger.TEN));
    assertThat(decoded, equalTo(expected));
}
 
Example #7
Source File: FunctionReturnDecoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testDecodeStaticArrayValue() {
    List<TypeReference<Type>> outputParameters = new ArrayList<>(1);
    outputParameters.add((TypeReference)
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(2) {});
    outputParameters.add((TypeReference) new TypeReference<Uint256>() {});


    List<Type> decoded = FunctionReturnDecoder.decode(
            "0x0000000000000000000000000000000000000000000000000000000000000037"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "000000000000000000000000000000000000000000000000000000000000000a",
            outputParameters);

    List<Type> expected = Arrays.asList(
            new StaticArray<>(new Uint256(BigInteger.valueOf(55)), new Uint256(BigInteger.ONE)),
            new Uint256(BigInteger.TEN));
    assertThat(decoded, equalTo(expected));
}
 
Example #8
Source File: UtilsTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTypeName() throws ClassNotFoundException {
    assertThat(Utils.getTypeName(new TypeReference<Uint>(){}), is("uint256"));
    assertThat(Utils.getTypeName(new TypeReference<Int>(){}), is("int256"));
    assertThat(Utils.getTypeName(new TypeReference<Ufixed>(){}), is("ufixed256"));
    assertThat(Utils.getTypeName(new TypeReference<Fixed>(){}), is("fixed256"));

    assertThat(Utils.getTypeName(new TypeReference<Uint64>(){}), is("uint64"));
    assertThat(Utils.getTypeName(new TypeReference<Int64>(){}), is("int64"));
    assertThat(Utils.getTypeName(new TypeReference<Bool>(){}), is("bool"));
    assertThat(Utils.getTypeName(new TypeReference<Utf8String>(){}), is("string"));
    assertThat(Utils.getTypeName(new TypeReference<DynamicBytes>(){}), is("bytes"));

    assertThat(Utils.getTypeName(
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5){}),
            is("uint256[5]"));
    assertThat(Utils.getTypeName(
            new TypeReference<DynamicArray<Uint>>(){}),
            is("uint256[]"));
}
 
Example #9
Source File: TypeDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testStaticArray() {
    assertThat(TypeDecoder.decodeStaticArray(
            "000000000000000000000000000000000000000000000000000000000000000a"
            + "0000000000000000000000000000000000000000000000007fffffffffffffff",
            0,
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(2) {},
            2),
            is(new StaticArray<Uint256>(
                    new Uint256(BigInteger.TEN),
                    new Uint256(BigInteger.valueOf(Long.MAX_VALUE)))));

    assertThat(TypeDecoder.decodeStaticArray(
                    "000000000000000000000000000000000000000000000000000000000000000d"
                    + "48656c6c6f2c20776f726c642100000000000000000000000000000000000000"
                    + "000000000000000000000000000000000000000000000000000000000000000d"
                    + "776f726c64212048656c6c6f2c00000000000000000000000000000000000000",
            0,
            new TypeReference.StaticArrayTypeReference<StaticArray<Utf8String>>(2){},
            2
            ),
            equalTo(new StaticArray<Utf8String>(
                    new Utf8String("Hello, world!"),
                    new Utf8String("world! Hello,"))));
}
 
Example #10
Source File: TypeEncoder.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static String encode(Type parameter) {
    if (parameter instanceof NumericType) {
        return encodeNumeric(((NumericType) parameter));
    } else if (parameter instanceof Address) {
        return encodeAddress((Address) parameter);
    } else if (parameter instanceof Bool) {
        return encodeBool((Bool) parameter);
    } else if (parameter instanceof Bytes) {
        return encodeBytes((Bytes) parameter);
    } else if (parameter instanceof DynamicBytes) {
        return encodeDynamicBytes((DynamicBytes) parameter);
    } else if (parameter instanceof Utf8String) {
        return encodeString((Utf8String) parameter);
    } else if (parameter instanceof StaticArray) {
        return encodeArrayValues((StaticArray) parameter);
    } else if (parameter instanceof DynamicArray) {
        return encodeDynamicArray((DynamicArray) parameter);
    } else {
        throw new UnsupportedOperationException(
                "Type cannot be encoded: " + parameter.getClass());
    }
}
 
Example #11
Source File: TypeEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static String encode(Type parameter) {
    if (parameter instanceof NumericType) {
        return encodeNumeric(((NumericType) parameter));
    } else if (parameter instanceof Address) {
        return encodeAddress((Address) parameter);
    } else if (parameter instanceof Bool) {
        return encodeBool((Bool) parameter);
    } else if (parameter instanceof Bytes) {
        return encodeBytes((Bytes) parameter);
    } else if (parameter instanceof DynamicBytes) {
        return encodeDynamicBytes((DynamicBytes) parameter);
    } else if (parameter instanceof Utf8String) {
        return encodeString((Utf8String) parameter);
    } else if (parameter instanceof StaticArray) {
        return encodeArrayValues((StaticArray) parameter);
    } else if (parameter instanceof DynamicStruct) {
        return encodeDynamicStruct((DynamicStruct) parameter);
    } else if (parameter instanceof DynamicArray) {
        return encodeDynamicArray((DynamicArray) parameter);
    } else if (parameter instanceof PrimitiveType) {
        return encode(((PrimitiveType) parameter).toSolidityType());
    } else {
        throw new UnsupportedOperationException(
                "Type cannot be encoded: " + parameter.getClass());
    }
}
 
Example #12
Source File: AbiTypesGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private <T extends Type> void generateStaticArrayTypes(
        Class<T> superclass, String path) throws IOException {
    String packageName = createPackageName(superclass);
    ClassName className;

    for (int length = 1; length <= StaticArray.MAX_SIZE_OF_STATIC_ARRAY; length++) {

        TypeVariableName typeVariableName = TypeVariableName.get("T").withBounds(Type.class);

        MethodSpec constructorSpec = MethodSpec.constructorBuilder()
                .addModifiers(Modifier.PUBLIC)
                .addParameter(ParameterizedTypeName.get(ClassName.get(List.class),
                        typeVariableName), "values")
                .addStatement("super($L, $N)", length, "values")
                .build();

        MethodSpec arrayOverloadConstructorSpec = MethodSpec.constructorBuilder()
                .addAnnotation(SafeVarargs.class)
                .addModifiers(Modifier.PUBLIC)
                .addParameter(ArrayTypeName.of(typeVariableName), "values")
                .varargs()
                .addStatement("super($L, $N)", length, "values")
                .build();

        className = ClassName.get(packageName, superclass.getSimpleName() + length);

        TypeSpec bytesType = TypeSpec
                .classBuilder(className.simpleName())
                .addTypeVariable(typeVariableName)
                .addJavadoc(CODEGEN_WARNING)
                .superclass(ParameterizedTypeName.get(ClassName.get(superclass),
                        typeVariableName))
                .addModifiers(Modifier.PUBLIC)
                .addMethods(Arrays.asList(constructorSpec, arrayOverloadConstructorSpec))
                .build();

        write(packageName, bytesType, path);
    }
}
 
Example #13
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private static Class<?> getStaticArrayTypeReferenceClass(String type) {
    try {
        return Class.forName("org.web3j.abi.datatypes.generated.StaticArray" + type);
    } catch (ClassNotFoundException e) {
        // Unfortunately we can't encode it's length as a type if it's > 32.
        return StaticArray.class;
    }
}
 
Example #14
Source File: SolidityFunctionWrapperTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testBuildTypeName() {
    assertThat(buildTypeName("uint256"),
            is(ClassName.get(Uint256.class)));
    assertThat(buildTypeName("uint64"),
            is(ClassName.get(Uint64.class)));
    assertThat(buildTypeName("string"),
            is(ClassName.get(Utf8String.class)));

    assertThat(buildTypeName("uint256[]"),
            is(ParameterizedTypeName.get(DynamicArray.class, Uint256.class)));

    assertThat(buildTypeName("uint256[] storage"),
            is(ParameterizedTypeName.get(DynamicArray.class, Uint256.class)));

    assertThat(buildTypeName("uint256[] memory"),
            is(ParameterizedTypeName.get(DynamicArray.class, Uint256.class)));

    assertThat(buildTypeName("uint256[10]"),
            is(ParameterizedTypeName.get(StaticArray10.class, Uint256.class)));

    assertThat(buildTypeName("uint256[33]"),
            is(ParameterizedTypeName.get(StaticArray.class, Uint256.class)));

    assertThat(buildTypeName("uint256[10][3]"),
            is(ParameterizedTypeName.get(ClassName.get(StaticArray3.class),
                    ParameterizedTypeName.get(StaticArray10.class, Uint256.class))));

    assertThat(buildTypeName("uint256[2][]"),
            is(ParameterizedTypeName.get(ClassName.get(DynamicArray.class),
                    ParameterizedTypeName.get(StaticArray2.class, Uint256.class))));

    assertThat(buildTypeName("uint256[33][]"),
            is(ParameterizedTypeName.get(ClassName.get(DynamicArray.class),
                    ParameterizedTypeName.get(StaticArray.class, Uint256.class))));

    assertThat(buildTypeName("uint256[][]"),
            is(ParameterizedTypeName.get(ClassName.get(DynamicArray.class),
                    ParameterizedTypeName.get(DynamicArray.class, Uint256.class))));
}
 
Example #15
Source File: DefaultFunctionEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static int getLength(final List<Type> parameters) {
    int count = 0;
    for (final Type type : parameters) {
        if (type instanceof StaticArray) {
            count += ((StaticArray) type).getValue().size();
        } else {
            count++;
        }
    }
    return count;
}
 
Example #16
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static <T extends Array> T decode(
        String input, int offset, TypeReference<T> typeReference) {
    Class cls = ((ParameterizedType) typeReference.getType()).getRawType().getClass();
    if (StaticArray.class.isAssignableFrom(cls)) {
        return decodeStaticArray(input, offset, typeReference, 1);
    } else if (DynamicArray.class.isAssignableFrom(cls)) {
        return decodeDynamicArray(input, offset, typeReference);
    } else {
        throw new UnsupportedOperationException(
                "Unsupported TypeReference: "
                        + cls.getName()
                        + ", only Array types can be passed as TypeReferences");
    }
}
 
Example #17
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends Type> T instantiateStaticArray(
        TypeReference<T> typeReference, List<T> elements, int length) {
    try {
        Class<? extends StaticArray> arrayClass =
                (Class<? extends StaticArray>)
                        Class.forName("org.web3j.abi.datatypes.generated.StaticArray" + length);

        return (T) arrayClass.getConstructor(List.class).newInstance(elements);
    } catch (ReflectiveOperationException e) {
        throw new UnsupportedOperationException(e);
    }
}
 
Example #18
Source File: TypeDecoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends Type> T instantiateStaticArray(
        TypeReference<T> typeReference, List<T> elements) {
    try {
        Class<List> listClass = List.class;
        return typeReference.getClassType().getConstructor(listClass).newInstance(elements);
    } catch (ReflectiveOperationException e) {
        //noinspection unchecked
        return (T) new StaticArray<>(elements);
    }
}
 
Example #19
Source File: TypeEncoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFixedArray() {
    StaticArray<Ufixed> array =
            new StaticArray2<>(
                    Ufixed.class,
                    new Ufixed(BigInteger.valueOf(0x2), BigInteger.valueOf(0x2)),
                    new Ufixed(BigInteger.valueOf(0x8), BigInteger.valueOf(0x8)));

    assertEquals(
            TypeEncoder.encodeArrayValues(array),
            ("0000000000000000000000000000000220000000000000000000000000000000"
                    + "0000000000000000000000000000000880000000000000000000000000000000"));
}
 
Example #20
Source File: TypeDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticArray() throws Exception {
    assertEquals(
            TypeDecoder.decodeStaticArray(
                    "000000000000000000000000000000000000000000000000000000000000000a"
                            + "0000000000000000000000000000000000000000000000007fffffffffffffff",
                    0,
                    new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(2) {},
                    2),
            (new StaticArray2<>(
                    Uint256.class,
                    new Uint256(BigInteger.TEN),
                    new Uint256(BigInteger.valueOf(Long.MAX_VALUE)))));

    assertEquals(
            TypeDecoder.decodeStaticArray(
                    "000000000000000000000000000000000000000000000000000000000000000d"
                            + "48656c6c6f2c20776f726c642100000000000000000000000000000000000000"
                            + "000000000000000000000000000000000000000000000000000000000000000d"
                            + "776f726c64212048656c6c6f2c00000000000000000000000000000000000000",
                    0,
                    new TypeReference.StaticArrayTypeReference<StaticArray<Utf8String>>(2) {},
                    2),
            (new StaticArray2<>(
                    Utf8String.class,
                    new Utf8String("Hello, world!"),
                    new Utf8String("world! Hello,"))));

    Type arr = TypeDecoder.instantiateType("uint256[2]", new long[] {10, Long.MAX_VALUE});

    assertTrue(arr instanceof StaticArray2);
    StaticArray2 staticArray2 = (StaticArray2) arr;

    assertEquals(staticArray2.getComponentType(), Uint256.class);

    assertEquals(staticArray2.getValue().get(0), (new Uint256(BigInteger.TEN)));
    assertEquals(
            staticArray2.getValue().get(1), (new Uint256(BigInteger.valueOf(Long.MAX_VALUE))));
}
 
Example #21
Source File: TypeDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyStaticArray() {
    assertThrows(
            UnsupportedOperationException.class,
            () ->
                    TypeDecoder.decodeStaticArray(
                            "0000000000000000000000000000000000000000000000000000000000000000",
                            0,
                            new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(
                                    0) {},
                            0));
}
 
Example #22
Source File: SolidityFunctionWrapper.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static Class<?> getStaticArrayTypeReferenceClass(String type) {
    try {
        return Class.forName("org.web3j.abi.datatypes.generated.StaticArray" + type);
    } catch (ClassNotFoundException e) {
        // Unfortunately we can't encode it's length as a type if it's > 32.
        return StaticArray.class;
    }
}
 
Example #23
Source File: AbiTypesGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void generate(String destinationDir) throws IOException {
    generateIntTypes(Int.class, destinationDir);
    generateIntTypes(Uint.class, destinationDir);

    // TODO: Enable once Solidity supports fixed types - see
    // https://github.com/ethereum/solidity/issues/409
    // generateFixedTypes(Fixed.class, destinationDir);
    // generateFixedTypes(Ufixed.class, destinationDir);

    generateBytesTypes(Bytes.class, destinationDir);
    generateStaticArrayTypes(StaticArray.class, destinationDir);
}
 
Example #24
Source File: TypeDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test(expected = UnsupportedOperationException.class)
public void testEmptyStaticArray() {
    assertThat(TypeDecoder.decodeStaticArray(
            "0000000000000000000000000000000000000000000000000000000000000000",
            0,
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(0) {},
            0), is("invalid"));
}
 
Example #25
Source File: TypeEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFixedArray() {
    StaticArray<Ufixed> array = new StaticArray<>(
            new Ufixed(BigInteger.valueOf(0x2), BigInteger.valueOf(0x2)),
            new Ufixed(BigInteger.valueOf(0x8), BigInteger.valueOf(0x8))
    );

    assertThat(TypeEncoder.encodeArrayValues(array),
            is("0000000000000000000000000000000220000000000000000000000000000000"
                    + "0000000000000000000000000000000880000000000000000000000000000000"));
}
 
Example #26
Source File: FunctionEncoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private static int getLength(List<Type> parameters) {
    int count = 0;
    for (Type type:parameters) {
        if (type instanceof StaticArray) {
            count += ((StaticArray) type).getValue().size();
        } else {
            count++;
        }
    }
    return count;
}
 
Example #27
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T extends Type> T instantiateStaticArray(
        TypeReference<T> typeReference, List<T> elements) {
    try {
        Class<List> listClass = List.class;
        return typeReference.getClassType().getConstructor(listClass).newInstance(elements);
    } catch (ReflectiveOperationException e) {
        //noinspection unchecked
        return (T) new StaticArray<>(elements);
    }
}
 
Example #28
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public static <T extends Array> T decode(
        String input, int offset, TypeReference<T> typeReference) {
    Class cls = ((ParameterizedType) typeReference.getType()).getRawType().getClass();
    if (StaticArray.class.isAssignableFrom(cls)) {
        return decodeStaticArray(input, offset, typeReference, 1);
    } else if (DynamicArray.class.isAssignableFrom(cls)) {
        return decodeDynamicArray(input, offset, typeReference);
    } else {
        throw new UnsupportedOperationException("Unsupported TypeReference: "
                + cls.getName() + ", only Array types can be passed as TypeReferences");
    }
}
 
Example #29
Source File: SolidityFunctionWrapperTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildTypeName() {
    assertThat(buildTypeName("uint256"),
            is(ClassName.get(Uint256.class)));
    assertThat(buildTypeName("uint64"),
            is(ClassName.get(Uint64.class)));
    assertThat(buildTypeName("string"),
            is(ClassName.get(Utf8String.class)));

    assertThat(buildTypeName("uint256[]"),
            is(ParameterizedTypeName.get(DynamicArray.class, Uint256.class)));

    assertThat(buildTypeName("uint256[] storage"),
            is(ParameterizedTypeName.get(DynamicArray.class, Uint256.class)));

    assertThat(buildTypeName("uint256[] memory"),
            is(ParameterizedTypeName.get(DynamicArray.class, Uint256.class)));

    assertThat(buildTypeName("uint256[10]"),
            is(ParameterizedTypeName.get(StaticArray10.class, Uint256.class)));

    assertThat(buildTypeName("uint256[33]"),
            is(ParameterizedTypeName.get(StaticArray.class, Uint256.class)));

    assertThat(buildTypeName("uint256[10][3]"),
            is(ParameterizedTypeName.get(ClassName.get(StaticArray3.class),
                    ParameterizedTypeName.get(StaticArray10.class, Uint256.class))));

    assertThat(buildTypeName("uint256[2][]"),
            is(ParameterizedTypeName.get(ClassName.get(DynamicArray.class),
                    ParameterizedTypeName.get(StaticArray2.class, Uint256.class))));

    assertThat(buildTypeName("uint256[33][]"),
            is(ParameterizedTypeName.get(ClassName.get(DynamicArray.class),
                    ParameterizedTypeName.get(StaticArray.class, Uint256.class))));

    assertThat(buildTypeName("uint256[][]"),
            is(ParameterizedTypeName.get(ClassName.get(DynamicArray.class),
                    ParameterizedTypeName.get(DynamicArray.class, Uint256.class))));
}
 
Example #30
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private static Class<?> getStaticArrayTypeReferenceClass(String type) {
    try {
        return Class.forName("org.web3j.abi.datatypes.generated.StaticArray" + type);
    } catch (ClassNotFoundException e) {
        // Unfortunately we can't encode it's length as a type if it's > 32.
        return StaticArray.class;
    }
}