Skip to content

Commit

Permalink
Revert "Merge pull request #1806 from apex-crypto/feature/event_from_…
Browse files Browse the repository at this point in the history
…log"

This reverts commit 2bc0ad1, reversing
changes made to 7afb1e9.
  • Loading branch information
gtebrean committed Jun 8, 2023
1 parent 7a4870e commit c4ccf38
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1697,15 +1697,47 @@ MethodSpec buildEventFlowableFunction(
ParameterizedTypeName.get(
ClassName.get(Flowable.class), ClassName.get("", responseClassName));

return MethodSpec.methodBuilder(generatedFunctionName)
.addModifiers(Modifier.PUBLIC)
.addParameter(EthFilter.class, FILTER)
.returns(parameterizedTypeName)
.addStatement(
"return web3j.ethLogFlowable(filter).map(log -> "
+ getEventFromLogFunctionName(functionName)
+ "(log))")
.build();
MethodSpec.Builder flowableMethodBuilder =
MethodSpec.methodBuilder(generatedFunctionName)
.addModifiers(Modifier.PUBLIC)
.addParameter(EthFilter.class, FILTER)
.returns(parameterizedTypeName);

TypeSpec converter =
TypeSpec.anonymousClassBuilder("")
.addSuperinterface(
ParameterizedTypeName.get(
ClassName.get(io.reactivex.functions.Function.class),
ClassName.get(Log.class),
ClassName.get("", responseClassName)))
.addMethod(
MethodSpec.methodBuilder("apply")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(Log.class, "log")
.returns(ClassName.get("", responseClassName))
.addStatement(
"$T eventValues = extractEventParametersWithLog("
+ buildEventDefinitionName(functionName)
+ ", log)",
Contract.EventValuesWithLog.class)
.addStatement(
"$1T typedResponse = new $1T()",
ClassName.get("", responseClassName))
.addCode(
buildTypedResponse(
"typedResponse",
indexedParameters,
nonIndexedParameters,
true))
.addStatement("return typedResponse")
.build())
.build();

flowableMethodBuilder.addStatement(
"return web3j.ethLogFlowable(filter).map($L)", converter);

return flowableMethodBuilder.build();
}

MethodSpec buildDefaultEventFlowableFunction(String responseClassName, String functionName) {
Expand Down Expand Up @@ -1781,34 +1813,6 @@ MethodSpec buildEventTransactionReceiptFunction(
return transactionMethodBuilder.build();
}

MethodSpec buildEventLogFunction(
String responseClassName,
String functionName,
List<NamedTypeName> indexedParameters,
List<NamedTypeName> nonIndexedParameters) {

String generatedFunctionName = getEventFromLogFunctionName(functionName);
return MethodSpec.methodBuilder(generatedFunctionName)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(Log.class, "log")
.returns(ClassName.get("", responseClassName))
.addStatement(
"$T eventValues = staticExtractEventParametersWithLog("
+ buildEventDefinitionName(functionName)
+ ", log)",
Contract.EventValuesWithLog.class)
.addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName))
.addCode(
buildTypedResponse(
"typedResponse", indexedParameters, nonIndexedParameters, true))
.addStatement("return typedResponse")
.build();
}

private static String getEventFromLogFunctionName(String functionName) {
return "get" + Strings.capitaliseFirstLetter(functionName) + "EventFromLog";
}

List<MethodSpec> buildEventFunctions(
AbiDefinition functionDefinition, TypeSpec.Builder classBuilder)
throws ClassNotFoundException {
Expand Down Expand Up @@ -1849,9 +1853,7 @@ List<MethodSpec> buildEventFunctions(
methods.add(
buildEventTransactionReceiptFunction(
responseClassName, functionName, indexedParameters, nonIndexedParameters));
methods.add(
buildEventLogFunction(
responseClassName, functionName, indexedParameters, nonIndexedParameters));

methods.add(
buildEventFlowableFunction(
responseClassName, functionName, indexedParameters, nonIndexedParameters));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,20 +695,21 @@ public void testBuildEventConstantMultipleValueReturn() throws Exception {
+ " return responses;\n"
+ " }\n"
+ "\n"
+ " public static TransferEventResponse getTransferEventFromLog(org.web3j.protocol.core.methods.response.Log log) {\n"
+ " org.web3j.tx.Contract.EventValuesWithLog eventValues = staticExtractEventParametersWithLog(TRANSFER_EVENT, log);\n"
+ " TransferEventResponse typedResponse = new TransferEventResponse();\n"
+ " typedResponse.log = log;\n"
+ " typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
+ " typedResponse.from = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
+ " typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
+ " typedResponse.value = (java.math.BigInteger) eventValues.getNonIndexedValues().get(0).getValue();\n"
+ " typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
+ " return typedResponse;\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<TransferEventResponse> transferEventFlowable(org.web3j.protocol.core.methods.request.EthFilter filter) {\n"
+ " return web3j.ethLogFlowable(filter).map(log -> getTransferEventFromLog(log));\n"
+ " return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<org.web3j.protocol.core.methods.response.Log, TransferEventResponse>() {\n"
+ " @java.lang.Override\n"
+ " public TransferEventResponse apply(org.web3j.protocol.core.methods.response.Log log) {\n"
+ " org.web3j.tx.Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(TRANSFER_EVENT, log);\n"
+ " TransferEventResponse typedResponse = new TransferEventResponse();\n"
+ " typedResponse.log = log;\n"
+ " typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
+ " typedResponse.from = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
+ " typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
+ " typedResponse.value = (java.math.BigInteger) eventValues.getNonIndexedValues().get(0).getValue();\n"
+ " typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(1).getValue();\n"
+ " return typedResponse;\n"
+ " }\n"
+ " });\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<TransferEventResponse> transferEventFlowable(org.web3j.protocol.core.DefaultBlockParameter startBlock, org.web3j.protocol.core.DefaultBlockParameter endBlock) {\n"
Expand All @@ -730,7 +731,7 @@ public void testBuildEventConstantMultipleValueReturn() throws Exception {
+ " }\n"
+ "}\n";

assertEquals(expected, builder.build().toString());
assertEquals(builder.build().toString(), (expected));
}

@Test
Expand Down Expand Up @@ -779,21 +780,22 @@ public void testBuildEventWithNamedAndNoNamedParameters() throws Exception {
+ " return responses;\n"
+ " }\n"
+ "\n"
+ " public static TransferEventResponse getTransferEventFromLog(org.web3j.protocol.core.methods.response.Log log) {\n"
+ " org.web3j.tx.Contract.EventValuesWithLog eventValues = staticExtractEventParametersWithLog(TRANSFER_EVENT, log);\n"
+ " TransferEventResponse typedResponse = new TransferEventResponse();\n"
+ " typedResponse.log = log;\n"
+ " typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
+ " typedResponse.param1 = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
+ " typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
+ " typedResponse.param3 = (byte[]) eventValues.getNonIndexedValues().get(0).getValue();\n"
+ " typedResponse.param4 = (java.math.BigInteger) eventValues.getNonIndexedValues().get(1).getValue();\n"
+ " typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(2).getValue();\n"
+ " return typedResponse;\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<TransferEventResponse> transferEventFlowable(org.web3j.protocol.core.methods.request.EthFilter filter) {\n"
+ " return web3j.ethLogFlowable(filter).map(log -> getTransferEventFromLog(log));\n"
+ " return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<org.web3j.protocol.core.methods.response.Log, TransferEventResponse>() {\n"
+ " @java.lang.Override\n"
+ " public TransferEventResponse apply(org.web3j.protocol.core.methods.response.Log log) {\n"
+ " org.web3j.tx.Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(TRANSFER_EVENT, log);\n"
+ " TransferEventResponse typedResponse = new TransferEventResponse();\n"
+ " typedResponse.log = log;\n"
+ " typedResponse.id = (byte[]) eventValues.getIndexedValues().get(0).getValue();\n"
+ " typedResponse.param1 = (java.lang.String) eventValues.getIndexedValues().get(1).getValue();\n"
+ " typedResponse.to = (java.lang.String) eventValues.getIndexedValues().get(2).getValue();\n"
+ " typedResponse.param3 = (byte[]) eventValues.getNonIndexedValues().get(0).getValue();\n"
+ " typedResponse.param4 = (java.math.BigInteger) eventValues.getNonIndexedValues().get(1).getValue();\n"
+ " typedResponse.message = (java.lang.String) eventValues.getNonIndexedValues().get(2).getValue();\n"
+ " return typedResponse;\n"
+ " }\n"
+ " });\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<TransferEventResponse> transferEventFlowable(org.web3j.protocol.core.DefaultBlockParameter startBlock, org.web3j.protocol.core.DefaultBlockParameter endBlock) {\n"
Expand Down Expand Up @@ -849,16 +851,17 @@ public void testBuildEventWithNativeList() throws Exception {
+ " return responses;\n"
+ " }\n"
+ "\n"
+ " public static TransferEventResponse getTransferEventFromLog(org.web3j.protocol.core.methods.response.Log log) {\n"
+ " org.web3j.tx.Contract.EventValuesWithLog eventValues = staticExtractEventParametersWithLog(TRANSFER_EVENT, log);\n"
+ " TransferEventResponse typedResponse = new TransferEventResponse();\n"
+ " typedResponse.log = log;\n"
+ " typedResponse.array = (java.util.List<java.math.BigInteger>) ((org.web3j.abi.datatypes.Array) eventValues.getNonIndexedValues().get(0)).getNativeValueCopy();\n"
+ " return typedResponse;\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<TransferEventResponse> transferEventFlowable(org.web3j.protocol.core.methods.request.EthFilter filter) {\n"
+ " return web3j.ethLogFlowable(filter).map(log -> getTransferEventFromLog(log));\n"
+ " return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<org.web3j.protocol.core.methods.response.Log, TransferEventResponse>() {\n"
+ " @java.lang.Override\n"
+ " public TransferEventResponse apply(org.web3j.protocol.core.methods.response.Log log) {\n"
+ " org.web3j.tx.Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(TRANSFER_EVENT, log);\n"
+ " TransferEventResponse typedResponse = new TransferEventResponse();\n"
+ " typedResponse.log = log;\n"
+ " typedResponse.array = (java.util.List<java.math.BigInteger>) ((org.web3j.abi.datatypes.Array) eventValues.getNonIndexedValues().get(0)).getNativeValueCopy();\n"
+ " return typedResponse;\n"
+ " }\n"
+ " });\n"
+ " }\n"
+ "\n"
+ " public io.reactivex.Flowable<TransferEventResponse> transferEventFlowable(org.web3j.protocol.core.DefaultBlockParameter startBlock, org.web3j.protocol.core.DefaultBlockParameter endBlock) {\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.web3j.unittests.java;

import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -10,7 +11,6 @@
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
Expand Down Expand Up @@ -82,19 +82,20 @@ public static List<TestEventEventResponse> getTestEventEvents(TransactionReceipt
return responses;
}

public static TestEventEventResponse getTestEventEventFromLog(Log log) {
Contract.EventValuesWithLog eventValues = staticExtractEventParametersWithLog(TESTEVENT_EVENT, log);
TestEventEventResponse typedResponse = new TestEventEventResponse();
typedResponse.log = log;
typedResponse._contractNumber = (BigInteger) eventValues.getIndexedValues().get(0).getValue();
typedResponse.param1 = (byte[]) eventValues.getIndexedValues().get(1).getValue();
typedResponse.param2 = (String) eventValues.getNonIndexedValues().get(0).getValue();
typedResponse.param3 = (String) eventValues.getNonIndexedValues().get(1).getValue();
return typedResponse;
}

public Flowable<TestEventEventResponse> testEventEventFlowable(EthFilter filter) {
return web3j.ethLogFlowable(filter).map(log -> getTestEventEventFromLog(log));
return web3j.ethLogFlowable(filter).map(new Function<Log, TestEventEventResponse>() {
@Override
public TestEventEventResponse apply(Log log) {
Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(TESTEVENT_EVENT, log);
TestEventEventResponse typedResponse = new TestEventEventResponse();
typedResponse.log = log;
typedResponse._contractNumber = (BigInteger) eventValues.getIndexedValues().get(0).getValue();
typedResponse.param1 = (byte[]) eventValues.getIndexedValues().get(1).getValue();
typedResponse.param2 = (String) eventValues.getNonIndexedValues().get(0).getValue();
typedResponse.param3 = (String) eventValues.getNonIndexedValues().get(1).getValue();
return typedResponse;
}
});
}

public Flowable<TestEventEventResponse> testEventEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
Expand All @@ -104,21 +105,21 @@ public Flowable<TestEventEventResponse> testEventEventFlowable(DefaultBlockParam
}

public RemoteFunctionCall<BigInteger> _contractNumber() {
final Function function = new Function(FUNC__CONTRACTNUMBER,
final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(FUNC__CONTRACTNUMBER,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}

public RemoteFunctionCall<String> _testAddress() {
final Function function = new Function(FUNC__TESTADDRESS,
final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(FUNC__TESTADDRESS,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<TransactionReceipt> testEvent() {
final Function function = new Function(
final org.web3j.abi.datatypes.Function function = new org.web3j.abi.datatypes.Function(
FUNC_TESTEVENT,
Arrays.<Type>asList(),
Collections.<TypeReference<?>>emptyList());
Expand Down

0 comments on commit c4ccf38

Please sign in to comment.