Search in sources :

Example 1 with Int

use of org.fisco.bcos.web3j.abi.datatypes.Int in project web3sdk by FISCO-BCOS.

the class AbiTypesMapperGenerator method generateIntTypes.

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;
}
Also used : Uint(org.fisco.bcos.web3j.abi.datatypes.Uint) Uint(org.fisco.bcos.web3j.abi.datatypes.Uint) Int(org.fisco.bcos.web3j.abi.datatypes.Int)

Example 2 with Int

use of org.fisco.bcos.web3j.abi.datatypes.Int in project web3sdk by FISCO-BCOS.

the class TypeDecoder method decodeNumeric.

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)) {
            // take MSB as sign bit
            resultByteArray[0] = inputByteArray[0];
        }
        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);
    }
}
Also used : Uint(org.fisco.bcos.web3j.abi.datatypes.Uint) Int(org.fisco.bcos.web3j.abi.datatypes.Int) InvocationTargetException(java.lang.reflect.InvocationTargetException) BigInteger(java.math.BigInteger) Fixed(org.fisco.bcos.web3j.abi.datatypes.Fixed)

Example 3 with Int

use of org.fisco.bcos.web3j.abi.datatypes.Int in project web3sdk by FISCO-BCOS.

the class TypeDecoder method decodeDynamicArray.

@SuppressWarnings("unchecked")
public static <T extends Type> T decodeDynamicArray(String input, int offset, java.lang.reflect.Type type) {
    int length = decodeUintAsInt(input, offset);
    BiFunction<List<T>, String, T> function = (elements, typeName) -> {
        if (elements.isEmpty()) {
            return (T) DynamicArray.empty(typeName);
        } else {
            return (T) new DynamicArray<>(elements);
        }
    };
    int valueOffset = offset + MAX_BYTE_LENGTH_FOR_HEX_STRING;
    return decodeArrayElements(input, valueOffset, type, length, function);
}
Also used : Uint160(org.fisco.bcos.web3j.abi.datatypes.generated.Uint160) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Fixed(org.fisco.bcos.web3j.abi.datatypes.Fixed) BiFunction(java.util.function.BiFunction) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) StaticArray(org.fisco.bcos.web3j.abi.datatypes.StaticArray) Ufixed(org.fisco.bcos.web3j.abi.datatypes.Ufixed) ArrayList(java.util.ArrayList) FixedPointType(org.fisco.bcos.web3j.abi.datatypes.FixedPointType) Array(org.fisco.bcos.web3j.abi.datatypes.Array) Uint(org.fisco.bcos.web3j.abi.datatypes.Uint) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) BigInteger(java.math.BigInteger) Address(org.fisco.bcos.web3j.abi.datatypes.Address) DynamicArray(org.fisco.bcos.web3j.abi.datatypes.DynamicArray) Numeric(org.fisco.bcos.web3j.utils.Numeric) NumericType(org.fisco.bcos.web3j.abi.datatypes.NumericType) IntType(org.fisco.bcos.web3j.abi.datatypes.IntType) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Type(org.fisco.bcos.web3j.abi.datatypes.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Int(org.fisco.bcos.web3j.abi.datatypes.Int) Bytes(org.fisco.bcos.web3j.abi.datatypes.Bytes) DynamicArray(org.fisco.bcos.web3j.abi.datatypes.DynamicArray) ArrayList(java.util.ArrayList) List(java.util.List) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Uint(org.fisco.bcos.web3j.abi.datatypes.Uint)

Aggregations

Int (org.fisco.bcos.web3j.abi.datatypes.Int)3 Uint (org.fisco.bcos.web3j.abi.datatypes.Uint)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 BigInteger (java.math.BigInteger)2 Fixed (org.fisco.bcos.web3j.abi.datatypes.Fixed)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BiFunction (java.util.function.BiFunction)1 Address (org.fisco.bcos.web3j.abi.datatypes.Address)1 Array (org.fisco.bcos.web3j.abi.datatypes.Array)1 Bool (org.fisco.bcos.web3j.abi.datatypes.Bool)1 Bytes (org.fisco.bcos.web3j.abi.datatypes.Bytes)1 DynamicArray (org.fisco.bcos.web3j.abi.datatypes.DynamicArray)1 DynamicBytes (org.fisco.bcos.web3j.abi.datatypes.DynamicBytes)1 FixedPointType (org.fisco.bcos.web3j.abi.datatypes.FixedPointType)1 IntType (org.fisco.bcos.web3j.abi.datatypes.IntType)1 NumericType (org.fisco.bcos.web3j.abi.datatypes.NumericType)1 StaticArray (org.fisco.bcos.web3j.abi.datatypes.StaticArray)1