org.web3j.abi.datatypes.Ufixed Java Examples

The following examples show how to use org.web3j.abi.datatypes.Ufixed. 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 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 #2
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 #3
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 #4
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 #5
Source File: PlatOnTypeEncoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private static byte[] toByteArray(NumericType numericType) {
    BigInteger value = numericType.getValue();
    if (numericType instanceof Ufixed || numericType instanceof Uint) {
        if (value.bitLength() == MAX_BIT_LENGTH) {
            // As BigInteger is signed, if we have a 256 bit value, the resultant byte array
            // will contain a sign byte in it's MSB, which we should ignore for this unsigned
            // integer type.
            byte[] byteArray = new byte[MAX_BYTE_LENGTH];
            System.arraycopy(value.toByteArray(), 1, byteArray, 0, MAX_BYTE_LENGTH);
            return byteArray;
        }
    }
    return value.toByteArray();
}
 
Example #6
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;
    }
}
 
Example #7
Source File: TypeEncoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private static byte[] toByteArray(NumericType numericType) {
    BigInteger value = numericType.getValue();
    if (numericType instanceof Ufixed || numericType instanceof Uint) {
        if (value.bitLength() == MAX_BIT_LENGTH) {
            // As BigInteger is signed, if we have a 256 bit value, the resultant byte array
            // will contain a sign byte in it's MSB, which we should ignore for this unsigned
            // integer type.
            byte[] byteArray = new byte[MAX_BYTE_LENGTH];
            System.arraycopy(value.toByteArray(), 1, byteArray, 0, MAX_BYTE_LENGTH);
            return byteArray;
        }
    }
    return value.toByteArray();
}
 
Example #8
Source File: TypeEncoderTest.java    From client-sdk-java with Apache License 2.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 #9
Source File: AbiTypesMapperGenerator.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private MethodSpec.Builder generateFixedTypes(MethodSpec.Builder builder, String packageName) {
    for (int mBitSize = 8, nBitSize = Type.MAX_BIT_LENGTH - 8;
            mBitSize < Type.MAX_BIT_LENGTH && nBitSize > 0;
            mBitSize += 8, nBitSize -= 8) {
        String suffix = mBitSize + "x" + nBitSize;
        builder = addStatement(
                builder, packageName, Ufixed.TYPE_NAME + suffix,
                Ufixed.class.getSimpleName() + suffix);
        builder = addStatement(
                builder, packageName, Fixed.TYPE_NAME + suffix,
                Fixed.class.getSimpleName() + suffix);
    }
    return builder;
}
 
Example #10
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 #11
Source File: TypeEncoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private static byte[] toByteArray(NumericType numericType) {
    BigInteger value = numericType.getValue();
    if (numericType instanceof Ufixed || numericType instanceof Uint) {
        if (value.bitLength() == MAX_BIT_LENGTH) {
            // As BigInteger is signed, if we have a 256 bit value, the resultant byte array
            // will contain a sign byte in it's MSB, which we should ignore for this unsigned
            // integer type.
            byte[] byteArray = new byte[MAX_BYTE_LENGTH];
            System.arraycopy(value.toByteArray(), 1, byteArray, 0, MAX_BYTE_LENGTH);
            return byteArray;
        }
    }
    return value.toByteArray();
}
 
Example #12
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 #13
Source File: AbiTypesMapperGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private MethodSpec.Builder generateFixedTypes(MethodSpec.Builder builder, String packageName) {
    for (int mBitSize = 8, nBitSize = Type.MAX_BIT_LENGTH - 8;
            mBitSize < Type.MAX_BIT_LENGTH && nBitSize > 0;
            mBitSize += 8, nBitSize -= 8) {
        String suffix = mBitSize + "x" + nBitSize;
        builder = addStatement(
                builder, packageName, Ufixed.TYPE_NAME + suffix,
                Ufixed.class.getSimpleName() + suffix);
        builder = addStatement(
                builder, packageName, Fixed.TYPE_NAME + suffix,
                Fixed.class.getSimpleName() + suffix);
    }
    return builder;
}
 
Example #14
Source File: TypeEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static byte[] toByteArray(NumericType numericType) {
    BigInteger value = numericType.getValue();
    if (numericType instanceof Ufixed || numericType instanceof Uint) {
        if (value.bitLength() == MAX_BIT_LENGTH) {
            // As BigInteger is signed, if we have a 256 bit value, the resultant byte array
            // will contain a sign byte in it's MSB, which we should ignore for this unsigned
            // integer type.
            byte[] byteArray = new byte[MAX_BYTE_LENGTH];
            System.arraycopy(value.toByteArray(), 1, byteArray, 0, MAX_BYTE_LENGTH);
            return byteArray;
        }
    }
    return value.toByteArray();
}
 
Example #15
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"));
}