org.web3j.abi.datatypes.Int Java Examples

The following examples show how to use org.web3j.abi.datatypes.Int. 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: TypeDecoder.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
    try {
        byte[] inputByteArray = Numeric.hexStringToByteArray(input);
        int typeLengthAsBytes = getTypeLengthInBytes(type);

        byte[] resultByteArray = new byte[typeLengthAsBytes + 1];

        if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
            resultByteArray[0] = inputByteArray[0];  // take MSB as sign bit
        }

        int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
        System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);

        BigInteger numericValue = new BigInteger(resultByteArray);
        return type.getConstructor(BigInteger.class).newInstance(numericValue);

    } catch (NoSuchMethodException | SecurityException
            | InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example #2
Source File: TypeEncoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntEncode() {
    Int zero = new Int64(BigInteger.ZERO);
    assertThat(TypeEncoder.encodeNumeric(zero),
            is("0000000000000000000000000000000000000000000000000000000000000000"));

    Int maxLong = new Int64(BigInteger.valueOf(Long.MAX_VALUE));
    assertThat(TypeEncoder.encodeNumeric(maxLong),
            is("0000000000000000000000000000000000000000000000007fffffffffffffff"));

    Int minLong = new Int64(BigInteger.valueOf(Long.MIN_VALUE));
    assertThat(TypeEncoder.encodeNumeric(minLong),
            is("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000"));

    Int minusOne = new Int(BigInteger.valueOf(-1));
    assertThat(TypeEncoder.encodeNumeric(minusOne),
            is("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
}
 
Example #3
Source File: PlatOnTypeEncoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntEncode() {
    Int zero = new Int64(BigInteger.ZERO);
    assertThat(PlatOnTypeEncoder.encode(zero),
            is("0000000000000000"));

    Int maxLong = new Int64(BigInteger.valueOf(Long.MAX_VALUE));
    assertThat(PlatOnTypeEncoder.encode(maxLong),
            is("7fffffffffffffff"));
    
    Int minLong = new Int64(BigInteger.valueOf(Long.MIN_VALUE));
    assertThat(PlatOnTypeEncoder.encode(minLong),
            is("8000000000000000"));

    Int minusOne = new Int64(BigInteger.valueOf(-1));
    assertThat(PlatOnTypeEncoder.encode(minusOne),
            is("ffffffffffffffff"));
}
 
Example #4
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 #5
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 #6
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
    try {
        byte[] inputByteArray = Numeric.hexStringToByteArray(input);
        int typeLengthAsBytes = getTypeLengthInBytes(type);

        byte[] resultByteArray = new byte[typeLengthAsBytes + 1];

        if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
            resultByteArray[0] = inputByteArray[0];  // take MSB as sign bit
        }

        int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
        System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);

        BigInteger numericValue = new BigInteger(resultByteArray);
        return type.getConstructor(BigInteger.class).newInstance(numericValue);

    } catch (NoSuchMethodException | SecurityException
            | InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example #7
Source File: Utils.java    From web3j with Apache License 2.0 6 votes vote down vote up
static String getSimpleTypeName(Class<?> type) {
    String simpleName = type.getSimpleName().toLowerCase();

    if (type.equals(Uint.class)
            || type.equals(Int.class)
            || type.equals(Ufixed.class)
            || type.equals(Fixed.class)) {
        return simpleName + "256";
    } else if (type.equals(Utf8String.class)) {
        return "string";
    } else if (type.equals(DynamicBytes.class)) {
        return "bytes";
    } else {
        return simpleName;
    }
}
 
Example #8
Source File: TypeEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testIntEncode() {
    Int zero = new Int64(BigInteger.ZERO);
    assertThat(TypeEncoder.encodeNumeric(zero),
            is("0000000000000000000000000000000000000000000000000000000000000000"));

    Int maxLong = new Int64(BigInteger.valueOf(Long.MAX_VALUE));
    assertThat(TypeEncoder.encodeNumeric(maxLong),
            is("0000000000000000000000000000000000000000000000007fffffffffffffff"));

    Int minLong = new Int64(BigInteger.valueOf(Long.MIN_VALUE));
    assertThat(TypeEncoder.encodeNumeric(minLong),
            is("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000"));

    Int minusOne = new Int(BigInteger.valueOf(-1));
    assertThat(TypeEncoder.encodeNumeric(minusOne),
            is("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
}
 
Example #9
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 #10
Source File: AbiTypesGenerator.java    From web3j with Apache License 2.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(destinationDir);
    generateStaticArrayTypes(destinationDir);
}
 
Example #11
Source File: TypeEncoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrimitiveInt() {
    assertEquals(
            encode(new org.web3j.abi.datatypes.primitive.Int(0)),
            ("0000000000000000000000000000000000000000000000000000000000000000"));

    assertEquals(
            encode(new org.web3j.abi.datatypes.primitive.Int(Integer.MIN_VALUE)),
            ("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000"));

    assertEquals(
            encode(new org.web3j.abi.datatypes.primitive.Int(Integer.MAX_VALUE)),
            ("000000000000000000000000000000000000000000000000000000007fffffff"));
}
 
Example #12
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
    try {
        byte[] inputByteArray = Numeric.hexStringToByteArray(input);
        int typeLengthAsBytes = getTypeLengthInBytes(type);

        byte[] resultByteArray = new byte[typeLengthAsBytes + 1];

        if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
            resultByteArray[0] = inputByteArray[0]; // take MSB as sign bit
        }

        int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
        System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);

        BigInteger numericValue = new BigInteger(resultByteArray);
        return type.getConstructor(BigInteger.class).newInstance(numericValue);

    } catch (NoSuchMethodException
            | SecurityException
            | InstantiationException
            | IllegalAccessException
            | IllegalArgumentException
            | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example #13
Source File: AbiTypesMapperGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private MethodSpec.Builder generateIntTypes(MethodSpec.Builder builder, String packageName) {
    for (int bitSize = 8; bitSize <= Type.MAX_BIT_LENGTH; bitSize += 8) {

        builder = addStatement(builder, packageName,
                Uint.TYPE_NAME + bitSize, Uint.class.getSimpleName() + bitSize);
        builder = addStatement(builder, packageName,
                Int.TYPE_NAME + bitSize, Int.class.getSimpleName() + bitSize);
    }
    return builder;
}
 
Example #14
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 #15
Source File: Utils.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static String getSimpleTypeName(Class<?> type) {
    String simpleName = type.getSimpleName().toLowerCase();

    if (type.equals(Uint.class) || type.equals(Int.class)
            || type.equals(Ufixed.class) || type.equals(Fixed.class)) {
        return simpleName + "256";
    } else if (type.equals(Utf8String.class)) {
        return "string";
    } else if (type.equals(DynamicBytes.class)) {
        return "bytes";
    } else {
        return simpleName;
    }
}
 
Example #16
Source File: AbiTypesMapperGenerator.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private MethodSpec.Builder generateIntTypes(MethodSpec.Builder builder, String packageName) {
    for (int bitSize = 8; bitSize <= Type.MAX_BIT_LENGTH; bitSize += 8) {

        builder = addStatement(builder, packageName,
                Uint.TYPE_NAME + bitSize, Uint.class.getSimpleName() + bitSize);
        builder = addStatement(builder, packageName,
                Int.TYPE_NAME + bitSize, Int.class.getSimpleName() + bitSize);
    }
    return builder;
}
 
Example #17
Source File: AbiTypesGenerator.java    From client-sdk-java with Apache License 2.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 #18
Source File: Utils.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
static String getSimpleTypeName(Class<?> type) {
    String simpleName = type.getSimpleName().toLowerCase();

    if (type.equals(Uint.class) || type.equals(Int.class)
            || type.equals(Ufixed.class) || type.equals(Fixed.class)) {
        return simpleName + "256";
    } else if (type.equals(Utf8String.class)) {
        return "string";
    } else if (type.equals(DynamicBytes.class)) {
        return "bytes";
    } else {
        return simpleName;
    }
}