org.web3j.abi.datatypes.NumericType Java Examples
The following examples show how to use
org.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 client-sdk-java with Apache License 2.0 | 6 votes |
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: TypeEncoder.java From client-sdk-java with Apache License 2.0 | 6 votes |
@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 #3
Source File: TypeEncoder.java From client-sdk-java with Apache License 2.0 | 6 votes |
static String encodeNumeric(NumericType numericType) { byte[] rawValue = toByteArray(numericType); byte paddingValue = getPaddingValue(numericType); byte[] paddedRawValue = new byte[MAX_BYTE_LENGTH]; if (paddingValue != 0) { for (int i = 0; i < paddedRawValue.length; i++) { paddedRawValue[i] = paddingValue; } } System.arraycopy( rawValue, 0, paddedRawValue, MAX_BYTE_LENGTH - rawValue.length, rawValue.length); return Numeric.toHexStringNoPrefix(paddedRawValue); }
Example #4
Source File: TypeDecoder.java From web3j with Apache License 2.0 | 6 votes |
@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 #5
Source File: TypeDecoder.java From etherscan-explorer with GNU General Public License v3.0 | 6 votes |
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: TypeEncoder.java From etherscan-explorer with GNU General Public License v3.0 | 6 votes |
@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 #7
Source File: TypeEncoder.java From etherscan-explorer with GNU General Public License v3.0 | 6 votes |
static String encodeNumeric(NumericType numericType) { byte[] rawValue = toByteArray(numericType); byte paddingValue = getPaddingValue(numericType); byte[] paddedRawValue = new byte[MAX_BYTE_LENGTH]; if (paddingValue != 0) { for (int i = 0; i < paddedRawValue.length; i++) { paddedRawValue[i] = paddingValue; } } System.arraycopy( rawValue, 0, paddedRawValue, MAX_BYTE_LENGTH - rawValue.length, rawValue.length); return Numeric.toHexStringNoPrefix(paddedRawValue); }
Example #8
Source File: TypeEncoder.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
private static byte getPaddingValue(NumericType numericType) { if (numericType.getValue().signum() == -1) { return (byte) 0xff; } else { return 0; } }
Example #9
Source File: TypeEncoder.java From web3j with Apache License 2.0 | 5 votes |
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 #10
Source File: TypeEncoder.java From web3j with Apache License 2.0 | 5 votes |
private static byte getPaddingValue(NumericType numericType) { if (numericType.getValue().signum() == -1) { return (byte) 0xff; } else { return 0; } }
Example #11
Source File: TypeEncoder.java From web3j with Apache License 2.0 | 5 votes |
static String encodeNumeric(NumericType numericType) { byte[] rawValue = toByteArray(numericType); byte paddingValue = getPaddingValue(numericType); byte[] paddedRawValue = new byte[MAX_BYTE_LENGTH]; if (paddingValue != 0) { for (int i = 0; i < paddedRawValue.length; i++) { paddedRawValue[i] = paddingValue; } } System.arraycopy( rawValue, 0, paddedRawValue, MAX_BYTE_LENGTH - rawValue.length, rawValue.length); return Numeric.toHexStringNoPrefix(paddedRawValue); }
Example #12
Source File: TypeEncoder.java From web3j with Apache License 2.0 | 5 votes |
@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 #13
Source File: TypeDecoder.java From web3j with Apache License 2.0 | 5 votes |
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 #14
Source File: TypeEncoder.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
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: PlatOnTypeDecoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public static <T extends Type> T decode(byte[] data, Class<T> type) { if (NumericType.class.isAssignableFrom(type)) { return (T) decodeNumeric(data, (Class<NumericType>) type); } else if (Utf8String.class.isAssignableFrom(type)) { return (T)decodeUtf8String(data); } else { throw new UnsupportedOperationException( "Type cannot be encoded: " + type.getClass()); } }
Example #16
Source File: TypeDecoder.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
@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 #17
Source File: TypeEncoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
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 #18
Source File: TypeEncoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
private static byte getPaddingValue(NumericType numericType) { if (numericType.getValue().signum() == -1) { return (byte) 0xff; } else { return 0; } }
Example #19
Source File: PlatOnTypeEncoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
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 #20
Source File: PlatOnTypeEncoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
private static byte getPaddingValue(NumericType numericType) { if (numericType.getValue().signum() == -1) { return (byte) 0xff; } else { return 0; } }
Example #21
Source File: TypeDecoder.java From client-sdk-java with Apache License 2.0 | 5 votes |
@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 #22
Source File: TypeDecoder.java From etherscan-explorer with GNU General Public License v3.0 | 4 votes |
static <T extends NumericType> int getTypeLengthInBytes(Class<T> type) { return getTypeLength(type) >> 3; // divide by 8 }
Example #23
Source File: TypeDecoder.java From web3j with Apache License 2.0 | 4 votes |
static <T extends NumericType> int getTypeLengthInBytes(Class<T> type) { return getTypeLength(type) >> 3; // divide by 8 }
Example #24
Source File: TypeDecoder.java From web3j with Apache License 2.0 | 4 votes |
static Type instantiateAtomicType(Class<?> referenceClass, Object value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException { Object constructorArg = null; if (NumericType.class.isAssignableFrom(referenceClass)) { constructorArg = asBigInteger(value); } else if (BytesType.class.isAssignableFrom(referenceClass)) { if (value instanceof byte[]) { constructorArg = value; } else if (value instanceof BigInteger) { constructorArg = ((BigInteger) value).toByteArray(); } else if (value instanceof String) { constructorArg = Numeric.hexStringToByteArray((String) value); } } else if (Utf8String.class.isAssignableFrom(referenceClass)) { constructorArg = value.toString(); } else if (Address.class.isAssignableFrom(referenceClass)) { if (value instanceof BigInteger || value instanceof Uint160) { constructorArg = value; } else { constructorArg = value.toString(); } } else if (Bool.class.isAssignableFrom(referenceClass)) { if (value instanceof Boolean) { constructorArg = value; } else { BigInteger bival = asBigInteger(value); constructorArg = bival == null ? null : !bival.equals(BigInteger.ZERO); } } if (constructorArg == null) { throw new InstantiationException( "Could not create type " + referenceClass + " from arg " + value.toString() + " of type " + value.getClass()); } Class<?>[] types = new Class[] {constructorArg.getClass()}; Constructor cons = referenceClass.getConstructor(types); return (Type) cons.newInstance(constructorArg); }
Example #25
Source File: Short.java From web3j with Apache License 2.0 | 4 votes |
@Override public NumericType toSolidityType() { return new Int16(getValue()); }
Example #26
Source File: Double.java From web3j with Apache License 2.0 | 4 votes |
@Override public NumericType toSolidityType() { throw new UnsupportedOperationException("Fixed types are not supported"); }
Example #27
Source File: Number.java From web3j with Apache License 2.0 | 4 votes |
@Override public abstract NumericType toSolidityType();
Example #28
Source File: Float.java From web3j with Apache License 2.0 | 4 votes |
@Override public NumericType toSolidityType() { throw new UnsupportedOperationException("Fixed types are not supported"); }
Example #29
Source File: Long.java From web3j with Apache License 2.0 | 4 votes |
@Override public NumericType toSolidityType() { return new Int64(getValue()); }
Example #30
Source File: Int.java From web3j with Apache License 2.0 | 4 votes |
@Override public NumericType toSolidityType() { return new Int32(getValue()); }