use of org.fisco.bcos.web3j.abi.datatypes.Type in project web3sdk by FISCO-BCOS.
the class ResultEntity method typeToObject.
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;
}
use of org.fisco.bcos.web3j.abi.datatypes.Type in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildVariableLengthEventInitializer.
private static CodeBlock buildVariableLengthEventInitializer(String eventName, List<NamedTypeName> parameterTypes) {
List<Object> objects = new ArrayList<>();
objects.add(Event.class);
objects.add(eventName);
objects.add(Arrays.class);
objects.add(TypeReference.class);
for (NamedTypeName parameterType : parameterTypes) {
objects.add(TypeReference.class);
objects.add(parameterType.getTypeName());
}
String asListParams = parameterTypes.stream().map(type -> {
if (type.isIndexed()) {
return "new $T<$T>(true) {}";
} else {
return "new $T<$T>() {}";
}
}).collect(Collectors.joining(", "));
return CodeBlock.builder().addStatement("new $T($S, \n" + "$T.<$T<?>>asList(" + asListParams + "))", objects.toArray()).build();
}
use of org.fisco.bcos.web3j.abi.datatypes.Type in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method createMappedParameterTypes.
private String createMappedParameterTypes(AbiDefinition.NamedType namedType) {
String name = namedType.getName();
String type = namedType.getType();
AbiDefinition.NamedType.Type innerType = new AbiDefinition.NamedType.Type(type);
ParameterSpec parameterSpec = ParameterSpec.builder(buildTypeName(type), name).build();
if (parameterSpec.type instanceof ParameterizedTypeName) {
List<TypeName> typeNames = ((ParameterizedTypeName) parameterSpec.type).typeArguments;
if (typeNames.size() != 1) {
throw new UnsupportedOperationException("Only a single parameterized type is supported");
} else {
String parameterSpecType = parameterSpec.type.toString();
TypeName typeName = typeNames.get(0);
String typeMapInput = typeName + ".class";
if (typeName instanceof ParameterizedTypeName) {
List<TypeName> typeArguments = ((ParameterizedTypeName) typeName).typeArguments;
if (typeArguments.size() != 1) {
throw new UnsupportedOperationException("Only a single parameterized type is supported");
}
TypeName innerTypeName = typeArguments.get(0);
parameterSpecType = ((ParameterizedTypeName) parameterSpec.type).rawType.toString();
typeMapInput = ((ParameterizedTypeName) typeName).rawType + ".class, " + innerTypeName + ".class";
}
if (innerType.dynamicArray()) {
// dynamic array
return parameterSpec.name + ".isEmpty()?org.fisco.bcos.web3j.abi.datatypes.DynamicArray.empty" + "(\"" + type + "\"):" + "new " + parameterSpecType + "(\n" + " org.fisco.bcos.web3j.abi.Utils.typeMap(" + parameterSpec.name + ", " + typeMapInput + "))";
} else {
// static array
return "new " + parameterSpecType + "(\n" + " org.fisco.bcos.web3j.abi.Utils.typeMap(" + parameterSpec.name + ", " + typeMapInput + "))";
}
}
} else {
return "new " + parameterSpec.type + "(" + parameterSpec.name + ")";
}
}
use of org.fisco.bcos.web3j.abi.datatypes.Type in project web3sdk by FISCO-BCOS.
the class Ok method getTransInput.
public Tuple1<BigInteger> getTransInput(TransactionReceipt transactionReceipt) {
String data = transactionReceipt.getInput().substring(10);
final Function function = new Function(FUNC_TRANS, Arrays.<Type>asList(), Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {
}));
List<Type> results = FunctionReturnDecoder.decode(data, function.getOutputParameters());
;
return new Tuple1<BigInteger>((BigInteger) results.get(0).getValue());
}
use of org.fisco.bcos.web3j.abi.datatypes.Type in project web3sdk by FISCO-BCOS.
the class FunctionEncoder method encodeParameters.
public static String encodeParameters(List<Type> parameters, StringBuilder result) {
int dynamicDataOffset = Utils.getLength(parameters) * Type.MAX_BYTE_LENGTH;
StringBuilder dynamicData = new StringBuilder();
for (Type parameter : parameters) {
String encodedValue = TypeEncoder.encode(parameter);
if (parameter.dynamicType()) {
String encodedDataOffset = TypeEncoder.encodeNumeric(new Uint(BigInteger.valueOf(dynamicDataOffset)));
result.append(encodedDataOffset);
dynamicData.append(encodedValue);
dynamicDataOffset += (encodedValue.length() >> 1);
} else {
result.append(encodedValue);
}
}
result.append(dynamicData);
return result.toString();
}
Aggregations