Search in sources :

Example 1 with EventValues

use of org.fisco.bcos.web3j.abi.EventValues 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 2 with EventValues

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

the class Contract method staticExtractEventParameters.

public static EventValues staticExtractEventParameters(Event event, Log log) {
    List<String> topics = log.getTopics();
    String encodedEventSignature = EventEncoder.encode(event);
    if (!topics.get(0).equals(encodedEventSignature)) {
        return null;
    }
    List<Type> indexedValues = new ArrayList<>();
    List<Type> nonIndexedValues = FunctionReturnDecoder.decode(log.getData(), event.getNonIndexedParameters());
    List<TypeReference<Type>> indexedParameters = event.getIndexedParameters();
    for (int i = 0; i < indexedParameters.size(); i++) {
        Type value = FunctionReturnDecoder.decodeIndexedValue(topics.get(i + 1), indexedParameters.get(i));
        indexedValues.add(value);
    }
    return new EventValues(indexedValues, nonIndexedValues);
}
Also used : EventValues(org.fisco.bcos.web3j.abi.EventValues) Type(org.fisco.bcos.web3j.abi.datatypes.Type) ArrayList(java.util.ArrayList) TypeReference(org.fisco.bcos.web3j.abi.TypeReference)

Example 3 with EventValues

use of org.fisco.bcos.web3j.abi.EventValues 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)

Example 4 with EventValues

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

the class ContractAbiUtil method decodeEvent.

/**
 * @param log
 * @param abiDefinition
 * @return
 * @throws BaseException
 */
public static EventValues decodeEvent(Log log, AbiDefinition abiDefinition) throws BaseException {
    List<TypeReference<?>> finalOutputs = paramFormat(abiDefinition.getInputs());
    Event event = new Event(abiDefinition.getName(), finalOutputs);
    EventValues eventValues = Contract.staticExtractEventParameters(event, log);
    return eventValues;
}
Also used : EventValues(org.fisco.bcos.web3j.abi.EventValues) Event(org.fisco.bcos.web3j.abi.datatypes.Event) TypeReference(org.fisco.bcos.web3j.abi.TypeReference)

Aggregations

EventValues (org.fisco.bcos.web3j.abi.EventValues)4 TypeReference (org.fisco.bcos.web3j.abi.TypeReference)4 ArrayList (java.util.ArrayList)3 Type (org.fisco.bcos.web3j.abi.datatypes.Type)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)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 FunctionEncoder (org.fisco.bcos.web3j.abi.FunctionEncoder)2 FunctionReturnDecoder (org.fisco.bcos.web3j.abi.FunctionReturnDecoder)2 Function (org.fisco.bcos.web3j.abi.datatypes.Function)2 ObjectMapperFactory (org.fisco.bcos.web3j.protocol.ObjectMapperFactory)2 AbiDefinition (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)2 NamedType (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType)2