Search in sources :

Example 26 with EventValues

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

the class Contract method extractEventParameters.

/**
 * @desc 为了能在sol实例化出来的java类里面用到static的方法,而不需要实例化这个java类(load方法,用了name service,屏蔽了自身address)
 */
protected static EventValues extractEventParameters(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.bcos.web3j.abi.EventValues) Type(org.bcos.web3j.abi.datatypes.Type) ArrayList(java.util.ArrayList) TypeReference(org.bcos.web3j.abi.TypeReference)

Example 27 with EventValues

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

the class NodeAction method logMessageEventObservable.

public Observable<LogMessageEventResponse> logMessageEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("LogMessage", Arrays.<TypeReference<?>>asList(), Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {
    }, new TypeReference<Uint256>() {
    }, new TypeReference<Utf8String>() {
    }));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, LogMessageEventResponse>() {

        @Override
        public LogMessageEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            LogMessageEventResponse typedResponse = new LogMessageEventResponse();
            typedResponse.addr = (Address) eventValues.getNonIndexedValues().get(0);
            typedResponse.code = (Uint256) eventValues.getNonIndexedValues().get(1);
            typedResponse.msg = (Utf8String) eventValues.getNonIndexedValues().get(2);
            return typedResponse;
        }
    });
}
Also used : EventValues(org.bcos.web3j.abi.EventValues) EthFilter(org.bcos.web3j.protocol.core.methods.request.EthFilter) Utf8String(org.bcos.web3j.abi.datatypes.Utf8String) Address(org.bcos.web3j.abi.datatypes.Address) Log(org.bcos.web3j.protocol.core.methods.response.Log) Event(org.bcos.web3j.abi.datatypes.Event) TypeReference(org.bcos.web3j.abi.TypeReference) Uint256(org.bcos.web3j.abi.datatypes.generated.Uint256)

Example 28 with EventValues

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

the class NodeAction method getLogMessageEvents.

public List<LogMessageEventResponse> getLogMessageEvents(TransactionReceipt transactionReceipt) {
    final Event event = new Event("LogMessage", Arrays.<TypeReference<?>>asList(), Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {
    }, new TypeReference<Uint256>() {
    }, new TypeReference<Utf8String>() {
    }));
    List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
    ArrayList<LogMessageEventResponse> responses = new ArrayList<LogMessageEventResponse>(valueList.size());
    for (EventValues eventValues : valueList) {
        LogMessageEventResponse typedResponse = new LogMessageEventResponse();
        typedResponse.addr = (Address) eventValues.getNonIndexedValues().get(0);
        typedResponse.code = (Uint256) eventValues.getNonIndexedValues().get(1);
        typedResponse.msg = (Utf8String) eventValues.getNonIndexedValues().get(2);
        responses.add(typedResponse);
    }
    return responses;
}
Also used : EventValues(org.bcos.web3j.abi.EventValues) ArrayList(java.util.ArrayList) Event(org.bcos.web3j.abi.datatypes.Event) TypeReference(org.bcos.web3j.abi.TypeReference)

Example 29 with EventValues

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

the class SolidityFunctionWrapper method buildEventTransactionReceiptFunction.

static MethodSpec buildEventTransactionReceiptFunction(String responseClassName, String functionName, List<NamedTypeName> indexedParameters, List<NamedTypeName> nonIndexedParameters) throws ClassNotFoundException {
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(List.class), ClassName.get("", responseClassName));
    String generatedFunctionName = "get" + Strings.capitaliseFirstLetter(functionName) + "Events";
    MethodSpec.Builder transactionMethodBuilder = MethodSpec.methodBuilder(generatedFunctionName).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.STATIC).addParameter(TransactionReceipt.class, "transactionReceipt").returns(parameterizedTypeName);
    buildVariableLengthEventConstructor(transactionMethodBuilder, functionName, indexedParameters, nonIndexedParameters);
    transactionMethodBuilder.addStatement("$T valueList = extractEventParameters(event, " + "transactionReceipt)", ParameterizedTypeName.get(List.class, EventValues.class)).addStatement("$1T responses = new $1T(valueList.size())", ParameterizedTypeName.get(ClassName.get(ArrayList.class), ClassName.get("", responseClassName))).beginControlFlow("for ($T eventValues : valueList)", EventValues.class).addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName)).addCode(buildTypedResponse("typedResponse", indexedParameters, nonIndexedParameters)).addStatement("responses.add(typedResponse)").endControlFlow();
    transactionMethodBuilder.addStatement("return responses");
    return transactionMethodBuilder.build();
}
Also used : EventValues(org.bcos.web3j.abi.EventValues) MethodSpec(com.squareup.javapoet.MethodSpec) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Aggregations

EventValues (org.bcos.web3j.abi.EventValues)29 TypeReference (org.bcos.web3j.abi.TypeReference)27 Event (org.bcos.web3j.abi.datatypes.Event)26 ArrayList (java.util.ArrayList)16 Log (org.bcos.web3j.protocol.core.methods.response.Log)14 EthFilter (org.bcos.web3j.protocol.core.methods.request.EthFilter)13 Utf8String (org.bcos.web3j.abi.datatypes.Utf8String)12 Uint256 (org.bcos.web3j.abi.datatypes.generated.Uint256)11 Address (org.bcos.web3j.abi.datatypes.Address)9 Bytes32 (org.bcos.web3j.abi.datatypes.generated.Bytes32)2 Int256 (org.bcos.web3j.abi.datatypes.generated.Int256)2 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 List (java.util.List)1 StaticArray (org.bcos.web3j.abi.datatypes.StaticArray)1 Type (org.bcos.web3j.abi.datatypes.Type)1 TransactionReceipt (org.bcos.web3j.protocol.core.methods.response.TransactionReceipt)1