Search in sources :

Example 6 with NamedType

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType in project web3sdk by FISCO-BCOS.

the class TransactionDecoder method decodeInputReturnObject.

/**
 * @param input
 * @return
 * @throws BaseException
 */
public InputAndOutputResult decodeInputReturnObject(String input) throws BaseException {
    String updatedInput = addHexPrefixToString(input);
    // select abi
    AbiDefinition abiDefinition = selectAbiDefinition(updatedInput);
    // decode input
    List<NamedType> inputTypes = abiDefinition.getInputs();
    List<TypeReference<?>> inputTypeReferences = ContractAbiUtil.paramFormat(inputTypes);
    Function function = new Function(abiDefinition.getName(), null, inputTypeReferences);
    List<Type> resultType = FunctionReturnDecoder.decode(updatedInput.substring(10), function.getOutputParameters());
    // set result to java bean
    List<ResultEntity> resultList = new ArrayList<ResultEntity>();
    for (int i = 0; i < inputTypes.size(); i++) {
        resultList.add(new ResultEntity(inputTypes.get(i).getName(), inputTypes.get(i).getType(), resultType.get(i)));
    }
    String methodSign = decodeMethodSign(abiDefinition);
    return new InputAndOutputResult(methodSign, FunctionEncoder.buildMethodId(methodSign), resultList);
}
Also used : NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) ArrayList(java.util.ArrayList) Function(org.fisco.bcos.web3j.abi.datatypes.Function) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) TypeReference(org.fisco.bcos.web3j.abi.TypeReference)

Example 7 with NamedType

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType in project web3sdk by FISCO-BCOS.

the class TransactionDecoder method decodeEventLogReturnObject.

/**
 * @param log
 * @return LogResult
 * @throws BaseException
 */
public LogResult decodeEventLogReturnObject(Log log) throws BaseException {
    // decode log
    List<AbiDefinition> abiDefinitions = ContractAbiUtil.getEventAbiDefinitions(abi);
    LogResult result = new LogResult();
    for (AbiDefinition abiDefinition : abiDefinitions) {
        // String eventName = decodeMethodSign(abiDefinition);
        String eventSignature = EventEncoder.buildEventSignature(decodeMethodSign(abiDefinition));
        List<String> topics = log.getTopics();
        if ((null == topics) || topics.isEmpty() || !topics.get(0).equals(eventSignature)) {
            continue;
        }
        EventValues eventValued = ContractAbiUtil.decodeEvent(log, abiDefinition);
        if (null != eventValued) {
            List<EventResultEntity> resultEntityList = new ArrayList<EventResultEntity>();
            List<NamedType> inputs = abiDefinition.getInputs();
            List<NamedType> indexedInputs = inputs.stream().filter(NamedType::isIndexed).collect(Collectors.toList());
            List<NamedType> nonIndexedInputs = inputs.stream().filter(p -> !p.isIndexed()).collect(Collectors.toList());
            for (int i = 0; i < indexedInputs.size(); i++) {
                EventResultEntity eventEntity = new EventResultEntity(indexedInputs.get(i).getName(), indexedInputs.get(i).getType(), true, eventValued.getIndexedValues().get(i));
                resultEntityList.add(eventEntity);
            }
            for (int i = 0; i < nonIndexedInputs.size(); i++) {
                EventResultEntity eventEntity = new EventResultEntity(nonIndexedInputs.get(i).getName(), nonIndexedInputs.get(i).getType(), false, eventValued.getNonIndexedValues().get(i));
                resultEntityList.add(eventEntity);
            }
            // result.setEventName(eventName);
            result.setLogParams(resultEntityList);
            result.setLog(log);
            logger.debug(" event log result: {}", result);
            return result;
        }
    }
    return null;
}
Also used : EventValues(org.fisco.bcos.web3j.abi.EventValues) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) Logger(org.slf4j.Logger) FunctionEncoder(org.fisco.bcos.web3j.abi.FunctionEncoder) EventEncoder(org.fisco.bcos.web3j.abi.EventEncoder) ObjectMapperFactory(org.fisco.bcos.web3j.protocol.ObjectMapperFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Log(org.fisco.bcos.web3j.protocol.core.methods.response.Log) Tuple2(org.fisco.bcos.web3j.tuples.generated.Tuple2) LoggerFactory(org.slf4j.LoggerFactory) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) IOException(java.io.IOException) HashMap(java.util.HashMap) EventValues(org.fisco.bcos.web3j.abi.EventValues) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TypeReference(org.fisco.bcos.web3j.abi.TypeReference) List(java.util.List) Type(org.fisco.bcos.web3j.abi.datatypes.Type) Map(java.util.Map) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) FunctionReturnDecoder(org.fisco.bcos.web3j.abi.FunctionReturnDecoder) Function(org.fisco.bcos.web3j.abi.datatypes.Function) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) ArrayList(java.util.ArrayList) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)

Example 8 with NamedType

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType in project web3sdk by FISCO-BCOS.

the class ContractAbiUtil method getFuncOutputType.

/**
 * @param abiDefinition
 * @return
 */
public static List<String> getFuncOutputType(AbiDefinition abiDefinition) {
    List<String> outputList = new ArrayList<>();
    List<NamedType> outputs = abiDefinition.getOutputs();
    for (NamedType output : outputs) {
        outputList.add(output.getType());
    }
    return outputList;
}
Also used : NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) ArrayList(java.util.ArrayList)

Example 9 with NamedType

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType 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 + ")";
    }
}
Also used : Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) EncryptType(org.fisco.bcos.web3j.crypto.EncryptType) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) ParameterSpec(com.squareup.javapoet.ParameterSpec) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 10 with NamedType

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType in project web3sdk by FISCO-BCOS.

the class TransactionDecoder method decodeEventReturnObject.

public Tuple2<AbiDefinition, List<EventResultEntity>> decodeEventReturnObject(Log log) throws BaseException, IOException {
    Tuple2<AbiDefinition, List<EventResultEntity>> result = null;
    // decode log
    List<AbiDefinition> abiDefinitions = ContractAbiUtil.getEventAbiDefinitions(abi);
    for (AbiDefinition abiDefinition : abiDefinitions) {
        String eventSignature = EventEncoder.buildEventSignature(decodeMethodSign(abiDefinition));
        List<String> topics = log.getTopics();
        if ((null == topics) || topics.isEmpty() || !topics.get(0).equals(eventSignature)) {
            continue;
        }
        EventValues eventValued = ContractAbiUtil.decodeEvent(log, abiDefinition);
        if (null != eventValued) {
            List<EventResultEntity> resultEntityList = new ArrayList<EventResultEntity>();
            List<NamedType> inputs = abiDefinition.getInputs();
            List<NamedType> indexedInputs = inputs.stream().filter(NamedType::isIndexed).collect(Collectors.toList());
            List<NamedType> nonIndexedInputs = inputs.stream().filter(p -> !p.isIndexed()).collect(Collectors.toList());
            for (int i = 0; i < indexedInputs.size(); i++) {
                EventResultEntity eventEntity = new EventResultEntity(indexedInputs.get(i).getName(), indexedInputs.get(i).getType(), true, eventValued.getIndexedValues().get(i));
                resultEntityList.add(eventEntity);
            }
            for (int i = 0; i < nonIndexedInputs.size(); i++) {
                EventResultEntity eventEntity = new EventResultEntity(nonIndexedInputs.get(i).getName(), nonIndexedInputs.get(i).getType(), false, eventValued.getNonIndexedValues().get(i));
                resultEntityList.add(eventEntity);
            }
            result = new Tuple2<AbiDefinition, List<EventResultEntity>>(abiDefinition, resultEntityList);
            break;
        }
    }
    return result;
}
Also used : EventValues(org.fisco.bcos.web3j.abi.EventValues) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) Logger(org.slf4j.Logger) FunctionEncoder(org.fisco.bcos.web3j.abi.FunctionEncoder) EventEncoder(org.fisco.bcos.web3j.abi.EventEncoder) ObjectMapperFactory(org.fisco.bcos.web3j.protocol.ObjectMapperFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Log(org.fisco.bcos.web3j.protocol.core.methods.response.Log) Tuple2(org.fisco.bcos.web3j.tuples.generated.Tuple2) LoggerFactory(org.slf4j.LoggerFactory) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) IOException(java.io.IOException) HashMap(java.util.HashMap) EventValues(org.fisco.bcos.web3j.abi.EventValues) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TypeReference(org.fisco.bcos.web3j.abi.TypeReference) List(java.util.List) Type(org.fisco.bcos.web3j.abi.datatypes.Type) Map(java.util.Map) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) FunctionReturnDecoder(org.fisco.bcos.web3j.abi.FunctionReturnDecoder) Function(org.fisco.bcos.web3j.abi.datatypes.Function) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) ArrayList(java.util.ArrayList) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

NamedType (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType)10 AbiDefinition (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)8 ArrayList (java.util.ArrayList)7 Type (org.fisco.bcos.web3j.abi.datatypes.Type)6 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)5 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)4 TypeReference (org.fisco.bcos.web3j.abi.TypeReference)4 Function (org.fisco.bcos.web3j.abi.datatypes.Function)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ParameterSpec (com.squareup.javapoet.ParameterSpec)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 EventEncoder (org.fisco.bcos.web3j.abi.EventEncoder)2 EventValues (org.fisco.bcos.web3j.abi.EventValues)2 FunctionEncoder (org.fisco.bcos.web3j.abi.FunctionEncoder)2