org.fisco.bcos.web3j.abi.datatypes.NumericType Java Examples

The following examples show how to use org.fisco.bcos.web3j.abi.datatypes.NumericType. 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 web3sdk with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static <T extends Type> T decode(String input, int offset, Class<T> type) {
    if (NumericType.class.isAssignableFrom(type)) {
        return (T) decodeNumeric(input.substring(offset), (Class<NumericType>) type);
    } else if (Address.class.isAssignableFrom(type)) {
        return (T) decodeAddress(input.substring(offset));
    } else if (Bool.class.isAssignableFrom(type)) {
        return (T) decodeBool(input, offset);
    } else if (Bytes.class.isAssignableFrom(type)) {
        return (T) decodeBytes(input, offset, (Class<Bytes>) type);
    } else if (DynamicBytes.class.isAssignableFrom(type)) {
        return (T) decodeDynamicBytes(input, offset);
    } else if (Utf8String.class.isAssignableFrom(type)) {
        return (T) decodeUtf8String(input, offset);
    } else if (Array.class.isAssignableFrom(type)) {
        throw new UnsupportedOperationException(
                "Array types must be wrapped in a TypeReference");
    } else {
        throw new UnsupportedOperationException("Type cannot be encoded: " + type.getClass());
    }
}
 
Example #2
Source File: ContractTypeUtil.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
static <T extends NumericType> T encodeNumeric(String input, Class<T> type)
        throws FrontException {
    try {
        BigInteger numericValue = new BigInteger(input);
        return type.getConstructor(BigInteger.class).newInstance(numericValue);
    } catch (NoSuchMethodException | SecurityException | InstantiationException
            | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        log.error("encodeNumeric failed.");
        throw new FrontException(ConstantCode.CONTRACT_TYPE_PARAM_ERROR.getCode(),
                String.format("unable to create instance of type:%s", type.getName()));
    }
}
 
Example #3
Source File: ContractTypeUtil.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
static <T extends NumericType> T encodeNumeric(String input, Class<T> type)
        throws BaseException {
    try {
        BigInteger numericValue = new BigInteger(input);
        return type.getConstructor(BigInteger.class).newInstance(numericValue);
    } catch (NoSuchMethodException | SecurityException | InstantiationException
            | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        log.error("encodeNumeric failed.");
        throw new BaseException(201203,
                String.format("unable to create instance of type:%s", type.getName()));
    }
}
 
Example #4
Source File: ResultEntity.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static Object typeToObject(Type type) {
    Object obj = null;
    if (type instanceof NumericType) { // uint int
        obj = ((NumericType) type).getValue();
    } else if (type instanceof Bool) { // bool
        obj = ((Bool) type).getValue();
    } else if (type instanceof Address) { // address
        obj = type.toString();
    } else if (type instanceof Bytes) { // bytes32
        obj = new String(((Bytes) type).getValue()).trim();
    } else if (type instanceof DynamicBytes) { // bytes
        obj = new String(((DynamicBytes) type).getValue()).trim();
    } else if (type instanceof Utf8String) { // string
        obj = ((Utf8String) type).getValue();
    } else if (type instanceof Array) { // T[] T[k]
        List<Object> r = new ArrayList<Object>();
        List l = ((Array) type).getValue();
        for (int i = 0; i < l.size(); ++i) {
            r.add(typeToObject((Type) l.get(i)));
        }

        obj = (Object) r;
    } else {
        obj = (Object) obj;
    }

    return obj;
}
 
Example #5
Source File: TypeDecoder.java    From web3sdk 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 #6
Source File: BytesUtils.java    From WeBASE-Collect-Bee with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static String numericTypeListToString(Object typeList) {
    List<NumericType> list = (List<NumericType>) typeList;
    List<BigInteger> stringList = list.stream().map(t -> t.getValue()).collect(Collectors.toList());
    return JacksonUtils.toJson(stringList);
}
 
Example #7
Source File: TypeDecoder.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
static <T extends NumericType> int getTypeLengthInBytes(Class<T> type) {
    return getTypeLength(type) >> 3; // divide by 8
}